#include "msp430x44x.h"#define ROW P2OUT //矩陣鍵盤的行宏定義
#define COL P2IN //矩陣鍵盤的列宏定義
#define DPYOUT P3OUT //數(shù)碼管輸出口宏定義
unsigned char keyval; //鍵值
本文引用地址:http://m.butianyuan.cn/article/201611/320053.htm//共“陽(yáng)”極數(shù)碼管的碼表
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); //消抖動(dòng)延時(shí)
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; //共陽(yáng)極數(shù)碼管輸出口初始化
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];//數(shù)碼管顯示鍵值
}
}
評(píng)論