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

[复制链接]
914|2
 楼主| 肥羊 发表于 2025-3-10 17:04 | 显示全部楼层 |阅读模式
下面是我实现的代码
  1. void uart_Set_Bound_Parity(u32 B_index,u16 P_index )
  2. {
  3.    USART_InitTypeDef USART_InitStructure;
  4.    u32 Bound;
  5.    u16 Parity;
  6.    u32 bound_Arry[7]={9600,14400,19200,38400,56000,57600,115200};
  7.    u16 Parity_Arry[3]={USART_Parity_No,USART_Parity_Even,USART_Parity_Odd};
  8.    Bound=bound_Arry[B_index];
  9.    Parity=Parity_Arry[P_index];
  10.    //while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  11.    USART_Cmd(USART1, DISABLE);
  12.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);        //使能USART1,GPIOA时钟
  13.    USART_InitStructure.USART_BaudRate = Bound;//一般设置为9600;
  14.         if(USART_Parity_No==Parity)
  15.         {
  16.                 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  17.                 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  18.                 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  19.         }
  20.        
  21.         if(USART_Parity_Even==Parity)
  22.         {
  23.                 USART_InitStructure.USART_WordLength = USART_WordLength_9b;//字长为8位数据格式
  24.                 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  25.                 USART_InitStructure.USART_Parity = Parity;//偶校验位
  26.         }

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

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

  36.         USART_Init(USART1, &USART_InitStructure); //初始化串口
  37.         USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
  38.         USART_ITConfig(USART1, USART_IT_TC  , ENABLE);
  39.        
  40.         USART_Cmd(USART1, ENABLE);       

  41. }


  42. //设置波特率和校验位
  43. void uart_Set_Bound_ParityPrioc(void)
  44. {
  45.     u8 old_B_index,old_P_index;
  46.         u8 bounTemp,ParityTemp;
  47.        
  48.         old_B_index=Oldbound;
  49.         old_P_index=OldParity;
  50.         bounTemp=CompRum.boun;
  51.         ParityTemp=CompRum.Parity;
  52.     if((old_B_index!=bounTemp)||(old_P_index!=ParityTemp))
  53.     {
  54.        
  55.                 u32 Bound;
  56.                 u16 Parity;
  57.                 u32 bound_Arry[7]={9600,14400,19200,38400,56000,57600,115200};
  58.                 u16 Parity_Arry[3]={USART_Parity_No,USART_Parity_Even,USART_Parity_Odd};
  59.                 Bound=bound_Arry[CompRum.boun];
  60.                 Parity=Parity_Arry[CompRum.Parity];
  61.                 Oldbound=CompRum.boun;
  62.                 OldParity=CompRum.Parity;
  63.                 uart_Set_Bound_Parity(CompRum.boun,CompRum.Parity);
  64.                 //uart_init1(Bound,Parity);
  65.         }
  66. }

 楼主| 肥羊 发表于 2025-3-11 14:15 | 显示全部楼层
检查了一下自己的程序是其他地方搞错了这程序没有问题
 楼主| 肥羊 发表于 2025-3-11 14:16 | 显示全部楼层
检查了一下自己的程序是其他地方搞错了这程序没有问题
您需要登录后才可以回帖 登录 | 注册

本版积分规则

38

主题

287

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部