MSP430單片機矩陣鍵盤測試程序
unsigned char keybuff[10];
unsigned char keypoint=0;
void delay(int v)
{
while(v!=0)v--;
}
unsigned char key(void)
{
unsigned char x=0xff;
P1DIR=0X0F;
P1OUT=0X01; //掃描第一行
if((P1IN&0X70)==0X10)
x=0;
else
if((P1IN&0X70)==0X20)
x=1;
else
if((P1IN&0X70)==0x40)
x=2;
else
{
P1OUT=0X2; //掃描第二行
if((P1IN&0X70)==0X10)
x=3;
else
if((P1IN&0X70)==0X20)
x=4;
else
if((P1IN&0X70)==0x40)
x=5;
else
{
P1OUT=0X4; //掃描第三行
if((P1IN&0X70)==0X10)
x=6;
else
if((P1IN&0X70)==0X20)
x=7;
else
if((P1IN&0X70)==0x40)
x=8;
else
{P1OUT=8; //掃描第四行
if((P1IN&0X70)==0X10)
x=9;
else
if((P1IN&0X70)==0X20)
x=10;
else
if((P1IN&0X70)==0x40)
x=11;
}
}
}
return(x);
}
unsigned char keyj(void)
{unsigned char x;
P1DIR=0x0f;
P1OUT=0x0f; //鍵盤硬件:P10--P13為行線,最上面一根為P10
x=(P1IN&0X70); // P14--P16為列線,最左邊一根為P14,列線下拉
return(x); // 無按鍵,返回 0?; 有按鍵返回 非0
}
interrupt[PORT1_VECTOR] void port1key(void)
{
if(keyj()!=0X00)
{
delay(300) ; //消抖動
if(keyj()!=0X0)
{
keybuff[keypoint]=key(); //按鍵見鍵值保存到隊列
keypoint++; //
if(keypoint==10)
keypoint=0;
}
}
P1OUT=0X0F;
P1IFG=0X0; //清除中斷標志
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; /* // Stop WDT */
P1DIR=0XF;
P1OUT=0XF;
P1IES=0X0;
P1IE=0X70; //列線上升沿允許P1中斷
_EINT(); /*/ Enable interrupts */
while(1)
{
LPM0;
_NOP();
}
}
評論