Chinaunix首页 | 论坛 | 博客
  • 博客访问: 493865
  • 博文数量: 223
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2145
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-01 10:23
个人简介

该坚持的时候坚持,该妥协的时候妥协,该放弃的时候放弃

文章分类

全部博文(223)

文章存档

2017年(56)

2016年(118)

2015年(3)

2014年(46)

我的朋友

分类: 嵌入式

2017-02-19 20:06:11

一、设置RCC时钟

  1. //#define SYSCLK_HSE
  2. #define SYSCLK_FREQ_20MHz
  3. //#define SYSCLK_FREQ_36MHz
  4. //#define SYSCLK_FREQ_48MHz
  5. //#define SYSCLK_FREQ_72MHz

  6. /*******************************************************************************
  7. * Function Name : SetSysClock
  8. * Description : Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  9. * Input : None
  10. * Output : None
  11. * Return : None
  12. *******************************************************************************/
  13. void SetSysClock(void)
  14. {
  15. #if defined SYSCLK_HSE
  16.         SetSysClockToHSE();
  17. #elif defined SYSCLK_FREQ_20MHz
  18.         SetSysClockTo20();
  19. #elif defined SYSCLK_FREQ_36MHz
  20.         SetSysClockTo36();
  21. #elif defined SYSCLK_FREQ_48MHz
  22.         SetSysClockTo48();
  23. #elif defined SYSCLK_FREQ_72MHz
  24.         SetSysClockTo72();
  25. #endif
  26. }
SetSysClockToHSE:
  1. ErrorStatus HSEStartUpStatus;
  2. /*******************************************************************************
  3. * Function Name : SetSysClockToHSE
  4. * Description : Selects HSE as System clock source and configure HCLK,PCLK2 and PCLK1 prescalers.
  5. * Input : None
  6. * Output : None
  7. * Return : None
  8. *******************************************************************************/
  9. void SetSysClockToHSE(void)
  10. {
  11.     /* Deinitialize the RCC registers */
  12.     RCC_DeInit();
  13.     /* Enable the HSE */
  14.     RCC_HSEConfig(RCC_HSE_ON);
  15.     /* Wait till HSE is ready and if Time out is reached exit */
  16.     HSEStartUpStatus = RCC_WaitForHSEStartUp();
  17.     if(HSEStartUpStatus == SUCCESS)
  18.     {
  19.         /* Add here PLL ans system clock config */
  20.         
  21.         /* Enable The Prefetch Buffer */
  22.         FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  23.         
  24.         /* Configure Tthe Latency cycle: Set 0 Latency cycles */
  25.         FLASH_SetLatency(FLASH_Latency_0);
  26.         
  27.         /* Configure HCLK such as HCLK = SYSCLK */
  28.         RCC_HCLKConfig(RCC_SYSCLK_Div1);
  29.         
  30.         /* PCLK2 = HCLK*/
  31.         RCC_PCLK2Config(RCC_HCLK_Div1);
  32.         
  33.         /* PCLK1 = HCLK*/
  34.         RCC_PCLK1Config(RCC_HCLK_Div1);
  35.         
  36.         /* Select HSE as system clock source*/
  37.         RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
  38.         
  39.         /* Wait till HSE is used as system clock source */
  40.         if(RCC_GetSYSCLKSource() != 0x04)
  41.         {
  42.             
  43.         }
  44.         else
  45.         {
  46.             
  47.         }
  48.     }
  49.     else
  50.     {
  51.         /* If HSE fails to start-up. */
  52.         while(1)
  53.         {
  54.         }
  55.     }
  56. }
SetSysClockTo20:
  1. /*******************************************************************************
  2. * Function Name : SetSysClockTo20
  3. * Description : Sets System clock frequency to 20MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. *******************************************************************************/
  8. void SetSysClockTo20(void)
  9. {
  10.     /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/
  11.     /* RCC system reset(for debug purpose) */
  12.     RCC_DeInit();    
  13.     
  14.     /* Enable HSE */
  15.     RCC_HSEConfig(RCC_HSE_ON);
  16.     
  17.     /* Wait till HSE is ready */
  18.     HSEStartUpStatus = RCC_WaitForHSEStartUp();
  19.     if(HSEStartUpStatus == SUCCESS)
  20.     {
  21.      /* Enable Prefetch Buffer */
  22.         FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  23.         
  24.         /* Flash 0 wait state */
  25.         FLASH_SetLatency(FLASH_Latency_0);
  26.         
  27.      /* HCLK = SYSCLK */
  28.         RCC_HCLKConfig(RCC_SYSCLK_Div1);
  29.         
  30.      /* PCLK2 = HCLK */
  31.         RCC_PCLK2Config(RCC_HCLK_Div1);
  32.         
  33.      /* PCLK1 = HCLK */
  34.         RCC_PCLK1Config(RCC_HCLK_Div1);
  35.         
  36.      /* PLLCLK = (8MHz / 2) * 5 = 20 MHz */
  37.         RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_5);
  38.         
  39.         /* Enable PLL */
  40.         RCC_PLLCmd(ENABLE);
  41.         
  42.      /* Wait till PLL is ready */
  43.         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  44.         {
  45.             
  46.         }
  47.         
  48.      /* Select PLL as system clock source */
  49.         RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  50.         
  51.      /* Wait till PLL is used as system clock source */
  52.         if(RCC_GetSYSCLKSource() != 0x80)
  53.         {
  54.         }
  55.         else
  56.         {
  57.         }
  58.     }
  59.     else
  60.     {
  61.         /* If HSE fails to start-up. */
  62.         while(1)
  63.         {
  64.         }
  65.     }
