#include <msp430x14x.h>
#define uint unsigned int
#define uchar unsigned char
uchar Key_Map[]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e};
uchar Key_Pressed;
uchar Key_val;
uchar Key_Flag;
void delay(uint x)
{
uchar j;
while(x--)
{
for(j=0;j<120;j++);
}
}
void Init_Keypad(void)
{
P1DIR=0XF0;
P1OUT=0XF0;
Key_Pressed=0;
Key_val=0;
Key_Flag=0;
}
void Check_key(void)
{
uchar row,col,tmp1,tmp2;
tmp1=0x80;
for(row=0;row<4;row++)
{
P1OUT=0XF0;
P1OUT-=tmp1;
tmp1>>=1;
if((P1IN&0x0f)<0x0f)
{
tmp2=0x01;
for(col=0;col<4;col++)
{
if((P1IN&tmp2)==0x00)
{
Key_val= Key_Map[row*4+col];
return;
}
tmp2<<=1;
}
}
}
}
void Key_Event(void)
{
uchar tmp;
P1OUT=0x00;
tmp=P1IN;
if((Key_Pressed==0)&&(tmp&0x0f)<0x0f)
{
Key_Pressed=1;
delay(5000);
Check_key();
}
else if((Key_Pressed==1)&&((tmp&0x0f)==0x0f))
{
Key_Pressed=0;
Key_Flag=1;
}
else
{
;
}
}
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P2DIR=0xff;
while(1)
{
if(Key_Flag==1)
{
Key_Flag=0;
delay(50);
P2OUT=Key_val;
}
}
}
#define uint unsigned int
#define uchar unsigned char
uchar Key_Map[]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e};
uchar Key_Pressed;
uchar Key_val;
uchar Key_Flag;
void delay(uint x)
{
uchar j;
while(x--)
{
for(j=0;j<120;j++);
}
}
void Init_Keypad(void)
{
P1DIR=0XF0;
P1OUT=0XF0;
Key_Pressed=0;
Key_val=0;
Key_Flag=0;
}
void Check_key(void)
{
uchar row,col,tmp1,tmp2;
tmp1=0x80;
for(row=0;row<4;row++)
{
P1OUT=0XF0;
P1OUT-=tmp1;
tmp1>>=1;
if((P1IN&0x0f)<0x0f)
{
tmp2=0x01;
for(col=0;col<4;col++)
{
if((P1IN&tmp2)==0x00)
{
Key_val= Key_Map[row*4+col];
return;
}
tmp2<<=1;
}
}
}
}
void Key_Event(void)
{
uchar tmp;
P1OUT=0x00;
tmp=P1IN;
if((Key_Pressed==0)&&(tmp&0x0f)<0x0f)
{
Key_Pressed=1;
delay(5000);
Check_key();
}
else if((Key_Pressed==1)&&((tmp&0x0f)==0x0f))
{
Key_Pressed=0;
Key_Flag=1;
}
else
{
;
}
}
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P2DIR=0xff;
while(1)
{
if(Key_Flag==1)
{
Key_Flag=0;
delay(50);
P2OUT=Key_val;
}
}
}
