[STM32L1] STM32L151 使用内部晶振 HSI,设置系统时钟问题

[复制链接]
 楼主| weiyuliang 发表于 2016-6-30 11:54 | 显示全部楼层 |阅读模式
各位好!
在使用STM32L151内部HSI作为系统晶振的时候,遇到一个问题,就是系统时钟配置完成之后,读取系统时钟频率一直是16M, 即使设置了倍频还是一样,
用过这个芯片的朋友帮忙看下,是我那里配置出来问题?谢谢大家了

  1. /*********************************************************************************************************
  2. ** Function name : HSI_Configuration
  3. ** Input Para         :
  4. ** Output Para         :
  5. ** Created by    : weiyuliang 魏玉良
  6. ** Created Date  : 2016年06月30日
  7. ** Descriptions         : 设置系统时钟为HSI
  8. **
  9. ** -------------------------------------------------------------------------------------------------------
  10. ** Modified by   :
  11. ** Modified Date :
  12. ** Descriptions         :
  13. **
  14. ** -------------------------------------------------------------------------------------------------------
  15. **********************************************************************************************************/

  16. void HSI_Configuration(void)
  17. {
  18.         //RCC reset
  19.     RCC_DeInit();
  20.    
  21.     //Enable HSI
  22.     RCC_HSICmd(ENABLE);
  23.    
  24.     //wait HSI
  25.     while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY)== RESET);
  26.        
  27.         RCC_HSEConfig(RCC_HSE_OFF);  
  28.         if(RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET )
  29.         {
  30.                 while(1);
  31.         }
  32.        
  33.         RCC_AdjustHSICalibrationValue(16);
  34.        
  35.     FLASH_PrefetchBufferCmd(ENABLE);
  36.     FLASH_SetLatency(FLASH_Latency_1);
  37.        
  38.         /* Power enable */
  39.     RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  40.         /* Select the Voltage Range 1 (1.8 V) */
  41.     PWR->CR = PWR_CR_VOS_1;
  42.        
  43.        
  44.         RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
  45.     while (RCC_GetSYSCLKSource() != 0x04)
  46.         {
  47.         }

  48.     /* 设置AHB时钟=32 MHz */
  49.     RCC_HCLKConfig(RCC_SYSCLK_Div1);

  50.     /* 设置APB1时钟=32 MHz */
  51.     RCC_PCLK1Config(RCC_SYSCLK_Div1);

  52.     /* 设置APB2时钟=32 MHz */
  53.     RCC_PCLK2Config(RCC_SYSCLK_Div1);

  54.         /* 8M*4/2 = 32M */
  55.     RCC_PLLConfig(RCC_PLLSource_HSI,RCC_PLLMul_4, RCC_PLLDiv_2);
  56.    
  57.     /* Enable PLL */
  58.     RCC_PLLCmd(ENABLE);
  59.        
  60.     /* Wait till PLL is ready */
  61.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  62.     {
  63.     }
  64.        
  65. }



  66. /*********************************************************************************************************
  67. ** Function name : get_sys_clock_fre
  68. ** Input Para         :
  69. ** Output Para         :
  70. ** Created by    : weiyuliang 魏玉良
  71. ** Created Date  : 2016年06月30日
  72. ** Descriptions         : 读取系统时钟频率
  73. **
  74. ** -------------------------------------------------------------------------------------------------------
  75. ** Modified by   :
  76. ** Modified Date :
  77. ** Descriptions         :
  78. **
  79. ** -------------------------------------------------------------------------------------------------------
  80. **********************************************************************************************************/

  81. void get_sys_clock_fre(void)
  82. {
  83.     RCC_ClocksTypeDef get_rcc_clock;
  84.    
  85.     /* 获取系统时钟 */
  86.     RCC_GetClocksFreq(&get_rcc_clock);

  87.    
  88.     /* 输出 系统时钟频率 */
  89.     printf(" SYSCLK %d\n",    get_rcc_clock.SYSCLK_Frequency);
  90.     printf(" HCLK   %d\n"  ,  get_rcc_clock.HCLK_Frequency);
  91.     printf(" PCLK1  %d\n" ,   get_rcc_clock.PCLK1_Frequency);
  92.     printf(" PCLK2  %d\n" ,   get_rcc_clock.PCLK2_Frequency);
  93. }
mmuuss586 发表于 2016-6-30 20:15 | 显示全部楼层
把相应的时钟配置寄存器值读出来,看有没有写成功
 楼主| weiyuliang 发表于 2016-7-5 09:59 | 显示全部楼层
mmuuss586 发表于 2016-6-30 20:15
把相应的时钟配置寄存器值读出来,看有没有写成功

好的,多谢
 楼主| weiyuliang 发表于 2016-7-5 09:59 | 显示全部楼层
找到问题了,
有一个参数设置错了
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
    RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
    RTC_InitStructure.RTC_SynchPrediv = 0x00FF;
谢谢各位!
just_2010 发表于 2020-8-24 16:52 | 显示全部楼层
最近要用到,谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

97

帖子

0

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