#include <msp430x14x.h>
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();
}
}