电子说
步骤1:
我使用带有Arduino的Darlington ULN2803来控制Evil Mad Scientist的巨型共阳极7段显示器。
正常尺寸的7段显示器由7个矩形LED组成,这些LED排列成可以显示数字0-9。点亮正常的7段显示器的一部分相当于点亮LED,它需要大约3V和20mA的电流;仅由Arduino即可轻松实现。这个巨大的7段有点不同,因为每个段都由4组并联的15组红色LED线串联而成(图2)。要打开LED,段上的电压应为15 *(每个LED的正向电压),流过每个段的电流为4 *(每个LED的典型工作电流)。每节输出约为31.5V(15 * 2.1V)和80mA(4 * 20mA),显然Arduino将无法自行处理。
要控制此显示,我连接了共阳极到36V,然后选择性地将要点亮的显示器各部分接地。
下面的代码通过7段显示器上的数字0-9进行计数,每秒从一个数字滴答到另一个。它使用for循环将变量“ i”从0-9递增,然后使用switch/case语句打开相应的段(通过将其相应的Arduino引脚设置为高电平。
/*7 seg display with ULN2803
by RU4Realz
July 2012
arduino pin connections (via ULN2803):
arduino (digital) pin 0 7 seg pin f
1 g
2 a
3 b
4 c
5 d
6 e
*/
void setup() {
for (byte a=0;a《8;a++){
pinMode(a,OUTPUT);//set digital pins 0 - 6 as outputs
}
}
void loop() {
for (byte i = 0; i 《 10; i++){//for 0 -9
switch(i){
case 0://if i == 0, turn on appropriate leds
PORTD&=128;//turn digital 0-6 off
digitalWrite(0,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
break;
case 1://if i == 1
PORTD&=128;//turn digital 0-6 off
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
break;
case 2://if i == 2
PORTD&=128;//turn digital 0-6 off
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
break;
case 3:
PORTD&=128;
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
break;
case 4:
PORTD&=128;//turn digital 0-7 off
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
break;
case 5:
PORTD&=128;//turn digital 0-7 off
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
break;
case 6:
PORTD&=128;//turn digital 0-7 off
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
break;
case 7:
PORTD&=128;//turn digital 0-7 off
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
break;
case 8:
PORTD&=128;//turn digital 0-7 off
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
break;
case 9:
PORTD&=128;//turn digital 0-7 off
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
break;
}
delay (1000);//wait 1 sec
}
}
全部0条评论
快来发表一下你的评论吧 !