[STM32F0] 急急急!STM32F030 锁相环倍频错误

[复制链接]
 楼主| donsbin 发表于 2017-8-31 15:17 | 显示全部楼层 |阅读模式
参考官方DEMO,PLL选择HSE时钟源。外挂8M晶振,HSE一分频,锁相环6倍频。理论值48M
  1. void SetSysClock(void)
  2. {
  3.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  4.   
  5.   /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
  6.   /* Enable HSE */   
  7.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);

  8.   /* Wait till HSE is ready and if Time out is reached exit */
  9.   do
  10.   {
  11.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  12.     StartUpCounter++;  
  13.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));

  14.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  15.   {
  16.     HSEStatus = (uint32_t)0x01;
  17.   }
  18.   else
  19.   {
  20.     HSEStatus = (uint32_t)0x00;
  21.   }  

  22.   if (HSEStatus == (uint32_t)0x01)
  23.   {
  24.     /* Enable Prefetch Buffer and set Flash Latency */
  25.     FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;

  26.     /* HCLK = SYSCLK */
  27.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  28.       
  29.     /* PCLK = HCLK */
  30.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;

  31.     /* PLL configuration = HSE * 6 = 48 MHz */
  32.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  33.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);
  34.             
  35.     /* Enable PLL */
  36.     RCC->CR |= RCC_CR_PLLON;

  37.     /* Wait till PLL is ready */
  38.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  39.     {
  40.     }

  41.     /* Select PLL as system clock source */
  42.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  43.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   

  44.     /* Wait till PLL is used as system clock source */
  45.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
  46.     {
  47.     }
  48.   }
  49.   else
  50.   { /* If HSE fails to start-up, the application will have wrong clock
  51.          configuration. User can add here some code to deal with this error */
  52.   }  
  53. }
