December 10, 2022, 13:44
arduino #define latchPin 5 #define clockPin 6 #define dataPin 4 #define outputEnable 3 #define disp1 7 #define disp2 8 #define disp3 9 #define disp4 10 char *seg = (char []) {B11111100, B01100000, B11011010, B11110010, B01100110, B10110110, B10111110, B11100000, B11111110, B11110110}; byte leds = 0; int num = 0; void setup() { pinMode(latchPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(outputEnable, OUTPUT); pinMode(disp1, OUTPUT); digitalWrite(disp1, HIGH); pinMode(disp2, OUTPUT); digitalWrite(disp2, HIGH); pinMode(disp3, OUTPUT); digitalWrite(disp3, HIGH); pinMode(disp4, OUTPUT); digitalWrite(disp4, HIGH); cli(); TCCR1A = 0; TCCR1B = 0; TCCR1B |= B00000100; TIMSK1 |= B00000010; OCR1A = 31250; sei(); } ISR(TIMER1_COMPA_vect) { TCNT1 = 0; num++; } void loop() { writeDisplay(num); } void writeDisplay(int x) { for (int i = 0; i < 4; i++) { switch (i) { case 0: leds = seg[x & 0xFF]; updateShiftRegister(); digitalWrite(disp1, LOW); delay(1); digitalWrite(disp1, HIGH); break; case 1: leds = seg[(x >> 8) & 0xFF]; updateShiftRegister(); digitalWrite(disp2, LOW); delay(1); digitalWrite(disp2, HIGH); break; case 2: leds = seg[(x >> 16) & 0xFF]; updateShiftRegister(); digitalWrite(disp3, LOW); delay(1); digitalWrite(disp3, HIGH); break; case 3: leds = seg[(x >> 24) & 0xFF]; updateShiftRegister(); digitalWrite(disp4, LOW); delay(1); digitalWrite(disp4, HIGH); break; } } } void updateShiftRegister() { digitalWrite(outputEnable, HIGH); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, LSBFIRST, leds); digitalWrite(latchPin, HIGH); digitalWrite(outputEnable, LOW); }