Andrew Boyson / 1-wire

Dependents:   oldheating heating

Committer:
andrewboyson
Date:
Thu Apr 25 14:44:25 2019 +0000
Revision:
0:b4b170ce93a4
Created

Who changed what in which revision?

UserRevisionLine numberNew 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 0:b4b170ce93a4 33 default: sprintf(buffer, "%1.1f", value / 16.0 ); break;
andrewboyson 0:b4b170ce93a4 34 }
andrewboyson 0:b4b170ce93a4 35 }
andrewboyson 0:b4b170ce93a4 36 int16_t DS18B20ValueFromRom(char* rom)
andrewboyson 0:b4b170ce93a4 37 {
andrewboyson 0:b4b170ce93a4 38 for (int device = 0; device < DeviceCount; device++) if (memcmp(DeviceList + 8 * device, rom, 8) == 0) return DS18B20Value[device];
andrewboyson 0:b4b170ce93a4 39 return DS18B20_ERROR_NOT_FOUND;
andrewboyson 0:b4b170ce93a4 40 }
andrewboyson 0:b4b170ce93a4 41
andrewboyson 0:b4b170ce93a4 42 void DS18B20ReadValue(int oneWireResult, int device, char byte0, char byte1)
andrewboyson 0:b4b170ce93a4 43 {
andrewboyson 0:b4b170ce93a4 44 switch (oneWireResult)
andrewboyson 0:b4b170ce93a4 45 {
andrewboyson 0:b4b170ce93a4 46 case ONE_WIRE_RESULT_OK:
andrewboyson 0:b4b170ce93a4 47 DS18B20Value[device] = byte1;
andrewboyson 0:b4b170ce93a4 48 DS18B20Value[device] <<= 8;
andrewboyson 0:b4b170ce93a4 49 DS18B20Value[device] |= byte0;
andrewboyson 0:b4b170ce93a4 50 break;
andrewboyson 0:b4b170ce93a4 51 case ONE_WIRE_RESULT_CRC_ERROR: DS18B20Value[device] = DS18B20_ERROR_CRC; break;
andrewboyson 0:b4b170ce93a4 52 case ONE_WIRE_RESULT_NO_DEVICE_PRESENT: DS18B20Value[device] = DS18B20_ERROR_NO_DEVICE_PRESENT; break;
andrewboyson 0:b4b170ce93a4 53 case ONE_WIRE_RESULT_TIMED_OUT: DS18B20Value[device] = DS18B20_ERROR_TIMED_OUT; break;
andrewboyson 0:b4b170ce93a4 54 case ONE_WIRE_RESULT_NO_DEVICE_PARTICIPATING: DS18B20Value[device] = DS18B20_ERROR_NO_DEVICE_PARTICIPATING; break;
andrewboyson 0:b4b170ce93a4 55 default:
andrewboyson 0:b4b170ce93a4 56 LogF("Unknown OneWireResult %d\r\n", OneWireResult());
andrewboyson 0:b4b170ce93a4 57 break;
andrewboyson 0:b4b170ce93a4 58 }
andrewboyson 0:b4b170ce93a4 59 }
andrewboyson 0:b4b170ce93a4 60 void DS18B20Init()
andrewboyson 0:b4b170ce93a4 61 {
andrewboyson 0:b4b170ce93a4 62 for (int i = 0; i < DEVICE_MAX; i++) DS18B20Value[i] = DS18B20_ERROR_NOT_FOUND;
andrewboyson 0:b4b170ce93a4 63 }