/**
代码功能:
使数码管动态显示数字,并以十进制进位(计数)
软件环境:
ubuntu+sdcc+sublime+stcflash.py
硬件环境:
普中V2.0 51单片机开发板
*/
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
18 void main()
19 {
20 while (1)
21 {
22 DigDisplay();
23 }
24 }
25
26
27 void Delayms(u16 ms)
28 {
29 unsigned int i, j;
30 for(i = ms; i > 0; i--)
31 for(j = 110; j > 0; j--);
32 return;
33 }
34
35 void shumaguan(u8 n)
36 {
37 switch(n)
38 {
39 case 0:
40 LSA = 0;LSB = 0;LSC = 0;break;
41 case 1:
42 LSA = 1;LSB = 0;LSC = 0;break;
43 case 2:
44 LSA = 0;LSB = 1;LSC = 0;break;
45 case 3:
46 LSA = 1;LSB = 1;LSC = 0;break;
47 case 4:
48 LSA = 0;LSB = 0;LSC = 1;break;
49 case 5:
50 LSA = 1;LSB = 0;LSC = 1;break;
51 case 6:
52 LSA = 0;LSB = 1;LSC = 1;break;
53 case 7:
54 LSA = 1;LSB = 1;LSC = 1;break;
55 }
56 }
57
58 void DigDisplay()
59 {
60 u8 i1 = 0;
61 u8 i2 = 0;
62 u8 i3 = 0;
63 u8 i4 = 0;
64 u8 i5 = 0;
65 u8 i6 = 0;
66 u8 i7 = 0;
67 u8 i8 = 0;
68
69
70
71
72 for (i8 = 0; i8 < 10; i8++)
73 for (i7 = 0; i7 < 10; i7++)
74 for (i6 = 0; i6 < 10; i7++)
75 for (i5 = 0; i5 < 10; i5++)
76 for (i4 = 0; i4 < 10; i4++)
77 for (i3 = 0; i3 < 10; i3++)
78 for (i2 = 0; i2 < 10; i2++)
79 for (i1 = 0; i1 < 10; i1++)
80 {
81 u16 cnt = 10;
82 while (cnt--)
83 {
84 shumaguan(0); //选中第一个数码管
85 P0 = smgduan[i1]; //给他送一个数字
86 Delayms(1); //稍微延时一下下
87 shumaguan(1); //然后切换到第二个数码管。。。如此进行一直到最后一个数码管。。。最后再改变送入数码管的数值。。。再来一遍
88 P0 = smgduan[i2];
89 Delayms(1);
90 shumaguan(2);
91 P0 = smgduan[i3];
92 Delayms(1);
93 shumaguan(3);
94 P0 = smgduan[i4];
95 Delayms(1);
96 shumaguan(4);
97 P0 = smgduan[i5];
98 Delayms(1);
99 shumaguan(5);
100 P0 = smgduan[i6];
101 Delayms(1);
102 shumaguan(6);
103 P0 = smgduan[i7];
104 Delayms(1);
105 shumaguan(7);
106 P0 = smgduan[i8];
107 Delayms(1);
108 P0 = 0x00;
109 }
110 }
111 }