#include <util/delay.h>
#include "onewire.c"
#include "ds18x20.c"
uint8_t buffer[9] ={0x02, 0x1C, 0xB8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t roms[10][8];
void print_header(void) {
int8_t i, j;
Serial.print(F(" Index: "));
for (i=7; i>=0; i--) {
Serial.print(F(" "));
Serial.print(i);
Serial.print(F(" "));
}
Serial.println("");
}
void print_rom(uint8_t *data, uint8_t index) {
int8_t i, j;
Serial.print(F(" ROM#"));
if (index<10) {
Serial.print('0');
}
Serial.print(index);
Serial.print(' ');
for (i=7; i>=0; i--) {
Serial.print(F(" 0x"));
if (data[i]<16) {
Serial.print(F("0"));
}
Serial.print(data[i], HEX);
Serial.print(F(" "));
}
Serial.println("");
}
void print_scratchpad(uint8_t *data) {
uint8_t i;
Serial.println(F("\r\nPrinting scratchpad data"));
Serial.println(F("Index Value"));
for (i=0; i<9; i++) {
Serial.print(F(" "));
Serial.print(i);
Serial.print(F(" 0x"));
Serial.println(data[i], HEX);
}
}
void setup() {
int16_t x;
uint8_t i, j, crc;
volatile uint8_t rc;
uint8_t i_ds18B20, i_ds18S20;
Serial.begin(9600);
Serial.println(F("\r\nOneWire (tm) demo"));
rc;
Serial.println(F("\r\nReading ROM code of single device on bus."));
for (i=0, j=0; i<8; i++) j |= buffer[i];
crc=buffer[7];
buffer[7]=0;
if (rc==1) {
Serial.println(F("No response on bus!"));
while(1);
} else if (rc==2) {
Serial.println(F("CRC error! "));
Serial.print(F("Received CRC : 0x"));
Serial.println(crc, HEX);
Serial.print(F("Calculated CRC: 0x"));
Serial.println(F("There is probably more than one device on the bus.\r\n"));
} else if (j==0) {
Serial.println(F("All rom data zero."));
Serial.println(F("There is probably more than one device on the bus.\r\n"));
}
buffer[7]=crc;
print_header();
print_rom(buffer, 0);
if (buffer[0]==DS18S20_ID) i_ds18S20 = 1; else i_ds18S20 = 0;
if (buffer[0]==DS18B20_ID) i_ds18B20 = 1; else i_ds18B20 = 0;
if (i_ds18S20 || i_ds18B20) {
Serial.println(F("CRC error in scratchpad data!"));
}
print_scratchpad(buffer);
}
if (!i_ds18S20) {
Serial.println(F("\r\nNo DS18S20 found on bus."));
} else {
Serial.println(F("\r\nReading temperature of DS18S20 (9 Bit, 0.5 C resolution)"));
Serial.println(F("DS18S20 must be normal powered!"));
Serial.println(F("Press any key to stop"));
while(!Serial.available()) {
ds18S20_convert_t(0);
_delay_ms(750);
Serial.print(F("T: "));
Serial.print(x/10);
Serial.print('.');
Serial.print(abs(x)%10);
Serial.println(F(" C"));
}
}
while(Serial.available()) Serial.read();
if (!i_ds18B20) {
Serial.println(F("\r\nNo DS18B20 found on bus."));
} else {
Serial.println(F("\r\nReading temperature of DS18B20 (12 Bit, 0.0625 C resolution)"));
Serial.println(F("DS18B20 must be parasitc powered!"));
Serial.println(F("Press any key to stop"));
while(!Serial.available()) {
_delay_ms(750);
ONEWIRE_STRONG_PU_OFF
Serial.print(F("T: "));
Serial.print(x/10);
Serial.print('.');
Serial.print(abs(x)%10);
Serial.println(F(" C"));
}
}
while(Serial.available()) Serial.read();
Serial.println(F("\r\nSingle device demo finished."));
Serial.println(F("\r\nScaning OneWire bus for ROM codes.\r\n"));
i_ds18B20=10;
i_ds18S20=10;
print_header();
for(i=0, rc=0; i<10 && rc==0; i++) {
print_rom(buffer, i);
if(rc==0 || rc==4) {
memcpy(roms[i], buffer, 8);
if (i_ds18B20==10 && buffer[0]==0x28) {
i_ds18B20=i;
}
if (i_ds18B20==10 && buffer[0]==0x10) {
i_ds18S20=i;
}
} else {
if(rc==1) {
Serial.println(F("No response on bus!"));
} else if (rc==3) {
Serial.println(F("Scan error!"));
}
break;
}
}
if (!(rc==0 || rc==4)) {
Serial.println(F("\r\nError, no device found!"));
while(1);
} else {
Serial.print(F("\r\nScan finished, "));
Serial.print(i);
Serial.println(F(" devices found."));
}
if (i_ds18S20 == 10) {
Serial.println(F("\r\nNo DS18S20 found on bus!"));
} else {
Serial.println(F("\r\nReading temperature of DS18S20 (9 Bit, 0.5 C resolution)"));
Serial.println(F("DS18S20 must be normal powered"));
Serial.println(F("Press any key to stop"));
while(!Serial.available()) {
ds18S20_convert_t(0);
_delay_ms(750);
Serial.print("T: ");
Serial.print(x/10);
Serial.print(".");
Serial.print(abs(x)%10);
Serial.println(" C");
}
while(Serial.available()) Serial.read();
ds18S20_read_scratchpad(buffer);
Serial.print(F("\r\nCRC error in scratchpad data!"));
}
print_scratchpad(buffer);
ds18S20_copy_scratchpad(0);
ds18S20_recall_E2();
Serial.print(F("DS18S20 runs on "));
if (ds18S20_read_power_supply()) {
Serial.println(F("parasitic power."));
} else {
Serial.println(F("normal power."));
}
}
if (i_ds18B20 == 10) {
Serial.println(F("\r\nNo DS18B20 found on bus!"));
} else {
Serial.println(F("\r\nReading temperature of DS18B20 (12 Bit, 0.0625 C resolution)"));
Serial.println(F("DS18S20 must be parasitic powered"));
Serial.println(F("Press any key to stop"));
while(!Serial.available()) {
ds18B20_convert_t(1);
_delay_ms(750);
ONEWIRE_STRONG_PU_OFF
Serial.print(F("T: "));
Serial.print(x/10);
Serial.print('.');
Serial.print(abs(x)%10);
Serial.println(F(" C"));
}
while(Serial.available()) Serial.read();
ds18B20_read_scratchpad(buffer);
Serial.print(F("\r\nCRC error in scratchpad data!"));
}
print_scratchpad(buffer);
ds18B20_copy_scratchpad(1);
ds18B20_recall_E2();
Serial.print(F("\r\nDS18B20 runs on "));
if (ds18B20_read_power_supply()) {
Serial.println(F("parasitic power."));
} else {
Serial.println(F("normal power."));
}
}
Serial.println(F("\r\nMultiple device demo finished."));
Serial.println(F("Press Reset to restat."));
}
void loop() {
}