#include <util/delay.h>
#include "onewire.h"
#include "ds18x20.h"
#define MAX_ROMS 10
typedef uint8_t rom_t[MAX_ROMS][8];
uint8_t buffer[9] = {0x10, 0x1D, 0x9E, 0x09, 0x02, 0x08, 0x00, 0x55, 0x00};
rom_t roms;
rom_t alarm_roms;
void print_header(void);
void print_rom(uint8_t *data, uint8_t index);
int scan_bus(rom_t rom_list, uint8_t cmd);
int scan_bus(rom_t rom_list, 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 -2;
}
print_header();
rc=ONEWIRE_OK;
for (i = 0; (i < MAX_ROMS) && (rc == ONEWIRE_OK); i++) {
if (rc == ONEWIRE_OK || rc == ONEWIRE_LAST_CODE ) {
print_rom(buffer, i);
memcpy(rom_list[i], buffer, 8);
} else {
switch (rc) {
case ONEWIRE_NO_PRESENCE: Serial.println(F("No response on bus!")); break;
case ONEWIRE_GND_SHORT: Serial.println(F("Bus shorted to GND!")); break;
case ONEWIRE_CRC_ERROR:
Serial.println(F("CRC error!"));
print_rom(buffer, i);
break;
case ONEWIRE_SCAN_ERROR:
if (cmd == ONEWIRE_SEARCH_ROM) {
Serial.println(F("Scan error!"));
} else {
return 0;
}
break;
}
}
}
if (!(rc == ONEWIRE_OK || rc == ONEWIRE_LAST_CODE)) {
return -1;
} 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("Device");
}
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(" "));
}
switch (data[0]) {
case DS18B20_ID : Serial.print(F(" DS18B20")); break;
case DS18S20_ID : Serial.print(F(" DS18S20")); break;
case 0x01 : Serial.print(F(" DS2401")); break;
default : Serial.print(F(" unknown")); break;
}
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 int 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!"));
}
if (rc) {
Serial.println(F("\r\nCRC error in buffer data! (Serial CRC)"));
}
Serial.println(F("\r\nReading ROM code of single device on bus."));
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"));
}
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 resolution 0.0625 C)"));
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 == -1) {
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 = MAX_ROMS;
i_ds18S20 = MAX_ROMS;
for (i = 0; i < MAX_ROMS; i++) {
if (i_ds18B20 == MAX_ROMS && roms[i][0] == DS18B20_ID) {
i_ds18B20 = i;
}
if (i_ds18B20 == MAX_ROMS && roms[i][0] == DS18S20_ID) {
i_ds18S20 = i;
}
}
if (i_ds18S20 == MAX_ROMS) {
Serial.println(F("\r\nNo DS18S20 found on bus!"));
} else {
Serial.println(F("\r\nReading temperature of DS18S20 (9 Bit + extended resolution, effective resolution 0.0625 C )"));
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: "));
if (x<0) Serial.print('-');
x=abs(x);
Serial.print(x / 10);
Serial.print('.');
Serial.print(x % 10);
Serial.println(F(" C"));
}
}
while (Serial.available()) Serial.read();
Serial.println(F("\r\nWriting tl(15) & th(28) to DS18S20."));
ds18S20_read_scratchpad(buffer);
Serial.print(F("\r\nCRC error in scratchpad data!"));
}
print_scratchpad(buffer);
ds18S20_copy_scratchpad(0);
ds18S20_recall_E2();
ds18S20_read_scratchpad(buffer);
Serial.print(F("\r\nCRC error in scratchpad data!"));
}
print_scratchpad(buffer);
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 == MAX_ROMS) {
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!")); break;
} else {
Serial.print(F("T: "));
if (x<0) Serial.print('-');
x=abs(x);
Serial.print(x / 10);
Serial.print('.');
Serial.print(x % 10);
Serial.println(F(" C"));
}
for (i=0; i<1; i++) {
_delay_ms(1000);
}
}
while (Serial.available()) Serial.read();
Serial.println(F("\r\nWriting tl(10) & th(28) and adc resolution (12 bits) 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();
ds18B20_read_scratchpad(buffer);
Serial.print(F("\r\nCRC error in scratchpad data!"));
}
print_scratchpad(buffer);
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() && i_ds18S20 != MAX_ROMS && i_ds18B20 != MAX_ROMS) {
if (i_ds18S20 != MAX_ROMS) {
ds18S20_convert_t(0);
_delay_ms(1000);
}
if (i_ds18B20 != MAX_ROMS) {
ds18B20_convert_t(1);
_delay_ms(1000);
ONEWIRE_STRONG_PU_OFF
}
rc = scan_bus(alarm_roms, ONEWIRE_ALARM_SEARCH);
if (rc <= 0) {
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() {
}