Central Heating controller using the real time clock, PHY module for internet, 1-wire interface for temperature sensors, a system log and a configuration file
Dependencies: net 1-wire lpc1768 crypto clock web fram log
/media/uploads/andrewboyson/heating.sch
/media/uploads/andrewboyson/heating.brd
/media/uploads/andrewboyson/eagle.epf
heating/boiler.c@41:6413522ed343, 2019-03-25 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon Mar 25 10:35:43 2019 +0000
- Revision:
- 41:6413522ed343
- Parent:
- 35:bb8a6d1c034c
- Child:
- 48:6eac12df3ad5
Updated libraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:3c04f4b47041 | 1 | #include <string.h> |
andrewboyson | 0:3c04f4b47041 | 2 | #include <stdint.h> |
andrewboyson | 0:3c04f4b47041 | 3 | #include <stdbool.h> |
andrewboyson | 0:3c04f4b47041 | 4 | |
andrewboyson | 35:bb8a6d1c034c | 5 | #include "gpio.h" |
andrewboyson | 35:bb8a6d1c034c | 6 | #include "mstimer.h" |
andrewboyson | 1:ccc66fdf858d | 7 | #include "ds18b20.h" |
andrewboyson | 35:bb8a6d1c034c | 8 | #include "fram.h" |
andrewboyson | 0:3c04f4b47041 | 9 | |
andrewboyson | 5:82197a6997fd | 10 | #define BOILER_PUMP_DIR FIO2DIR(4) // P2.4 == p22 |
andrewboyson | 5:82197a6997fd | 11 | #define BOILER_PUMP_PIN FIO2PIN(4) |
andrewboyson | 5:82197a6997fd | 12 | #define BOILER_PUMP_SET FIO2SET(4) |
andrewboyson | 5:82197a6997fd | 13 | #define BOILER_PUMP_CLR FIO2CLR(4) |
andrewboyson | 5:82197a6997fd | 14 | |
andrewboyson | 5:82197a6997fd | 15 | #define BOILER_CALL_DIR FIO2DIR(5) // P2.5 == p21 |
andrewboyson | 5:82197a6997fd | 16 | #define BOILER_CALL_PIN FIO2PIN(5) |
andrewboyson | 5:82197a6997fd | 17 | #define BOILER_CALL_SET FIO2SET(5) |
andrewboyson | 5:82197a6997fd | 18 | #define BOILER_CALL_CLR FIO2CLR(5) |
andrewboyson | 0:3c04f4b47041 | 19 | |
andrewboyson | 0:3c04f4b47041 | 20 | static char tankRom[8]; static int iTankRom; |
andrewboyson | 0:3c04f4b47041 | 21 | static char outputRom[8]; static int iOutputRom; |
andrewboyson | 0:3c04f4b47041 | 22 | static char returnRom[8]; static int iReturnRom; |
andrewboyson | 0:3c04f4b47041 | 23 | |
andrewboyson | 0:3c04f4b47041 | 24 | static int32_t tankSetPoint; static int iTankSetPoint; |
andrewboyson | 0:3c04f4b47041 | 25 | static int32_t tankHysteresis; static int iTankHysteresis; |
andrewboyson | 0:3c04f4b47041 | 26 | static int32_t runOnResidual; static int iRunOnResidual; |
andrewboyson | 0:3c04f4b47041 | 27 | static int32_t runOnTime; static int iRunOnTime; |
andrewboyson | 0:3c04f4b47041 | 28 | |
andrewboyson | 0:3c04f4b47041 | 29 | char* BoilerGetTankRom () { return tankRom; } |
andrewboyson | 0:3c04f4b47041 | 30 | char* BoilerGetOutputRom () { return outputRom; } |
andrewboyson | 0:3c04f4b47041 | 31 | char* BoilerGetReturnRom () { return returnRom; } |
andrewboyson | 0:3c04f4b47041 | 32 | int BoilerGetTankSetPoint () { return (int) tankSetPoint; } |
andrewboyson | 0:3c04f4b47041 | 33 | int BoilerGetTankHysteresis() { return (int) tankHysteresis; } |
andrewboyson | 0:3c04f4b47041 | 34 | int BoilerGetRunOnResidual () { return (int) runOnResidual; } |
andrewboyson | 0:3c04f4b47041 | 35 | int BoilerGetRunOnTime () { return (int) runOnTime; } |
andrewboyson | 0:3c04f4b47041 | 36 | |
andrewboyson | 0:3c04f4b47041 | 37 | void BoilerSetTankRom (char* value) { memcpy(tankRom, value, 8); FramWrite(iTankRom, 8, tankRom ); } |
andrewboyson | 0:3c04f4b47041 | 38 | void BoilerSetOutputRom (char* value) { memcpy(outputRom, value, 8); FramWrite(iOutputRom, 8, outputRom ); } |
andrewboyson | 0:3c04f4b47041 | 39 | void BoilerSetReturnRom (char* value) { memcpy(returnRom, value, 8); FramWrite(iReturnRom, 8, returnRom ); } |
andrewboyson | 0:3c04f4b47041 | 40 | void BoilerSetTankSetPoint (int value) { tankSetPoint = (int32_t)value; FramWrite(iTankSetPoint, 4, &tankSetPoint ); } |
andrewboyson | 0:3c04f4b47041 | 41 | void BoilerSetTankHysteresis (int value) { tankHysteresis = (int32_t)value; FramWrite(iTankHysteresis, 4, &tankHysteresis ); } |
andrewboyson | 0:3c04f4b47041 | 42 | void BoilerSetRunOnResidual (int value) { runOnResidual = (int32_t)value; FramWrite(iRunOnResidual, 4, &runOnResidual ); } |
andrewboyson | 0:3c04f4b47041 | 43 | void BoilerSetRunOnTime (int value) { runOnTime = (int32_t)value; FramWrite(iRunOnTime, 4, &runOnTime ); } |
andrewboyson | 0:3c04f4b47041 | 44 | |
andrewboyson | 0:3c04f4b47041 | 45 | int BoilerInit() |
andrewboyson | 0:3c04f4b47041 | 46 | { |
andrewboyson | 0:3c04f4b47041 | 47 | int address; |
andrewboyson | 0:3c04f4b47041 | 48 | int32_t def4; |
andrewboyson | 0:3c04f4b47041 | 49 | address = FramLoad( 8, tankRom, 0); if (address < 0) return -1; iTankRom = address; |
andrewboyson | 0:3c04f4b47041 | 50 | address = FramLoad( 8, outputRom, 0); if (address < 0) return -1; iOutputRom = address; |
andrewboyson | 0:3c04f4b47041 | 51 | address = FramLoad( 8, returnRom, 0); if (address < 0) return -1; iReturnRom = address; |
andrewboyson | 0:3c04f4b47041 | 52 | def4 = 80; address = FramLoad( 4, &tankSetPoint, &def4); if (address < 0) return -1; iTankSetPoint = address; |
andrewboyson | 0:3c04f4b47041 | 53 | def4 = 5; address = FramLoad( 4, &tankHysteresis, &def4); if (address < 0) return -1; iTankHysteresis = address; |
andrewboyson | 0:3c04f4b47041 | 54 | def4 = 2; address = FramLoad( 4, &runOnResidual, &def4); if (address < 0) return -1; iRunOnResidual = address; |
andrewboyson | 0:3c04f4b47041 | 55 | def4 = 360; address = FramLoad( 4, &runOnTime, &def4); if (address < 0) return -1; iRunOnTime = address; |
andrewboyson | 0:3c04f4b47041 | 56 | |
andrewboyson | 5:82197a6997fd | 57 | BOILER_PUMP_DIR = 1; //Set the direction to 1 == output |
andrewboyson | 5:82197a6997fd | 58 | BOILER_CALL_DIR = 1; //Set the direction to 1 == output |
andrewboyson | 0:3c04f4b47041 | 59 | |
andrewboyson | 0:3c04f4b47041 | 60 | return 0; |
andrewboyson | 0:3c04f4b47041 | 61 | } |
andrewboyson | 0:3c04f4b47041 | 62 | bool BoilerCall = false; |
andrewboyson | 35:bb8a6d1c034c | 63 | static void controlBoilerCall() |
andrewboyson | 0:3c04f4b47041 | 64 | { |
andrewboyson | 0:3c04f4b47041 | 65 | int tankTemp16ths = DS18B20ValueFromRom(tankRom); |
andrewboyson | 0:3c04f4b47041 | 66 | if (DS18B20IsValidValue(tankTemp16ths)) //Ignore values which are likely to be wrong |
andrewboyson | 0:3c04f4b47041 | 67 | { |
andrewboyson | 0:3c04f4b47041 | 68 | int tankUpper16ths = tankSetPoint << 4; |
andrewboyson | 0:3c04f4b47041 | 69 | int hysteresis16ths = tankHysteresis << 4; |
andrewboyson | 0:3c04f4b47041 | 70 | int tankLower16ths = tankUpper16ths - hysteresis16ths; |
andrewboyson | 0:3c04f4b47041 | 71 | |
andrewboyson | 0:3c04f4b47041 | 72 | if (tankTemp16ths >= tankUpper16ths) BoilerCall = false; |
andrewboyson | 0:3c04f4b47041 | 73 | if (tankTemp16ths <= tankLower16ths) BoilerCall = true; |
andrewboyson | 0:3c04f4b47041 | 74 | } |
andrewboyson | 35:bb8a6d1c034c | 75 | } |
andrewboyson | 35:bb8a6d1c034c | 76 | bool BoilerPump = false; |
andrewboyson | 35:bb8a6d1c034c | 77 | static void controlBoilerPump() |
andrewboyson | 35:bb8a6d1c034c | 78 | { |
andrewboyson | 0:3c04f4b47041 | 79 | int boilerOutput16ths = DS18B20ValueFromRom(outputRom); |
andrewboyson | 0:3c04f4b47041 | 80 | int boilerReturn16ths = DS18B20ValueFromRom(returnRom); |
andrewboyson | 0:3c04f4b47041 | 81 | int boilerResidual16ths = boilerOutput16ths - boilerReturn16ths; |
andrewboyson | 0:3c04f4b47041 | 82 | int boilerTempsAreValid = DS18B20IsValidValue(boilerOutput16ths) && DS18B20IsValidValue(boilerReturn16ths); |
andrewboyson | 0:3c04f4b47041 | 83 | |
andrewboyson | 35:bb8a6d1c034c | 84 | static uint32_t msTimerBoilerPumpRunOn = 0; |
andrewboyson | 0:3c04f4b47041 | 85 | if (BoilerCall) |
andrewboyson | 0:3c04f4b47041 | 86 | { |
andrewboyson | 0:3c04f4b47041 | 87 | BoilerPump = true; |
andrewboyson | 35:bb8a6d1c034c | 88 | msTimerBoilerPumpRunOn = MsTimerCount; |
andrewboyson | 0:3c04f4b47041 | 89 | } |
andrewboyson | 0:3c04f4b47041 | 90 | else |
andrewboyson | 0:3c04f4b47041 | 91 | { |
andrewboyson | 41:6413522ed343 | 92 | if (MsTimerRelative(msTimerBoilerPumpRunOn, runOnTime * 1000)) BoilerPump = false; |
andrewboyson | 35:bb8a6d1c034c | 93 | if (boilerTempsAreValid && boilerResidual16ths < runOnResidual << 4) BoilerPump = false; |
andrewboyson | 0:3c04f4b47041 | 94 | } |
andrewboyson | 35:bb8a6d1c034c | 95 | } |
andrewboyson | 35:bb8a6d1c034c | 96 | void BoilerMain() |
andrewboyson | 35:bb8a6d1c034c | 97 | { |
andrewboyson | 35:bb8a6d1c034c | 98 | controlBoilerCall(); |
andrewboyson | 35:bb8a6d1c034c | 99 | if (BoilerCall) BOILER_CALL_SET; |
andrewboyson | 35:bb8a6d1c034c | 100 | else BOILER_CALL_CLR; |
andrewboyson | 0:3c04f4b47041 | 101 | |
andrewboyson | 35:bb8a6d1c034c | 102 | controlBoilerPump(); |
andrewboyson | 5:82197a6997fd | 103 | if (BoilerPump) BOILER_PUMP_SET; |
andrewboyson | 5:82197a6997fd | 104 | else BOILER_PUMP_CLR; |
andrewboyson | 0:3c04f4b47041 | 105 | |
andrewboyson | 0:3c04f4b47041 | 106 | } |