调试好的msp430f149接4*4键盘代码
单片机源程序如下:
- #include "msp430x14x.h"
- #include "1602.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define key_DIR P5DIR
- #define key_OUT P5OUT
- #define key_IN P5IN
- uchar keymap[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
- uchar i,j;
- uchar checkkey(void)
- {
- return(key_IN^0xf0);
- }
- uchar getkeyvalue(void)
- {
- uchar hang,lie,temp1,temp2;
- uchar key_val;
- temp1=0x08;
- for(hang=0;hang<4;hang++)
- {
- key_OUT=0x0f;
- key_OUT=~temp1;
- temp1 >>=1;
- if((key_IN&0xf0)!=0xf0)
- {
- temp2=0x10;
- for(lie=0;lie<4;lie++)
- {
- if((key_IN & temp2) == 0x00)
- {
- key_val=keymap[hang*4+lie];
- break;
- }
- temp2<<=1;
- }
- }
- }
- return(key_val);
- }
- int main( void )
- {
- // Stop watchdog timer to prevent time out reset
- WDTCTL = WDTPW + WDTHOLD;
- uchar t,keyvalue;
- LcdReset();
- key_DIR=0x0f;
- while(1)
- {
- key_OUT=0x00;
- Delay5ms();
- t=checkkey();
- if(t)
- {
- keyvalue=getkeyvalue();
- switch(keyvalue)
- {
- case 0:LcdReset();i=0;break;
- case 1:Disp1Char(0,0,'1');j=1;break;
- case 2:Disp1Char(2,0,'2');j=2;break;
- case 3:Disp1Char(3,0,'3');j=3;break;
- case 4:Disp1Char(4,0,'4');j=4;break;
- case 5:Disp1Char(5,0,'5');j=5;break;
- case 6:Disp1Char(6,0,'6');j=6;break;
- case 7:Disp1Char(7,0,'7');j=7;break;
- case 8:Disp1Char(8,0,'8');j=8;break;
- case 9:Disp1Char(9,0,'9');j=9;break;
- case 10:Disp1Char(10,0,'A');i=10;break;
- case 11:Disp1Char(11,0,'B');i=11;break;
- case 12:Disp1Char(12,0,'C');i=12;break;
- case 13:Disp1Char(13,0,'D');i=13;break;
- case 14:Disp1Char(14,0,'E');i=14;break;
- case 15:Disp1Char(15,0,'F');i=15;break;
-
- }
- }
- }
- //return 0;
- }
复制代码
所有程序51hei提供下载:
矩阵键盘.zip
(153.83 KB, 下载次数: 44)
|