/* Two-Digit Thermometer David Johnson-Davies - www.technoblogy.com - 9th April 2019 ATtiny84 @ 8 MHz (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/ */ #include #include // Comment out for a common anode display #define commoncathode // Seven-segment definitions uint8_t charArray[] = { // 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 '-' 0b0110001, // 12 '-1' 0b1001111, // 13 'E' 0b0001110, // 14 'L' 0b0011101, // 15 'o' 0b0110111, // 16 'H' 0b0010000, // 17 'i' 0x80, // 18 '.' }; const int Blank = 10; const int Minus = 11; const int Minus1 = 12; const int Error = 13; const int Lo = 14; const int Hi = 16; const int DP = 18; volatile char Buffer[] = {2, 2}; // One Wire Protocol ********************************************** // Buffer to read data or ROM code static union { uint8_t DataBytes[9]; unsigned int DataWords[4]; }; const int Digit0Pin = 0; // PB0 const int Digit1Pin = 1; // PB1 const int OneWirePin = 2; // PB2 const int ReadROM = 0x33; const int MatchROM = 0x55; const int SkipROM = 0xCC; const int ConvertT = 0x44; const int ReadScratchpad = 0xBE; inline void PinLow () { DDRB = DDRB | 1<>OneWirePin & 1; } void DelayMicros (unsigned int micro) { TCNT1 = 0; TIFR1 = 1<> 1; } } uint8_t OneWireRead () { uint8_t data = 0; for (int i = 0; i<8; i++) { LowRelease(6, 9); data = data | PinRead()<>1 ^ ((crc & 1) ? 0x8c : 0); } return crc; } // Display multiplexer ********************************************** uint8_t digit = 0; void DisplayNextDigit () { #if defined(commoncathode) PORTB = PORTB | 1< 9) {temp0 = Hi; temp1 = Hi+1; } else if (tens == -1) temp0 = Minus1; else if ((tens == 0) && (units >= 0)) temp0 = Blank; else if ((tens == 0) && (units < 0)) temp0 = Minus; Buffer[0] = temp0; Buffer[1] = temp1; } // Display Error void DisplayError (int no) { Buffer[0] = Error; Buffer[1] = no; } volatile uint8_t Ticks = 0; // Flash the display on for n ticks void DisplayOn (uint8_t n) { Ticks = n; TIMSK0 = 1< 0); TIMSK0 = 0; // Interrupts off #if defined(commoncathode) PORTB = PORTB | 1<>4); // Round to nearest degree } else DisplayError(1); // CRC error } } // Watchdog timer ********************************************** // Use Watchdog for time delay; n=0 is 16ms; n=6 is 1sec ; n=9 is 8secs, void WDDelay(int n) { WDTCSR = 1<