/* Eight-Digit Display Demo David Johnson-Davies - www.technoblogy.com - 21st September 2015 ATtiny2313 @ 1MHz (internal oscillator; BOD disabled) CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ // Seven-segment definitions const int charArrayLen = 12; char charArray[charArrayLen] = { // ABCDEFG Segments 0b1111110, // 0 0b0110000, // 1 0b1101101, // 2 0b1111001, // 3 0b0110011, // 4 0b1011011, // 5 0b1011111, // 6 0b1110000, // 7 0b1111111, // 8 0b1111011, // 9 0b0000000, // 10 Space 0b0000001 // 11 Dash }; const int Dash = 11; const int Space = 10; const int Ndigits = 8; char Buffer[Ndigits] = {Dash, Dash, Dash, Dash, Dash, Dash, Dash, Dash}; char dp = -1; // Decimal point position 0 to 7 char digit = 0; char Digits[] = {PIN_D4, PIN_D5, PIN_D6, PIN_A0, PIN_B0, PIN_B1, PIN_B2, PIN_B3}; // Display multiplexer ********************************************** void DisplayNextDigit() { pinMode(Digits[digit], INPUT); digit = (digit+1) % 8; // Make all segment pins high-impedance inputs DDRD = 0; // PD6 to PD0 DDRB &= 0xF0; // PB3 to PB0 DDRA &= 0xFE; // PA0 int segs = charArray[Buffer[digit]]; // Take lit segments low and make them outputs. // Segments D to G go to PD3 to PD0 // Top three segments A to C go to PB2 to PB0 or PD6 to PD4 if (digit < 4) { PORTB = (PORTB & 0xF8) | ~((segs & 0x70)>>4); DDRB = (DDRB & 0xF8) | ((segs & 0x70)>>4); PORTD = ~(segs & 0x0F); DDRD = (segs & 0x0F); } else { PORTD = ~(segs & 0x7F); DDRD = (segs & 0x7F); } // Decimal point goes to PB3 or PA0 if (dp == digit) { if (digit < 4) { digitalWrite(PIN_B3,LOW); pinMode(PIN_B3,OUTPUT); } else { digitalWrite(PIN_A0,LOW); pinMode(PIN_A0,OUTPUT); } } // Take current digit pin high digitalWrite(Digits[digit],HIGH); pinMode(Digits[digit],OUTPUT); } // Display an eight-digit decimal number void Display (long number) { boolean dig = false; long j=10000000; for (int d=0; d<8 ; d++) { int i = (number/j) % 10; if (!i && !dig && d<7) Buffer[d]=Space; else { Buffer[d]=i; dig = true; } j=j/10; } } // Timer interrupt - multiplexes display ISR (TIMER1_COMPA_vect) { DisplayNextDigit(); } void setup() { // Set up Timer1 to multiplex the display TCCR1A = 0<