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/radiator.c@0:3c04f4b47041, 2018-01-11 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Jan 11 17:40:08 2018 +0000
- Revision:
- 0:3c04f4b47041
- Child:
- 1:ccc66fdf858d
Removed dependence on Mbed OS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:3c04f4b47041 | 1 | #include <stdint.h> |
andrewboyson | 0:3c04f4b47041 | 2 | #include <string.h> |
andrewboyson | 0:3c04f4b47041 | 3 | #include <stdbool.h> |
andrewboyson | 0:3c04f4b47041 | 4 | |
andrewboyson | 0:3c04f4b47041 | 5 | #include "peripherals.h" |
andrewboyson | 0:3c04f4b47041 | 6 | #include "program.h" |
andrewboyson | 0:3c04f4b47041 | 7 | #include "ds18b20.h" |
andrewboyson | 0:3c04f4b47041 | 8 | #include "fram.h" |
andrewboyson | 0:3c04f4b47041 | 9 | #include "radiator.h" |
andrewboyson | 0:3c04f4b47041 | 10 | |
andrewboyson | 0:3c04f4b47041 | 11 | #define HALL_PB_MASK 1ULL << 5 // P0. 5 == p29; |
andrewboyson | 0:3c04f4b47041 | 12 | #define HALL_LED_MASK 1ULL << 10 // P0.10 == p28; |
andrewboyson | 0:3c04f4b47041 | 13 | #define RADIATOR_PUMP_MASK 1ULL << 3 // P2. 3 == p23; |
andrewboyson | 0:3c04f4b47041 | 14 | |
andrewboyson | 0:3c04f4b47041 | 15 | |
andrewboyson | 0:3c04f4b47041 | 16 | static char htgMode; static int iMode; |
andrewboyson | 0:3c04f4b47041 | 17 | static char htgOverride; static int iOverride; |
andrewboyson | 0:3c04f4b47041 | 18 | static char hallRom[8]; static int iHallRom; |
andrewboyson | 0:3c04f4b47041 | 19 | static int32_t nightTemperature; static int iNightTemperature; |
andrewboyson | 0:3c04f4b47041 | 20 | static int32_t frostTemperature; static int iFrostTemperature; |
andrewboyson | 0:3c04f4b47041 | 21 | |
andrewboyson | 0:3c04f4b47041 | 22 | bool RadiatorGetMode (){ return (bool)htgMode; } |
andrewboyson | 0:3c04f4b47041 | 23 | bool RadiatorGetOverride (){ return (bool)htgOverride; } |
andrewboyson | 0:3c04f4b47041 | 24 | char* RadiatorGetHallRom (){ return hallRom; } |
andrewboyson | 0:3c04f4b47041 | 25 | int RadiatorGetNightTemperature(){ return (int)nightTemperature; } |
andrewboyson | 0:3c04f4b47041 | 26 | int RadiatorGetFrostTemperature(){ return (int)frostTemperature; } |
andrewboyson | 0:3c04f4b47041 | 27 | |
andrewboyson | 0:3c04f4b47041 | 28 | static void setMode ( bool value) { htgMode = (char)value; FramWrite(iMode, 1, &htgMode ); } |
andrewboyson | 0:3c04f4b47041 | 29 | static void setOverride ( bool value) { htgOverride = (char)value; FramWrite(iOverride, 1, &htgOverride ); } |
andrewboyson | 0:3c04f4b47041 | 30 | void RadiatorSetHallRom (char* value) { memcpy(hallRom, value, 8); FramWrite(iHallRom, 8, hallRom ); } |
andrewboyson | 0:3c04f4b47041 | 31 | void RadiatorSetNightTemperature ( int value) { nightTemperature = (int32_t)value; FramWrite(iNightTemperature, 4, &nightTemperature); } |
andrewboyson | 0:3c04f4b47041 | 32 | void RadiatorSetFrostTemperature ( int value) { frostTemperature = (int32_t)value; FramWrite(iFrostTemperature, 4, &frostTemperature); } |
andrewboyson | 0:3c04f4b47041 | 33 | |
andrewboyson | 0:3c04f4b47041 | 34 | static bool outputBeforeOverride = false; |
andrewboyson | 0:3c04f4b47041 | 35 | static void makeOutputBeforeOverride() |
andrewboyson | 0:3c04f4b47041 | 36 | { |
andrewboyson | 0:3c04f4b47041 | 37 | //See if the temperature is too low |
andrewboyson | 0:3c04f4b47041 | 38 | int hallTemp16ths = DS18B20ValueFromRom(hallRom); |
andrewboyson | 0:3c04f4b47041 | 39 | int nightTemp16ths = nightTemperature << 4; |
andrewboyson | 0:3c04f4b47041 | 40 | int frostTemp16ths = frostTemperature << 4; |
andrewboyson | 0:3c04f4b47041 | 41 | |
andrewboyson | 0:3c04f4b47041 | 42 | static bool tooCold = false; //This is static to ride through invalid temperature reads |
andrewboyson | 0:3c04f4b47041 | 43 | if (DS18B20IsValidValue(hallTemp16ths)) |
andrewboyson | 0:3c04f4b47041 | 44 | { |
andrewboyson | 0:3c04f4b47041 | 45 | tooCold = hallTemp16ths < frostTemp16ths; |
andrewboyson | 0:3c04f4b47041 | 46 | if (htgMode) tooCold |= hallTemp16ths < nightTemp16ths; |
andrewboyson | 0:3c04f4b47041 | 47 | } |
andrewboyson | 0:3c04f4b47041 | 48 | |
andrewboyson | 0:3c04f4b47041 | 49 | outputBeforeOverride = htgMode && ProgramTimerOutput || tooCold; |
andrewboyson | 0:3c04f4b47041 | 50 | } |
andrewboyson | 0:3c04f4b47041 | 51 | static void adjustOverride() |
andrewboyson | 0:3c04f4b47041 | 52 | { |
andrewboyson | 0:3c04f4b47041 | 53 | static bool previousOutput = false; |
andrewboyson | 0:3c04f4b47041 | 54 | if (previousOutput != outputBeforeOverride) setOverride(false); |
andrewboyson | 0:3c04f4b47041 | 55 | previousOutput = outputBeforeOverride; |
andrewboyson | 0:3c04f4b47041 | 56 | } |
andrewboyson | 0:3c04f4b47041 | 57 | bool RadiatorPump = false; |
andrewboyson | 0:3c04f4b47041 | 58 | static void makeOutputWithOverride() |
andrewboyson | 0:3c04f4b47041 | 59 | { |
andrewboyson | 0:3c04f4b47041 | 60 | RadiatorPump = htgOverride ? !outputBeforeOverride : outputBeforeOverride ; |
andrewboyson | 0:3c04f4b47041 | 61 | } |
andrewboyson | 0:3c04f4b47041 | 62 | |
andrewboyson | 0:3c04f4b47041 | 63 | void RadiatorSetMode(bool value) //Summer is false, Winter is true |
andrewboyson | 0:3c04f4b47041 | 64 | { |
andrewboyson | 0:3c04f4b47041 | 65 | if (htgMode == (char)value) return; //Ignore no change |
andrewboyson | 0:3c04f4b47041 | 66 | setMode(value); //Change to the new value |
andrewboyson | 0:3c04f4b47041 | 67 | |
andrewboyson | 0:3c04f4b47041 | 68 | bool prevOutputBeforeOverride = outputBeforeOverride; |
andrewboyson | 0:3c04f4b47041 | 69 | makeOutputBeforeOverride(); |
andrewboyson | 0:3c04f4b47041 | 70 | |
andrewboyson | 0:3c04f4b47041 | 71 | if (htgOverride) //Only deal with an override that is already set; if it wasn't set don't change it |
andrewboyson | 0:3c04f4b47041 | 72 | { |
andrewboyson | 0:3c04f4b47041 | 73 | if (htgMode) //Summer -> Winter |
andrewboyson | 0:3c04f4b47041 | 74 | { |
andrewboyson | 0:3c04f4b47041 | 75 | if (outputBeforeOverride != prevOutputBeforeOverride) setOverride(0); //Adjust the override to leave the heat as it was - off or on. |
andrewboyson | 0:3c04f4b47041 | 76 | } |
andrewboyson | 0:3c04f4b47041 | 77 | else //Winter -> Summer |
andrewboyson | 0:3c04f4b47041 | 78 | { |
andrewboyson | 0:3c04f4b47041 | 79 | setOverride(0); //turn off the heat. |
andrewboyson | 0:3c04f4b47041 | 80 | } |
andrewboyson | 0:3c04f4b47041 | 81 | } |
andrewboyson | 0:3c04f4b47041 | 82 | |
andrewboyson | 0:3c04f4b47041 | 83 | makeOutputWithOverride(); |
andrewboyson | 0:3c04f4b47041 | 84 | } |
andrewboyson | 0:3c04f4b47041 | 85 | void RadiatorSetOverride(bool value) |
andrewboyson | 0:3c04f4b47041 | 86 | { |
andrewboyson | 0:3c04f4b47041 | 87 | setOverride(value); |
andrewboyson | 0:3c04f4b47041 | 88 | makeOutputBeforeOverride(); |
andrewboyson | 0:3c04f4b47041 | 89 | makeOutputWithOverride(); } |
andrewboyson | 0:3c04f4b47041 | 90 | |
andrewboyson | 0:3c04f4b47041 | 91 | void RadiatorChgMode (){ RadiatorSetMode (!RadiatorGetMode ()); } |
andrewboyson | 0:3c04f4b47041 | 92 | void RadiatorChgOverride(){ RadiatorSetOverride(!RadiatorGetOverride()); } |
andrewboyson | 0:3c04f4b47041 | 93 | |
andrewboyson | 0:3c04f4b47041 | 94 | int RadiatorInit() |
andrewboyson | 0:3c04f4b47041 | 95 | { |
andrewboyson | 0:3c04f4b47041 | 96 | int address; |
andrewboyson | 0:3c04f4b47041 | 97 | char def1; |
andrewboyson | 0:3c04f4b47041 | 98 | int32_t def4; |
andrewboyson | 0:3c04f4b47041 | 99 | def1 = 0; address = FramLoad( 1, &htgMode, &def1); if (address < 0) return -1; iMode = address; |
andrewboyson | 0:3c04f4b47041 | 100 | def1 = 0; address = FramLoad( 1, &htgOverride, &def1); if (address < 0) return -1; iOverride = address; |
andrewboyson | 0:3c04f4b47041 | 101 | address = FramLoad( 8, hallRom, 0); if (address < 0) return -1; iHallRom = address; |
andrewboyson | 0:3c04f4b47041 | 102 | def4 = 15; address = FramLoad( 4, &nightTemperature, &def4); if (address < 0) return -1; iNightTemperature = address; |
andrewboyson | 0:3c04f4b47041 | 103 | def4 = 8; address = FramLoad( 4, &frostTemperature, &def4); if (address < 0) return -1; iFrostTemperature = address; |
andrewboyson | 0:3c04f4b47041 | 104 | |
andrewboyson | 0:3c04f4b47041 | 105 | LPC_GPIO2->FIODIR |= RADIATOR_PUMP_MASK; //Set the direction to 1 == output |
andrewboyson | 0:3c04f4b47041 | 106 | LPC_GPIO0->FIODIR |= HALL_LED_MASK; //Set the direction to 1 == output |
andrewboyson | 0:3c04f4b47041 | 107 | |
andrewboyson | 0:3c04f4b47041 | 108 | return 0; |
andrewboyson | 0:3c04f4b47041 | 109 | } |
andrewboyson | 0:3c04f4b47041 | 110 | void RadiatorMain() |
andrewboyson | 0:3c04f4b47041 | 111 | { |
andrewboyson | 0:3c04f4b47041 | 112 | //deal with pushbutton |
andrewboyson | 0:3c04f4b47041 | 113 | static bool prevHallPb = false; |
andrewboyson | 0:3c04f4b47041 | 114 | bool thisHallPb = LPC_GPIO0->FIOPIN & HALL_PB_MASK; |
andrewboyson | 0:3c04f4b47041 | 115 | bool hallPbPressed = thisHallPb && !prevHallPb; |
andrewboyson | 0:3c04f4b47041 | 116 | prevHallPb = thisHallPb; |
andrewboyson | 0:3c04f4b47041 | 117 | if (hallPbPressed) RadiatorChgOverride(); |
andrewboyson | 0:3c04f4b47041 | 118 | |
andrewboyson | 0:3c04f4b47041 | 119 | //Make the radiator output |
andrewboyson | 0:3c04f4b47041 | 120 | makeOutputBeforeOverride(); |
andrewboyson | 0:3c04f4b47041 | 121 | adjustOverride(); |
andrewboyson | 0:3c04f4b47041 | 122 | makeOutputWithOverride(); |
andrewboyson | 0:3c04f4b47041 | 123 | |
andrewboyson | 0:3c04f4b47041 | 124 | //Pump output |
andrewboyson | 0:3c04f4b47041 | 125 | if (RadiatorPump) LPC_GPIO2->FIOSET = RADIATOR_PUMP_MASK; |
andrewboyson | 0:3c04f4b47041 | 126 | else LPC_GPIO2->FIOCLR = RADIATOR_PUMP_MASK; |
andrewboyson | 0:3c04f4b47041 | 127 | |
andrewboyson | 0:3c04f4b47041 | 128 | |
andrewboyson | 0:3c04f4b47041 | 129 | //Display the led |
andrewboyson | 0:3c04f4b47041 | 130 | if (RadiatorPump) LPC_GPIO0->FIOSET = HALL_LED_MASK; |
andrewboyson | 0:3c04f4b47041 | 131 | else LPC_GPIO0->FIOCLR = HALL_LED_MASK; |
andrewboyson | 0:3c04f4b47041 | 132 | } |