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();
    }
}

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM