zoukankan      html  css  js  c++  java
  • 按键中断总结

    一、RCC初始化


    1. /****************************************************************************
    2. * Function Name : RCC_Configuration
    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 RCC_Configuration(void)
    10. {
    11.     /* Deinitialize the RCC registers */
    12.     RCC_DeInit();
    13.     
    14.     /* Enable the HSE */    
    15.     RCC_HSEConfig(RCC_HSE_ON);
    16.     
    17.     /* Wait till HSE is ready and if Time out is reached exit */
    18.     HSEStartUpStatus = RCC_WaitForHSEStartUp();
    19.     if(HSEStartUpStatus == SUCCESS)
    20.     {
    21.         /* Add here PLL ans system clock config */
    22.         
    23.         /* Enable The Prefetch Buffer */
    24.         FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    25.         
    26.         /* Configure Tthe Latency cycle: Set 0 Latency cycles */
    27.         FLASH_SetLatency(FLASH_Latency_2);
    28.         
    29.         /* Configure HCLK such as HCLK = SYSCLK */
    30.         RCC_HCLKConfig(RCC_SYSCLK_Div1);
    31.         
    32.         /* PCLK2 = HCLK */
    33.         RCC_PCLK2Config(RCC_HCLK_Div1);
    34.         
    35.         /* PCLK1 = HCLK/2 */
    36.         RCC_PCLK1Config(RCC_HCLK_Div2);
    37.         
    38.         /* Select HSE as system clock source*/
    39.         RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
    40.         
    41.         /* PLLCLK = 8MHz * 9 = 72MHz */
    42.         RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    43.         
    44.         /* ADCCLK = PCLK/4 */
    45.         RCC_ADCCLKConfig(RCC_PCLK2_Div4);
    46.         
    47.         /* Enable PLL */
    48.         RCC_PLLCmd(ENABLE);
    49.         
    50.         /* Wait till PLL is ready */
    51.         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
    52.         
    53.         /* Select PLL as system clock source */
    54.         RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till HSE is used as system clock source */
    55.         while(RCC_GetSYSCLKSource() != 0x08)
    56.         {
    57.             
    58.         }
    59.     }
    60.     else
    61.     {
    62.         /* If HSE fails to start-up. */
    63.         while(1)
    64.         {
    65.         }
    66.     }
    67. }
    相比之前的时钟初始化,多初始化了ADCCLK。

    二、GPIO初始化


    1. /****************************************************************************
    2. * Function Name : GPIO_Configuration
    3. * Description :
    4. * Input : None
    5. * Output : None
    6. * Return : None
    7. ****************************************************************************/
    8. void GPIO_Configuration(void)
    9. {
    10.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);

    11.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    12.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    13.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    14.     GPIO_Init(GPIOB, &GPIO_InitStructure);
    15.     
    16.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    17.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    18.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    19.     GPIO_Init(GPIOB, &GPIO_InitStructure);    
    20. }
    使用PB8作为推挽输出,PB6为浮空输入。

    三、NVIC初始化


    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.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    20.     
    21.     NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
    22.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    23.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    24.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    25.     NVIC_Init(&NVIC_InitStructure);    
    26. }
    因位PB6位输入,也就是外部中断源。所以初始化EXTI9_5_IRQn作为外部中断源

    四、EXTI初始化


    1. /****************************************************************************
    2. * Function Name : EXTI_PE6_Config
    3. * Description :
    4. * Input : None
    5. * Output : None
    6. * Return : None
    7. ****************************************************************************/
    8. void EXTI_PE6_Config(void)
    9. {
    10.     GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource6);
    11.     
    12.     EXTI_InitStructure.EXTI_Line = EXTI_Line6;
    13.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    14.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    15.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    16.     EXTI_Init(&EXTI_InitStructure);
    17.     
    18.     EXTI_GenerateSWInterrupt(EXTI_Line6);

    19. }
    设置了外部中断源,把GPIOB6作为中断源。外部中断线是6,结合原理图使用下降沿触发。
    EXTI_GenerateSWInterrupt是触发一次软件中断。

    同时需要编写中断函数在stm32f10x_it.c中:
    1. /*******************************************************************************
    2. * Function Name : EXTI9_5_IRQHandler
    3. * Description : This function handles External lines 9 to 5 interrupt request.
    4. * Input : None
    5. * Output : None
    6. * Return : None
    7. *******************************************************************************/
    8. void EXTI9_5_IRQHandler(void)
    9. {
    10.     if(EXTI_GetITStatus(EXTI_Line6) != RESET)
    11.     {
    12.         GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_8)));
    13.         
    14.         EXTI_ClearITPendingBit(EXTI_Line6);
    15.     }
    16. }

    五、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.     RCC_Configuration();
    11.     
    12.     NVIC_Configuration();
    13.     
    14.     GPIO_Configuration();
    15.     
    16.     EXTI_PE6_Config();
    17.     
    18.     while(1)
    19.     {
    20.     }    
    21. }





    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    2016/3/16 高级查询 ①连接查询 ②联合查询 ③子查询 无关 相关
    2016/3/13 七种查询 (普通查询 条件查询 排序查询 模糊查询 统计查询 分组查询 分页查询 )
    2016/3/13 MySQL 增删查改 CRUD 用代码实现
    2016/3/10 数据库简单操作( 创建数据库 创建表 数值类型 主键 外键 自动递增 )
    2016/3/10 PHP环境搭建 LAMP WAMP
    2016/3/10 PHP (超文本预处理器) 是什么?
    2016/3/1 淘宝 腾讯 网易 css初始化代码 以及最基础的初始化
    判断i在字符串中出现的次数(2016.1.12P141-1)
    2016-1-9作业——输出二维数组的和
    2016-1-8作业
  • 原文地址:https://www.cnblogs.com/ch122633/p/7363271.html
Copyright © 2011-2022 走看看