[STM32F4] STM32F411--按键中断模式

[复制链接]
1407|0
 楼主| C洛达尔多 发表于 2016-3-8 20:35 | 显示全部楼层 |阅读模式
bsp_key.c
  1. #include "bsp_key.h"

  2. void KEY_Init(void)
  3. {
  4.         GPIO_InitTypeDef GPIO_InitStructure;
  5.         
  6.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
  7.         /******使能GPIOC时钟*****/
  8.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
  9.         /******KEY对应引脚*****/
  10.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
  11.         /******普通输入模式*****/
  12.         GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
  13.         /*****上拉******/
  14.         GPIO_Init(GPIOC,&GPIO_InitStructure);
  15.         /******初始化PC13*****/
  16. }

bsp_exit.c
  1. #include "bsp_exit.h"
  2. #include "bsp_key.h"

  3. void EXTIX_Init(void)
  4. {
  5.         NVIC_InitTypeDef NVIC_InitStructure;
  6.         EXTI_InitTypeDef EXTI_InitStructure;
  7.         
  8.         KEY_Init();//按键对应的IO口初始化
  9.         
  10.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);//使用中断即为使用复用功能,这点M4与M3有区别
  11.         
  12.         SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC,EXTI_PinSource13);//PC13连接线13
  13.         
  14.         EXTI_InitStructure.EXTI_Line=EXTI_Line13;//LINE13
  15.         EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;//中断事件
  16.         EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;//上升沿触发
  17.         EXTI_InitStructure.EXTI_LineCmd=ENABLE;//使能LINE13
  18.         EXTI_Init(&EXTI_InitStructure);
  19.         
  20.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//设置系统化中断优先级分组1
  21.         NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQn;//外部中断10-15
  22.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00;//抢占优先级0
  23.         NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x02;//子优先级2
  24.         NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//使能外部中断通道
  25.         NVIC_Init(&NVIC_InitStructure);
  26.         
  27. }
bsp_key.h
  1. #ifndef _BSP_KEY_H_
  2. #define _BSP_KEY_H_
  3. #include "stm32f4xx.h"

  4. #define KEY GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)

  5. void KEY_Init(void);
  6.         
  7. #endif
修改stm32f4xx_it.c
  1. #include "stm32f4xx_it.h"
  2. #include "bsp_led.h"
  3. extern void TimingDelay_Decrement(void);
  4. ……
  5. ……
  6. ……

  7. void SysTick_Handler(void)
  8. {
  9.         TimingDelay_Decrement();
  10. }

  11. void EXTI15_10_IRQHandler(void)
  12. {
  13.         if(EXTI_GetITStatus(EXTI_Line13) != RESET) //确保是否产生了EXTI Line中断
  14.         {
  15.                         
  16.                 LED_TOGGLE;
  17.                 EXTI_ClearITPendingBit(EXTI_Line13);     //清除中断标志位
  18.         }  
  19. }


您需要登录后才可以回帖 登录 | 注册

本版积分规则

28

主题

236

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部