#include "project.h" #define DS1820_READ_SCRATCHPAD 0xbe #define DS1820_CONVERT_T 0x44 #define TIMEOUT 150 static unsigned extract_leu16 (uint8_t *d) { uint32_t u; u = (uint32_t) d[0] | (((uint32_t) d[1]) << 8); return u; } static int extract_les16 (uint8_t *d) { uint32_t u; u = extract_leu16 (d); if (u & 0x8000) u |= 0xffff0000; return (int) u; } static int ds1820_read_sp (const Onewire_addr *a, unsigned page, uint8_t *buf, unsigned len) { if (onewire_reset_and_select (a)) return ~0U; onewire_write_byte (DS1820_READ_SCRATCHPAD); onewire_read_bytes (buf, len); if ((len == 9) && onewire_check_crc (buf, 8, buf[8])) return ~0U; return 0; } static int ds1820_convert_t (const Onewire_addr *a) { if (onewire_reset_and_select (a)) return ~0U; onewire_write_byte (DS1820_CONVERT_T); if (onewire_wait_complete (TIMEOUT)) return ~0U; return 0; } int ds1820_read (const Onewire_addr *a, int *temp) { uint8_t buf[9]; int t; if (ds1820_read_sp (a, 0, buf, 9)) return 1; if (ds1820_convert_t (a)) return 1; if (ds1820_read_sp (a, 0, buf, 9)) return 1; t = extract_les16 (&buf[0]); t <<= 4 ; if (temp) *temp = t; return 0; }