/* Tiny Thermocouple Thermometer David Johnson-Davies - www.technoblogy.com - 5th March 2019 ATtiny85 @ 1 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 // Constants int const commands = 0x00; int const onecommand = 0x80; int const data = 0x40; // OLED 128 x 32 monochrome display ********************************************** const int OLEDAddress = 0x3C; // Initialisation sequence for SSD1306 OLED module int const InitLen = 15; const uint8_t Init[InitLen] PROGMEM = { 0xA8, // Set multiplex 0x1F, // for 32 rows 0x8D, // Charge pump 0x14, 0x20, // Memory mode 0x01, // Vertical addressing 0xA1, // Flip horizontally 0xC8, // Flip vertically 0xDA, // Set comp ins 0x02, 0xD9, // Set pre charge 0xF1, 0xDB, // Set vcom detect 0x40, // brighter 0xAF // Display on }; void InitDisplay () { Wire.beginTransmission(OLEDAddress); Wire.write(commands); for (uint8_t i=0; i>8); } } } Wire.endTransmission(); } uint8_t DigitChar (unsigned int number, unsigned int divisor) { return (number/divisor) % 10; } // Display a number from -999°C to 999°C starting at line, column void PlotTemperature (int temp, int line, int column) { boolean dig = false; unsigned int j = 1000; if (temp < 0) { PlotChar(Minus, line, column); temp = - temp; column = column + Scale; } for (int d=j; d>0; d=d/10) { char c = DigitChar(temp, d); if (c == 0 && !dig && d>1) c = Space; else dig = true; PlotChar(c, line, column); column = column + Scale; } PlotChar(Degree, line, column); column = column + Scale; PlotChar(Centigrade, line, column); } // Readings ********************************************** void SetupInternal () { ADMUX = 0<>9; unsigned int difference = Temperature[n+1] - Temperature[n]; unsigned int proportion = adc - (n<<9); unsigned int extra = ((unsigned long)proportion * difference)>>9; return (Temperature[n] + extra + 5)/10; } ISR(ADC_vect) { // ADC conversion complete } // Setup ********************************************** // Calibrations const int ADCOffset = -7; const int InternalOffset = -1; void setup() { Wire.begin(); InitDisplay(); ClearDisplay(); } void loop () { // // Internal SetupInternal(); ReadInternal(); // Throw away first reading int internal = 0; for (int i=0; i<16; i++) internal = internal + ReadInternal(); internal = (internal>>4) - 276 + InternalOffset; Scale = 1; PlotTemperature(internal, 3, 6); // // Thermocouple SetupThermocouple(); ReadThermocouple(); // Throw away first reading int reading = 0; for (int i=0; i<16; i++) reading = reading + ReadThermocouple(); reading = Convert(max((reading>>2) + ADCOffset*4, 0)); Scale = 2; PlotTemperature(reading + internal, 0, 0); delay(1000); }