这次我们进行的实验是使用arduino驱动一块共阳四位数码管。驱动数码管限流电阻肯定是必不可少的,限流电阻有两种接法,一种是在d1-d4阳极接,总共接4颗。这种接法好处是需求电阻比较少,但是会产生每一位上显示不同数字亮度会不一样,1最亮,8最暗。另外一种接法就是在其他8个引脚上接,这种接法亮度显示均匀,但是用电阻较多。本次实验使用8颗220Ω电阻(因为没有100Ω电阻,所以使用220Ω的代替,100欧姆亮度会比较高)。
下图为4位数码管
2011-8-6 16:08 上传
(118.5 KB)
4位数码管总共有12个引脚,小数点朝下正放在面前时,左下角为1,其他管脚顺序为逆时针旋转。左上角为最大的12号管脚。
2011-8-6 16:08 上传
(62.87 KB)
下图为数码管的说明手册
2011-8-6 16:08 上传
(143.54 KB)
下面是硬件连接图
2011-8-6 16:08 上传
(154.12 KB)
2011-8-6 16:12 上传
(202.16 KB)
把下面的代码复制下载到控制板中,看看效果。
ARDUINO
//设置阴极接口
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++;
}
}
上面代码效果如下,为3种花样组成。