打印
[STM32F1]

如何STM32标准库串口运行中修改波特率?

[复制链接]
418|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
肥羊|  楼主 | 2025-3-10 17:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下面是我实现的代码
void uart_Set_Bound_Parity(u32 B_index,u16 P_index )
{
   USART_InitTypeDef USART_InitStructure;
   u32 Bound;
   u16 Parity;
   u32 bound_Arry[7]={9600,14400,19200,38400,56000,57600,115200};
   u16 Parity_Arry[3]={USART_Parity_No,USART_Parity_Even,USART_Parity_Odd};
   Bound=bound_Arry[B_index];
   Parity=Parity_Arry[P_index];
   //while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
   USART_Cmd(USART1, DISABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);        //使能USART1,GPIOA时钟
   USART_InitStructure.USART_BaudRate = Bound;//一般设置为9600;
        if(USART_Parity_No==Parity)
        {
                USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
                USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
                USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
        }
       
        if(USART_Parity_Even==Parity)
        {
                USART_InitStructure.USART_WordLength = USART_WordLength_9b;//字长为8位数据格式
                USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
                USART_InitStructure.USART_Parity = Parity;//偶校验位
        }

        if(USART_Parity_Odd==Parity)
        {
                USART_InitStructure.USART_WordLength = USART_WordLength_9b;//字长为8位数据格式
                USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
                USART_InitStructure.USART_Parity = Parity;//奇校验位
        }
       

        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;        //收发模式

        USART_Init(USART1, &USART_InitStructure); //初始化串口
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
        USART_ITConfig(USART1, USART_IT_TC  , ENABLE);
       
        USART_Cmd(USART1, ENABLE);       

}


//设置波特率和校验位
void uart_Set_Bound_ParityPrioc(void)
{
    u8 old_B_index,old_P_index;
        u8 bounTemp,ParityTemp;
       
        old_B_index=Oldbound;
        old_P_index=OldParity;
        bounTemp=CompRum.boun;
        ParityTemp=CompRum.Parity;
    if((old_B_index!=bounTemp)||(old_P_index!=ParityTemp))
    {
       
                u32 Bound;
                u16 Parity;
                u32 bound_Arry[7]={9600,14400,19200,38400,56000,57600,115200};
                u16 Parity_Arry[3]={USART_Parity_No,USART_Parity_Even,USART_Parity_Odd};
                Bound=bound_Arry[CompRum.boun];
                Parity=Parity_Arry[CompRum.Parity];
                Oldbound=CompRum.boun;
                OldParity=CompRum.Parity;
                uart_Set_Bound_Parity(CompRum.boun,CompRum.Parity);
                //uart_init1(Bound,Parity);
        }
}

使用特权

评论回复
沙发
肥羊|  楼主 | 2025-3-11 14:15 | 只看该作者
检查了一下自己的程序是其他地方搞错了这程序没有问题

使用特权

评论回复
板凳
肥羊|  楼主 | 2025-3-11 14:16 | 只看该作者
检查了一下自己的程序是其他地方搞错了这程序没有问题

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

38

主题

286

帖子

1

粉丝