代码改变世界

矩阵键盘的检测及使用

2010-08-09 18:22  李龙江  阅读(587)  评论(0编辑  收藏  举报

基于TX-1C开发板,电路连接图如下:

 1 #include <reg52.h>
 2  #define uint unsigned int
 3  #define uchar unsigned char
 4 sbit dula=P2^6;        //段选
 5 sbit wela=P2^7;        //位选
 6 
 7 uchar code tab[]={
 8     0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
 9     0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
10 };
11 void display(uchar);
12 void main()
13 {
14     uchar row,col,sum;
15     dula=1;
16     P0=0x00;
17     dula=0;
18     P0=0xFF;
19 
20     wela=1;
21     P0=0xC0;
22     wela=0;
23 
24     while (1)
25     {
26         P3=0X0f;
27         row=P3;     //
28 
29         P3=0Xf0;
30         col=P3;     //
31         sum=row+col;//读取到的键盘值
32         switch(sum)
33         {
34             case 0xee:display(0);
35                 break;
36             case 0xde:display(1);
37                 break;
38             case 0xbe:display(2);
39                 break;
40             case 0x7e:display(3);
41                 break;
42             case 0xed:display(4);
43                 break;
44             case 0xdd:display(5);
45                 break;
46             case 0xbd:display(6);
47                 break;
48             case 0x7d:display(7);
49                 break;
50             case 0xeb:display(8);
51                 break;
52             case 0xdb:display(9);
53                 break;
54             case 0xbb:display(10);
55                 break;
56             case 0x7b:display(11);
57                 break;
58             case 0xe7:display(12);
59                 break;
60             case 0xd7:display(13);
61                 break;
62             case 0xb7:display(14);
63                 break;
64             case 0x77:display(15);
65                 break;
66         }
67     }
68 }
69 void display(uchar num)
70 {
71     dula=1;
72     P0=tab[num];
73     dula=0;
74 }

弄了一天晕晕乎乎的一遍遍的修改看效果终于出来了,呵呵,庆祝下