拆开包装的一瞬间本小白惊呆了:MD居然有十二个引脚!!老子的OLED屏幕才4个引脚!!
经过一番简单的搜索。本小白找到了这个:
怎么说呢,不明觉厉吧,不过这种高深莫测的图显然不是我这种层次的人能够揣测的,于是我继续搜索,找到了这个:
如图所示(图片来自百度经验):
从这张图上,可以简单看出四位数码管的工作原理,四位数码管一共8*4个发光二极管,包括四个数管,每个数管为a,b,c,d,e,f,g,h八段(第八段为数管左下角的小数点,图中未画出)。共12个引脚,其中4个引脚控制标号1234的四个数管是否发光,8个引脚控制abcdefgh八段LED灯是否发光。
那么,怎么用ARDUINO来控制呢?
事实上,数码管分为共阳数码管和共阴数码管。顾名思义,共阳即共阳极,共阴即共阴极。对于共阳数码管,因为阳极共用,因此各引脚实际上与阴极相连,因此需给予低电平方能触发,共阴极引脚则相反。本小白采用的是共阳数码管,因此后续均以共阳数码管为基础讨论。
数码管的控制与LED灯的控制类似,比如给予引脚1和引脚a低电平,其余引脚高电平,则1a处的数码管被点亮。
接线也和LED灯类似,引脚1234直接与UNO的IO接口相连即可,控制LED的引脚abcdefgh则需串联一220Ω的电阻。接线时,本小白采用的方案是:引脚a-h分别对应UNO的1-8,引脚1-4分别对于UNO的12-9(注意此处顺序!)
最终接线图:
(只需要知道接线异常麻烦即可,下边是有源蜂鸣器,另有他用,可以忽略)
接好线以后,就可以开始愉快的玩耍了!
玩耍过程中发现一个严肃的问题,按照数码管的工作原理,四个数管显示的内容始终相同啊。如果我想让数码管显示“5678”,应该怎么办呢?
实验表明,如果我让数管1快速显示“5”然后关闭,数管2快速显示“6”然后关闭……
只要速度够快,人眼看到的就是“5678”!
示例程序如图所示(相关函数省略,简介及功能在注释里面):
clearLEDs(); //此处为将八个LED的引脚设为高电平,用于防止重影
pickDigit(1); //此处为将引脚1设为低电平,其余3引脚为高电平
pickNumber(5); //此处为控制八个LED使数码管输出为“5”
delay(2);
clearLEDs();
pickDigit(2);
pickNumber(6);
delay(2);
clearLEDs();
pickDigit(3);
pickNumber(7);
delay(2);
clearLEDs();
pickDigit(4);
pickNumber(8);
delay(2);
这样,就可以控制数码管显示四个不同内容了
库函数包括六个:
void begin();
该函数用于引脚的初始化,由于某种特殊的原因(懒),此处要求引脚连接方式固定(见上文)
void pickNumber(int number);
该函数用于将8个LED发光样式设定为对应的数字(0~9);
void pickDigit(int digit);
该函数用于设定某一数管发光(低电平),其余不发光(高电平)
void clearLEDs();
该函数用于清空数码管(设置8个LED均为高电平)
void printNumber(int n);
该函数用于显示不带小点的数字(0~9999)
void pointNumber(int n, boolean n1, boolean n2, boolean n3, boolean n4);
该函数用于输出带小点的数字(0~9999),后四个参数分别对应四个小点的发光状态(1为发光,0为不发光),
如pointNumber(2134,0,1,1,0)的显示结果为21.3.4
下面为h文件
#ifndef LED4_H
#define LED4_H
#include "Arduino.h"
class LED4 {
public:
void begin(); //用于引脚的初始化
void pickNumber(int number); //用于将8个LED发光样式设定为对应的数字(0~9)
void pickDigit(int digit); //用于设定某一数管发光(低电平),其余不发光(高电平)
void clearLEDs(); //用于清空数码管
void printNumber(int n); //用于显示不带小点的数字(0~9999)
void pointNumber(int n, boolean n1, boolean n2, boolean n3, boolean n4); //用于输出带小点的数字(0~9999),后四个参数分别对应四个小点的发光状态(1为发光,0为不发光)
private:
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;
};
#endif
下面为cpp文件:(懒得敲注释了,不过应该比较易懂,见谅)
#include "Arduino.h"
#include "LED4.h"
void LED4::begin()
{
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 LED4::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 LED4::pickDigit(int digit)
{
digitalWrite(d1, LOW);
digitalWrite(d2, LOW);
digitalWrite(d3, LOW);
digitalWrite(d4, LOW);
switch (digit)
{
case 1:
digitalWrite(d1, HIGH);
break;
case 2:
digitalWrite(d2, HIGH);
break;
case 3:
digitalWrite(d3, HIGH);
break;
default:
digitalWrite(d4, HIGH);
break;
}
}
void LED4::pickNumber(int number)
{
switch (number)
{
case 0:
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
break;
case 1:
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;
case 2:
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
break;
case 3:
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
break;
case 4:
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
case 5:
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
case 6:
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
case 7:
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;
case 8:
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
case 9:
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
}
}
void LED4::printNumber(int n)
{
int i, j, k, l;
i = n / 1000;
j = (n % 1000) / 100;
k = (n % 100) / 10;
l = n % 10;
clearLEDs();
pickDigit(1);
pickNumber(i);
delay(2);
clearLEDs();
pickDigit(2);
pickNumber(j);
delay(2);
clearLEDs();
pickDigit(3);
pickNumber(k);
delay(2);
clearLEDs();
pickDigit(4);
pickNumber(l);
delay(2);
}
void LED4::pointNumber(int n, boolean n1, boolean n2, boolean n3, boolean n4)
{
int i, j, k, l;
i = n / 1000;
j = (n % 1000) / 100;
k = (n % 100) / 10;
l = n % 10;
clearLEDs();
pickDigit(1);
pickNumber(i);
if(n1)
digitalWrite(p, LOW);
delay(2);
clearLEDs();
pickDigit(2);
pickNumber(j);
if (n2)
digitalWrite(p, LOW);
delay(2);
clearLEDs();
pickDigit(3);
pickNumber(k);
if (n3)
digitalWrite(p, LOW);
delay(2);
clearLEDs();
pickDigit(4);
pickNumber(l);
if (n4)
digitalWrite(p, LOW);
delay(2);
}
keywords.txt文件省略
下面为实例程序,用于番茄钟
#include "LED4.h"
LED4 led4;
int i,j,k;
int worktime = 45; //番茄钟工作时间
int resttime = 8; //番茄钟休息时间
int nowtime = 0; //当前剩余时间
void setup()
{
led4.begin();
pinMode(13,OUTPUT);
}
void loop()
{
start(worktime); //开始工作
FMtone(3000); //工作结束,蜂鸣器发声提示
start(resttime); //开始休息
FMtone(1000); //休息结束,蜂鸣器发声提示
}
/*用于蜂鸣器发声*/
void FMtone(int t)
{
digitalWrite(13,HIGH);
delay(t);
digitalWrite(13,LOW);
}
/*用于开始时长为t的倒计时*/
void start(int t)
{
for(k = 0;k < 125;k++) /*下面循环默认从XX:59开始,此语句用于修正为从XX:00开始*/
led4.pointNumber(t * 100,0,1,0,0);
for(i = t - 1;i >= 0;i--) /*分钟*/
{
for(j = 59;j >= 0;j--) /*秒*/
{
nowtime = i * 100 + j;
for(k = 0;k < 118;k++) /*库中pointNumber函数包括4个delay(2),此处理应为1000/8 = 125,但经实际修正,改为118*/
{
led4.pointNumber(nowtime,0,1,0,0);
}
}
}
}
库下载地址:
https://download.csdn.net/download/weixin_43771001/10963621
以上