static void ds18x20Select(ds18x20Dev *dev) { uint8_t i; ds18x20SendByte(DS18X20_CMD_MATCH_ROM); for (i = 0; i < 8; i++) ds18x20SendByte(dev->id[i]); return; }
static void ds18x20ConvertTemp(void) { ds18x20SendByte(DS18X20_CMD_SKIP_ROM); ds18x20SendByte(DS18X20_CMD_CONVERT); #ifdef DS18X20_PARASITE_POWER // Set active 1 on port for at least 750ms as parasitic power PORT(ONE_WIRE) |= ONE_WIRE_LINE; DDR(ONE_WIRE) |= ONE_WIRE_LINE; #endif return; }
static void convertTemp(void) { ds18x20SendByte(DS18X20_CMD_SKIP_ROM); ds18x20SendByte(DS18X20_CMD_CONVERT); #ifdef DS18X20_PARASITE_POWER /* Set active 1 on port for 750ms as parasitic power */ DS18X20_PORT |= DS18X20_WIRE; DS18X20_DDR |= DS18X20_WIRE; #endif setTempTimer(TEMP_MEASURE_TIME); return; }
static void ds18x20GetAllTemps() { uint8_t i, j; uint8_t crc; static uint8_t arr[DS18X20_SCRATCH_LEN]; for (i = 0; i < devCount; i++) { if (ds18x20IsOnBus()) { ds18x20Select(&devs[i]); ds18x20SendByte(DS18X20_CMD_READ_SCRATCH); // Control scratchpad checksum crc = 0; for (j = 0; j < DS18X20_SCRATCH_LEN; j++) { arr[j] = ds18x20GetByte(); crc = _crc_ibutton_update(crc, arr[j]); } if (crc == 0) { // Save first 2 bytes (temperature) of scratchpad for (j = 0; j < DS18X20_SCRATCH_TEMP_LEN; j++) devs[i].sp[j] = arr[j]; } } } 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; }
void ds18x20GetAllTemps() { uint8_t i, j; uint8_t arr[9]; for (i = 0; i < devCount; i++) { if (ds18x20IsOnBus()) { ds18x20Select(&devs[i]); ds18x20SendByte(DS18X20_CMD_READ_SCRATCH); for (j = 0; j < 9; j++) arr[j] = ds18x20GetByte(); if (!calcCRC8(arr, sizeof(arr))) { for (j = 0; j < 9; j++) devs[i].sp[j] = arr[j]; } } } return; }