按键中断控制LED亮灭
#include <ioCC2530.h>
#define LED1 P1_0 //P1.0端口控制LED1发光二极管
#define LED2 P1_1 //P1.1端口控制LED1发光二极管
#define KEY0 P0_4 //P0.4端口控Key1
typedef unsigned char uchar;
typedef unsigned int uint;
uchar KeyValue1=0;
void delay(unsigned int time) {
while (time > 0) {
time--;
}
}
void InitKey()//初始化外部中断
{
P0IEN |= 0x10; // P0.4 设置为中断方式 1:中断使能
PICTL |= 0x10; //下降沿触发
IEN1 |= 0x20; //允许P0口中断,在IEN1的第5位;
EA = 1; //打开中断
}
#pragma vector = P0INT_VECTOR //中断
__interrupt void P0_ISR(void)
{
if(P0IFG &= 0x10) //按键中断
{
delay(1000);
if(KeyValue1==0) //按键中断
{
KeyValue1 = 1; //产生中断保存中断状态1
}
else if(KeyValue1==1)
{
KeyValue1 = 2; //产生中断保存中断状态2
}
else if(KeyValue1==2)
{
KeyValue1 = 3; //产生中断保存中断状态3
}
}
P0IFG = 0; //清中断标志
P0IF = 0; //清端口1中断标志
}
void main(void) {
P1SEL &= ~0x03;
P1DIR |= 0X03; //定义P1.0、P1.1端口为输出
P0DIR &= ~(1 << 4);
InitKey();
delay(1000);//默认点亮LED,因此直接利用延时函数即可,无需重复操作
LED1=LED2=0;
while(1){
if(KeyValue1 == 1)
{
LED1=1;
delay(1000);
}
if(KeyValue1 == 2)
{
LED2=1;
delay(1000);
}
if(KeyValue1 == 3)
{
LED1=LED2=0;
delay(1000);
KeyValue1 = 0; //产生中断保存中断状态
}
}
}