#include #include #include #include void ds18s20_convert(const struct owaddr *addr) { int power, status; ow_matchrom(addr); ow_writecommand(DS18S20_POWER); power = ow_readbit(); ow_matchrom(addr); if (power == 0) { // Parasite power ow_spu(1); ow_writecommand(DS18S20_CONVERT); usleep(800000); ow_spu(0); } else { // External power ow_writecommand(DS18S20_CONVERT); do { usleep(100000); status = ow_readbit(); } while (status != 1); } } /* Public functions */ void ds18s20_readsp(const struct owaddr *addr, unsigned char *buf) { // Read 9-byte stratchpad ow_matchrom(addr); ow_writecommand(DS18S20_READSP); ow_readbytes(buf, 9); } float ds18s20_temperature(const struct owaddr *addr) { unsigned char buf[9]; ds18s20_convert(addr); ds18s20_readsp(addr, buf); if (ow_crc(buf, 9) != 0) { // Try again once ds18s20_readsp(addr, buf); if (ow_crc(buf, 9) != 0) { fprintf(stderr, "ds18s20 stratchpad CRC failed\n"); return 85.; } } int temp_read, count_remain, count_per_c; float result; temp_read = buf[0] >> 1; if (buf[1]) temp_read = - temp_read; count_remain = buf[6]; count_per_c = buf[7]; result = temp_read - 0.25; result += ((float) count_per_c - count_remain) / count_per_c; return result; }