|
| 1 | +#include "DS18B20.h" |
| 2 | +#include "mbed.h" |
| 3 | + |
| 4 | +void debug_search_for_ds18b20_address(OneWire& bus) { |
| 5 | + uint8_t addr[8]; |
| 6 | + |
| 7 | + if (!bus.search(addr)) { |
| 8 | + printf("No more addresses.\r\n\r\n"); |
| 9 | + bus.reset_search(); |
| 10 | + ThisThread::sleep_for(250ms); |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + printf("ROM = "); |
| 15 | + for (uint8_t byte : addr) { |
| 16 | + printf(" %x", byte); |
| 17 | + } |
| 18 | + |
| 19 | + if (OneWire::crc8(addr, 7) != addr[7]) { |
| 20 | + printf("CRC is not valid!\r\n\r\n"); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + // the first ROM byte indicates which chip |
| 25 | + switch (addr[0]) { |
| 26 | + case 0x10: |
| 27 | + printf(" Chip = DS18S20 (type_s) = 1\r\n"); // or old DS1820 |
| 28 | + break; |
| 29 | + case 0x28: |
| 30 | + printf(" Chip = DS18B20 (type_s) = 0\r\n"); |
| 31 | + break; |
| 32 | + case 0x22: |
| 33 | + printf(" Chip = DS1822 (type_s) = 0\r\n"); |
| 34 | + break; |
| 35 | + default: |
| 36 | + printf("Device is not a DS18x20 family device.\r\n"); |
| 37 | + return; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +DS18B20::DS18B20(OneWire& onewire_bus, uint64_t device_address) |
| 42 | + : bus(onewire_bus), address(device_address) {} |
| 43 | + |
| 44 | +void DS18B20::start_conversion(bool parasite_power_mode) { |
| 45 | + bus.reset(); |
| 46 | + bus.select((uint8_t*)&address); |
| 47 | + bus.write(0x44, parasite_power_mode ? 1 : 0); // start conversion, with parasite power on at the end |
| 48 | +} |
| 49 | + |
| 50 | +uint8_t DS18B20::retrieve_conversion(bool type_s_sensor) { |
| 51 | + // we might do a ds18b20.depower() here, but the reset will take care of it. |
| 52 | + |
| 53 | + /* uint8_t present = */ bus.reset(); |
| 54 | + bus.select((uint8_t*)&address); |
| 55 | + bus.write(0xBE); // Read Scratchpad |
| 56 | + |
| 57 | + // printf(" Data = %x \r\n\r\n", present); |
| 58 | + uint8_t data[12]; |
| 59 | + for (int i = 0; i < 9; i++) { // we need 9 bytes |
| 60 | + data[i] = bus.read(); |
| 61 | + // printf(" %x", data[i]); |
| 62 | + } |
| 63 | + // printf("\r\n"); |
| 64 | + // printf(" CRC= %x \r\n\r\n", OneWire::crc8(data, 8)); |
| 65 | + |
| 66 | + // Convert the data to actual temperature |
| 67 | + // because the result is a 16 bit signed integer, it should |
| 68 | + // be stored to an "int16_t" type, which is always 16 bits |
| 69 | + // even when compiled on a 32 bit processor. |
| 70 | + int16_t raw = ((data[1] << 8) | data[0]); |
| 71 | + if (type_s_sensor) { |
| 72 | + raw = raw << 3; // 9 bit resolution default |
| 73 | + if (data[7] == 0x10) { |
| 74 | + // "count remain" gives full 12 bit resolution |
| 75 | + raw = (raw & 0xFFF0) + 12 - data[6]; |
| 76 | + } |
| 77 | + } else { |
| 78 | + uint8_t cfg = (data[4] & 0x60); |
| 79 | + // at lower res, the low bits are undefined, so let's zero them |
| 80 | + if (cfg == 0x00) |
| 81 | + raw = raw & ~7; // 9 bit resolution, 93.75 ms |
| 82 | + else if (cfg == 0x20) |
| 83 | + raw = raw & ~3; // 10 bit res, 187.5 ms |
| 84 | + else if (cfg == 0x40) |
| 85 | + raw = raw & ~1; // 11 bit res, 375 ms |
| 86 | + //// default is 12 bit resolution, 750 ms conversion time |
| 87 | + } |
| 88 | + uint8_t celsius_x2 = raw / 8; |
| 89 | + // float celsius = (float)raw / 16.0; |
| 90 | + // fahrenheit = celsius * 1.8 + 32.0; |
| 91 | + return celsius_x2; |
| 92 | +} |
0 commit comments