因为课程有单片机, 所以今天将上年买的
TX-B开发板拿了出来玩玩. 写了一个关于行列扫描的单片机程序.
参考:
矩阵键盘检测
键盘与单片机的连接电路图
![](https://www.cnblogs.com/images/cnblogs_com/fengmk2/1.gif)
6位数码管与单片机的连接电路图
![](https://www.cnblogs.com/images/cnblogs_com/fengmk2/scm2.gif)
共阴极数码管数字编码
![](http://www.txmcu.com/files/pic/2006813234022.jpg)
矩阵键盘的四行分别与P3.0-P3.3连接,四列分别与P3.4-P3.7连接。
代码使用"KeilC51v612" 编写, 代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
电话键盘
1
#include <reg51.H>
2
//模拟电话键盘, 按动一个键, 显示相应的数值, 行列按普通电话的3 x 4方式排布
3
unsigned char t, key, a, count;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
sbit light8 = P1^7;
6
//控制数码管段选的锁存器锁存端 下降沿过后锁存,即先令dula = 1,再令dula=0,完成锁存
7
sbit dula=P2^6;
8
//控制数码管位选的锁存器锁存端 过程如dula
9
sbit wela=P2^7;
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
unsigned char code table[]=
{0x3f,0x06,0x5b,0x4f,0x66,
12
0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0-F数码管的编码
13
void display(unsigned char num) //数码管显示函数
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
15
P0=table[num]; //将第num个显示编码送到P0口,从而送到总线上
16
dula=1; //产生一个下降沿将数据锁存
17
dula=0;
18
P0= 0xc0;
19
wela=1; //同理dula
20
wela=0;
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
//获取键值, 通过对列检测, base是每一行第一列的数值, 这里的行列按普通电话的3 x 4方式排布
24
unsigned char getKey(unsigned char da, unsigned char base)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
26
switch(da)
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
case 0xe0: //检测第一列
29
return base;
30
case 0xd0: //检测第二列
31
return base + 1;
32
case 0xb0: //检测第三列
33
return base + 2;
34
default : //其它返回上一次按的值
35
return key;
36
}
37
}
38![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
//等待按键被释放, 若按键一直被按着,则使发光二极管8亮
40
void wait()
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
42
while(t != 0xf0)
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
t = P3;
45
t = t & 0xf0;
46
light8 = 0;
47
}
48
light8 = 1;
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
51
//延时函数, 例i=10,则大概延时10ms.
52
void delay(unsigned char i)
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
54
unsigned char j, k;
55
for(j = i; j > 0; j--)
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
for(k = 125; k > 0; k--);
58
}
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
void main()
62![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
63
dula = 0; //初始化dula和wela
64
wela = 0;
65
while(1)
66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
67
a = 0xfe; //先检测第一行
68
for(count = 0; count < 4; count ++)
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
P3 = a;
71
t = P3 & 0xf0; //读P3口
72
if(t != 0xf0) //判断是否有键按下
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
delay(10); //延时10ms去抖动
75
t = P3 & 0xf0; //再次检验是否有键按下
76
if(t != 0xf0)
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78
switch(count) //判断哪一行有键被按下
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
case 0:
81
key = getKey(t, 1); //获取第一行的哪一列键值
82
break;
83
case 1:
84
key = getKey(t, 4);
85
break;
86
case 2:
87
key = getKey(t, 7);
88
break;
89
case 3:
90
if(t == 0xd0) //检测是否第4行第2列有键被按下
91
key = 0;
92
break;
93
}
94
wait();
95
display(key);
96
P1 = a; //提示哪一行有键被按下
97
}
98
}
99
a = (a << 1) | (a >> 7); //继续检测下一行, 循环左移一位实现
100
}
101
}
102
}
呵呵, 我的专业很奇怪, 软件硬件都要掌握, 很多时候我都把握不住方向, 所以只能两样都要懂, 不过我还是觉得软件比较适合我.
感谢郭大哥的实验板, 也是靠着这块板, 我能快速对单片机产生很大兴趣.
TX-1B实验板功能图 (目前已经被TX-1C代替了)
![](https://www.cnblogs.com/images/cnblogs_com/fengmk2/TX-1B_Function.jpg)