打印
[STM32F4]

STM32F411--按键中断模式

[复制链接]
1097|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
C洛达尔多|  楼主 | 2016-3-8 20:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
bsp_key.c
#include "bsp_key.h"

void KEY_Init(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
        /******使能GPIOC时钟*****/
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
        /******KEY对应引脚*****/
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
        /******普通输入模式*****/
        GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
        /*****上拉******/
        GPIO_Init(GPIOC,&GPIO_InitStructure);
        /******初始化PC13*****/
}

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

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

#define KEY GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)

void KEY_Init(void);
        
#endif
修改stm32f4xx_it.c
#include "stm32f4xx_it.h"
#include "bsp_led.h"
extern void TimingDelay_Decrement(void);
……
……
……

void SysTick_Handler(void)
{
        TimingDelay_Decrement();
}

void EXTI15_10_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line13) != RESET) //确保是否产生了EXTI Line中断
        {
                        
                LED_TOGGLE;
                EXTI_ClearITPendingBit(EXTI_Line13);     //清除中断标志位
        }  
}


发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

28

主题

236

帖子

0

粉丝