static void ds18x20SendByte(uint8_t byte) { uint8_t i; for (i = 0; i < 8; i++) ds18x20SendBit(byte & (1<<i)); return; }
static void ds18x20SendByte(uint8_t byte) { uint8_t i; for (i = 0; i < 8; i++) { ds18x20SendBit(byte & 0x01); byte >>= 1; } return; }
static uint8_t ds18x20SearchRom(uint8_t *bitPattern, uint8_t lastDeviation) { uint8_t currBit; uint8_t newDeviation = 0; uint8_t bitMask = 0x01; uint8_t bitA; uint8_t bitB; // Send SEARCH ROM command on the bus ds18x20SendByte(DS18X20_CMD_SEARCH_ROM); // Walk through all 64 bits for (currBit = 0; currBit < DS18X20_ID_LEN * 8; currBit++) { // Read bit from bus twice. bitA = ds18x20GetBit(); bitB = ds18x20GetBit(); if (bitA && bitB) { // Both bits 1 = ERROR return 0xFF; } else if (!(bitA || bitB)) { // Both bits 0 if (currBit == lastDeviation) { // Select 1 if device has been selected *bitPattern |= bitMask; } else if (currBit > lastDeviation) { // Select 0 if no, and remember device (*bitPattern) &= ~bitMask; newDeviation = currBit; } else if (!(*bitPattern & bitMask)) { // Otherwise just remember device newDeviation = currBit; } } else { // Bits differ if (bitA) *bitPattern |= bitMask; else *bitPattern &= ~bitMask; } // Send the selected bit to the bus. ds18x20SendBit(*bitPattern & bitMask); // Adjust bitMask and bitPattern pointer. bitMask <<= 1; if (!bitMask) { bitMask = 0x01; bitPattern++; } } return newDeviation; }