通过接口RCC_GetClocksFreq读取时钟配置信息,DEBUG截图如下
M``T_RUK(WOU$WO_W(R4N3H.png
通过反计算,外部晶振变成了13M,此时串口通信数据异常。


配置HSI作为PLL时钟源,串口通信正常。DEBUG数据48M
撒打算的70831151549.png

非常头痛,求帮助




1504163708(1).jpg
东方教主 发表于 2017-8-31 15:26 | 显示全部楼层
晶振电路有问题,先确认晶振是否启振。可剔除匹配电容。
然后看下波形频率,系统时钟都不对,串口波特率肯定偏了030这款片子有点问题,不推荐使用
看看.png

jiekou001 发表于 2017-8-31 15:34 | 显示全部楼层
买看懂,不是你要48M的吗,怎么还头疼。
 楼主| donsbin 发表于 2017-8-31 15:47 | 显示全部楼层
jiekou001 发表于 2017-8-31 15:34
买看懂,不是你要48M的吗,怎么还头疼。

我要配置48M,可结果变成了81M。你都没看我问的问题
 楼主| donsbin 发表于 2017-8-31 15:57 | 显示全部楼层
东方教主 发表于 2017-8-31 15:26
晶振电路有问题,先确认晶振是否启振。可剔除匹配电容。
然后看下波形频率,系统时钟都不对,串口波特率肯 ...

做硬件的已经确认硬件正常。这个应该是软件问题
戈卫东 发表于 2017-8-31 16:01 | 显示全部楼层
没看明白。
不过,新手一般不要指责厂家BUG啥的。。。。。
jiekou001 发表于 2017-8-31 16:42 | 显示全部楼层
donsbin 发表于 2017-8-31 15:47
我要配置48M,可结果变成了81M。你都没看我问的问题

不好意思,没注意图片里的红字,原来写那了。。还真没见过这个。。用的  那个图形化工具配置的时钟吗
xinpian101 发表于 2017-8-31 16:47 | 显示全部楼层
stsw-stm32088.zip (494.36 KB, 下载次数: 7)



xinpian101 发表于 2017-8-31 16:51 | 显示全部楼层
  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=288409]@file[/url]    system_stm32f0xx.c
  4.   * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  5.   * [url=home.php?mod=space&uid=895143]@version[/url] V1.0.1
  6.   * [url=home.php?mod=space&uid=212281]@date[/url]    31-August-2017
  7.   * [url=home.php?mod=space&uid=247401]@brief[/url]   CMSIS Cortex-M0 Device Peripheral Access Layer System Source File.
  8.   *          This file contains the system clock configuration for STM32F0xx devices,
  9.   *          and is generated by the clock configuration tool  
  10.   *          STM32f0xx_Clock_Configuration_V1.0.1.xls
  11.   *
  12.   * 1.  This file provides two functions and one global variable to be called from
  13.   *     user application:
  14.   *      - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
  15.   *                      and Divider factors, AHB/APBx prescalers and Flash settings),
  16.   *                      depending on the configuration made in the clock xls tool.
  17.   *                      This function is called at startup just after reset and
  18.   *                      before branch to main program. This call is made inside
  19.   *                      the "startup_stm32f0xx.s" file.
  20.   *
  21.   *      - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  22.   *                                  by the user application to setup the SysTick
  23.   *                                  timer or configure other parameters.
  24.   *
  25.   *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  26.   *                                 be called whenever the core clock is changed
  27.   *                                 during program execution.
  28.   *
  29.   * 2. After each device reset the HSI (8 MHz Range) is used as system clock source.
  30.   *    Then SystemInit() function is called, in "startup_stm32f0xx.s" file, to
  31.   *    configure the system clock before to branch to main program.
  32.   *
  33.   * 3. If the system clock source selected by user fails to startup, the SystemInit()
  34.   *    function will do nothing and HSI still used as system clock source. User can
  35.   *    add some code to deal with this issue inside the SetSysClock() function.
  36.   *
  37.   * 4. The default value of HSE crystal is set to 8MHz, refer to "HSE_VALUE" define
  38.   *    in "stm32f0xx.h" file. When HSE is used as system clock source, directly or
  39.   *    through PLL, and you are using different crystal you have to adapt the HSE
  40.   *    value to your own configuration.
  41.   *
  42.   * 5. This file configures the system clock as follows:
  43.   *=============================================================================
  44.   *=============================================================================
  45.   *        System Clock source                    | PLL (HSE)
  46.   *-----------------------------------------------------------------------------
  47.   *        SYSCLK(Hz)                             | 48000000
  48.   *-----------------------------------------------------------------------------
  49.   *        HCLK(Hz)                               | 48000000
  50.   *-----------------------------------------------------------------------------
  51.   *        AHB Prescaler                          | 1
  52.   *-----------------------------------------------------------------------------
  53.   *        APB Prescaler                          | 1
  54.   *-----------------------------------------------------------------------------
  55.   *        HSE Frequency(Hz)                      | 8000000
  56.   *----------------------------------------------------------------------------
  57.   *        PLLMUL                                 | 6
  58.   *-----------------------------------------------------------------------------
  59.   *        PREDIV                                 | 1
  60.   *-----------------------------------------------------------------------------
  61.   *        Flash Latency(WS)                      | 1
  62.   *-----------------------------------------------------------------------------
  63.   *        Prefetch Buffer                        | ON
  64.   *-----------------------------------------------------------------------------
  65.   ******************************************************************************
  66.   * @attention
  67.   *
  68.   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  69.   *
  70.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  71.   * You may not use this file except in compliance with the License.
  72.   * You may obtain a copy of the License at:
  73.   *
  74.   *      
  75.   *
  76.   * Unless required by applicable law or agreed to in writing, software
  77.   * distributed under the License is distributed on an "AS IS" BASIS,
  78.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  79.   * See the License for the specific language governing permissions and
  80.   * limitations under the License.
  81.   *
  82.   ******************************************************************************
  83.   */

  84. /** @addtogroup CMSIS
  85.   * @{
  86.   */

  87. /** @addtogroup stm32f0xx_system
  88.   * @{
  89.   */  
  90.   
  91. /** @addtogroup STM32F0xx_System_Private_Includes
  92.   * @{
  93.   */

  94. #include "stm32f0xx.h"

  95. /**
  96.   * @}
  97.   */

  98. /** @addtogroup STM32F0xx_System_Private_TypesDefinitions
  99.   * @{
  100.   */

  101. /**
  102.   * @}
  103.   */

  104. /** @addtogroup STM32F0xx_System_Private_Defines
  105.   * @{
  106.   */
  107. /**
  108.   * @}
  109.   */

  110. /** @addtogroup STM32F0xx_System_Private_Macros
  111.   * @{
  112.   */

  113. /**
  114.   * @}
  115.   */

  116. /** @addtogroup STM32F0xx_System_Private_Variables
  117.   * @{
  118.   */
  119. uint32_t SystemCoreClock    = 48000000;
  120. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};

  121. /**
  122.   * @}
  123.   */

  124. /** @addtogroup STM32F0xx_System_Private_FunctionPrototypes
  125.   * @{
  126.   */

  127. static void SetSysClock(void);

  128. /**
  129.   * @}
  130.   */

  131. /** @addtogroup STM32F0xx_System_Private_Functions
  132.   * @{
  133.   */

  134. /**
  135.   * @brief  Setup the microcontroller system.
  136.   *         Initialize the Embedded Flash Interface, the PLL and update the
  137.   *         SystemCoreClock variable.
  138.   * @param  None
  139.   * @retval None
  140.   */
  141. void SystemInit (void)
  142. {   
  143.   /* Set HSION bit */
  144.   RCC->CR |= (uint32_t)0x00000001;

  145.   /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits */
  146.   RCC->CFGR &= (uint32_t)0xF8FFB80C;
  147.   
  148.   /* Reset HSEON, CSSON and PLLON bits */
  149.   RCC->CR &= (uint32_t)0xFEF6FFFF;

  150.   /* Reset HSEBYP bit */
  151.   RCC->CR &= (uint32_t)0xFFFBFFFF;

  152.   /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
  153.   RCC->CFGR &= (uint32_t)0xFFC0FFFF;

  154.   /* Reset PREDIV1[3:0] bits */
  155.   RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;

  156.   /* Reset USARTSW[1:0], I2CSW, CECSW and ADCSW bits */
  157.   RCC->CFGR3 &= (uint32_t)0xFFFFFEAC;

  158.   /* Reset HSI14 bit */
  159.   RCC->CR2 &= (uint32_t)0xFFFFFFFE;

  160.   /* Disable all interrupts */
  161.   RCC->CIR = 0x00000000;

  162.   /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
  163.   SetSysClock();
  164. }

  165. /**
  166.   * @brief  Update SystemCoreClock according to Clock Register Values
  167.   *         The SystemCoreClock variable contains the core clock (HCLK), it can
  168.   *         be used by the user application to setup the SysTick timer or configure
  169.   *         other parameters.
  170.   *
  171.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]   Each time the core clock (HCLK) changes, this function must be called
  172.   *         to update SystemCoreClock variable value. Otherwise, any configuration
  173.   *         based on this variable will be incorrect.         
  174.   *
  175.   * @note   - The system frequency computed by this function is not the real
  176.   *           frequency in the chip. It is calculated based on the predefined
  177.   *           constant and the selected clock source:
  178.   *
  179.   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  180.   *                                             
  181.   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  182.   *                          
  183.   *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  184.   *             or HSI_VALUE(*) multiplied/divided by the PLL factors.
  185.   *
  186.   *         (*) HSI_VALUE is a constant defined in stm32f0xx.h file (default value
  187.   *             8 MHz) but the real value may vary depending on the variations
  188.   *             in voltage and temperature.
  189.   *
  190.   *         (**) HSE_VALUE is a constant defined in stm32f0xx.h file (default value
  191.   *              8 MHz), user has to ensure that HSE_VALUE is same as the real
  192.   *              frequency of the crystal used. Otherwise, this function may
  193.   *              have wrong result.
  194.   *
  195.   *         - The result of this function could be not correct when using fractional
  196.   *           value for HSE crystal.
  197.   * @param  None
  198.   * @retval None
  199.   */
  200. void SystemCoreClockUpdate (void)
  201. {
  202.   uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0;

  203.   /* Get SYSCLK source -------------------------------------------------------*/
  204.   tmp = RCC->CFGR & RCC_CFGR_SWS;
  205.   
  206.   switch (tmp)
  207.   {
  208.     case 0x00:  /* HSI used as system clock */
  209.       SystemCoreClock = HSI_VALUE;
  210.       break;
  211.     case 0x04:  /* HSE used as system clock */
  212.       SystemCoreClock = HSE_VALUE;
  213.       break;
  214.     case 0x08:  /* PLL used as system clock */
  215.       /* Get PLL clock source and multiplication factor ----------------------*/
  216.       pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  217.       pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  218.       pllmull = ( pllmull >> 18) + 2;
  219.       
  220.       if (pllsource == 0x00)
  221.       {
  222.         /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  223.         SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  224.       }
  225.       else
  226.       {
  227.         prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  228.         /* HSE oscillator clock selected as PREDIV1 clock entry */
  229.         SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  230.       }      
  231.       break;
  232.     default: /* HSI used as system clock */
  233.       SystemCoreClock = HSI_VALUE;
  234.       break;
  235.   }
  236.   /* Compute HCLK clock frequency ----------------*/
  237.   /* Get HCLK prescaler */
  238.   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  239.   /* HCLK clock frequency */
  240.   SystemCoreClock >>= tmp;  
  241. }

  242. /**
  243.   * @brief  Configures the System clock frequency, AHB/APBx prescalers and Flash
  244.   *         settings.
  245.   * @note   This function should be called only once the RCC clock configuration
  246.   *         is reset to the default reset state (done in SystemInit() function).
  247.   * @param  None
  248.   * @retval None
  249.   */
  250. static void SetSysClock(void)
  251. {
  252.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;

  253. /******************************************************************************/
  254. /*            PLL (clocked by HSE) used as System clock source                */
  255. /******************************************************************************/

  256.   /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
  257.   /* Enable HSE */   
  258.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);

  259.   /* Wait till HSE is ready and if Time out is reached exit */
  260.   do
  261.   {
  262.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  263.     StartUpCounter++;  
  264.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));

  265.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  266.   {
  267.     HSEStatus = (uint32_t)0x01;
  268.   }
  269.   else
  270.   {
  271.     HSEStatus = (uint32_t)0x00;
  272.   }  

  273.   if (HSEStatus == (uint32_t)0x01)
  274.   {
  275.     /* Enable Prefetch Buffer and set Flash Latency */
  276.     FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;

  277.      /* HCLK = SYSCLK / 1 */
  278.      RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  279.       
  280.      /* PCLK = HCLK / 1 */
  281.      RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;

  282.     /* PLL configuration */
  283.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  284.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);

  285.     /* Enable PLL */
  286.     RCC->CR |= RCC_CR_PLLON;

  287.     /* Wait till PLL is ready */
  288.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  289.     {
  290.     }

  291.     /* Select PLL as system clock source */
  292.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  293.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   

  294.     /* Wait till PLL is used as system clock source */
  295.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
  296.     {
  297.     }
  298.   }
  299.   else
  300.   { /* If HSE fails to start-up, the application will have wrong clock
  301.          configuration. User can add here some code to deal with this error */
  302.   }
  303. }

  304. /**
  305.   * @}
  306.   */

  307. /**
  308.   * @}
  309.   */

  310. /**
  311.   * @}
  312.   */

  313. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
