STM32F03开发板--系统时钟设置SysTick

首先我先分析下startup_stm32f0xx.s启动代码,其中
/* Call the clock system intitialization function.*/
  bl  SystemInit
/* Call the application\'s entry point.*/
  bl main
发现开发板上电启动过程中,先调用了SystemInit()函数,再进入main()函数。
SystemInit()函数在文件system_stm32f0xx.c中,它的作用是设置系统时钟SYSCLK。
下面是SystemInit()源码:
void SystemInit (void)
{   
  /* Set HSION bit 操作时钟控制寄存器,将内部8M高速时钟使能,从这里可以看出系统启动后是首先依靠内部时钟源而工作的。*/
  RCC->CR |= (uint32_t)0x00000001;
  /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits 其主要设置了MCO(微控制器时钟输出)PLL相关(PLL倍频系数,PLL输入时钟源),ADCPRE(ADC时钟),PPRE,HPRE(AHB预分频系数),SW(系统时钟切换)系统时钟切换到HSI,由它作为系统初始时钟*/
  RCC->CFGR &= (uint32_t)0xF8FFB80C;  
  /* Reset HSEON, CSSON and PLLON bits 先在关闭HSE,CSS,,PLL等的情况下配置好与之相关参数然后开启,达到生效的目的。*/
  RCC->CR &= (uint32_t)0xFEF6FFFF;
  /* Reset HSEBYP bit */
  RCC->CR &= (uint32_t)0xFFFBFFFF;
  /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
  RCC->CFGR &= (uint32_t)0xFFC0FFFF;
  /* Reset PREDIV1[3:0] bits */
  RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;
  /* Reset USARTSW[1:0], I2CSW, CECSW and ADCSW bits */
  RCC->CFGR3 &= (uint32_t)0xFFFFFEAC;
  /* Reset HSI14 bit */
  RCC->CR2 &= (uint32_t)0xFFFFFFFE;
  /* Disable all interrupts */
  RCC->CIR = 0x00000000;
  /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
  SetSysClock();
}

发现函数将RCC registers中与时钟配置相关的寄存器复位Reset ,并使能高速内部时钟(1: HSI oscillator ON),最后调用了SetSysClock()函数;在SetSysClock()函数中检测的是有没有外部时钟源X2:
在这里插入图片描述
由于在板子上没有焊接8MHZ的外部晶振,所以系统使用高速内部时钟源:也是8MHZ
在这里插入图片描述
高速时钟提供给芯片主体的主时钟.低速时钟只是提供给芯片中的RTC(实时时钟)及独立看门狗使用。内部时钟是在芯片内部RC振荡器产生的,起振较快,所以时钟在芯片刚上电的时候,默认使用内部高速时钟。而外部时钟信号是由外部的晶振输入的,在精度和稳定性上都有很大优势,所以上电之后我们再通过软件配置,转而采用外部时钟信号.
STM32有以下4个时钟源:
高速外部时钟(HSE):以外部晶振作时钟源,晶振频率可取范围为4~16MHz,我们一般采用8MHz的晶振。
高速内部时钟(HSI): 由内部RC振荡器产生,频率为8MHz,但不稳定。
低速外部时钟(LSE):以外部晶振作时钟源,主要提供给实时时钟模块,所以一般采用32.768KHz。
低速内部时钟(LSI):由内部RC振荡器产生,也主要提供给实时时钟模块,频率大约为40KHz。

#include \"stm32f0xx.h\"
#include \"stm32f0xx_rcc.h\"
#include \"stm32f0xx_gpio.h\"
uint32_t TimingDelay;
void Systick_Init(void)
{
        if (SysTick_Config(SystemCoreClock / 1000))//设置为 1 毫秒
        {
                while (1);
        }
}
void TimingDelay_Decrement(void)
{
        if (TimingDelay != 0x00)
        {
                TimingDelay--;
        }
}
void Delay_ms(__IO uint32_t nTime)//延迟函数,设置为 MS
{
        TimingDelay = nTime;//时钟滴答数
        while(TimingDelay != 0);
}
void LED_Init()
{
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; /*!< GPIO Input Mode  */
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_2;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOC, &GPIO_InitStructure);
}
int main(void)
{
        Systick_Init();
        LED_Init();
    while(1)
    {
            GPIO_WriteBit(GPIOC, GPIO_Pin_8,
                             (BitAction)((1-GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_8))));
            Delay_ms(1000);
                GPIO_WriteBit(GPIOC, GPIO_Pin_9,
                 (BitAction)((1-GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_9))));
                Delay_ms(1000);
    }
}

转http://home.eeworld.com.cn/my/space-uid-415653-blogid-221238.html

猜你喜欢

转载自blog.csdn.net/ZenNaiHeQiao/article/details/84136392