STM8 串口波特率与设置值不一致问题

最近调试STM8L串口通讯,发现设置了波特率115200,实际波特率却是57600。心想这种问题一般是串口的时钟分频设置不对,找了半天没有找到单独设置串口时钟的地方,费了不少时间。

void Uart1_Configer(void)
{

    CLK_PeripheralClockConfig (CLK_Peripheral_USART3,ENABLE);   //开启ADC/USART时钟
    GPIO_Init(GPIOA, GPIO_Pin_2, GPIO_Mode_Out_PP_High_Fast);   //TX
    GPIO_Init(GPIOA, GPIO_Pin_3, GPIO_Mode_In_FL_No_IT);        //RX
    USART_Init(USART1,115200,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,
               (USART_Mode_TypeDef)(USART_Mode_Rx|USART_Mode_Tx));  //USART初始化
    USART_ClearITPendingBit(USART1,USART_IT_RXNE);
    ITC_SetSoftwarePriority(USART1_RX_IRQn,ITC_PriorityLevel_3);    //优先级最高
    USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
    USART_Cmd (USART1,ENABLE);  //使能USART
}

最后发现在固件库文件里有设置外部晶振频率的宏定义,这个坑确实大。

把HSE_VALUE由16000000改成8000000就好了。

/**
 * @brief In the following line adjust the value of External High Speed oscillator (HSE)
   used in your application

   Tip: To avoid modifying this file each time you need to use different HSE, you
        can define the HSE value in your toolchain compiler preprocessor.
  */
#if !defined  HSE_Value
 #define HSE_VALUE   ((uint32_t)16000000) /*!< Typical Value of the HSE in Hz */
#endif /* HSE_Value */