#include "
msp430x44x.h"
#define ROW P2OUT //矩阵键盘的行宏定义
#define COL P2IN //矩阵键盘的列宏定义
#define DPYOUT P3OUT //数码管输出口宏定义
unsigned char keyval; //键值
//共“阳”极数码管的码表
unsigned char LED7CA[] ={~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,
~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71,0xff};
unsigned char keyscan()
{
int i=0;
unsigned char key=0;
ROW = 0x8f; //先置三行输出低电平
if((COL & 0x0f)!= 0x0f) //是否有按键
{
do i++;
while(i<3000); //消抖动延时
ROW = 0xbf; //扫描第一行
if((COL & 0x0f)== 0x0f)
{ ROW = 0xdf; //扫描第二行
if((COL & 0x0f)== 0x0f)
{ ROW = 0xef; //扫描第三行
if((COL & 0x0f)== 0x0f)
key = 15;
else key = ~((ROW & 0XF0)|(COL & 0X0F));
}
else key = ~((ROW & 0XF0)|(COL & 0X0F));
}
else key = ~((ROW & 0XF0)|(COL & 0X0F));
if(key != 15)
switch(key) //获取有效地键值
{ case 0x48: key=0; break;
case 0x44: key=1; break;
case 0x42: key=2; break;
case 0x41: key=3; break;
case 0x28: key=4; break;
case 0x24: key=5; break;
case 0x22: key=6; break;
case 0x21: key=7; break;
case 0x18: key=8; break;
case 0x14: key=9; break;
case 0x12: key=10;break;
case 0x11: key=11;break;
default: key=15;
}
}
else key = 15;
keyval=key;
return key;
}
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P3DIR |= 0xff;
P3OUT = 0xff; //共阳极数码管输出口初始化
P2DIR |= BIT4 +BIT5+BIT6; //先配置矩阵键盘的行(输出)
//P2.6 第一行,P2.5 第二行,P2.4 第三行
//P2.3 第一列,P2.2 第二列,P2.1 第三列,P2.0 第四列
keyval=16;
while(1)
{ keyscan();
if(keyval != 15 && keyval<17)
DPYOUT = LED7CA[keyval];//数码管显示键值
}
}