#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];
uint8_t alarm_roms[10][8];
void print_header(void);
void print_rom(uint8_t *data, uint8_t index);
uint8_t scan_bus(uint8_t rom_list[10][8], uint8_t cmd);
uint8_t scan_bus(uint8_t rom_list[10][8], uint8_t cmd) {
uint8_t i, j, rc;
if (cmd == ONEWIRE_SEARCH_ROM) {
Serial.println(F("\r\nScaning OneWire bus for ROM codes.\r\n"));
} else if (cmd == ONEWIRE_ALARM_SEARCH) {
Serial.println(F("\r\nScaning OneWire bus for devices with active alarm flag.\r\n"));
} else {
return 255;
}
print_header();
for (i = 0, rc = 0; i < 10 && rc == 0; i++) {
if (rc == ONEWIRE_OK || rc == ONEWIRE_LAST_CODE ) {
print_rom(buffer, i);
memcpy(rom_list[i], buffer, 8);
} else {
if (rc == ONEWIRE_NO_PRESENCE) {
Serial.println(F("No response on bus!"));
} else if (rc == ONEWIRE_SCAN_ERROR) {
Serial.println(F("Scan error!"));
}
break;
}
}
if (!(rc == ONEWIRE_OK || rc == ONEWIRE_LAST_CODE)) {
return 255;
} else {
return i;
}
}
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;
if (rc) {
Serial.println(F("\r\nCRC error in buffer data!"));
}
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 == ONEWIRE_NO_PRESENCE) {
Serial.println(F("No response on bus!"));
while (1);
} else if (rc == ONEWIRE_CRC_ERROR) {
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 ((rc == 0) && (j >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 + extended resolution, effective 0.0625 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);
if (rc) {
Serial.println(F("CRC error!"));
} else {
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
if (rc) {
Serial.println(F("CRC error!"));
} else {
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."));
rc = scan_bus(roms, ONEWIRE_SEARCH_ROM);
if (rc == 255) {
Serial.println(F("\r\nError, no device found!"));
while (1);
} else {
Serial.print(F("\r\nScan finished, "));
Serial.print(rc);
Serial.println(F(" devices found."));
}
i_ds18B20 = 10;
i_ds18S20 = 10;
for (i = 0; i < 10; i++) {
if (i_ds18B20 == 10 && roms[i][0] == 0x28) {
i_ds18B20 = i;
}
if (i_ds18B20 == 10 && roms[i][0] == 0x10) {
i_ds18S20 = i;
}
}
if (i_ds18S20 == 10) {
Serial.println(F("\r\nNo DS18S20 found on bus!"));
} else {
Serial.println(F("\r\nReading temperature of DS18S20 (9 Bit + extended resolution, effective 0.0625 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);
if (rc) {
Serial.println(F("CRC error!"));
} else {
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\nWriting th(25) & tl(15) to DS18S20."));
Serial.print(F("\r\nNo response on bus!"));
}
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("DS18B20 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
if (rc) {
Serial.println(F("CRC error!"));
} else {
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\nWriting th(25) & tl(10) and configuration byte (0xFF) to DS18B20."));
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("Scaning for alarm devices."));
Serial.println(F("Press any key to stop"));
while (!Serial.available()) {
if (i_ds18S20 != 10) {
ds18S20_convert_t(0);
_delay_ms(1000);
}
if (i_ds18B20 != 10) {
ds18B20_convert_t(1);
_delay_ms(1000);
ONEWIRE_STRONG_PU_OFF
}
rc = scan_bus(alarm_roms, ONEWIRE_ALARM_SEARCH);
if (rc == 255) {
Serial.println(F("\r\nNo alarm device found!"));
} else {
Serial.print(F("\r\nScan finished, "));
Serial.print(rc);
Serial.println(F(" devices found."));
}
}
while (Serial.available()) Serial.read();
Serial.println(F("\r\nMultiple device demo finished."));
Serial.println(F("Press Reset to restart."));
}
void loop() {
}