Bước 1: Linh kiện
1 - Led 7 đoạn A chung
2 - Điện trở 330 ohm,
Bước 2: Nối mạch
chân a LED nối vào chân 0 arduino; //
chân b LED nối vào chân 1;
chân c LED nối vào chân 2;
chân d LED nối vào chân 3;
chân e LED nối vào chân 4;
chân f LED nối vào chân 5;
chân g LED nối vào chân 6;
chân ca4 LED nối vào chân 8;
chân ca3 LED nối vào chân 9;
chân ca2 LED nối vào chân10;
chân ca1 LED nối vào chân 11;
Bước 3: Chương trình
int a = 0; //
int b = 1;
int c = 2;
int d = 3;
int e = 4;
int f = 5;
int g = 6;
int ca4=8;
int ca3=9;
int ca2=10;
int ca1=11;
int pins[]={0,1,2,3,4,5,6};
int canode[4]={11,10,9,8};
int numeri[]=
{
B10111111, // 0
B10000110, // 1
B11011011, // 2
B11001111, // 3
B11100110, // 4
B11101101, // 5
B11111101, // 6
B10000111, // 7
B11111111, // 8
B11101111, // 9
};
void setup()
{
for(int k = 1; k <= 12; k++)
{
pinMode(k - 1, OUTPUT);
digitalWrite(k - 1, HIGH);
}
digitalWrite(ca4,LOW);
digitalWrite(ca3,LOW);
digitalWrite(ca2,LOW);
digitalWrite(ca1,LOW);
}
void loop()
{
int num=1234;
for(int digit=0;digit<=3;digit++)
{
display3(num %10,canode[digit]);
//digitalWrite(11-digit,HIGH);
//delay(10);
//digitalWrite(11-digit,LOW);
num=num/10;
/*display2(num/10 %10);
digitalWrite(ca2,HIGH);
delay(10);
digitalWrite(ca2,LOW);
display2(num/100 %10);
digitalWrite(ca3,HIGH);
delay(10);
digitalWrite(ca3,LOW);
display2(num/1000 %10);
digitalWrite(ca4,HIGH);
delay(10);
digitalWrite(ca4,LOW);*/
}
}
void display(int n)
{ // spegnere tutti i led
for(int k = 1; k <= 7; k++)
{
digitalWrite(k - 1, HIGH);
}
//controllare il dato n trasmesso da stampare e stamparlo
switch (n)
{
case 0:
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
// digitalWrite(g, LOW);
break;
case 1:
// digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
// digitalWrite(d, LOW);
// digitalWrite(e, LOW);
// digitalWrite(f, LOW);
// digitalWrite(g, LOW);
break;
case 2:
digitalWrite(a, LOW);
digitalWrite(b, LOW);
// digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
// digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
case 3:
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
// digitalWrite(e, LOW);
// digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
case 4:
// digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
// digitalWrite(d, LOW);
// digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
case 5:
digitalWrite(a, LOW);
// digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
// digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
case 6:
digitalWrite(a, LOW);
// digitalWrite(b, LOW);
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, LOW);
// digitalWrite(e, LOW);
// digitalWrite(f, LOW);
// digitalWrite(g, LOW);
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, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
}
}
void display2(int num)
{ // accendere tutti i led
for(int k = 1; k <= 7; k++)
{
digitalWrite(k - 1, LOW);
}
if (num==1 || num==4) digitalWrite(a,HIGH);
if (num==5 || num==6) digitalWrite(b,HIGH);
if (num==2) digitalWrite(c,HIGH);
if (num==1 || num==4 || num==7) digitalWrite(d,HIGH);
if (num==1 || num==3 || num==4 || num==5 || num==7 || num==9) digitalWrite(e,HIGH);
if (num==1 || num==2 || num==3 || num==7) digitalWrite(f,HIGH);
if (num==1 || num==7 || num==0) digitalWrite(g,HIGH);
}
void display3(int cifra,int dig)
{
for(int seg=0;seg<=6;seg++)
{
digitalWrite(seg,HIGH);
}
for(int seg=0;seg<=6;seg++)
{
boolean statoseg=bitRead(numeri[cifra],seg);
digitalWrite(seg,!statoseg);
digitalWrite(dig,HIGH);
delay(5);
digitalWrite(dig,LOW);
}
//digitalWrite(dig,HIGH);
//delay(5);
//digitalWrite(dig,LOW);
}