|
转载自:零知开源快速入门16-数码管显示(4位)
http://www.lingzhilab.com/forum.php?mod=viewthread&tid=195&fromuid=2
(出处: 零知实验室)
一、工具原料
- 电脑,windows系统
- 零知开发板
- micro-usb线
- 4位数码管一个(共阳)
- 220Ω 电阻8个
- 面包板一个+若干跳线
二、硬件连接
1、电路原理图
2、电路连接
三、方法步骤
1、打开零知实验室软件开发工具,然后新建项目,输入以下代码:
- /**
- * 文件: 数码管-4位.ino by 零知实验室(www.lingzhilab.com)
- * -- 零知开源,让电子制作变得更简单! --
- * 时间: 2018/06/26 19:23
- * 说明: 4位数码管显示
- **/
- //设置阴极接口
- int a = 12;
- int b = 6;
- int c = 3;
- int d = 1;
- int e = 0;
- int f = 11;
- int g = 4;
- int dp = 2;
- //设置阳极接口
- int d4 = 5;
- int d3 = 9;
- int d2 = 10;
- int d1 = 13;
- //设置变量
- long n = 1230;
- int x = 100;
- int del = 55; //此处数值对时钟进行微调
- void Display(unsigned char x, unsigned char Number);//显示功能函数
- // the setup routine runs once when you press reset:
- void setup() {
- // put your setup code here, to run once:
- 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(dp, OUTPUT);
- }
- // the loop routine runs over and over again forever:
- void loop() {
- // put your main code here, to run repeatedly:
- Display(1, 5);
- Display(2, 2);
- Display(3, 0);
- Display(4, 2);
- }
- ///////////////////////////////////////////////////////////////
- void WeiXuan(unsigned char n)//
- {
- switch(n)
- {
- case 1:
- digitalWrite(d1,HIGH);
- digitalWrite(d2, LOW);
- digitalWrite(d3, LOW);
- digitalWrite(d4, LOW);
- break;
- case 2:
- digitalWrite(d1, LOW);
- digitalWrite(d2, HIGH);
- digitalWrite(d3, LOW);
- digitalWrite(d4, LOW);
- break;
- case 3:
- digitalWrite(d1,LOW);
- digitalWrite(d2, LOW);
- digitalWrite(d3, HIGH);
- digitalWrite(d4, LOW);
- break;
- case 4:
- digitalWrite(d1, LOW);
- digitalWrite(d2, LOW);
- digitalWrite(d3, LOW);
- digitalWrite(d4, HIGH);
- break;
- default :
- digitalWrite(d1, LOW);
- digitalWrite(d2, LOW);
- digitalWrite(d3, LOW);
- digitalWrite(d4, LOW);
- break;
- }
- }
- void Num_0()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, LOW);
- digitalWrite(f, LOW);
- digitalWrite(g, HIGH);
- digitalWrite(dp,HIGH);
- }
- void Num_1()
- {
- digitalWrite(a, HIGH);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, HIGH);
- digitalWrite(e, HIGH);
- digitalWrite(f, HIGH);
- digitalWrite(g, HIGH);
- digitalWrite(dp,HIGH);
- }
- void Num_2()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, HIGH);
- digitalWrite(d, LOW);
- digitalWrite(e, LOW);
- digitalWrite(f, HIGH);
- digitalWrite(g, LOW);
- digitalWrite(dp,HIGH);
- }
- void Num_3()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, HIGH);
- digitalWrite(f, HIGH);
- digitalWrite(g, LOW);
- digitalWrite(dp,HIGH);
- }
- void Num_4()
- {
- digitalWrite(a, HIGH);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, HIGH);
- digitalWrite(e, HIGH);
- digitalWrite(f, LOW);
- digitalWrite(g, LOW);
- digitalWrite(dp,HIGH);
- }
- void Num_5()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, HIGH);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, HIGH);
- digitalWrite(f, LOW);
- digitalWrite(g, LOW);
- digitalWrite(dp,HIGH);
- }
- void Num_6()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, HIGH);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, LOW);
- digitalWrite(f, LOW);
- digitalWrite(g, LOW);
- digitalWrite(dp,HIGH);
- }
- void Num_7()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, HIGH);
- digitalWrite(e, HIGH);
- digitalWrite(f, HIGH);
- digitalWrite(g, HIGH);
- digitalWrite(dp,HIGH);
- }
- void Num_8()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, LOW);
- digitalWrite(f, LOW);
- digitalWrite(g, LOW);
- digitalWrite(dp,HIGH);
- }
- void Num_9()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, HIGH);
- digitalWrite(f, LOW);
- digitalWrite(g, LOW);
- digitalWrite(dp,HIGH);
- }
- void Clear() //清屏
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, LOW);
- digitalWrite(f, LOW);
- digitalWrite(g, LOW);
- digitalWrite(dp,LOW);
- }
- void pickNumber(unsigned char n)
- {
- switch(n)
- {
- case 0:Num_0();
- break;
- case 1:Num_1();
- break;
- case 2:Num_2();
- break;
- case 3:Num_3();
- break;
- case 4:Num_4();
- break;
- case 5:Num_5();
- break;
- case 6:Num_6();
- break;
- case 7:Num_7();
- break;
- case 8:Num_8();
- break;
- case 9:Num_9();
- break;
- default:Clear();
- break;
- }
- }
- void Display(unsigned char x, unsigned char Number)
- {
- WeiXuan(x);
- pickNumber(Number);
- delay(1);
- Clear() ;
- }
[color=rgb(51, 102, 153) !important]复制代码
2、按照之前的方式先【编译】,然后【上传】到开发板中。
四、成果展示
上面步骤成功完成后,我们就可以看到预期的结果了,可以看到显示“5202”数字,如下:
|
|