125 OneWire::OneWire(uint8_t pin)
128 bitmask = PIN_TO_BITMASK(pin);
129 baseReg = PIN_TO_BASEREG(pin);
142 uint8_t OneWire::reset(
void)
144 IO_REG_TYPE mask = bitmask;
145 volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg;
147 uint8_t retries = 125;
150 DIRECT_MODE_INPUT(reg, mask);
154 if (--retries == 0)
return 0;
155 delayMicroseconds(2);
156 }
while ( !DIRECT_READ(reg, mask));
159 DIRECT_WRITE_LOW(reg, mask);
160 DIRECT_MODE_OUTPUT(reg, mask);
162 delayMicroseconds(480);
164 DIRECT_MODE_INPUT(reg, mask);
165 delayMicroseconds(70);
166 r = !DIRECT_READ(reg, mask);
168 delayMicroseconds(410);
176 void OneWire::write_bit(uint8_t v)
178 IO_REG_TYPE mask=bitmask;
179 volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg;
183 DIRECT_WRITE_LOW(reg, mask);
184 DIRECT_MODE_OUTPUT(reg, mask);
185 delayMicroseconds(10);
186 DIRECT_WRITE_HIGH(reg, mask);
188 delayMicroseconds(55);
191 DIRECT_WRITE_LOW(reg, mask);
192 DIRECT_MODE_OUTPUT(reg, mask);
193 delayMicroseconds(65);
194 DIRECT_WRITE_HIGH(reg, mask);
196 delayMicroseconds(5);
204 uint8_t OneWire::read_bit(
void)
206 IO_REG_TYPE mask=bitmask;
207 volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg;
211 DIRECT_MODE_OUTPUT(reg, mask);
212 DIRECT_WRITE_LOW(reg, mask);
213 delayMicroseconds(3);
214 DIRECT_MODE_INPUT(reg, mask);
215 delayMicroseconds(10);
216 r = DIRECT_READ(reg, mask);
218 delayMicroseconds(53);
229 void OneWire::write(uint8_t v, uint8_t power ) {
232 for (bitMask = 0x01; bitMask; bitMask <<= 1) {
233 OneWire::write_bit( (bitMask & v)?1:0);
237 DIRECT_MODE_INPUT(baseReg, bitmask);
238 DIRECT_WRITE_LOW(baseReg, bitmask);
243 void OneWire::write_bytes(
const uint8_t *buf, uint16_t count,
bool power ) {
244 for (uint16_t i = 0 ; i < count ; i++)
248 DIRECT_MODE_INPUT(baseReg, bitmask);
249 DIRECT_WRITE_LOW(baseReg, bitmask);
257 uint8_t OneWire::read() {
261 for (bitMask = 0x01; bitMask; bitMask <<= 1) {
262 if ( OneWire::read_bit()) r |= bitMask;
267 void OneWire::read_bytes(uint8_t *buf, uint16_t count) {
268 for (uint16_t i = 0 ; i < count ; i++)
275 void OneWire::select(
const uint8_t rom[8])
281 for (i = 0; i < 8; i++) write(rom[i]);
292 void OneWire::depower()
295 DIRECT_MODE_INPUT(baseReg, bitmask);
305 void OneWire::reset_search()
309 LastDeviceFlag = FALSE;
310 LastFamilyDiscrepancy = 0;
311 for(
int i = 7; ; i--) {
320 void OneWire::target_search(uint8_t family_code)
323 ROM_NO[0] = family_code;
324 for (uint8_t i = 1; i < 8; i++)
326 LastDiscrepancy = 64;
327 LastFamilyDiscrepancy = 0;
328 LastDeviceFlag = FALSE;
347 uint8_t OneWire::search(uint8_t *newAddr,
bool search_mode )
349 uint8_t id_bit_number;
350 uint8_t last_zero, rom_byte_number, search_result;
351 uint8_t id_bit, cmp_id_bit;
353 unsigned char rom_byte_mask, search_direction;
370 LastDeviceFlag = FALSE;
371 LastFamilyDiscrepancy = 0;
376 if (search_mode ==
true) {
387 cmp_id_bit = read_bit();
390 if ((id_bit == 1) && (cmp_id_bit == 1))
395 if (id_bit != cmp_id_bit)
396 search_direction = id_bit;
401 if (id_bit_number < LastDiscrepancy)
402 search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
405 search_direction = (id_bit_number == LastDiscrepancy);
408 if (search_direction == 0)
410 last_zero = id_bit_number;
414 LastFamilyDiscrepancy = last_zero;
420 if (search_direction == 1)
421 ROM_NO[rom_byte_number] |= rom_byte_mask;
423 ROM_NO[rom_byte_number] &= ~rom_byte_mask;
426 write_bit(search_direction);
434 if (rom_byte_mask == 0)
441 while(rom_byte_number < 8);
444 if (!(id_bit_number < 65))
447 LastDiscrepancy = last_zero;
450 if (LastDiscrepancy == 0)
451 LastDeviceFlag = TRUE;
453 search_result = TRUE;
458 if (!search_result || !ROM_NO[0])
461 LastDeviceFlag = FALSE;
462 LastFamilyDiscrepancy = 0;
463 search_result = FALSE;
465 for (
int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i];
467 return search_result;
477 #if ONEWIRE_CRC8_TABLE 480 static const uint8_t PROGMEM dscrc_table[] = {
481 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
482 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
483 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
484 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
485 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
486 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
487 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
488 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
489 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
490 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
491 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
492 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
493 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
494 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
495 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
496 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
505 uint8_t OneWire::crc8(
const uint8_t *addr, uint8_t len)
510 crc = pgm_read_byte(dscrc_table + (crc ^ *addr++));
519 uint8_t OneWire::crc8(
const uint8_t *addr, uint8_t len)
524 uint8_t inbyte = *addr++;
525 for (uint8_t i = 8; i; i--) {
526 uint8_t mix = (crc ^ inbyte) & 0x01;
528 if (mix) crc ^= 0x8C;
537 bool OneWire::check_crc16(
const uint8_t* input, uint16_t len,
const uint8_t* inverted_crc, uint16_t crc)
539 crc = ~crc16(input, len, crc);
540 return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1];
543 uint16_t OneWire::crc16(
const uint8_t* input, uint16_t len, uint16_t crc)
545 static const uint8_t oddparity[16] =
546 { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
548 for (uint16_t i = 0 ; i < len ; i++) {
551 uint16_t cdata = input[i];
552 cdata = (cdata ^ crc) & 0xff;
555 if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4])