/*
* 例 4-2:0~9999 上數計數器
*/
const byte _7SegCode[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d,
0x07, 0x7f, 0x6f}; // 數字 0~9 字型碼
// 字節對應 GPIO 腳位,依序為 a b c d e f g dp
const byte _7SegPin[8] = {12,14,33,34,26,21,20,36};
const byte _7ComPin[4] = {19,18,5,17};
// com1->19,com2->18,com3->5,com4->17
const byte com[4][4]={{1,1,1,0}, // com4 正常工作,其餘 off
{1,1,0,1}, // com3 正常工作,其餘 off
{1,0,1,1}, // com2 正常工作,其餘 off
{0,1,1,1}}; // com1 正常工作,其餘 off
int thous, hund, tens, units;
void setup() {
for(int i=0;i<8;i++) {
pinMode(_7SegPin[i], OUTPUT); // 設定 ESP32 GPIO 為輸出
}
for(int x=0;x<4;x++) {
pinMode(_7ComPin[x], OUTPUT); // 設定 ESP32 GPIO 為輸出
}
}
void loop() {
for(int cnt=0;cnt<10000;cnt++) {
thous = cnt/1000; // 取出千位數
hund = cnt%1000/100; // 取出百位數
tens = cnt%100/10; // 取出十位數
units = cnt%10; // 取出個位數
byte Value[4] = {units, tens, hund, thous};
// 顯示順序為個位->十位->百位->千位
for(int scan=0;scan<4;scan++) { // com1~4 掃描碼
for(int j=0;j<4;j++) {
digitalWrite(_7ComPin[j], com[scan][j]); // 分別寫入 com1~4
}
for(int seg=0;seg<8;seg++) { // 七段顯示器顯示數字
digitalWrite(_7SegPin[seg], (_7SegCode[Value[scan]] >> seg) &
0x01);
}
delay(2);
}
}
}
esp:0
esp:1
esp:2
esp:3
esp:4
esp:5
esp:6
esp:7
esp:8
esp:9
esp:10
esp:11
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:20
esp:21
esp:26
esp:33
esp:34
esp:35
esp:36
esp:37
esp:38
esp:39
esp:40
esp:41
esp:42
esp:45
esp:46
esp:3V3
esp:5V
esp:GND.1
esp:TX
esp:RX
esp:RST
esp:GND.2
sevseg1:A
sevseg1:B
sevseg1:C
sevseg1:D
sevseg1:E
sevseg1:F
sevseg1:G
sevseg1:DP
sevseg1:DIG1
sevseg1:DIG2
sevseg1:DIG3
sevseg1:DIG4
sevseg1:COM
sevseg1:CLN
not1:IN
not1:OUT
not2:IN
not2:OUT
not3:IN
not3:OUT
not4:IN
not4:OUT