CC2530外部中断实现按键控制LED闪烁
中断任务:
1.系统初始化D1(P1.0)、D2(P1.1)闪一次灭掉。
2.按一次KEY1(P0.1),D1、D2同时闪烁;再按一次KEY1,D1、D2灭掉。
3.按一次KEY2(P0.1),D1、D2交替闪烁;再按一次KEY2,D1、D2灭掉。
#include <ioCC2530.h> #define D1 P1_0 //定义P1.0口为D1控制端 #define D2 P1_1 //定义P1.1口为D2控制端 #define KEY1 P0_1 //定义P0.1口为S1控制端 #define KEY2 P1_2 #define ON 1 //高电平点亮 #define OFF 0//低电平熄灭 typedef unsigned char uchar; typedef unsigned int uint; uchar KeyValue0=0; uchar KeyValue1=0; void Delay(uint time)//延时函数 { uint i,j; for (i=0; i<time; i++) for (j=0; j<530; j++); } void InitLed(void)//初始化LED { P1SEL &= ~0x03; P1DIR |= 0x03; Delay(1000);//默认点亮LED,因此直接利用延时函数即可,无需重复操作 D1=D2=OFF; } void InitKey()//初始化外部中断 { P0IEN |= 0x2; // P0.1 设置为中断方式 1:中断使能 P1IEN |= 0x4; PICTL |= 0x3; //下降沿触发 IEN1 |= 0x20; //允许P0口中断; IEN2 |= 0x10; //允许P1口中断; EA = 1; //打开中断 } void Key_5() { if(KeyValue0 == 1) { D2 = D1 = ON; } else if(KeyValue0 == 2) { D2 = D1 = OFF; KeyValue0 = 0; //产生中断保存中断状态 } } void Key_4() { while(KeyValue1 == 1) { D1 = ON;Delay(100); D2 = ON; D1 = OFF;Delay(100); D2 = OFF; } while(KeyValue1 == 2) { D1 = D2 = OFF; KeyValue1 = OFF; //产生中断保存中断状态 } } #pragma vector = P0INT_VECTOR __interrupt void P0_ISR(void) { if(P0IFG &= 0x02) //按键中断 { Delay(10); //延时去抖 if(KeyValue0==0) //按键中断 { KeyValue0 = 1; //产生中断保存中断状态 } else if(KeyValue0==1) { KeyValue0 = 2; //产生中断保存中断状态 } } P0IFG = 0; //清中断标志 P0IF = 0; //清端口0中断标志 } #pragma vector = P1INT_VECTOR __interrupt void P1_ISR(void) { if(P1IFG &= 0x04) //按键中断 { Delay(10); if(KeyValue1==0) //按键中断 { KeyValue1 = 1; //产生中断保存中断状态1 } else if(KeyValue1==1) { KeyValue1 = 2; //产生中断保存中断状态2 } } P1IFG = 0; //清中断标志 P1IF = 0; //清端口1中断标志 } /**************************************************************************** * 程序入口函数 ****************************************************************************/ void main(void) { InitLed(); //设置LED灯相应的IO口 InitKey(); //设置KEY相应的IO口外部中断 while(1) { Key_4(); Key_5(); } }