xinpian101 发表于 2017-8-31 16:52 | 显示全部楼层
hah ,我用工具生成的代码。、应该错不了。你对比看看你哪儿漏了。
 楼主| donsbin 发表于 2017-8-31 17:48 | 显示全部楼层
xinpian101 发表于 2017-8-31 16:52
hah ,我用工具生成的代码。、应该错不了。你对比看看你哪儿漏了。

代码是一样的,都是官方DEMO
xinpian101 发表于 2017-8-31 18:30 | 显示全部楼层
donsbin 发表于 2017-8-31 17:48
代码是一样的,都是官方DEMO

那就是说代码是没问题的了。而且这个单片机据说只能达到48M,你怎么搞的那么大的,不科学吖
 楼主| donsbin 发表于 2017-8-31 18:46 | 显示全部楼层
mmuuss586 发表于 2017-8-31 20:17 | 显示全部楼层
看了下代码没啥问题,你一句句跟踪下,是不是哪里又初始化了一变,覆盖掉了
ADaiPlay 发表于 2017-9-1 14:34 | 显示全部楼层
本帖最后由 ADaiPlay 于 2017-9-1 14:43 编辑

帮顶
 楼主| donsbin 发表于 2017-9-1 17:22 | 显示全部楼层
mmuuss586 发表于 2017-8-31 20:17
看了下代码没啥问题,你一句句跟踪下,是不是哪里又初始化了一变,覆盖掉了 ...

用MDK跑了下代码,读取配置正常 48M。 SJ0$](`T1@CBAANNUYYKA.png
原先用的是IAR跑得,目前问题还没找到
mmuuss586 发表于 2017-9-1 18:32 | 显示全部楼层
donsbin 发表于 2017-9-1 17:22
用MDK跑了下代码,读取配置正常 48M。
原先用的是IAR跑得,目前问题还没找到
...

那就是编译方面问题了;

代码都一样的
usstwk 发表于 2017-9-1 23:00 | 显示全部楼层
yjmwxwx 发表于 2017-9-3 15:17 | 显示全部楼层
本帖最后由 yjmwxwx 于 2017-9-3 15:25 编辑
donsbin 发表于 2017-9-1 17:22
用MDK跑了下代码,读取配置正常 48M。
原先用的是IAR跑得,目前问题还没找到
...
RCC_CFGR(0X40021004)里是什么?

 楼主| donsbin 发表于 2017-9-8 15:43 | 显示全部楼层

CFGR 寄存器,与配置信息一致。 PLLx6  PLL为系统时钟
CFGR.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:思亘七险,点落九宫,神游八极,纵横十方。

20

主题

96

帖子

2

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