花了两小时,对照着视频的讲解,终于成功做完这个实验~~
代码如下:
main.c
cpp
#include "system.h"
#include "led.h"
#include "SysTick.h"
#include "beep.h"
#include "DigitalTube.h"
#include "key.h"
#include "exti.h"
int main()
{
u8 i=0;
SysTick_Init(72);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断优先级分组
LED_Init();
// BEEP_Init();
// DigitalTube_Init();
key_Init();
My_Exti_Init();
while(1)
{
i++;
if(i%20 ==0)
{
led1=!led1;//LED1闪
}
delay_ms(10);
}
}
exti.c
cpp
#include "exti.h"
#include "SysTick.h"
#include "key.h"
#include "led.h"
void My_Exti_Init(void) //因为Exti_Init函数名已在库函数中使用了,所以这里取这个名称
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
//(1)使能IO口时钟,配置IO口模式为输入,这一步在key_Init()已做了,所以这里不需要
//(2)开启 AFIO 时钟,设置 IO 口与中断线的映射关系
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//开启 AFIO 时钟
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);//配置PA0为EXT0的输入线
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2);//配置PE2为EXT2的输入线
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource3);//配置PE3为EXT3的输入线
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4);//配置PE4为EXT4的输入线
//(3)配置中断分组(NVIC),中断分组NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);放在main里了,
// 并对中断分组初始化以及使能中断
NVIC_InitStructure.NVIC_IRQChannel= EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel= EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel= EXTI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel= EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//(4)初始化EXTI
//配置EXTI_Line0的模式、触发方式并打开
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//配置EXTI_Line2,3,4的模式、触发方式并打开
EXTI_InitStructure.EXTI_Line = EXTI_Line2|EXTI_Line3|EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
//以下是中断服务程序
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0)==1) //判断是否有按键按下而产生外部中断
{
delay_ms(10);//消除抖动
if(K_UP ==1)
{
led2 = ~led2;
}
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2)==1) //判断是否有按键按下而产生外部中断
{
delay_ms(10);//消除抖动
if(K_LEFT ==0)
{
led3 = ~led3;
}
}
EXTI_ClearITPendingBit(EXTI_Line2);
}
void EXTI3_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line3)==1) //判断是否有按键按下而产生外部中断
{
delay_ms(10);//消除抖动
if(K_DOWN ==0)
{
led4 = ~led4;
}
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
void EXTI4_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4)==1) //判断是否有按键按下而产生外部中断
{
delay_ms(10);//消除抖动
if(K_RIGHT ==0)
{
led5 = ~led5;
}
}
EXTI_ClearITPendingBit(EXTI_Line4);
}
exti.h
cpp
#ifndef _exti_H
#define _exti_H
#include "system.h"
void My_Exti_Init(void) ;
#endif