|
| 1 | +#include "GPIO.h" |
| 2 | +#include "TWI.h" |
| 3 | +#include "Driver/DS2482.h" |
| 4 | + |
| 5 | +// Configure: Software or Hardware TWI |
| 6 | +// #define USE_SOFTWARE_TWI |
| 7 | +#if defined(USE_SOFTWARE_TWI) |
| 8 | +#include "Software/TWI.h" |
| 9 | +#if defined(SAM) |
| 10 | +Software::TWI<BOARD::D8,BOARD::D9> twi; |
| 11 | +#else |
| 12 | +Software::TWI<BOARD::D18,BOARD::D19> twi; |
| 13 | +#endif |
| 14 | +#else |
| 15 | +#include "Hardware/TWI.h" |
| 16 | +Hardware::TWI twi; |
| 17 | +#endif |
| 18 | + |
| 19 | +DS2482 owi(twi); |
| 20 | + |
| 21 | +const size_t ROM_MAX = 8; |
| 22 | +const uint8_t CHARBITS = 8; |
| 23 | + |
| 24 | +// 1-Wire ROM and DS18B20 commands |
| 25 | +enum { |
| 26 | + SEARCH_ROM = 0xF0, |
| 27 | + READ_ROM = 0x33, |
| 28 | + MATCH_ROM = 0x55, |
| 29 | + SKIP_ROM = 0xCC, |
| 30 | + ALARM_SEARCH = 0xEC, |
| 31 | + CONVERT_T = 0x44, |
| 32 | + READ_SCRATCHPAD = 0xBE |
| 33 | +}; |
| 34 | + |
| 35 | +// DS18B20 scratchpad |
| 36 | +struct scratchpad_t { |
| 37 | + int16_t temperature; |
| 38 | + int8_t high_trigger; |
| 39 | + int8_t low_trigger; |
| 40 | + uint8_t configuration; |
| 41 | + uint8_t reserved[3]; |
| 42 | + uint8_t crc; |
| 43 | +}; |
| 44 | + |
| 45 | +// 1-Wire CRC calculation |
| 46 | +uint8_t one_wire_crc_update(uint8_t crc, uint8_t data) |
| 47 | +{ |
| 48 | + crc = crc ^ data; |
| 49 | + for (uint8_t i = 0; i < 8; i++) { |
| 50 | + if (crc & 0x01) |
| 51 | + crc = (crc >> 1) ^ 0x8C; |
| 52 | + else |
| 53 | + crc >>= 1; |
| 54 | + } |
| 55 | + return (crc); |
| 56 | +} |
| 57 | + |
| 58 | +// Check assertion to be true, otherwise print line and expression |
| 59 | +#define ASSERT(expr) \ |
| 60 | + do { \ |
| 61 | + if (!(expr)) { \ |
| 62 | + Serial.print(__LINE__); \ |
| 63 | + Serial.println(F(":assert:" #expr)); \ |
| 64 | + Serial.flush(); \ |
| 65 | + exit(0); \ |
| 66 | + } \ |
| 67 | + } while (0) |
| 68 | + |
| 69 | + |
| 70 | +// Print and evaluate expression |
| 71 | +#define TRACE(expr) \ |
| 72 | + do { \ |
| 73 | + Serial.print(#expr "="); \ |
| 74 | + Serial.println(expr); \ |
| 75 | + } while (0) |
| 76 | + |
| 77 | + |
| 78 | +void setup() |
| 79 | +{ |
| 80 | + Serial.begin(57600); |
| 81 | + while (!Serial); |
| 82 | + |
| 83 | + // Reset and configure the TWI 1-Wire Master |
| 84 | + ASSERT(owi.device_reset()); |
| 85 | + ASSERT(owi.write_configuration()); |
| 86 | + |
| 87 | + // Read and print registers |
| 88 | + uint8_t config; |
| 89 | + ASSERT(owi.set_read_pointer(owi.CONFIGURATION_REGISTER, config)); |
| 90 | + TRACE(config); |
| 91 | + |
| 92 | + uint8_t data; |
| 93 | + ASSERT(owi.set_read_pointer(owi.READ_DATA_REGISTER, data)); |
| 94 | + TRACE(data); |
| 95 | + |
| 96 | + uint8_t status; |
| 97 | + ASSERT(owi.set_read_pointer(owi.STATUS_REGISTER, status)); |
| 98 | + TRACE(status); |
| 99 | + |
| 100 | +#if defined(DS2482_800) |
| 101 | + uint8_t channel; |
| 102 | + ASSERT(owi.set_read_pointer(owi.CHANNEL_SELECTION_REGISTER, channel)); |
| 103 | + TRACE(channel); |
| 104 | +#endif |
| 105 | + |
| 106 | + // Read and print rom from device on 1-Wire bus |
| 107 | + uint8_t rom[ROM_MAX] = { 0 }; |
| 108 | + uint8_t crc = 0; |
| 109 | + ASSERT(owi.one_wire_reset()); |
| 110 | + ASSERT(owi.one_wire_write_byte(READ_ROM)); |
| 111 | + Serial.print(F("read_rom=")); |
| 112 | + for (size_t i = 0; i < sizeof(rom); i++) { |
| 113 | + owi.one_wire_read_byte(rom[i]); |
| 114 | + if (rom[i] < 0x10) Serial.print(0); |
| 115 | + Serial.print(rom[i], HEX); |
| 116 | + crc = one_wire_crc_update(crc, rom[i]); |
| 117 | + } |
| 118 | + Serial.println(); |
| 119 | + ASSERT(crc == 0); |
| 120 | + |
| 121 | + // Search device on 1-Wire bus and print rom |
| 122 | + uint8_t bits = 0; |
| 123 | + uint8_t ix = 0; |
| 124 | + uint8_t value = 0; |
| 125 | + uint8_t dir = 0; |
| 126 | + int8_t res = 0; |
| 127 | + bool id; |
| 128 | + bool nid; |
| 129 | + crc = 0; |
| 130 | + ASSERT(owi.one_wire_reset()); |
| 131 | + ASSERT(owi.one_wire_write_byte(SEARCH_ROM)); |
| 132 | + Serial.print(F("search_rom=")); |
| 133 | + do { |
| 134 | + res = owi.one_wire_triplet(dir); |
| 135 | + if (res < 0) break; |
| 136 | + id = (res & 1) != 0; |
| 137 | + nid = (res & 2) != 0; |
| 138 | + value = (value >> 1); |
| 139 | + if (dir) value |= 0x80; |
| 140 | + bits += 1; |
| 141 | + if (bits == CHARBITS) { |
| 142 | + rom[ix] = value; |
| 143 | + if (rom[ix] < 0x10) Serial.print(0); |
| 144 | + Serial.print(rom[ix], HEX); |
| 145 | + crc = one_wire_crc_update(crc, rom[ix]); |
| 146 | + ix += 1; |
| 147 | + bits = 0; |
| 148 | + value = 0; |
| 149 | + } |
| 150 | + } while (id != nid); |
| 151 | + Serial.println(); |
| 152 | + ASSERT(crc == 0); |
| 153 | +} |
| 154 | + |
| 155 | +void loop() |
| 156 | +{ |
| 157 | + // Convert and read temperature |
| 158 | + ASSERT(owi.one_wire_reset()); |
| 159 | + ASSERT(owi.one_wire_write_byte(SKIP_ROM)); |
| 160 | + ASSERT(owi.one_wire_write_byte(CONVERT_T)); |
| 161 | + delay(750); |
| 162 | + |
| 163 | + ASSERT(owi.one_wire_reset()); |
| 164 | + ASSERT(owi.one_wire_write_byte(SKIP_ROM)); |
| 165 | + ASSERT(owi.one_wire_write_byte(READ_SCRATCHPAD)); |
| 166 | + |
| 167 | + // Print scatchpad and calculate check sum |
| 168 | + scratchpad_t scratchpad; |
| 169 | + uint8_t* p = (uint8_t*) &scratchpad; |
| 170 | + uint8_t crc = 0; |
| 171 | + Serial.print(F("read_scratchpad=")); |
| 172 | + for (size_t i = 0; i < sizeof(scratchpad); i++) { |
| 173 | + ASSERT(owi.one_wire_read_byte(p[i])); |
| 174 | + if (i == sizeof(scratchpad) - 1) Serial.print(F(",crc=")); |
| 175 | + if (p[i] < 0x10) Serial.print('0'); |
| 176 | + Serial.print(p[i], HEX); |
| 177 | + crc = one_wire_crc_update(crc, p[i]); |
| 178 | + } |
| 179 | + ASSERT(crc == 0); |
| 180 | + |
| 181 | + // Print temperature (convert from fixed to floating point number) |
| 182 | + Serial.print(','); |
| 183 | + float temperature = scratchpad.temperature * 0.0625; |
| 184 | + TRACE(temperature); |
| 185 | + delay(2000); |
| 186 | +} |
0 commit comments