1. 主页
  2. 文档
  3. Arduino教程(持续更新)...
  4. 2.基础例程
  5. 2.10 驱动四位数码管

2.10 驱动四位数码管

这次我们进行的实验是使用arduino驱动一块共阳四位数码管。驱动数码管限流电阻肯定是必不可少的,限流电阻有两种接法,一种是在d1-d4阳极接,总共接4颗。这种接法好处是需求电阻比较少,但是会产生每一位上显示不同数字亮度会不一样,1最亮,8最暗。另外一种接法就是在其他8个引脚上接,这种接法亮度显示均匀,但是用电阻较多。本次实验使用8颗220Ω电阻(因为没有100Ω电阻,所以使用220Ω的代替,100欧姆亮度会比较高)。

4位数码管总共有12个引脚,小数点朝下正放在面前时,左下角为1,其他管脚顺序为逆时针旋转。左上角为最大的12号管脚。

2.10 驱动四位数码管

下图为数码管的说明手册

2.10 驱动四位数码管

下面是硬件连接图

2.10 驱动四位数码管
2.10 驱动四位数码管

代码:

//设置阴极接口

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);

}





 访问次数: 367

我们要如何帮助您?

发表回复

登录后才能评论