MSP430按键中断Ⅱ
扫描二维码
随时随地手机看文章
#include
typedef unsigned char uchar;
typedef unsigned int uint;
#define keyin (P1IN & 0X0F)
#define wei_h P5OUT |= BIT5
#define wei_l P5OUT &= BIT5
#define duan_l P6OUT &= ~BIT6
#define duan_h P6OUT |= BIT6
uchar table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//uchar num = 0;
uint keyval=0;
void delay(int z)
{
int i,j;
for(i=z;i>0;i--)
for(j=112;j>0;j--);
}
void display(uchar dat)
{
P4OUT = table[dat];
duan_h;
duan_l;
P4OUT = 0x00;
wei_h;
wei_l;
}
void main( void )
{
// uint temp,keyval;
P1IES = 0X0F; //下降沿触发
P1IE = 0X0F;
P1DIR = 0XF0;
P1OUT = 0X00;
// P1DIR = 0XFF;P1OUT = 0XFF;
P4DIR = 0XFF;P4OUT = 0X00;
P5DIR = 0XFF;P5OUT = 0XF7;
P6DIR = 0XFF;
_EINT();
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
while(1)
{
LPM1;
}
}
#pragma vector = PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
if(P1IFG & 0X0F)
{
switch(P1IFG)
{
case 0x01:
if(keyin == 0x0e)
{
delay(20);
if(keyin == 0x0e)
{
while(keyin != 0x0f)
keyval = 1;
display(keyval);
P1IFG = 0; //必须软件清除
return;
}
}
case 0x02:
if(keyin == 0x0d)
{
delay(20);
if(keyin == 0x0d)
{
while(keyin != 0x0f)
keyval = 1;
display(keyval);
P1IFG = 0; //必须软件清除
return;
}
}
case 0x03:
if(keyin == 0x0b)
{
delay(20);
if(keyin == 0x0b)
{
while(keyin != 0x0f)
keyval = 1;
display(keyval);
P1IFG = 0; //必须软件清除
return;
}
}
case 0x04:
if(keyin == 0x07)
{
delay(20);
if(keyin == 0x07)
{
while(keyin != 0x0f)
keyval = 1;
display(keyval);
P1IFG = 0; //必须软件清除
return;
}
}
}
}
}