1 #include "8051.h"
2 typedef unsigned char u8;
3 typedef unsigned int u16;
4 u8 smgduan[]= {
5 /*0 1 2 3 4 5 6 7 */
6 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
7 /*8 9 A B C D E F */
8 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
9
10 // P0口为数码管的位选的8位输入引脚
11 // P0 = 0x00;
12
13
14 void Delayms(u16 ms);
15 void shumaguan(u8 n);
16 void DigDisplay();
17 void KeyTest();
18 void smg(u8 n, u8 m);
19 void key_4x4();
20
21 void main()
22 {
23 while(1)
24 {
25 key_4x4();
26 }
27 }
28
29 // 不精确的延时函数
30 void Delayms(u16 ms)
31 {
32 unsigned int i, j;
33 for(i = ms; i > 0; i--)
34 for(j = 110; j > 0; j--);
35 return;
36 }
37
38 // 数码管根据74HC138译码器选择对应的段(选择哪个数码管显示)
39 void shumaguan(u8 n)
40 {
41 switch(n)
42 {
43 case 0:
44 LSA = 0;LSB = 0;LSC = 0;break;
45 case 1:
46 LSA = 1;LSB = 0;LSC = 0;break;
47 case 2:
48 LSA = 0;LSB = 1;LSC = 0;break;
49 case 3:
50 LSA = 1;LSB = 1;LSC = 0;break;
51 case 4:
52 LSA = 0;LSB = 0;LSC = 1;break;
53 case 5:
54 LSA = 1;LSB = 0;LSC = 1;break;
55 case 6:
56 LSA = 0;LSB = 1;LSC = 1;break;
57 case 7:
58 LSA = 1;LSB = 1;LSC = 1;break;
59 }
60 }
61
62 // 数码管显示数字,并以十进制递增
63 void DigDisplay()
64 {
65 u8 i1 = 0;
66 u8 i2 = 0;
67 u8 i3 = 0;
68 u8 i4 = 0;
69 u8 i5 = 0;
70 u8 i6 = 0;
71 u8 i7 = 0;
72 u8 i8 = 0;
73
74
75
76
77 for (i8 = 0; i8 < 10; i8++)
78 for (i7 = 0; i7 < 10; i7++)
79 for (i6 = 0; i6 < 10; i7++)
80 for (i5 = 0; i5 < 10; i5++)
81 for (i4 = 0; i4 < 10; i4++)
82 for (i3 = 0; i3 < 10; i3++)
83 for (i2 = 0; i2 < 10; i2++)
84 for (i1 = 0; i1 < 10; i1++)
85 {
86 u16 cnt = 10;
87 while (cnt--)
88 {
89 shumaguan(0); //选中第一个数码管
90 P0 = smgduan[i1]; //给他送一个数字
91 Delayms(1); //稍微延时一下下
92 shumaguan(1); //然后切换到第二个数码管。。。如此进行一直到最后一个数码管。。。最后再改变送入数码管的数值。。。再来一遍
93 P0 = smgduan[i2];
94 Delayms(1);
95 shumaguan(2);
96 P0 = smgduan[i3];
97 Delayms(1);
98 shumaguan(3);
99 P0 = smgduan[i4];
100 Delayms(1);
101 shumaguan(4);
102 P0 = smgduan[i5];
103 Delayms(1);
104 shumaguan(5);
105 P0 = smgduan[i6];
106 Delayms(1);
107 shumaguan(6);
108 P0 = smgduan[i7];
109 Delayms(1);
110 shumaguan(7);
111 P0 = smgduan[i8];
112 Delayms(1);
113 P0 = 0x00;
114 }
115 }
116 }
117
118 // 测试独立按键功能
119 void KeyTest()
120 {
121 while (1)
122 {
123 /*
124 * 不推荐此做法
125 * 此做法占用内存高
126 */
127 if (!K1)
128 {
129 // 软件延时消除抖动
130 Delayms(10);
131 if (!K1)
132 {
133 LED_1 = !LED_1;
134 }
135 // 确认按键已经按下
136 while(!K1);
137 }