簡易四位一體數碼管顯示數字(部分程式碼查詢獲得)
共陰極數碼管是指八段數碼管的八段發光二極體的陰極(負極)都連在一起,而陽極對應的各段分別控制
共陰級輸入高電平有效,共陽級輸入低電平有效。
HS42056 1K-32是共陰極四位一體數碼管,有十二個引腳,a,b,c,d,e,f,g,dp為段引腳,1,2,3,4為四個數碼管的位。
#define d_a 2 //定義數碼管a 接引腳 2
#define d_b 3
#define d_c 4
#define d_d 5
#define d_e 6
#define d_f 7
#define d_g 8
#define d_h 9
#define COM1 10 //定義 第一組數碼管 com1 接引腳2
#define COM2 11
#define COM3 12
#define COM4 13
unsigned char num[17][8] =
{
//a b c d e f g h
{1, 1, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 0, 1, 0},
{1, 1, 1, 1, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{1, 0, 1, 1, 0, 1, 1, 0},
{1, 0, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 1, 0, 1, 1, 0},
{1, 1, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 1, 1, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 1},
};
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()
{
Display(1,1);//第一位數碼管顯示l的值
Display(2,3);
Display(3,1);
Display(4,4);
}
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); //選擇位1
digitalWrite(COM2,HIGH);
digitalWrite(COM3,HIGH);
digitalWrite(COM4,HIGH);
break;
case 2:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,LOW); //選擇位2
digitalWrite(COM3,HIGH);
digitalWrite(COM4,HIGH);
break;
case 3:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,HIGH);
digitalWrite(COM3,LOW); //選擇位3
digitalWrite(COM4,HIGH);
break;
case 4:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,HIGH);
digitalWrite(COM3,HIGH);
digitalWrite(COM4,LOW); //選擇位4
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]);
}