打印
[STM32F1]

求教STM32修改官方库分频以及自设分频两种情况电流不同原因

[复制链接]
860|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
夜雨声烦、|  楼主 | 2016-1-12 21:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
运行STM32F105,由于外部晶振是12MHZ,所以对官方库进行修改:
首先stm32f10x.h修改晶振为#define HSE_VALUE    ((uint32_t)12000000)。
想把主频设置为48MHz,system_stm32f10x.c文件修改函数
static void SetSysClockTo48(void)
{
  __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  
  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
  /* Enable HSE */   
  RCC->CR |= ((uint32_t)RCC_CR_HSEON);

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

  if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  {
    HSEStatus = (uint32_t)0x01;
  }
  else
  {
    HSEStatus = (uint32_t)0x00;
  }  

  if (HSEStatus == (uint32_t)0x01)
  {
    /* Enable Prefetch Buffer */
    FLASH->ACR |= FLASH_ACR_PRFTBE;

    /* Flash 1 wait state */
    FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
    FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1;   

    /* HCLK = SYSCLK */
    RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
      
    /* PCLK2 = HCLK */
    RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
   
    /* PCLK1 = HCLK */
    RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
   
#ifdef STM32F10X_CL
    /* Configure PLLs ------------------------------------------------------*/
    /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
    /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
        
    RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
                              RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);

                if (HSE_VALUE == 25000000)
                {
                        /* Configure PLLs ------------------------------------------------------*/
                        /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
                        /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */

                        RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
                                                                                                                         RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
                }
                else if (HSE_VALUE == 8000000)
                {
                        /* Configure PLLs ------------------------------------------------------*/
                        /* PLL2 configuration: PLL2CLK = (HSE / 2) * 10 = 40 MHz */
                        /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
                        RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV2 | RCC_CFGR2_PLL2MUL10 |
                                                                                                                         RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
                }
                else if (HSE_VALUE == 12000000)
                {
                        /* Configure PLLs ------------------------------------------------------*/
                        /* PLL2 configuration: PLL2CLK = (HSE / 3) * 10 = 40 MHz */
                        /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
                        RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV3 | RCC_CFGR2_PLL2MUL10 |
                                                                                                                         RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
                }
                else
                {               
                        /* Configure PLLs ------------------------------------------------------*/
                        /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
                        /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */

                        RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
                                                                                                                         RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
                }
  
    /* Enable PLL2 */
    RCC->CR |= RCC_CR_PLL2ON;
    /* Wait till PLL2 is ready */
    while((RCC->CR & RCC_CR_PLL2RDY) == 0)
    {
    }
   
    /* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */
    RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
                            RCC_CFGR_PLLMULL6);
#else   
    /*  PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
    RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);
#endif /* STM32F10X_CL */

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

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

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

    /* Wait till PLL is used as system clock source */
    while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
    {
    }
  }
  else
  { /* If HSE fails to start-up, the application will have wrong clock
         configuration. User can add here some code to deal with this error */
  }
}

通过函数
   RCC_ClocksTypeDef rcc_clk;
  RCC_GetClocksFreq(&rcc_clk);
获取到时钟分频正确且MCU运行正常(如串口输出,定时器中断)

然后自己写了个函数设置分频
void Set_System(void)
{
    ErrorStatus HSEStartUpStatus;

    /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/   
    /* RCC system reset(for debug purpose) */
    RCC_DeInit();

    /* Enable HSE */
    RCC_HSEConfig(RCC_HSE_ON);

    /* Wait till HSE is ready */
    HSEStartUpStatus = RCC_WaitForHSEStartUp();

    if (HSEStartUpStatus == SUCCESS)
    {
        /* Enable Prefetch Buffer */
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        /* Flash 2 wait state */
        FLASH_SetLatency(FLASH_Latency_2);

        /* HCLK = SYSCLK */
        RCC_HCLKConfig(RCC_SYSCLK_Div1);

        /* PCLK2 = HCLK */
        RCC_PCLK2Config(RCC_HCLK_Div1);

        /* PCLK1 = HCLK/2 */
        RCC_PCLK1Config(RCC_HCLK_Div2);

#ifdef STM32F10X_CL
        /* Configure PLLs *********************************************************/
        /* PLL2 configuration: PLL2CLK = (HSE / 3) * 10 = 40 MHz */
                                /* HSE_VALUE = 120000000 HZ, stm32f10x.h */
        RCC_PREDIV2Config(RCC_PREDIV2_Div3);
        RCC_PLL2Config(RCC_PLL2Mul_10);

        /* Enable PLL2 */
        RCC_PLL2Cmd(ENABLE);

        /* Wait till PLL2 is ready */
        while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET)
        {}

        /* PLL configuration: PLLCLK = (PLL2 / 5) * 6 = 48 MHz */
        RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
        RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_6);
#else
        /* PLLCLK = 8MHz * 9 = 72 MHz */
                                RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
#endif
        /* Enable PLL */
        RCC_PLLCmd(ENABLE);

        /* Wait till PLL is ready */
        while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
        {
        }

        /* Select PLL as system clock source */
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        /* Wait till PLL is used as system clock source */
        while(RCC_GetSYSCLKSource() != 0x08)
        {
        }
                                
      RCC_OTGFSCLKConfig(RCC_OTGFSCLKSource_PLLVCO_Div3);
    }
    else
    {
        /* If HSE fails to start-up, the application will have wrong clock configuration.
        User can add here some code to deal with this error */   

        /* Go to infinite loop */
        while (1)
        {
        }
    }
}
这个效果和修改官方库一样,分频正确且MCU运行正常,但是比较电流发现,修改官方库的方法比自己写的分频函数电流小了2个ma。
求教有经验的大神指点原因?
沙发
shashaa| | 2016-1-12 22:14 | 只看该作者
while((RCC->CR & RCC_CR_PLL2RDY) == 0)
    {
    }
这个while循环感觉没什么用啊

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

2

帖子

0

粉丝