Arduino Uno驱动四位数码管
我用的是 四位共阳极数码管 型号是5461BS-1 数码管
接线方法
UNO引脚 | 数据码 |
---|
3 | 1 | 4 | 2 | 5 | 3 | 6 | 4 | 7 | A | 8 | B | 9 | C | 10 | D | 11 | E | 12 | F | 13 | G |
程序源码
我写了个倒计时
#define d_a 7 //定义数码管a 接引脚 2
#define d_b 8
#define d_c 9
#define d_d 10
#define d_e 11
#define d_f 12
#define d_g 13
#define d_h 2
#define COM1 3 //定义 第一组数码管 com1 接引脚2
#define COM2 4
#define COM3 5
#define COM4 6
//数码管0-9码值 这个是共阳极的和共阴的不一样
unsigned char num[10][8] =
{
// a b c d e f g h 定义0-9的编码
{0, 0, 0, 0, 0, 0, 1, 1}, // 0
{1, 0, 0, 1, 1, 1, 1, 1}, // 1
{0, 0, 1, 0, 0, 1, 0, 1}, // 2
{0, 0, 0, 0, 1, 1, 0, 1}, // 3
{1, 0, 0, 1, 1, 0, 0, 1}, // 4
{0, 1, 0, 0, 1, 0, 0, 1}, // 5
{0, 1, 0, 0, 0, 0, 0, 1}, // 6
{0, 0, 0, 1, 1, 1, 1, 1}, // 7
{0, 0, 0, 0, 0, 0, 0, 1}, // 8
{0, 0, 0, 0, 1, 0, 0, 1}, // 9
};
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()
{
for (int n = 500; n >= 0; n--)
{
if (n == 500)
{
for (int q = 0; q < 125; q++)
{
delay(20);
Display(4, 0);
delay(5);
Display(2, 5); //第1位数码管显示l的值
delay(5);
Display(3, 0);
delay(5);
}
}
int ge = n / 1 % 10;
int shi = n / 10 % 10;
int bai = n / 100 % 10;
for (int q = 0; q < 125; q++)
{
Display(2, bai); //第1位数码管显示l的值
delay(2);
Display(3, shi);
delay(2);
Display(4, ge);
delay(4);
}
}
}
int pos[4][4] = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
};
void show(int poss)
{
for (int i = 0; i <= 4; i++)
{
digitalWrite(i + 3, pos[poss - 1][i]);
}
}
void Display(unsigned char com, unsigned char n) //显示函数,com可选数值范围1-4,num可选数值范围0-9
{
show(com);
digitalWrite(d_a, HIGH); //去除余晖共阴用LOW
digitalWrite(d_b, HIGH);
digitalWrite(d_c, HIGH);
digitalWrite(d_d, HIGH);
digitalWrite(d_e, HIGH);
digitalWrite(d_f, HIGH);
digitalWrite(d_g, HIGH);
digitalWrite(d_h, HIGH);
digitalWrite(d_a, num[n][0]); // a查询码值表
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]);
}
|