4位数码管实验

代码

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//设置阴极接口
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
int f = 6;
int g = 7;
int p = 8;
//设置阳极接口
int d4 = 9;
int d3 = 10;
int d2 = 11;
int d1 = 12;
//设置变量 16.
int del = 100;
int buttoncount = 0;
int loopcount = 0;
void setup()
{
pinMode(d1, OUTPUT);
pinMode(d2, OUTPUT);
pinMode(d3, OUTPUT);
pinMode(d4, OUTPUT);
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(p, OUTPUT);
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(p, HIGH);
}
void loop()
{
roulette(4); //轮转效果
delay(100);
zigzag(2); //Z 字型效果
delay(100);
circles(4); //圈状效果
delay(100);
}

void pickDigit(int x) //定义 pickDigit(x),其作用是 开启 dx 端口
{
digitalWrite(d1, LOW);
digitalWrite(d2, LOW);
digitalWrite(d3, LOW);
digitalWrite(d4, LOW);

switch(x)
{
case 1:
digitalWrite(d1, HIGH);
break;
case 2:
digitalWrite(d2, HIGH);
break;
case 3:
digitalWrite(d3, HIGH);
break;
default:
digitalWrite(d4, HIGH);
break;
}
}

void clearLEDs() //清屏
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(p, HIGH);
}

void roulette(int x) //设置轮转效果
{
loopcount = 0;
while (loopcount < x)
{
digitalWrite(a, LOW);
pickDigit(1);
delay(del);
pickDigit(2);
delay(del);
pickDigit(3);
delay(del);
pickDigit(4);
delay(del);
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
delay(del);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
delay(del);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
delay(del);
pickDigit(3);
delay(del);
pickDigit(2);
delay(del);
pickDigit(1);
delay(del);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
delay(del);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
delay(del);
clearLEDs();
loopcount++;
}
}

void zigzag(int x) //设置 Z 字形效果
{
loopcount = 0;
while(loopcount < x)
{
digitalWrite(a, LOW);
pickDigit(1);
delay(del);
pickDigit(2);
delay(del);
pickDigit(3);
delay(del);
pickDigit(4);
delay(del);
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
delay(del);
digitalWrite(b, HIGH);
digitalWrite(g, LOW);
delay(del);
pickDigit(3);
delay(del);
pickDigit(2);
delay(del);
pickDigit(1);
delay(del);
digitalWrite(g, HIGH);
digitalWrite(e, LOW);
delay(del);
digitalWrite(e, HIGH);
digitalWrite(d, LOW);
delay(del);
pickDigit(2);
delay(del);
pickDigit(3);
delay(del);
pickDigit(4);
delay(del);
digitalWrite(d, HIGH);
digitalWrite(c, LOW);
delay(del);
digitalWrite(c, HIGH);
digitalWrite(g, LOW);
delay(del);
pickDigit(3);
delay(del);
pickDigit(2);
delay(del);
pickDigit(1);
delay(del);
digitalWrite(g, HIGH);
digitalWrite(f, LOW);
delay(del);
clearLEDs();
loopcount++;
}
}

void circles(int x) //设置圈状效果
{
loopcount = 0;
while (loopcount < x)
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
pickDigit(1);
delay(250);
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(f, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
pickDigit(2);
delay(250);
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(f, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
pickDigit(3);
delay(250);
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(f, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
pickDigit(4);
delay(250);
clearLEDs();
loopcount++;
}
}

简易秒表

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//设置阴极接口
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
int f = 6;
int g = 7;
int p = 8;
//设置阳极接口
int d4 = 9;
int d3 = 10;
int d2 = 11;
int d1 = 12;
//设置变量
long n = 0;
int x = 100;
int del = 55; //此处数值对时钟进行微调

void setup()
{
pinMode(d1, OUTPUT);
pinMode(d2, OUTPUT);
pinMode(d3, OUTPUT);
pinMode(d4, OUTPUT);
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(p, OUTPUT);
}

void loop()
{
clearLEDs();
pickDigit(1); //定义 pickDigit(x),其作用是 开启 dx 端口
pickNumber((n/x/1000)%10); //定义 pickNumber(x),其作用 是显示数字 x
delayMicroseconds(del);

clearLEDs();
pickDigit(2);
pickNumber((n/x/100)%10);
delayMicroseconds(del);

clearLEDs();
pickDigit(3);
dispDec(3); //设定开启小数点
pickNumber((n/x/10)%10);
delayMicroseconds(del);

clearLEDs();
pickDigit(4);
pickNumber(n/x%10);
delayMicroseconds(del);

n++;

if (digitalRead(13) == HIGH)
{
n = 0;
}
}

void pickDigit(int x) //定义 pickDigit(x),其作用是 开启 dx 端口
{
digitalWrite(d1, LOW);
digitalWrite(d2, LOW);
digitalWrite(d3, LOW);
digitalWrite(d4, LOW);

switch(x)
{
case 1:
digitalWrite(d1, HIGH);
break;
case 2:
digitalWrite(d2, HIGH);
break;
case 3:
digitalWrite(d3, HIGH);
break;
default:
digitalWrite(d4, HIGH);
break;
}
}

void pickNumber(int x) //定义 pickNumber(x),其作用 是显示数字 x
{
switch(x)
{
default:
zero();
break;
case 1:
one();
break;
case 2:
two();
break;
case 3:
three();
break;
case 4:
four();
break;
case 5:
five();
break;
case 6:
six();
break;
case 7:
seven();
break;
case 8:
eight();
break;
case 9:
nine();
break;
}
}

void dispDec(int x) //设定开启小数点
{
digitalWrite(p, LOW);
}

void clearLEDs() //清屏
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(p, HIGH);
}

void zero() //定义数字 0 时阴极那些管脚开关
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}

void one() //定义数字 1 时阴极那些管脚开关
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}

void two() //定义数字 2 时阴极那些管脚开关
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
}

void three() //定义数字 3 时阴极那些管脚开关
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
}

void four() //定义数字 4 时阴极那些管脚开关
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}

void five() //定义数字 5 时阴极那些管脚开关
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}

void six() //定义数字 6 时阴极那些管脚开关
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}

void seven() //定义数字 7 时阴极那些管脚开关
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}

void eight() //定义数字 8 时阴极那些管脚开关
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}

void nine() //定义数字 9 时阴极那些管脚开关
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}