时钟是一个MCU的脉搏,研究清楚脉搏才能更清楚的把握整个MCU的运行。本文主要研究STM32F10x系列,利用官方库文件进行初始化设置。开发环境为MDK4.6,库文件V3.5版本,STM32参考手册。
利用MDK自带仿真器,仿真发现。芯片启动首先打开system_stmf10x.c文件,调用void SystemInit(void)函数。下面贴上代码和中文注释
- void SystemInit (void)
- {
- /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
- /* Set HSION bit */
- RCC->CR |= (uint32_t)0x00000001; //内部高速时钟使能
-
-
- /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
- //SW:HSI(内部高速8M RC震荡器)作为时钟源
- //AHB(SYSCLK)不分频
- //APB1,APB2不分频
- // PCLK2 2分频后作为ADC时钟
- // HSI时钟2分频后作为PLL输入时钟
- //MCO没有时钟输出
- RCC->CFGR &= (uint32_t)0xF8FF0000;
-
-
- /* Reset HSEON, CSSON and PLLON bits */
- //HSE振荡器关闭, 时钟安全系统监测器关闭PLL关闭
- RCC->CR &= (uint32_t)0xFEF6FFFF;
-
-
- /* Reset HSEBYP bit */
- //清零来旁路外部晶体振荡器。只有在外部4-25MHz振荡器关闭的情况下,才能写入该位。
- RCC->CR &= (uint32_t)0xFFFBFFFF;
-
-
- /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
- //HSI时钟2分频后作为PLL输入时钟
- //HSE不分频器作为PLL输入
- //0000:PLL 2倍频输出
- //0:PLL时钟1.5倍分频作为USB时钟
- RCC->CFGR &= (uint32_t)0xFF80FFFF;
-
-
- /* Disable all interrupts and clear pending bits */
- RCC->CIR = 0x009F0000;
-
-
- /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
- /* Configure the Flash Latency cycles and enable prefetch buffer */
- SetSysClock();
-
-
- SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
- }
|