信相关的0-100的闪烁
题目
郭天祥的第四节课的课的实验,就是0-100的实验的,每一秒进行一次,
分析
第一使用函数时,使用全局变量,只管导入就行了,而那些数据处理交给其他函数,自己拿来直接使用, 就可以,第二,尽量函数,使得在while()中的只有函数存在,还有最好也把display这个函数也拉到if外面,当i=0的时候display是不执行的,我觉得你看到的效果应该是数码管亮暗跳一下跳一下的,如果没有,大概是时间太短,你把i的值扩大应该就会看到数码管数字跳一下跳一下而有一个特别亮的状态,这个是这道题的核心,因此,对于定时器处理尽量写在定时器函数中,而对于display函数就是直接写成函数,写在while中就行了
代码 1 # include <reg52.h>
2 # define uint unsigned int
3 # define uchar unsigned char
4 uchar temp,ge,shi,bai,a;
5 uchar code table [] =
6 {
7 0x3f,0x06,0x5b,0x4f,
8 0x66,0x6d,0x7d,0x07,
9 0x7f,0x6f,0x77,0x7c,
10 0x39,0x5e,0x79,0x71
11 };
12 sbit wela = P2^7;
13 sbit dula = P2^6;
14 void init();
15 void delay(uint z);
16 void display(uchar ge,uchar shi,uchar bai);
17 void main()
18 {
19 init();
20 a = 0;
21 bai = 0;
22 shi = 0;
23 while(1)
24 {
25 display(bai,shi,ge);
26 }
27 }
28 void init()
29 {
30 TMOD = 0x01;
31 TH0 = (65536-50000)/256;
32 TL0 = (65536-50000)%256;
33 EA = 1;
34 ET0 = 1;
35 TR0 = 1;
36 }
37 void display(uchar bai,uchar shi,uchar ge)
38 {
39 wela = 1;
40 P0 = 0xfe;
41 wela = 0;
42 P0 = 0xff;
43 dula = 1;
44 P0 = table[bai];
45 dula = 0;
46 delay(5);
47
48 wela = 1;
49 P0 = 0xfd;
50 wela = 0;
51 P0 = 0xff;
52 dula = 1;
53 P0 = table[shi];
54 dula = 0;
55 delay(5);
56
57 wela = 1;
58 P0 = 0xfb;
59 wela = 0;
60 P0 = 0xff;
61 dula = 1;
62 P0 = table[ge];
63 dula = 0;
64 delay(5);
65 }
66
67 void timer0 () interrupt 1
68 {
69 TH0 = (65536-50000)/256;
70 TL0 = (65536-50000)%256;
71 a++;
72 if(a == 20)//每隔一秒temp变化1
73 {
74 a = 0;
75 temp ++;
76 if(temp == 100)
77 temp = 0;
78 bai = temp/100;
79 shi = temp%100/10;
80 ge = temp%10;
81
82 }
83 }
84 void delay(uint z)
85 {
86 uint x,y;
87 for(x = z; x > 0;x--)
88 {
89 for(y = 110; y > 0;y--);
90 }
91
3 # define uchar unsigned char
4 uchar temp,ge,shi,bai,a;
5 uchar code table [] =
6 {
7 0x3f,0x06,0x5b,0x4f,
8 0x66,0x6d,0x7d,0x07,
9 0x7f,0x6f,0x77,0x7c,
10 0x39,0x5e,0x79,0x71
11 };
12 sbit wela = P2^7;
13 sbit dula = P2^6;
14 void init();
15 void delay(uint z);
16 void display(uchar ge,uchar shi,uchar bai);
17 void main()
18 {
19 init();
20 a = 0;
21 bai = 0;
22 shi = 0;
23 while(1)
24 {
25 display(bai,shi,ge);
26 }
27 }
28 void init()
29 {
30 TMOD = 0x01;
31 TH0 = (65536-50000)/256;
32 TL0 = (65536-50000)%256;
33 EA = 1;
34 ET0 = 1;
35 TR0 = 1;
36 }
37 void display(uchar bai,uchar shi,uchar ge)
38 {
39 wela = 1;
40 P0 = 0xfe;
41 wela = 0;
42 P0 = 0xff;
43 dula = 1;
44 P0 = table[bai];
45 dula = 0;
46 delay(5);
47
48 wela = 1;
49 P0 = 0xfd;
50 wela = 0;
51 P0 = 0xff;
52 dula = 1;
53 P0 = table[shi];
54 dula = 0;
55 delay(5);
56
57 wela = 1;
58 P0 = 0xfb;
59 wela = 0;
60 P0 = 0xff;
61 dula = 1;
62 P0 = table[ge];
63 dula = 0;
64 delay(5);
65 }
66
67 void timer0 () interrupt 1
68 {
69 TH0 = (65536-50000)/256;
70 TL0 = (65536-50000)%256;
71 a++;
72 if(a == 20)//每隔一秒temp变化1
73 {
74 a = 0;
75 temp ++;
76 if(temp == 100)
77 temp = 0;
78 bai = temp/100;
79 shi = temp%100/10;
80 ge = temp%10;
81
82 }
83 }
84 void delay(uint z)
85 {
86 uint x,y;
87 for(x = z; x > 0;x--)
88 {
89 for(y = 110; y > 0;y--);
90 }
91
92 }
浙公网安备 33010602011771号