Manages the 1-wire bus
Dependents: oldheating heating
1-wire/ds18b20.c@11:3859fee99d5d, 2021-02-18 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Feb 18 16:47:12 2021 +0000
- Revision:
- 11:3859fee99d5d
- Parent:
- 1:c272b1fcc834
Added 'value not set' to the list of possible values.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:b4b170ce93a4 | 1 | #include <stdint.h> |
andrewboyson | 0:b4b170ce93a4 | 2 | #include <string.h> |
andrewboyson | 0:b4b170ce93a4 | 3 | #include <stdio.h> |
andrewboyson | 0:b4b170ce93a4 | 4 | |
andrewboyson | 0:b4b170ce93a4 | 5 | #include "log.h" |
andrewboyson | 0:b4b170ce93a4 | 6 | #include "1-wire.h" |
andrewboyson | 0:b4b170ce93a4 | 7 | #include "1-wire-device.h" |
andrewboyson | 0:b4b170ce93a4 | 8 | #include "ds18b20.h" |
andrewboyson | 0:b4b170ce93a4 | 9 | |
andrewboyson | 0:b4b170ce93a4 | 10 | int16_t DS18B20Value[DEVICE_MAX]; |
andrewboyson | 0:b4b170ce93a4 | 11 | |
andrewboyson | 0:b4b170ce93a4 | 12 | int DS18B20RomCount; |
andrewboyson | 0:b4b170ce93a4 | 13 | char* DS18B20RomNames [DEVICE_MAX]; |
andrewboyson | 0:b4b170ce93a4 | 14 | char DS18B20Roms [DEVICE_MAX * 8]; |
andrewboyson | 0:b4b170ce93a4 | 15 | void (*DS18B20RomSetters[DEVICE_MAX])(char*); |
andrewboyson | 0:b4b170ce93a4 | 16 | |
andrewboyson | 0:b4b170ce93a4 | 17 | |
andrewboyson | 0:b4b170ce93a4 | 18 | #define MAX_TEMP_16THS 1600 |
andrewboyson | 0:b4b170ce93a4 | 19 | #define MIN_TEMP_16THS -160 |
andrewboyson | 0:b4b170ce93a4 | 20 | int DS18B20IsValidValue(int16_t value) |
andrewboyson | 0:b4b170ce93a4 | 21 | { |
andrewboyson | 0:b4b170ce93a4 | 22 | return value < MAX_TEMP_16THS && value > MIN_TEMP_16THS; |
andrewboyson | 0:b4b170ce93a4 | 23 | } |
andrewboyson | 0:b4b170ce93a4 | 24 | void DS18B20ValueToString(int16_t value, char* buffer) |
andrewboyson | 0:b4b170ce93a4 | 25 | { |
andrewboyson | 0:b4b170ce93a4 | 26 | switch (value) |
andrewboyson | 0:b4b170ce93a4 | 27 | { |
andrewboyson | 0:b4b170ce93a4 | 28 | case DS18B20_ERROR_CRC: strcpy (buffer, "CRC error" ); break; |
andrewboyson | 0:b4b170ce93a4 | 29 | case DS18B20_ERROR_NOT_FOUND: strcpy (buffer, "ROM not found" ); break; |
andrewboyson | 0:b4b170ce93a4 | 30 | case DS18B20_ERROR_TIMED_OUT: strcpy (buffer, "Timed out" ); break; |
andrewboyson | 0:b4b170ce93a4 | 31 | case DS18B20_ERROR_NO_DEVICE_PRESENT: strcpy (buffer, "No device detected after reset"); break; |
andrewboyson | 0:b4b170ce93a4 | 32 | case DS18B20_ERROR_NO_DEVICE_PARTICIPATING: strcpy (buffer, "Device removed during search" ); break; |
andrewboyson | 11:3859fee99d5d | 33 | case DS18B20_ERROR_VALUE_NOT_SET: strcpy (buffer, "Value not set" ); break; |
andrewboyson | 0:b4b170ce93a4 | 34 | default: sprintf(buffer, "%1.1f", value / 16.0 ); break; |
andrewboyson | 0:b4b170ce93a4 | 35 | } |
andrewboyson | 0:b4b170ce93a4 | 36 | } |
andrewboyson | 11:3859fee99d5d | 37 | void DS18B20Log(int16_t value) |
andrewboyson | 11:3859fee99d5d | 38 | { |
andrewboyson | 11:3859fee99d5d | 39 | switch (value) |
andrewboyson | 11:3859fee99d5d | 40 | { |
andrewboyson | 11:3859fee99d5d | 41 | case DS18B20_ERROR_CRC: Log ("CRC error" ); break; |
andrewboyson | 11:3859fee99d5d | 42 | case DS18B20_ERROR_NOT_FOUND: Log ("ROM not found" ); break; |
andrewboyson | 11:3859fee99d5d | 43 | case DS18B20_ERROR_TIMED_OUT: Log ("Timed out" ); break; |
andrewboyson | 11:3859fee99d5d | 44 | case DS18B20_ERROR_NO_DEVICE_PRESENT: Log ("No device detected after reset"); break; |
andrewboyson | 11:3859fee99d5d | 45 | case DS18B20_ERROR_NO_DEVICE_PARTICIPATING: Log ("Device removed during search" ); break; |
andrewboyson | 11:3859fee99d5d | 46 | case DS18B20_ERROR_VALUE_NOT_SET: Log ("Value not set" ); break; |
andrewboyson | 11:3859fee99d5d | 47 | default: LogF("%1.1f", value / 16.0 ); break; |
andrewboyson | 11:3859fee99d5d | 48 | } |
andrewboyson | 11:3859fee99d5d | 49 | } |
andrewboyson | 0:b4b170ce93a4 | 50 | int16_t DS18B20ValueFromRom(char* rom) |
andrewboyson | 0:b4b170ce93a4 | 51 | { |
andrewboyson | 0:b4b170ce93a4 | 52 | for (int device = 0; device < DeviceCount; device++) if (memcmp(DeviceList + 8 * device, rom, 8) == 0) return DS18B20Value[device]; |
andrewboyson | 0:b4b170ce93a4 | 53 | return DS18B20_ERROR_NOT_FOUND; |
andrewboyson | 0:b4b170ce93a4 | 54 | } |
andrewboyson | 0:b4b170ce93a4 | 55 | |
andrewboyson | 0:b4b170ce93a4 | 56 | void DS18B20ReadValue(int oneWireResult, int device, char byte0, char byte1) |
andrewboyson | 0:b4b170ce93a4 | 57 | { |
andrewboyson | 0:b4b170ce93a4 | 58 | switch (oneWireResult) |
andrewboyson | 0:b4b170ce93a4 | 59 | { |
andrewboyson | 0:b4b170ce93a4 | 60 | case ONE_WIRE_RESULT_OK: |
andrewboyson | 0:b4b170ce93a4 | 61 | DS18B20Value[device] = byte1; |
andrewboyson | 0:b4b170ce93a4 | 62 | DS18B20Value[device] <<= 8; |
andrewboyson | 0:b4b170ce93a4 | 63 | DS18B20Value[device] |= byte0; |
andrewboyson | 0:b4b170ce93a4 | 64 | break; |
andrewboyson | 0:b4b170ce93a4 | 65 | case ONE_WIRE_RESULT_CRC_ERROR: DS18B20Value[device] = DS18B20_ERROR_CRC; break; |
andrewboyson | 0:b4b170ce93a4 | 66 | case ONE_WIRE_RESULT_NO_DEVICE_PRESENT: DS18B20Value[device] = DS18B20_ERROR_NO_DEVICE_PRESENT; break; |
andrewboyson | 0:b4b170ce93a4 | 67 | case ONE_WIRE_RESULT_TIMED_OUT: DS18B20Value[device] = DS18B20_ERROR_TIMED_OUT; break; |
andrewboyson | 0:b4b170ce93a4 | 68 | case ONE_WIRE_RESULT_NO_DEVICE_PARTICIPATING: DS18B20Value[device] = DS18B20_ERROR_NO_DEVICE_PARTICIPATING; break; |
andrewboyson | 0:b4b170ce93a4 | 69 | default: |
andrewboyson | 0:b4b170ce93a4 | 70 | LogF("Unknown OneWireResult %d\r\n", OneWireResult()); |
andrewboyson | 0:b4b170ce93a4 | 71 | break; |
andrewboyson | 0:b4b170ce93a4 | 72 | } |
andrewboyson | 0:b4b170ce93a4 | 73 | } |
andrewboyson | 0:b4b170ce93a4 | 74 | void DS18B20Init() |
andrewboyson | 0:b4b170ce93a4 | 75 | { |
andrewboyson | 11:3859fee99d5d | 76 | for (int i = 0; i < DEVICE_MAX; i++) DS18B20Value[i] = DS18B20_ERROR_VALUE_NOT_SET; |
andrewboyson | 0:b4b170ce93a4 | 77 | } |