SetSysClockTo36:
  1. /*******************************************************************************
  2. * Function Name : SetSysClockTo36
  3. * Description : Sets System clock frequency to 36MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. *******************************************************************************/
  8. void SetSysClockTo36(void)
  9. {
  10.     /* RCC system reset */
  11.     RCC_DeInit();
  12.     /* Enable HSE */
  13.     RCC_HSEConfig(RCC_HSE_ON);
  14.     
  15.     /* Wait till HSE is ready */
  16.     HSEStartUpStatus = RCC_WaitForHSEStartUp();
  17.     if(HSEStartUpStatus == SUCCESS)
  18.     {
  19.         /* Enable Prefetch Buffer*/
  20.         FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  21.         
  22.         /* Flash 1 wait state */
  23.         FLASH_SetLatency(FLASH_Latency_1);
  24.         
  25.         /* HCLK = SYSCLK */
  26.         RCC_HCLKConfig(RCC_SYSCLK_Div1);
  27.         
  28.         /* PCLK2 = HCLK */
  29.         RCC_PCLK2Config(RCC_HCLK_Div1);
  30.         
  31.         /* PCLK1 = HCLK */
  32.         RCC_PCLK1Config(RCC_HCLK_Div1);
  33.         
  34.         /* PLLCLK = (8MHz / 2) * 9 = 36MHz */
  35.         RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9);
  36.         
  37.         /* Enable PLL */
  38.         RCC_PLLCmd(ENABLE);
  39.         
  40.         /* Wait till PLL is ready */
  41.         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  42.         
  43.         /* Select PLL as system clock source */
  44.         RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  45.         
  46.         /* Wait till PLL is used as system clock source */
  47.         if(RCC_GetSYSCLKSource() == 0x08)
  48.         {
  49.             
  50.         }
  51.         else
  52.         {
  53.             
  54.         }
  55.     }
  56.     else
  57.     {
  58.         /* If HSE fails to start-up. */
  59.         while(1)
  60.         {
  61.         }
  62.     }
  63. }
SetSysClockTo48:
  1. /*******************************************************************************
  2. * Function Name : SetSysClockTo48
  3. * Description : Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. *******************************************************************************/
  8. void SetSysClockTo48(void)
  9. {
  10.     /* RCC system reset */
  11.     RCC_DeInit();
  12.     /* Enable HSE */
  13.     RCC_HSEConfig(RCC_HSE_ON);
  14.     
  15.     /* Wait till HSE is ready */
  16.     HSEStartUpStatus = RCC_WaitForHSEStartUp();
  17.     if(HSEStartUpStatus == SUCCESS)
  18.     {
  19.         /* Enable Prefetch Buffer*/
  20.         FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  21.         
  22.         /* Flash 1 wait state */
  23.         FLASH_SetLatency(FLASH_Latency_1);
  24.         
  25.         /* HCLK = SYSCLK */
  26.         RCC_HCLKConfig(RCC_SYSCLK_Div1);
  27.         
  28.         /* PCLK2 = HCLK */
  29.         RCC_PCLK2Config(RCC_HCLK_Div1);
  30.         
  31.         /* PCLK1 = HCLK/2 */
  32.         RCC_PCLK1Config(RCC_HCLK_Div2);
  33.         
  34.         /* PLLCLK = 8MHz * 6 = 48MHz */
  35.         RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6);
  36.         
  37.         /* Enable PLL */
  38.         RCC_PLLCmd(ENABLE);
  39.         
  40.         /* Wait till PLL is ready */
  41.         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  42.         
  43.         /* Select PLL as system clock source */
  44.         RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  45.         
  46.         /* Wait till PLL is used as system clock source */
  47.         if(RCC_GetSYSCLKSource() == 0x08)
  48.         {
  49.             
  50.         }
  51.         else
  52.         {
  53.             
  54.         }
  55.     }
  56.     else
  57.     {
  58.         /* If HSE fails to start-up. */
  59.         while(1)
  60.         {
  61.         }
  62.     }    
  63. }
