一、设置RCC时钟
- //#define SYSCLK_HSE
- #define SYSCLK_FREQ_20MHz
- //#define SYSCLK_FREQ_36MHz
- //#define SYSCLK_FREQ_48MHz
- //#define SYSCLK_FREQ_72MHz
- /*******************************************************************************
- * Function Name : SetSysClock
- * Description : Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SetSysClock(void)
- {
- #if defined SYSCLK_HSE
- SetSysClockToHSE();
- #elif defined SYSCLK_FREQ_20MHz
- SetSysClockTo20();
- #elif defined SYSCLK_FREQ_36MHz
- SetSysClockTo36();
- #elif defined SYSCLK_FREQ_48MHz
- SetSysClockTo48();
- #elif defined SYSCLK_FREQ_72MHz
- SetSysClockTo72();
- #endif
- }
SetSysClockToHSE:
- ErrorStatus HSEStartUpStatus;
- /*******************************************************************************
- * Function Name : SetSysClockToHSE
- * Description : Selects HSE as System clock source and configure HCLK,PCLK2 and PCLK1 prescalers.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SetSysClockToHSE(void)
- {
- /* Deinitialize the RCC registers */
- RCC_DeInit();
- /* Enable the HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- /* Wait till HSE is ready and if Time out is reached exit */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* Add here PLL ans system clock config */
-
- /* Enable The Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
-
- /* Configure Tthe Latency cycle: Set 0 Latency cycles */
- FLASH_SetLatency(FLASH_Latency_0);
-
- /* Configure HCLK such as HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
-
- /* PCLK2 = HCLK*/
- RCC_PCLK2Config(RCC_HCLK_Div1);
-
- /* PCLK1 = HCLK*/
- RCC_PCLK1Config(RCC_HCLK_Div1);
-
- /* Select HSE as system clock source*/
- RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
-
- /* Wait till HSE is used as system clock source */
- if(RCC_GetSYSCLKSource() != 0x04)
- {
-
- }
- else
- {
-
- }
- }
- else
- {
- /* If HSE fails to start-up. */
- while(1)
- {
- }
- }
- }
SetSysClockTo20:
- /*******************************************************************************
- * Function Name : SetSysClockTo20
- * Description : Sets System clock frequency to 20MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SetSysClockTo20(void)
- {
- /* 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 0 wait state */
- FLASH_SetLatency(FLASH_Latency_0);
-
- /* HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
-
- /* PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);
-
- /* PCLK1 = HCLK */
- RCC_PCLK1Config(RCC_HCLK_Div1);
-
- /* PLLCLK = (8MHz / 2) * 5 = 20 MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_5);
-
- /* 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 */
- if(RCC_GetSYSCLKSource() != 0x80)
- {
- }
- else
- {
- }
- }
- else
- {
- /* If HSE fails to start-up. */
- while(1)
- {
- }
- }
SetSysClockTo36:
- /*******************************************************************************
- * Function Name : SetSysClockTo36
- * Description : Sets System clock frequency to 36MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SetSysClockTo36(void)
- {
- /* RCC system reset */
- 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 1 wait state */
- FLASH_SetLatency(FLASH_Latency_1);
-
- /* HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
-
- /* PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);
-
- /* PCLK1 = HCLK */
- RCC_PCLK1Config(RCC_HCLK_Div1);
-
- /* PLLCLK = (8MHz / 2) * 9 = 36MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9);
-
- /* 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 */
- if(RCC_GetSYSCLKSource() == 0x08)
- {
-
- }
- else
- {
-
- }
- }
- else
- {
- /* If HSE fails to start-up. */
- while(1)
- {
- }
- }
- }
SetSysClockTo48:
- /*******************************************************************************
- * Function Name : SetSysClockTo48
- * Description : Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SetSysClockTo48(void)
- {
- /* RCC system reset */
- 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 1 wait state */
- FLASH_SetLatency(FLASH_Latency_1);
-
- /* HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
-
- /* PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);
-
- /* PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2);
-
- /* PLLCLK = 8MHz * 6 = 48MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6);
-
- /* 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 */
- if(RCC_GetSYSCLKSource() == 0x08)
- {
-
- }
- else
- {
-
- }
- }
- else
- {
- /* If HSE fails to start-up. */
- while(1)
- {
- }
- }
- }
SetSysClockTo72:
- /*******************************************************************************
- * Function Name : SetSysClockTo72
- * Description : Sets System clock frequency to 72MHz and configure HCLK, PCLK2
- * and PCLK1 prescalers.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SetSysClockTo72(void)
- {
- /* RCC system reset */
- 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 1 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);
-
- /* PLLCLK = 8MHz * 6 = 48MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
-
- /* 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 */
- if(RCC_GetSYSCLKSource() == 0x08)
- {
-
- }
- else
- {
-
- }
- }
- else
- {
- /* If HSE fails to start-up. */
- while(1)
- {
- }
- }
- }
二、中断初始化
- /****************************************************************************
- * Function Name : NVIC_Configuration
- * Description : Configures Vector Table base location.
- * Input : None
- * Output : None
- * Return : None
- ****************************************************************************/
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- #ifdef VET_TAB_RAM
- /* Set the Vector Table base location at 0x2000 0000 */
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #else
- /* Set the Vector Table base location at 0x8000 0000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
- #endif
-
- /* Enable and configure RCC global IRQ channel */
- NVIC_InitStructure.NVIC_IRQChannel = RCC_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
三、GPIO口初始化
- /****************************************************************************
- * Function Name : LED_Init
- * Description : Configures GPIOB
- * Input : None
- * Output : None
- * Return : None
- ****************************************************************************/
- void LED_Init()
- {
- /* Configure all the GPIOB in Output Push-Pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
-
- /* Enable GPIOB clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
-
- /* Initialize GPIOB*/
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
四、main函数
- /****************************************************************************
- * Function Name : main
- * Description : Main program.
- * Input : None
- * Output : None
- * Return : None
- ****************************************************************************/
- int main(void)
- {
- /* Set system clock HCLK PCLK2 PCLK1 and PLLCLK */
- SetSysClock();
-
- /* Get current frequency and fill in RCC_ClockFreq */
- RCC_GetClocksFreq(&RCC_ClockFreq);
-
- /* Enable Clock Security System(CSS), this will generate an NMI exception (Non-Maskable Interrupt)
- when HSE clock fails*/
- RCC_ClockSecuritySystemCmd(ENABLE);
-
- /* NVIC configuration */
- NVIC_Configuration();
-
- /* LED configuration */
- LED_Init();
-
- /* Set the GPIOB prot pin 15 and pin 14*/
- GPIO_SetBits(GPIOB, GPIO_Pin_15 | GPIO_Pin_14);
-
- /* Output HSE clock on MCO pin */
- RCC_MCOConfig(RCC_MCO_HSE);
-
- while(1)
- {
- GPIO_Write(GPIOB, (u16)~GPIO_ReadOutputData(GPIOB));
- Delay(DELAY_COUNT);
- }
- }
10-03 08:39