#include <OneWire.h>
#include "nano.h"
#include "LCD.h"
// include the library code:
OneWire ds(7); // on pin 7 (a 4.7K resistor is necessary)
float celsius;
void setup(void) {
// Serial.begin(9600);
nano_init();
init_LCD(); // initialize LCD
LCD_cmd(0x0C); // display on, cursor off
delay(100);
DS18B20_write_key();
lcd_clear();
lcd_set_cursor(0, 0);
LCD_writeString("Code: ");
lcd_set_cursor(1, 0);
LCD_writeString("TEMP: ");
}
void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[9];
byte addr[8];
ds.reset();
ds.write(0xCC, 1);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(750); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
// ds.select(addr);
ds.write(0xCC);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
lcd_set_cursor(0, 6);
lcd_print_hex(data[2]);
lcd_set_cursor(0, 9);
lcd_print_hex(data[3]);
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
raw = raw << 3; // 9 bit resolution default
raw = (raw & 0xFFF0) + 12 - data[6];
celsius = (float)raw / 16.0;
lcd_set_cursor(1, 6);
lcd_print_float(celsius);
// Serial.print(" Temperature = ");
// Serial.print(celsius);
// Serial.println(" C");
}
void DS18B20_write_key(void)
{
ds.reset();
ds.write(0xCC, 1);
ds.write(0x4E, 1); //write_scratchpad
ds.write(0x24, 1); // Byte1 User data //
ds.write(0x10, 1); // Byte2 User data //
ds.write(0x7F, 1); // config reg
ds.reset();
ds.write(0xCC);
ds.write(0x48); //copy_scratchpad
delayMicroseconds(2);
ds.reset();
}