unsigned long time_delay=0; byte state_7=0; byte segCount=0; //共陽極 dp a b c d e f g byte seven_seg_digits[10] = {0b00000001, // = 0 0b01001111, // = 1 0b00010010, // = 2 0b00000110, // = 3 0b01001100, // = 4 0b00100100, // = 5 0b00100000, // = 6 0b00001111, // = 7 0b00000000, // = 8 0b00000100 // = 9 }; void setup() { pinMode(2, OUTPUT); //a pinMode(3, OUTPUT); //b pinMode(4, OUTPUT); //c pinMode(5, OUTPUT); //d pinMode(6, OUTPUT); //e pinMode(7, OUTPUT); //f pinMode(8, OUTPUT); //g } void sevenSegWrite(byte digit) { //顯示特定數字的副程式 byte pin = 2; //我們的七段顯示是從pin2開始的 for (byte i = 0; i < 7;i++) { digitalWrite(pin+i, (seven_seg_digits[digit]>>(6-i))&0b00000001); } } void loop() { //數字從9開始倒數,每個數字間隔1秒 if (state_7==0) { sevenSegWrite(segCount); time_delay=millis(); state_7=1; } else { if((millis()-time_delay)>1000) { state_7=0; if(segCount==9) { segCount=0; } else { segCount++; } } } }
unsigned long time_delay=0; byte state_7=0; byte segCount=0; //共陽極 dp a b c d e f g byte seven_seg_digits[10] = {0b00000001, // = 0 0b01001111, // = 1 0b00010010, // = 2 0b00000110, // = 3 0b01001100, // = 4 0b00100100, // = 5 0b00100000, // = 6 0b00001111, // = 7 0b00000000, // = 8 0b00000100 // = 9 }; void setup() { DDRD=0x7f;//設定0~7腳為輸出 } void loop() { //數字從9開始倒數,每個數字間隔1秒 if (state_7==0) { PORTD=seven_seg_digits[segCount]; time_delay=millis(); state_7=1; } else { if((millis()-time_delay)>1000) { state_7=0; if(segCount==9) { segCount=0; } else { segCount++; } } } }