#include <msp430x24x.h>
#define uchar unsigned char
#define uint unsigned int
uchar table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(unsigned int i) //延时子程序
{
while(i–);
}
uchar keyvalue()
{
uchar key;
uchar np10,np11,np12,np13;
P1DIR=0x0f;
//第一排
P1OUT=~BIT3;
delay(10);
np10=P1IN&BIT4;
if(np10==0)
{
key=0;
}
np11=P1IN&BIT5;
if(np11==0)
{
key=1;
}
np12=P1IN&BIT6;
if(np12==0)
{
key=2;
}
np13=P1IN&BIT7;
if(np13==0)
{
key=3;
}
//第二行
P1OUT=~BIT2;
delay(10);
np10=P1IN&BIT4;
if(np10==0)
{
key=4;
}
np11=P1IN&BIT5;
if(np11==0)
{
key=5;
}
np12=P1IN&BIT6;
if(np12==0)
{
key=6;
}
np13=P1IN&BIT7;
if(np13==0)
{
key=7;
}
//第三行
P1OUT=~BIT1;
delay(10);
np10=P1IN&BIT4;
if(np10==0)
{
key=8;
}
np11=P1IN&BIT5;
if(np11==0)
{
key=9;
}
np12=P1IN&BIT6;
if(np12==0)
{
key=10;
}
np13=P1IN&BIT7;
if(np13==0)
{
key=11;
}
//第四行
P1OUT=~BIT0;
delay(10);
np10=P1IN&BIT4;
if(np10==0)
{
key=12;
}
np11=P1IN&BIT5;
if(np11==0)
{
key=13;
}
np12=P1IN&BIT6;
if(np12==0)
{
key=14;
}
np13=P1IN&BIT7;
if(np13==0)
{
key=15;
}
P1OUT=0X00;
return key;
while(1)
{
if((P1IN&0X0F)==0x0f)
break;
}
}
void main()
{
uchar key_value;
WDTCTL=WDTPW+WDTHOLD;
P1DIR=0X0F;
P2DIR=0XFF;
P2OUT=0XFF;
while(1)
{
if((P1IN&0XF0)!=0XF0)
{
delay(100);
if((P1IN&0XF0)!=0XF0)
{
delay(100);
if((P1IN&0XF0)!=0XF0)
{
key_value=keyvalue();
}
}
}
P2OUT=~key_value;
}
}