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
Diff: heating/radiator.c
- Revision:
- 0:3c04f4b47041
- Child:
- 1:ccc66fdf858d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/heating/radiator.c Thu Jan 11 17:40:08 2018 +0000 @@ -0,0 +1,132 @@ +#include <stdint.h> +#include <string.h> +#include <stdbool.h> + +#include "peripherals.h" +#include "program.h" +#include "ds18b20.h" +#include "fram.h" +#include "radiator.h" + +#define HALL_PB_MASK 1ULL << 5 // P0. 5 == p29; +#define HALL_LED_MASK 1ULL << 10 // P0.10 == p28; +#define RADIATOR_PUMP_MASK 1ULL << 3 // P2. 3 == p23; + + +static char htgMode; static int iMode; +static char htgOverride; static int iOverride; +static char hallRom[8]; static int iHallRom; +static int32_t nightTemperature; static int iNightTemperature; +static int32_t frostTemperature; static int iFrostTemperature; + +bool RadiatorGetMode (){ return (bool)htgMode; } +bool RadiatorGetOverride (){ return (bool)htgOverride; } +char* RadiatorGetHallRom (){ return hallRom; } +int RadiatorGetNightTemperature(){ return (int)nightTemperature; } +int RadiatorGetFrostTemperature(){ return (int)frostTemperature; } + +static void setMode ( bool value) { htgMode = (char)value; FramWrite(iMode, 1, &htgMode ); } +static void setOverride ( bool value) { htgOverride = (char)value; FramWrite(iOverride, 1, &htgOverride ); } +void RadiatorSetHallRom (char* value) { memcpy(hallRom, value, 8); FramWrite(iHallRom, 8, hallRom ); } +void RadiatorSetNightTemperature ( int value) { nightTemperature = (int32_t)value; FramWrite(iNightTemperature, 4, &nightTemperature); } +void RadiatorSetFrostTemperature ( int value) { frostTemperature = (int32_t)value; FramWrite(iFrostTemperature, 4, &frostTemperature); } + +static bool outputBeforeOverride = false; +static void makeOutputBeforeOverride() +{ + //See if the temperature is too low + int hallTemp16ths = DS18B20ValueFromRom(hallRom); + int nightTemp16ths = nightTemperature << 4; + int frostTemp16ths = frostTemperature << 4; + + static bool tooCold = false; //This is static to ride through invalid temperature reads + if (DS18B20IsValidValue(hallTemp16ths)) + { + tooCold = hallTemp16ths < frostTemp16ths; + if (htgMode) tooCold |= hallTemp16ths < nightTemp16ths; + } + + outputBeforeOverride = htgMode && ProgramTimerOutput || tooCold; +} +static void adjustOverride() +{ + static bool previousOutput = false; + if (previousOutput != outputBeforeOverride) setOverride(false); + previousOutput = outputBeforeOverride; +} +bool RadiatorPump = false; +static void makeOutputWithOverride() +{ + RadiatorPump = htgOverride ? !outputBeforeOverride : outputBeforeOverride ; +} + +void RadiatorSetMode(bool value) //Summer is false, Winter is true +{ + if (htgMode == (char)value) return; //Ignore no change + setMode(value); //Change to the new value + + bool prevOutputBeforeOverride = outputBeforeOverride; + makeOutputBeforeOverride(); + + if (htgOverride) //Only deal with an override that is already set; if it wasn't set don't change it + { + if (htgMode) //Summer -> Winter + { + if (outputBeforeOverride != prevOutputBeforeOverride) setOverride(0); //Adjust the override to leave the heat as it was - off or on. + } + else //Winter -> Summer + { + setOverride(0); //turn off the heat. + } + } + + makeOutputWithOverride(); +} +void RadiatorSetOverride(bool value) +{ + setOverride(value); + makeOutputBeforeOverride(); + makeOutputWithOverride(); } + +void RadiatorChgMode (){ RadiatorSetMode (!RadiatorGetMode ()); } +void RadiatorChgOverride(){ RadiatorSetOverride(!RadiatorGetOverride()); } + +int RadiatorInit() +{ + int address; + char def1; + int32_t def4; + def1 = 0; address = FramLoad( 1, &htgMode, &def1); if (address < 0) return -1; iMode = address; + def1 = 0; address = FramLoad( 1, &htgOverride, &def1); if (address < 0) return -1; iOverride = address; + address = FramLoad( 8, hallRom, 0); if (address < 0) return -1; iHallRom = address; + def4 = 15; address = FramLoad( 4, &nightTemperature, &def4); if (address < 0) return -1; iNightTemperature = address; + def4 = 8; address = FramLoad( 4, &frostTemperature, &def4); if (address < 0) return -1; iFrostTemperature = address; + + LPC_GPIO2->FIODIR |= RADIATOR_PUMP_MASK; //Set the direction to 1 == output + LPC_GPIO0->FIODIR |= HALL_LED_MASK; //Set the direction to 1 == output + + return 0; +} +void RadiatorMain() +{ + //deal with pushbutton + static bool prevHallPb = false; + bool thisHallPb = LPC_GPIO0->FIOPIN & HALL_PB_MASK; + bool hallPbPressed = thisHallPb && !prevHallPb; + prevHallPb = thisHallPb; + if (hallPbPressed) RadiatorChgOverride(); + + //Make the radiator output + makeOutputBeforeOverride(); + adjustOverride(); + makeOutputWithOverride(); + + //Pump output + if (RadiatorPump) LPC_GPIO2->FIOSET = RADIATOR_PUMP_MASK; + else LPC_GPIO2->FIOCLR = RADIATOR_PUMP_MASK; + + + //Display the led + if (RadiatorPump) LPC_GPIO0->FIOSET = HALL_LED_MASK; + else LPC_GPIO0->FIOCLR = HALL_LED_MASK; +} \ No newline at end of file