目录
步骤一 材料准备
硬件准备:
软件准备:可以使用Arduino IDE编程,也可以使用Circuits在线编程还可以在线仿真(如果没有硬件的),下面是在线编程的面板。Circuits地址
步骤二 原理说明
数码管有abcdefg还包括一个dp,这些是数码管中组成“8”的每一笔,还有一个公共端称为com。将我们的a-dp连接在8个I/O口上,再加上每一个数码管都有一个com端口,通过这个com端口对数码管的控制。如果需要第一个(从左开始数)数码管就需要将a-dp输入想要的显示结果,将com1端口置低,如果需要数码管2显示的话将com2端口置低其他com端口置高。
步骤三 电路搭建
步骤四 编写程序
#define d_a 0
#define d_b 1
#define d_c 2
#define d_d 3
#define d_e 4
#define d_f 5
#define d_g 6
#define d_h 7
#define COM1 8
#define COM2 9
#define COM3 10
#define COM4 11
unsigned char shi=11;
unsigned char feng=12;
unsigned char miao=0;
unsigned char t=5;
unsigned char num[17][8] = {
//a b c d e f g h
{1, 1, 1, 1, 1, 1, 0, 0}, //0
{0, 1, 1, 0, 0, 0, 0, 0}, //1
{1, 1, 0, 1, 1, 0, 1, 0}, //2
{1, 1, 1, 1, 0, 0, 1, 0}, //3
{0, 1, 1, 0, 0, 1, 1, 0}, //4
{1, 0, 1, 1, 0, 1, 1, 0}, //5
{1, 0, 1, 1, 1, 1, 1, 0}, //6
{1, 1, 1, 0, 0, 0, 0, 0}, //7
{1, 1, 1, 1, 1, 1, 1, 0}, //8
{1, 1, 1, 1, 0, 1, 1, 0}, //9
{1, 1, 1, 0, 1, 1, 1, 1}, //A
{1, 1, 1, 1, 1, 1, 1, 1}, //B
{1, 0, 0, 1, 1, 1, 0, 1}, //C
{1, 1, 1, 1, 1, 1, 0, 1}, //D
{1, 0, 0, 1, 1, 1, 1, 1}, //E
{1, 0, 0, 0, 1, 1, 1, 1}, //F
{0, 0, 0, 0, 0, 0, 0, 1}, //.
};
void Display(unsigned char com,unsigned char n) {
digitalWrite(d_a,LOW);
digitalWrite(d_b,LOW);
digitalWrite(d_c,LOW);
digitalWrite(d_d,LOW);
digitalWrite(d_e,LOW);
digitalWrite(d_f,LOW);
digitalWrite(d_g,LOW);
digitalWrite(d_h,LOW);
switch(com) {
case 1:
digitalWrite(COM1,LOW);
digitalWrite(COM2,HIGH);
digitalWrite(COM3,HIGH);
digitalWrite(COM4,HIGH);
break;
case 2:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,LOW);
digitalWrite(COM3,HIGH);
digitalWrite(COM4,HIGH);
break;
case 3:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,HIGH);
digitalWrite(COM3,LOW);
digitalWrite(COM4,HIGH);
break;
case 4:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,HIGH);
digitalWrite(COM3,HIGH);
digitalWrite(COM4,LOW);
break;
default:break;
}
digitalWrite(d_a,num[n][0]);
digitalWrite(d_b,num[n][1]);
digitalWrite(d_c,num[n][2]);
digitalWrite(d_d,num[n][3]);
digitalWrite(d_e,num[n][4]);
digitalWrite(d_f,num[n][5]);
digitalWrite(d_g,num[n][6]);
digitalWrite(d_h,num[n][7]);
}
void Tim(unsigned char shi,unsigned char feng)
{
unsigned char shi_s =shi/10;
unsigned char shi_g = shi%10;
unsigned char feng_s = feng/10;
unsigned char feng_g = feng%10;
Display(1,shi_s);
Display(2,shi_g);
Display(2,16);
Display(3,feng_s);
Display(4,feng_g);
}
void setup()
{
pinMode(d_a,OUTPUT);
pinMode(d_b,OUTPUT);
pinMode(d_c,OUTPUT);
pinMode(d_d,OUTPUT);
pinMode(d_e,OUTPUT);
pinMode(d_f,OUTPUT);
pinMode(d_g,OUTPUT);
pinMode(d_h,OUTPUT);
pinMode(COM1,OUTPUT);
pinMode(COM2,OUTPUT);
pinMode(COM3,OUTPUT);
pinMode(COM4,OUTPUT);
}
void loop() //模拟一个时钟
{
if(miao==60)
{
miao=0;
feng++;
if(feng==60)
{
shi++;
}
}
else
{
miao++;
}
while(t!=0)
{
Tim(shi,feng);
delay(1);
t--;
}
t=5;
}