|
本帖最后由 OpenJumper 于 2022-1-17 14:26 编辑
[md]## 一、四位数码管显示实验
在上一课中,我们学习了一位数码管的控制,通过程序来使数码管点亮或熄灭,但是遇到多位数字,我们又该怎么显示呢?所以,我们今天要学习的就是四位数码管的控制,四位数码管并不是简单的1+1+1+1的样子,那么它究竟跟一位数码管有什么区别呢,就让我们来看今天的课程。
## 二、实验器材
+ UNO控制板:1块
+ 共阳极四位数码管:1个
+ 面包板:1块
+ 面包板跳线:若干
+ 220Ω电阻:8个
## 三、连线示意图
图7-1
## 四、硬件连接图
图7-2
## 五、硬件知识要点
图7-3
将限流电阻的一端插到数字 I/O 中,另一端和数码管的字段引脚相连,剩下的六个字段和一个小数点依次按照这种方法连接。将公共极 COM 如果是共阳极的就 接到+5V,如果是共阴极的就接到 GND。4 位数码管总共有 12个引脚,小数点朝下正放在面前时,左下角为 1,其他管脚顺序为逆时针旋转。左上角为最大的12号管脚。
图7-4
驱动数码管限流电阻肯定是必不可少的,限流电阻有两种接法,一种是在d1-d4阳极接,总共接4颗。这种接法好处是需求电阻比较少,但是会产生每一位上显示不同数字亮度会不一样,1最亮,8最暗。另外一种接法就是在其他8个引脚上接,这种接法亮度显示均匀,但是用电阻较多。
## 六、程序编写
根据上面的程序,数码管会以三种不同的样式显示,供大家选择。[/md]
- //设置阴极接口
- 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;
- //设置变量
- 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++;
- }
- }
复制代码 [md]
## 七、程序知识要点
switch / case语句:
和 if 语句相同,switch…case通过程序员设定的在不同条件下执行的代码控制程序的流程。特别地,switch语句将变量值和case语句中设定的值进行比较。当一个case语句中的设定值与变量值相同时,这条case语句将被执行。
关键字break可用于退出switch语句,通常每条case语句都以break结尾。如果没有break语句,switch语句将会一直执行接下来的语句(一直向下)直到遇见一个break,或者直到switch语句结尾。
[/md]
- switch (var) {
- case 1:
- //当var等于1时,执行一些语句
- break;
- case 2
- //当var等于2时,执行一些语句
- break;
- default:
- //如果没有任何匹配,执行default
- //default可有可不有
- }
复制代码 [md]
## 八、拓展阅读
在本章的拓展阅读中,我们为大家制作一款简易的秒表,其程序如下:
[/md]
- //设置阴极接口
- 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);
- pickNumber((n/x/1000)%10);
- 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);
- }
复制代码 |
|