/* 1-Wire Interface v2 David Johnson-Davies - www.technoblogy.com - 19th August 2018 ATtiny85 @ 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/ */ // One Wire Protocol ********************************************** // Buffer to read data or ROM code static union { uint8_t DataBytes[9]; unsigned int DataWords[4]; }; const int OneWirePin = 3; 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 (int micro) { TCNT1 = 0; TIFR = 1<>1) - 1; while ((TIFR & 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; } // Serial Seven-Segment Display ********************************************** const int Select = 0; const int DataOut = 1; const int ClockPin = 2; const int Clear_Display = 0x76; const int Cursor_Control = 0x79; const int Decimal_Control = 0x77; void SendByte(char data) { digitalWrite(Select, LOW); shiftOut(DataOut, ClockPin, MSBFIRST, data); digitalWrite(Select, HIGH); } // Clear display void ClearDisplay () { SendByte(Clear_Display); } // Display Error void DisplayError (char *str) { SendByte(Clear_Display); SendByte(Cursor_Control); SendByte(0); while (*str) SendByte(*str++); } // Display a four digit hexadecimal number void Display (int n) { SendByte(Cursor_Control); SendByte(0); for (int i=12; i>=0; i=i-4) { SendByte((n>>i) & 0x0F); } } // Display temperature void DisplayTemp (int t) { SendByte(Cursor_Control); SendByte(0); int temp = t>>4; int points = 0x20; // Display degree symbol if (temp>=100) SendByte(temp/100); // Hundreds if (temp<0) SendByte('-'); SendByte((abs(temp)/10) % 10); // Tens SendByte(abs(temp) % 10); // Units if (temp>=0 && temp<100) { points = 0x22; // Display decimal point SendByte(((t & 0x0F)*10)>>4); // Tenths } SendByte(Decimal_Control); SendByte(points); SendByte('C'); } // Setup ********************************************** void setup() { pinMode(ClockPin, OUTPUT); pinMode(DataOut, OUTPUT); pinMode(Select, OUTPUT); digitalWrite(Select, HIGH); digitalWrite(ClockPin, LOW); digitalWrite(DataOut, LOW); OneWireSetup(); } // Read temperature of a single DS18B20 or MAX31820 on the bus void Temperature () { cli(); // No interrupts if (OneWireReset() != 0) { sei(); DisplayError("Err"); } else { OneWireWrite(SkipROM); OneWireWrite(ConvertT); while (OneWireRead() != 0xFF); OneWireReset(); OneWireWrite(SkipROM); OneWireWrite(ReadScratchpad); OneWireReadBytes(9); sei(); // Interrupts if (OneWireCRC(9) == 0) { DisplayTemp(DataWords[0]); } else DisplayError("Crc"); } delay(1000); } // Display serial number of a single device on the 1-wire bus void SerialNumber () { ClearDisplay(); cli(); if (OneWireReset() != 0) { sei(); DisplayError("Err"); } else { OneWireWrite(ReadROM); OneWireReadBytes(8); sei(); if (OneWireCRC(8) == 0) { for (int i=3; i>=0; i--) { Display(DataWords[i]); delay(1000); } } else DisplayError("Crc"); } delay(1000); } void loop () { Temperature(); SerialNumber(); }