圣源电子制作
标题: arduino学习笔记26 4位数码管实验 [打印本页]
作者: SYDZ__顺 时间: 2012-4-28 19:32
标题: arduino学习笔记26 4位数码管实验
arduino学习笔记26 4位数码管实验
这次我们进行的实验是使用arduino驱动一块共阳四位数码管。驱动数码管限流电阻肯定是必不可少的,限流电阻有两种接法,一种是在d1-d4阳极接,总共接4颗。这种接法好处是需求电阻比较少,但是会产生每一位上显示不同数字亮度会不一样,1最亮,8最暗。另外一种接法就是在其他8个引脚上接,这种接法亮度显示均匀,但是用电阻较多。本次实验使用8颗220Ω电阻(因为没有100Ω电阻,所以使用220Ω的代替,100欧姆亮度会比较高)。
下图为4位数码管
(, 下载次数: 220)
4位数码管总共有12个引脚,小数点朝下正放在面前时,左下角为1,其他管脚顺序为逆时针旋转。左上角为最大的12号管脚。
(, 下载次数: 224)
下图为数码管的说明手册
(, 下载次数: 222)
(, 下载次数: 218)
把下面的代码复制下载到控制板中,看看效果。 1. //设置阴极接口
2. int a = 1;
3. int b = 2;
4. int c = 3;
5. int d = 4;
6. int e = 5;
7. int f = 6;
8. int g = 7;
9. int p = 8;
10. //设置阳极接口
11. int d4 = 9;
12. int d3 = 10;
13. int d2 = 11;
14. int d1 = 12;
15. //设置变量
16. int del = 100;
17. int buttoncount = 0;
18. int loopcount = 0;
19.
20. void setup()
21. {
22. pinMode(d1, OUTPUT);
23. pinMode(d2, OUTPUT);
24. pinMode(d3, OUTPUT);
25. pinMode(d4, OUTPUT);
26. pinMode(a, OUTPUT);
27. pinMode(b, OUTPUT);
28. pinMode(c, OUTPUT);
29. pinMode(d, OUTPUT);
30. pinMode(e, OUTPUT);
31. pinMode(f, OUTPUT);
32. pinMode(g, OUTPUT);
33. pinMode(p, OUTPUT);
34. digitalWrite(a, HIGH);
35. digitalWrite(b, HIGH);
36. digitalWrite(c, HIGH);
37. digitalWrite(d, HIGH);
38. digitalWrite(e, HIGH);
39. digitalWrite(f, HIGH);
40. digitalWrite(g, HIGH);
41. digitalWrite(p, HIGH);
42. }
43.
44. void loop()
45. {
46. roulette(4); //轮转效果
47. delay(100);
48. zigzag(2); //Z字型效果
49. delay(100);
50. circles(4); //圈状效果
51. delay(100);
52. }
53.
54. void pickDigit(int x) //定义pickDigit(x),其作用是开启dx端口
55. {
56. digitalWrite(d1, LOW);
57. digitalWrite(d2, LOW);
58. digitalWrite(d3, LOW);
59. digitalWrite(d4, LOW);
60.
61. switch(x)
62. {
63. case 1:
64. digitalWrite(d1, HIGH);
65. break;
66. case 2:
67. digitalWrite(d2, HIGH);
68. break;
69. case 3:
70. digitalWrite(d3, HIGH);
71. break;
72. default:
73. digitalWrite(d4, HIGH);
74. break;
75. }
76. }
77.
78. void clearLEDs() //清屏
79. {
80. digitalWrite(a, HIGH);
81. digitalWrite(b, HIGH);
82. digitalWrite(c, HIGH);
83. digitalWrite(d, HIGH);
84. digitalWrite(e, HIGH);
85. digitalWrite(f, HIGH);
86. digitalWrite(g, HIGH);
87. digitalWrite(p, HIGH);
88. }
89.
90. void roulette(int x) //设置轮转效果
91. {
92. loopcount = 0;
93. while (loopcount < x)
94. {
95. digitalWrite(a, LOW);
96. pickDigit(1);
97. delay(del);
98. pickDigit(2);
99. delay(del);
100. pickDigit(3);
101. delay(del);
102. pickDigit(4);
103. delay(del);
104. digitalWrite(a, HIGH);
105. digitalWrite(b, LOW);
106. delay(del);
107. digitalWrite(b, HIGH);
108. digitalWrite(c, LOW);
109. delay(del);
110. digitalWrite(c, HIGH);
111. digitalWrite(d, LOW);
112. delay(del);
113. pickDigit(3);
114. delay(del);
115. pickDigit(2);
116. delay(del);
117. pickDigit(1);
118. delay(del);
119. digitalWrite(d, HIGH);
120. digitalWrite(e, LOW);
121. delay(del);
122. digitalWrite(e, HIGH);
123. digitalWrite(f, LOW);
124. delay(del);
125. clearLEDs();
126. loopcount++;
127. }
128. }
129.
130. void zigzag(int x) //设置Z字形效果
131. {
132. loopcount = 0;
133. while(loopcount < x)
134. {
135. digitalWrite(a, LOW);
136. pickDigit(1);
137. delay(del);
138. pickDigit(2);
139. delay(del);
140. pickDigit(3);
141. delay(del);
142. pickDigit(4);
143. delay(del);
144. digitalWrite(a, HIGH);
145. digitalWrite(b, LOW);
146. delay(del);
147. digitalWrite(b, HIGH);
148. digitalWrite(g, LOW);
149. delay(del);
150. pickDigit(3);
151. delay(del);
152. pickDigit(2);
153. delay(del);
154. pickDigit(1);
155. delay(del);
156. digitalWrite(g, HIGH);
157. digitalWrite(e, LOW);
158. delay(del);
159. digitalWrite(e, HIGH);
160. digitalWrite(d, LOW);
161. delay(del);
162. pickDigit(2);
163. delay(del);
164. pickDigit(3);
165. delay(del);
166. pickDigit(4);
167. delay(del);
168. digitalWrite(d, HIGH);
169. digitalWrite(c, LOW);
170. delay(del);
171. digitalWrite(c, HIGH);
172. digitalWrite(g, LOW);
173. delay(del);
174. pickDigit(3);
175. delay(del);
176. pickDigit(2);
177. delay(del);
178. pickDigit(1);
179. delay(del);
180. digitalWrite(g, HIGH);
181. digitalWrite(f, LOW);
182. delay(del);
183. clearLEDs();
184. loopcount++;
185. }
186. }
187.
188. void circles(int x) //设置圈状效果
189. {
190. loopcount = 0;
191. while (loopcount < x)
192. {
193. digitalWrite(a, LOW);
194. digitalWrite(b, LOW);
195. digitalWrite(f, LOW);
196. digitalWrite(g, LOW);
197. pickDigit(1);
198. delay(250);
199. digitalWrite(a, HIGH);
200. digitalWrite(b, HIGH);
201. digitalWrite(f, HIGH);
202. digitalWrite(c, LOW);
203. digitalWrite(d, LOW);
204. digitalWrite(e, LOW);
205. pickDigit(2);
206. delay(250);
207. digitalWrite(a, LOW);
208. digitalWrite(b, LOW);
209. digitalWrite(f, LOW);
210. digitalWrite(c, HIGH);
211. digitalWrite(d, HIGH);
212. digitalWrite(e, HIGH);
213. pickDigit(3);
214. delay(250);
215. digitalWrite(a, HIGH);
216. digitalWrite(b, HIGH);
217. digitalWrite(f, HIGH);
218. digitalWrite(c, LOW);
219. digitalWrite(d, LOW);
220. digitalWrite(e, LOW);
221. pickDigit(4);
222. delay(250);
223. clearLEDs();
224. loopcount++;
225. }
226. }
复制代码
上面代码效果如下,为3种花样组成。
再把下面代码复制下载到控制板中,看看效果。
1. //设置阴极接口
2. int a = 1;
3. int b = 2;
4. int c = 3;
5. int d = 4;
6. int e = 5;
7. int f = 6;
8. int g = 7;
9. int p = 8;
10. //设置阳极接口
11. int d4 = 9;
12. int d3 = 10;
13. int d2 = 11;
14. int d1 = 12;
15. //设置变量
16. long n = 0;
17. int x = 100;
18. int del = 55; //此处数值对时钟进行微调
19.
20. void setup()
21. {
22. pinMode(d1, OUTPUT);
23. pinMode(d2, OUTPUT);
24. pinMode(d3, OUTPUT);
25. pinMode(d4, OUTPUT);
26. pinMode(a, OUTPUT);
27. pinMode(b, OUTPUT);
28. pinMode(c, OUTPUT);
29. pinMode(d, OUTPUT);
30. pinMode(e, OUTPUT);
31. pinMode(f, OUTPUT);
32. pinMode(g, OUTPUT);
33. pinMode(p, OUTPUT);
34. }
35.
36. void loop()
37. {
38. clearLEDs();
39. pickDigit(1);
40. pickNumber((n/x/1000)%10);
41. delayMicroseconds(del);
42.
43. clearLEDs();
44. pickDigit(2);
45. pickNumber((n/x/100)%10);
46. delayMicroseconds(del);
47.
48. clearLEDs();
49. pickDigit(3);
50. dispDec(3);
51. pickNumber((n/x/10)%10);
52. delayMicroseconds(del);
53.
54. clearLEDs();
55. pickDigit(4);
56. pickNumber(n/x%10);
57. delayMicroseconds(del);
58.
59. n++;
60.
61. if (digitalRead(13) == HIGH)
62. {
63. n = 0;
64. }
65. }
66.
67. void pickDigit(int x) //定义pickDigit(x),其作用是开启dx端口
68. {
69. digitalWrite(d1, LOW);
70. digitalWrite(d2, LOW);
71. digitalWrite(d3, LOW);
72. digitalWrite(d4, LOW);
73.
74. switch(x)
75. {
76. case 1:
77. digitalWrite(d1, HIGH);
78. break;
79. case 2:
80. digitalWrite(d2, HIGH);
81. break;
82. case 3:
83. digitalWrite(d3, HIGH);
84. break;
85. default:
86. digitalWrite(d4, HIGH);
87. break;
88. }
89. }
90.
91. void pickNumber(int x) //定义pickNumber(x),其作用是显示数字x
92. {
93. switch(x)
94. {
95. default:
96. zero();
97. break;
98. case 1:
99. one();
100. break;
101. case 2:
102. two();
103. break;
104. case 3:
105. three();
106. break;
107. case 4:
108. four();
109. break;
110. case 5:
111. five();
112. break;
113. case 6:
114. six();
115. break;
116. case 7:
117. seven();
118. break;
119. case 8:
120. eight();
121. break;
122. case 9:
123. nine();
124. break;
125. }
126. }
127.
128. void dispDec(int x) //设定开启小数点
129. {
130. digitalWrite(p, LOW);
131. }
132.
133. void clearLEDs() //清屏
134. {
135. digitalWrite(a, HIGH);
136. digitalWrite(b, HIGH);
137. digitalWrite(c, HIGH);
138. digitalWrite(d, HIGH);
139. digitalWrite(e, HIGH);
140. digitalWrite(f, HIGH);
141. digitalWrite(g, HIGH);
142. digitalWrite(p, HIGH);
143. }
144.
145. void zero() //定义数字0时阴极那些管脚开关
146. {
147. digitalWrite(a, LOW);
148. digitalWrite(b, LOW);
149. digitalWrite(c, LOW);
150. digitalWrite(d, LOW);
151. digitalWrite(e, LOW);
152. digitalWrite(f, LOW);
153. digitalWrite(g, HIGH);
154. }
155.
156. void one() //定义数字1时阴极那些管脚开关
157. {
158. digitalWrite(a, HIGH);
159. digitalWrite(b, LOW);
160. digitalWrite(c, LOW);
161. digitalWrite(d, HIGH);
162. digitalWrite(e, HIGH);
163. digitalWrite(f, HIGH);
164. digitalWrite(g, HIGH);
165. }
166.
167. void two() //定义数字2时阴极那些管脚开关
168. {
169. digitalWrite(a, LOW);
170. digitalWrite(b, LOW);
171. digitalWrite(c, HIGH);
172. digitalWrite(d, LOW);
173. digitalWrite(e, LOW);
174. digitalWrite(f, HIGH);
175. digitalWrite(g, LOW);
176. }
177.
178. void three() //定义数字3时阴极那些管脚开关
179. {
180. digitalWrite(a, LOW);
181. digitalWrite(b, LOW);
182. digitalWrite(c, LOW);
183. digitalWrite(d, LOW);
184. digitalWrite(e, HIGH);
185. digitalWrite(f, HIGH);
186. digitalWrite(g, LOW);
187. }
188.
189. void four() //定义数字4时阴极那些管脚开关
190. {
191. digitalWrite(a, HIGH);
192. digitalWrite(b, LOW);
193. digitalWrite(c, LOW);
194. digitalWrite(d, HIGH);
195. digitalWrite(e, HIGH);
196. digitalWrite(f, LOW);
197. digitalWrite(g, LOW);
198. }
199.
200. void five() //定义数字5时阴极那些管脚开关
201. {
202. digitalWrite(a, LOW);
203. digitalWrite(b, HIGH);
204. digitalWrite(c, LOW);
205. digitalWrite(d, LOW);
206. digitalWrite(e, HIGH);
207. digitalWrite(f, LOW);
208. digitalWrite(g, LOW);
209. }
210.
211. void six() //定义数字6时阴极那些管脚开关
212. {
213. digitalWrite(a, LOW);
214. digitalWrite(b, HIGH);
215. digitalWrite(c, LOW);
216. digitalWrite(d, LOW);
217. digitalWrite(e, LOW);
218. digitalWrite(f, LOW);
219. digitalWrite(g, LOW);
220. }
221.
222. void seven() //定义数字7时阴极那些管脚开关
223. {
224. digitalWrite(a, LOW);
225. digitalWrite(b, LOW);
226. digitalWrite(c, LOW);
227. digitalWrite(d, HIGH);
228. digitalWrite(e, HIGH);
229. digitalWrite(f, HIGH);
230. digitalWrite(g, HIGH);
231. }
232.
233. void eight() //定义数字8时阴极那些管脚开关
234. {
235. digitalWrite(a, LOW);
236. digitalWrite(b, LOW);
237. digitalWrite(c, LOW);
238. digitalWrite(d, LOW);
239. digitalWrite(e, LOW);
240. digitalWrite(f, LOW);
241. digitalWrite(g, LOW);
242. }
243.
244. void nine() //定义数字9时阴极那些管脚开关
245. {
246. digitalWrite(a, LOW);
247. digitalWrite(b, LOW);
248. digitalWrite(c, LOW);
249. digitalWrite(d, LOW);
250. digitalWrite(e, HIGH);
251. digitalWrite(f, LOW);
252. digitalWrite(g, LOW);
253. }
复制代码
这次的代码为简易的秒表,效果如下,精准度不是很高,需要大家微调参数。
欢迎光临 圣源电子制作 (http://syyyd.com/) |
Powered by Discuz! X3.4 |