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

Committer:
andrewboyson
Date:
Fri Feb 16 19:43:06 2018 +0000
Revision:
5:82197a6997fd
Parent:
1:ccc66fdf858d
Child:
11:fa01ea25b62d
Removed reliance on defs.h

Who changed what in which revision?

UserRevisionLine numberNew 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 5:82197a6997fd 5 #include "gpio.h"
andrewboyson 1:ccc66fdf858d 6 #include "tick.h"
andrewboyson 1:ccc66fdf858d 7 #include "ds18b20.h"
andrewboyson 1:ccc66fdf858d 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
andrewboyson 0:3c04f4b47041 25 static int32_t tankSetPoint; static int iTankSetPoint;
andrewboyson 0:3c04f4b47041 26 static int32_t tankHysteresis; static int iTankHysteresis;
andrewboyson 0:3c04f4b47041 27 static int32_t runOnResidual; static int iRunOnResidual;
andrewboyson 0:3c04f4b47041 28 static int32_t runOnTime; static int iRunOnTime;
andrewboyson 0:3c04f4b47041 29
andrewboyson 0:3c04f4b47041 30 char* BoilerGetTankRom () { return tankRom; }
andrewboyson 0:3c04f4b47041 31 char* BoilerGetOutputRom () { return outputRom; }
andrewboyson 0:3c04f4b47041 32 char* BoilerGetReturnRom () { return returnRom; }
andrewboyson 0:3c04f4b47041 33 int BoilerGetTankSetPoint () { return (int) tankSetPoint; }
andrewboyson 0:3c04f4b47041 34 int BoilerGetTankHysteresis() { return (int) tankHysteresis; }
andrewboyson 0:3c04f4b47041 35 int BoilerGetRunOnResidual () { return (int) runOnResidual; }
andrewboyson 0:3c04f4b47041 36 int BoilerGetRunOnTime () { return (int) runOnTime; }
andrewboyson 0:3c04f4b47041 37
andrewboyson 0:3c04f4b47041 38 void BoilerSetTankRom (char* value) { memcpy(tankRom, value, 8); FramWrite(iTankRom, 8, tankRom ); }
andrewboyson 0:3c04f4b47041 39 void BoilerSetOutputRom (char* value) { memcpy(outputRom, value, 8); FramWrite(iOutputRom, 8, outputRom ); }
andrewboyson 0:3c04f4b47041 40 void BoilerSetReturnRom (char* value) { memcpy(returnRom, value, 8); FramWrite(iReturnRom, 8, returnRom ); }
andrewboyson 0:3c04f4b47041 41 void BoilerSetTankSetPoint (int value) { tankSetPoint = (int32_t)value; FramWrite(iTankSetPoint, 4, &tankSetPoint ); }
andrewboyson 0:3c04f4b47041 42 void BoilerSetTankHysteresis (int value) { tankHysteresis = (int32_t)value; FramWrite(iTankHysteresis, 4, &tankHysteresis ); }
andrewboyson 0:3c04f4b47041 43 void BoilerSetRunOnResidual (int value) { runOnResidual = (int32_t)value; FramWrite(iRunOnResidual, 4, &runOnResidual ); }
andrewboyson 0:3c04f4b47041 44 void BoilerSetRunOnTime (int value) { runOnTime = (int32_t)value; FramWrite(iRunOnTime, 4, &runOnTime ); }
andrewboyson 0:3c04f4b47041 45
andrewboyson 0:3c04f4b47041 46
andrewboyson 0:3c04f4b47041 47 int BoilerInit()
andrewboyson 0:3c04f4b47041 48 {
andrewboyson 0:3c04f4b47041 49 int address;
andrewboyson 0:3c04f4b47041 50 int32_t def4;
andrewboyson 0:3c04f4b47041 51 address = FramLoad( 8, tankRom, 0); if (address < 0) return -1; iTankRom = address;
andrewboyson 0:3c04f4b47041 52 address = FramLoad( 8, outputRom, 0); if (address < 0) return -1; iOutputRom = address;
andrewboyson 0:3c04f4b47041 53 address = FramLoad( 8, returnRom, 0); if (address < 0) return -1; iReturnRom = address;
andrewboyson 0:3c04f4b47041 54 def4 = 80; address = FramLoad( 4, &tankSetPoint, &def4); if (address < 0) return -1; iTankSetPoint = address;
andrewboyson 0:3c04f4b47041 55 def4 = 5; address = FramLoad( 4, &tankHysteresis, &def4); if (address < 0) return -1; iTankHysteresis = address;
andrewboyson 0:3c04f4b47041 56 def4 = 2; address = FramLoad( 4, &runOnResidual, &def4); if (address < 0) return -1; iRunOnResidual = address;
andrewboyson 0:3c04f4b47041 57 def4 = 360; address = FramLoad( 4, &runOnTime, &def4); if (address < 0) return -1; iRunOnTime = address;
andrewboyson 0:3c04f4b47041 58
andrewboyson 5:82197a6997fd 59 BOILER_PUMP_DIR = 1; //Set the direction to 1 == output
andrewboyson 5:82197a6997fd 60 BOILER_CALL_DIR = 1; //Set the direction to 1 == output
andrewboyson 0:3c04f4b47041 61
andrewboyson 0:3c04f4b47041 62 return 0;
andrewboyson 0:3c04f4b47041 63 }
andrewboyson 0:3c04f4b47041 64 bool BoilerPump = false;
andrewboyson 0:3c04f4b47041 65 bool BoilerCall = false;
andrewboyson 0:3c04f4b47041 66
andrewboyson 0:3c04f4b47041 67 void BoilerMain()
andrewboyson 0:3c04f4b47041 68 {
andrewboyson 0:3c04f4b47041 69 //Control boiler call
andrewboyson 0:3c04f4b47041 70 int tankTemp16ths = DS18B20ValueFromRom(tankRom);
andrewboyson 0:3c04f4b47041 71 if (DS18B20IsValidValue(tankTemp16ths)) //Ignore values which are likely to be wrong
andrewboyson 0:3c04f4b47041 72 {
andrewboyson 0:3c04f4b47041 73 int tankUpper16ths = tankSetPoint << 4;
andrewboyson 0:3c04f4b47041 74 int hysteresis16ths = tankHysteresis << 4;
andrewboyson 0:3c04f4b47041 75 int tankLower16ths = tankUpper16ths - hysteresis16ths;
andrewboyson 0:3c04f4b47041 76
andrewboyson 0:3c04f4b47041 77 if (tankTemp16ths >= tankUpper16ths) BoilerCall = false;
andrewboyson 0:3c04f4b47041 78 if (tankTemp16ths <= tankLower16ths) BoilerCall = true;
andrewboyson 0:3c04f4b47041 79 }
andrewboyson 5:82197a6997fd 80 if (BoilerCall) BOILER_CALL_SET;
andrewboyson 5:82197a6997fd 81 else BOILER_CALL_CLR;
andrewboyson 0:3c04f4b47041 82
andrewboyson 0:3c04f4b47041 83 //Control boiler circulation pump
andrewboyson 0:3c04f4b47041 84 int boilerOutput16ths = DS18B20ValueFromRom(outputRom);
andrewboyson 0:3c04f4b47041 85 int boilerReturn16ths = DS18B20ValueFromRom(returnRom);
andrewboyson 0:3c04f4b47041 86 int boilerResidual16ths = boilerOutput16ths - boilerReturn16ths;
andrewboyson 0:3c04f4b47041 87 int boilerTempsAreValid = DS18B20IsValidValue(boilerOutput16ths) && DS18B20IsValidValue(boilerReturn16ths);
andrewboyson 0:3c04f4b47041 88
andrewboyson 1:ccc66fdf858d 89 static int64_t offTicks;
andrewboyson 0:3c04f4b47041 90 if (BoilerCall)
andrewboyson 0:3c04f4b47041 91 {
andrewboyson 0:3c04f4b47041 92 BoilerPump = true;
andrewboyson 1:ccc66fdf858d 93 offTicks = Ticks();
andrewboyson 0:3c04f4b47041 94 }
andrewboyson 0:3c04f4b47041 95 else
andrewboyson 0:3c04f4b47041 96 {
andrewboyson 1:ccc66fdf858d 97 int secondsSinceBoilerOff = (Ticks() - offTicks) >> TICK_ONE_SECOND_SHIFT;
andrewboyson 0:3c04f4b47041 98 if (secondsSinceBoilerOff > runOnTime) BoilerPump = false;
andrewboyson 0:3c04f4b47041 99 if (boilerTempsAreValid)
andrewboyson 0:3c04f4b47041 100 {
andrewboyson 0:3c04f4b47041 101 int boilerStillSupplyingHeat = boilerResidual16ths > runOnResidual;
andrewboyson 0:3c04f4b47041 102 if (!boilerStillSupplyingHeat) BoilerPump = false;
andrewboyson 0:3c04f4b47041 103 }
andrewboyson 0:3c04f4b47041 104 }
andrewboyson 0:3c04f4b47041 105
andrewboyson 5:82197a6997fd 106 if (BoilerPump) BOILER_PUMP_SET;
andrewboyson 5:82197a6997fd 107 else BOILER_PUMP_CLR;
andrewboyson 0:3c04f4b47041 108
andrewboyson 0:3c04f4b47041 109 }