SetSysClockTo72:
  1. /*******************************************************************************
  2. * Function Name : SetSysClockTo72
  3. * Description : Sets System clock frequency to 72MHz and configure HCLK, PCLK2
  4. * and PCLK1 prescalers.
  5. * Input : None
  6. * Output : None
  7. * Return : None
  8. *******************************************************************************/
  9. void SetSysClockTo72(void)
  10. {
  11.     /* RCC system reset */
  12.     RCC_DeInit();
  13.     /* Enable HSE */
  14.     RCC_HSEConfig(RCC_HSE_ON);
  15.     
  16.     /* Wait till HSE is ready */
  17.     HSEStartUpStatus = RCC_WaitForHSEStartUp();
  18.     if(HSEStartUpStatus == SUCCESS)
  19.     {
  20.         /* Enable Prefetch Buffer*/
  21.         FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  22.         
  23.         /* Flash 1 wait state */
  24.         FLASH_SetLatency(FLASH_Latency_2);
  25.         
  26.         /* HCLK = SYSCLK */
  27.         RCC_HCLKConfig(RCC_SYSCLK_Div1);
  28.         
  29.         /* PCLK2 = HCLK */
  30.         RCC_PCLK2Config(RCC_HCLK_Div1);
  31.         
  32.         /* PCLK1 = HCLK/2 */
  33.         RCC_PCLK1Config(RCC_HCLK_Div2);
  34.         
  35.         /* PLLCLK = 8MHz * 6 = 48MHz */
  36.         RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  37.         
  38.         /* Enable PLL */
  39.         RCC_PLLCmd(ENABLE);
  40.         
  41.         /* Wait till PLL is ready */
  42.         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  43.         
  44.         /* Select PLL as system clock source */
  45.         RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  46.         
  47.         /* Wait till PLL is used as system clock source */
  48.         if(RCC_GetSYSCLKSource() == 0x08)
  49.         {
  50.             
  51.         }
  52.         else
  53.         {
  54.             
  55.         }
  56.     }
  57.     else
  58.     {
  59.         /* If HSE fails to start-up. */
  60.         while(1)
  61.         {
  62.         }
  63.     }    
  64. }

二、中断初始化


  1. /****************************************************************************
  2. * Function Name : NVIC_Configuration
  3. * Description : Configures Vector Table base location.
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. ****************************************************************************/
  8. void NVIC_Configuration(void)
  9. {
  10.     NVIC_InitTypeDef NVIC_InitStructure;

  11. #ifdef VET_TAB_RAM
  12.     /* Set the Vector Table base location at 0x2000 0000 */
  13.     NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  14. #else
  15.     /* Set the Vector Table base location at 0x8000 0000 */
  16.     NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  17. #endif
  18.     
  19.     /* Enable and configure RCC global IRQ channel */
  20.     NVIC_InitStructure.NVIC_IRQChannel = RCC_IRQn;
  21.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  22.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  23.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  24.     NVIC_Init(&NVIC_InitStructure);
  25. }

三、GPIO口初始化


  1. /****************************************************************************
  2. * Function Name : LED_Init
  3. * Description : Configures GPIOB
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. ****************************************************************************/
  8. void LED_Init()
  9. {
  10.     /* Configure all the GPIOB in Output Push-Pull */
  11.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14;
  12.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  14.     
  15.     /* Enable GPIOB clock */
  16.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  17.     
  18.     /* Initialize GPIOB*/
  19.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  20. }

四、main函数

  1. /****************************************************************************
  2. * Function Name : main
  3. * Description : Main program.
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. ****************************************************************************/

  8. int main(void)
  9. {
  10.     /* Set system clock HCLK PCLK2 PCLK1 and PLLCLK */
  11.     SetSysClock();
  12.     
  13.     /* Get current frequency and fill in RCC_ClockFreq */
  14.     RCC_GetClocksFreq(&RCC_ClockFreq);
  15.     
  16.     /* Enable Clock Security System(CSS), this will generate an NMI exception (Non-Maskable Interrupt)
  17.      when HSE clock fails*/
  18.     RCC_ClockSecuritySystemCmd(ENABLE);
  19.     
  20.     /* NVIC configuration */
  21.     NVIC_Configuration();
  22.     
  23.     /* LED configuration */
  24.     LED_Init();
  25.     
  26.     /* Set the GPIOB prot pin 15 and pin 14*/
  27.     GPIO_SetBits(GPIOB, GPIO_Pin_15 | GPIO_Pin_14);
  28.     
  29.     /* Output HSE clock on MCO pin */
  30.     RCC_MCOConfig(RCC_MCO_HSE);
  31.             
  32.     while(1)
  33.     {
  34.         GPIO_Write(GPIOB, (u16)~GPIO_ReadOutputData(GPIOB));

  35.         Delay(DELAY_COUNT);        
  36.     }
  37. }








阅读(1740) | 评论(0) | 转发(0) |
0

上一篇:PCB一些设置记录

下一篇:按键中断总结

给主人留下些什么吧!~~