PWM引脚和串口引脚接在一起使用

STM32串口和PWM引脚接在一起后是否能正常使用,经测试,PWM输出占空比为零时串口可以发送数据,当PWM波的高电平是无法发送,特此记录

今天调试又发现,如果串口引脚不做调整PWM波低电平会被拉高,所以在PWM输出的时候要把TX的引脚设为不影响PWM电平的状态,例如设置成输入,下面是我做的修改

void change_mode(uint8_t mode)

{

    GPIO_InitTypeDef GPIO_InitStruct;

    //TX

    if(mode == 1)

    {

        GPIO_InitStruct.Pin = USART2_TX_Pin;

        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

        GPIO_InitStruct.Pull = GPIO_NOPULL;

        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

        GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

        HAL_GPIO_Init(USART2_TX_GPIO_Port, &GPIO_InitStruct);

    }

    else if(mode == 2)

    {

        GPIO_InitStruct.Pin = USART2_TX_Pin;

        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

        GPIO_InitStruct.Pull = GPIO_NOPULL;

        HAL_GPIO_Init(USART2_TX_GPIO_Port, &GPIO_InitStruct);

    }

}

发布了123 篇原创文章 · 获赞 59 · 访问量 168万+

猜你喜欢

转载自blog.csdn.net/shaynerain/article/details/102970041
今日推荐