1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
//
// Created by NightRain on 2024/11/18.
//
#include "tool.h"
#include "mcs51/8052.h"
// 共阳4位数码管的段码数组
unsigned char SMG_NoDot[18] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92,
0x82, 0xf8, 0x80, 0x90, 0x88, 0x80,
0xc6, 0xc0, 0x86, 0x8e, 0xbf, 0x7f};
unsigned int hour = 0;
unsigned int minute = 0;
unsigned int second = 0;
void DisplaySMG_Dynamic();
void Delay(unsigned int time);
void DelaySMG(unsigned int time);
void Init74HC138(unsigned char n);
void SelectBit(unsigned char pos);
void SelectSegment(unsigned char value);
// 延时函数
void Delay(unsigned int time) {
while (time--) {
DisplaySMG_Dynamic();
}
}
// 延时函数
void DelaySMG(unsigned int time) {
while (time--)
;
}
// 选择通道,即具体选择哪个锁存器
void Init74HC138(unsigned char n) {
switch (n) {
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
void int74h573(unsigned int n) {
switch (n) {
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
// 选择位
void SelectBit(unsigned char pos) {
Init74HC138(6); // Open COM end
P0 = 0x00; // Turn off all segments
DelaySMG(10); // Short delay to eliminate shadow
P0 = (0x01 << pos);
}
// 选择段
void SelectSegment(unsigned char value) {
Init74HC138(7); // 打开段选端
P0 = value;
}
void time_increase() {
second++;
if (second == 60) {
second = 0;
minute++;
if (minute == 60) {
minute = 0;
hour++;
if (hour == 24) {
hour = 0;
}
}
}
}
// 动态显示数码管
void DisplaySMG_Dynamic() {
SelectBit(0);
SelectSegment(SMG_NoDot[hour / 10]);
DelaySMG(500);
SelectBit(1);
SelectSegment(SMG_NoDot[hour % 10]);
DelaySMG(500);
SelectBit(2);
SelectSegment(SMG_NoDot[16]);
DelaySMG(500);
SelectBit(3);
SelectSegment(SMG_NoDot[minute / 10]);
DelaySMG(500);
SelectBit(4);
SelectSegment(SMG_NoDot[minute % 10]);
DelaySMG(500);
SelectBit(5);
SelectSegment(SMG_NoDot[16]);
DelaySMG(500);
SelectBit(6);
SelectSegment(SMG_NoDot[second / 10]);
DelaySMG(500);
SelectBit(7);
SelectSegment(SMG_NoDot[second % 10]);
DelaySMG(500);
}
|