|
使4個(gè)按鍵中斷來(lái)控制兩個(gè)LED的亮滅。
led1 連接 PA(0) 一直閃爍提示系統(tǒng)正常工作
led2 連接 PA(1)
led3 連接 PA(2)
K_UP 連接 PB(0) 按一下控制led2 亮
K_DOWN 連接 PB(1) 按一下控制led2 滅
K_LEFT 連接 PB(2) 按一下控制led3 亮
K_RIGHT 連接 PB(3) 按一下控制led3 滅
編寫(xiě)的程序編譯后沒(méi)有錯(cuò)誤和警告。下載在開(kāi)發(fā)板后,led1一直閃爍提示系統(tǒng)正常工作,按一下K_UP鍵led2亮,再按一下K_DOWN鍵led2滅。但是按K_LEFT和K_RIGHT沒(méi)反應(yīng),無(wú)法控制led3的亮滅。請(qǐng)那位高手幫忙看下下面程序有什么問(wèn)題?
20190403_145046.jpg (3.51 MB, 下載次數(shù): 58)
下載附件
2019-4-3 14:51 上傳
1.jpg (55.3 KB, 下載次數(shù): 53)
下載附件
2019-4-3 15:08 上傳
MAIN.C
#include "system.h"
#include "led.h"
#include "SysTick.h"
#include "key.h"
#include "exti.h"
int main()
{
u8 i=0;
SysTick_Init(72);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
LED_Init();
KEY_Init();
My_EXTI_Init();
while(1)
{
i++;
if(i%20==0)
{
led1=!led1;
}
delay_ms(10);
}
}
EXTI.C
#include "exti.h"
#include "Systick.h"
#include "led.h"
#include "key.h"
void My_EXTI_Init()
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1)
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource2)
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource3)
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=EXTI1_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI2_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
EXTI_InitStructure.EXTI_Line=EXTI_Line0|EXTI_Line1|EXTI_Line2|EXTI_Line3;
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==0)
{
led2=1;
}
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
void EXTI1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line1)==1)
{
delay_ms(10);
if(K_DOWN==0)
{
led2=0;
}
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2)==1)
{
delay_ms(10);
if(K_LEFT==0)
{
led3=1;
}
}
EXTI_ClearITPendingBit(EXTI_Line2);
}
void EXTI3_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line3)==1)
{
delay_ms(10);
if(K_RIGHT==0)
{
led3=0;
}
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
EXTI.H
#ifndef _exti_H
#define _exti_H
#include "system.h"
void My_EXTI_Init(void);
#endif
LED.C
#include "led.h"
#include "system.h"
void LED_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2);
}
LED.H
#ifndef _led_H
#define _led_H
#include "system.h"
void LED_Init(void);
#define led1 PAout(0)
#define led2 PAout(1)
#define led3 PAout(2)
#endif
KEY.C
#include "key.h"
#include "SysTick.h"
void KEY_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
u8 KEY_Scan(u8 mode)
{
static u8 key=1;
if(key==1&&(K_UP==0||K_DOWN==0||K_LEFT==0||K_RIGHT==0))
{
delay_ms(10);
key=0;
if(K_UP==0)
{
return KEY_UP;
}
else if(K_DOWN==0)
{
return KEY_DOWN;
}
else if(K_LEFT==0)
{
return KEY_LEFT;
}
else if(K_RIGHT==0)
{
return KEY_RIGHT;
}
}
else if(K_UP==1&&K_DOWN==1&&K_LEFT==1&&K_RIGHT==1)
{
key=1;
}
if(mode==1)
{
key=1;
}
return 0;
}
KEY.H
#ifndef _key_H
#define _key_H
#include "system.h"
void KEY_Init(void);
u8 KEY_Scan(u8 mode);
#define K_UP PBin(0)
#define K_DOWN PBin(1)
#define K_LEFT PBin(2)
#define K_RIGHT PBin(3)
#define KEY_UP 1
#define KEY_DOWN 2
#define KEY_LEFT 3
#define KEY_RIGHT 4
#endif
|
|