你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

关于STM32L476时钟设置使得串口波特率加倍问题

[复制链接]
天堂隔壁 提问时间:2017-9-9 14:49 /
阅读主题, 点击返回1楼
收藏 2 评论10 发布时间:2017-9-9 14:49
10个回答
nyszx 回答时间:2018-1-19 16:40:33
1、首先对楼主的一些建议:想要外设测试样例,不要去网上找,网上的根本没保证,就简单最可靠的就是去STM32CubeMX库里面找每个系列都对应的官方开发板,都有外设的驱动样例。以STM32L476RG-Nucleo为例,打开里面有很多代码可以学习参考:
QQ截图20180119161723.jpg
各种外设驱动样例:
QQ截图20180119161713.jpg
2、其次,楼主只贴了RCC代码,无法明确问题原因,只能帮楼主分析下,如果使用完整的HAL驱动的话,串口初始化,会执行底层的HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)函数,里面会获取UART时钟源,并根据时钟源去查找系统当前时钟,进而进行波特率等更时钟相关的寄存器设置,而程序是如何知道系统当前时钟的呢?答案是stm32l4xx_hal_conf.h中定义的,并不是神奇的软件能够自动识别硬件得到的,只是根据RCC寄存器值获取分频、倍频等参数状态+基础时钟(HSE\HSI\LSE\LSI等)计算出来的,关键的就在这些定义上,如:
  1. /* ########################## Oscillator Values adaptation ####################*/
  2. /**
  3.   * @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
  4.   *        This value is used by the RCC HAL module to compute the system frequency
  5.   *        (when HSE is used as system clock source, directly or through the PLL).
  6.   */
  7. #if !defined  (HSE_VALUE)
  8.   #define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
  9. #endif /* HSE_VALUE */

  10. #if !defined  (HSE_STARTUP_TIMEOUT)
  11.   #define HSE_STARTUP_TIMEOUT    ((uint32_t)100)   /*!< Time out for HSE start up, in ms */
  12. #endif /* HSE_STARTUP_TIMEOUT */

  13. /**
  14.   * @brief Internal Multiple Speed oscillator (MSI) default value.
  15.   *        This value is the default MSI range value after Reset.
  16.   */
  17. #if !defined  (MSI_VALUE)
  18.   #define MSI_VALUE    ((uint32_t)4000000) /*!< Value of the Internal oscillator in Hz*/
  19. #endif /* MSI_VALUE */

  20. /**
  21.   * @brief Internal High Speed oscillator (HSI) value.
  22.   *        This value is used by the RCC HAL module to compute the system frequency
  23.   *        (when HSI is used as system clock source, directly or through the PLL).
  24.   */
  25. #if !defined  (HSI_VALUE)
  26.   #define HSI_VALUE    ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
  27. #endif /* HSI_VALUE */

  28. /**
  29.   * @brief Internal Low Speed oscillator (LSI) value.
  30.   */
  31. #if !defined  (LSI_VALUE)
  32. #define LSI_VALUE  ((uint32_t)32000)       /*!< LSI Typical Value in Hz*/
  33. #endif /* LSI_VALUE */                      /*!< Value of the Internal Low Speed oscillator in Hz
  34.                                              The real value may vary depending on the variations
  35.                                              in voltage and temperature.*/
  36. /**
  37.   * @brief External Low Speed oscillator (LSE) value.
  38.   *        This value is used by the UART, RTC HAL module to compute the system frequency
  39.   */
  40. #if !defined  (LSE_VALUE)
  41.   #define LSE_VALUE    ((uint32_t)32768) /*!< Value of the External oscillator in Hz*/
  42. #endif /* LSE_VALUE */

  43. #if !defined  (LSE_STARTUP_TIMEOUT)
  44.   #define LSE_STARTUP_TIMEOUT    ((uint32_t)5000)   /*!< Time out for LSE start up, in ms */
  45. #endif /* HSE_STARTUP_TIMEOUT */

  46. /**
  47.   * @brief External clock source for SAI1 peripheral
  48.   *        This value is used by the RCC HAL module to compute the SAI1 & SAI2 clock source
  49.   *        frequency.
  50.   */
  51. #if !defined  (EXTERNAL_SAI1_CLOCK_VALUE)
  52.   #define EXTERNAL_SAI1_CLOCK_VALUE    ((uint32_t)48000) /*!< Value of the SAI1 External clock source in Hz*/
  53. #endif /* EXTERNAL_SAI1_CLOCK_VALUE */

  54. /**
  55.   * @brief External clock source for SAI2 peripheral
  56.   *        This value is used by the RCC HAL module to compute the SAI1 & SAI2 clock source
  57.   *        frequency.
  58.   */
  59. #if !defined  (EXTERNAL_SAI2_CLOCK_VALUE)
  60.   #define EXTERNAL_SAI2_CLOCK_VALUE    ((uint32_t)48000) /*!< Value of the SAI2 External clock source in Hz*/
  61. #endif /* EXTERNAL_SAI2_CLOCK_VALUE */
复制代码
比如如果你使用外部8M高速晶振,你需要修改HSE_VALUE为8000000,假设你实际使用的外部晶振为12M,而你的HSE_VALUE仍为8000000,系统也会跑起来,但是通过HAL_RCC_GetSysClockFreq()等获取时钟的函数得到的值就不对了。也许这就是问题原因的所在,希望楼主能解决此问题。

评分

参与人数 1蝴蝶豆 +5 收起 理由
zero99 + 5

查看全部评分

12
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版