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@7:a62134646ac8, 2018-03-20 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Mar 20 18:34:53 2018 +0000
- Revision:
- 7:a62134646ac8
- Parent:
- 6:b325de442777
- Child:
- 35:bb8a6d1c034c
Libraries
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 | 5:82197a6997fd | 5 | #include "gpio.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 | 6:b325de442777 | 10 | #include "debounce.h" |
andrewboyson | 5:82197a6997fd | 11 | |
andrewboyson | 5:82197a6997fd | 12 | #define HALL_LED_DIR FIO0DIR(10) // P0.10 == p28; |
andrewboyson | 5:82197a6997fd | 13 | #define HALL_LED_PIN FIO0PIN(10) |
andrewboyson | 5:82197a6997fd | 14 | #define HALL_LED_SET FIO0SET(10) |
andrewboyson | 5:82197a6997fd | 15 | #define HALL_LED_CLR FIO0CLR(10) |
andrewboyson | 5:82197a6997fd | 16 | |
andrewboyson | 5:82197a6997fd | 17 | #define RADIATOR_PUMP_DIR FIO2DIR(03) // P2.03 == p23; |
andrewboyson | 5:82197a6997fd | 18 | #define RADIATOR_PUMP_PIN FIO2PIN(03) |
andrewboyson | 5:82197a6997fd | 19 | #define RADIATOR_PUMP_SET FIO2SET(03) |
andrewboyson | 5:82197a6997fd | 20 | #define RADIATOR_PUMP_CLR FIO2CLR(03) |
andrewboyson | 0:3c04f4b47041 | 21 | |
andrewboyson | 0:3c04f4b47041 | 22 | static char htgMode; static int iMode; |
andrewboyson | 0:3c04f4b47041 | 23 | static char htgOverride; static int iOverride; |
andrewboyson | 0:3c04f4b47041 | 24 | static char hallRom[8]; static int iHallRom; |
andrewboyson | 0:3c04f4b47041 | 25 | static int32_t nightTemperature; static int iNightTemperature; |
andrewboyson | 0:3c04f4b47041 | 26 | static int32_t frostTemperature; static int iFrostTemperature; |
andrewboyson | 0:3c04f4b47041 | 27 | |
andrewboyson | 0:3c04f4b47041 | 28 | bool RadiatorGetMode (){ return (bool)htgMode; } |
andrewboyson | 0:3c04f4b47041 | 29 | bool RadiatorGetOverride (){ return (bool)htgOverride; } |
andrewboyson | 0:3c04f4b47041 | 30 | char* RadiatorGetHallRom (){ return hallRom; } |
andrewboyson | 0:3c04f4b47041 | 31 | int RadiatorGetNightTemperature(){ return (int)nightTemperature; } |
andrewboyson | 0:3c04f4b47041 | 32 | int RadiatorGetFrostTemperature(){ return (int)frostTemperature; } |
andrewboyson | 0:3c04f4b47041 | 33 | |
andrewboyson | 0:3c04f4b47041 | 34 | static void setMode ( bool value) { htgMode = (char)value; FramWrite(iMode, 1, &htgMode ); } |
andrewboyson | 0:3c04f4b47041 | 35 | static void setOverride ( bool value) { htgOverride = (char)value; FramWrite(iOverride, 1, &htgOverride ); } |
andrewboyson | 0:3c04f4b47041 | 36 | void RadiatorSetHallRom (char* value) { memcpy(hallRom, value, 8); FramWrite(iHallRom, 8, hallRom ); } |
andrewboyson | 0:3c04f4b47041 | 37 | void RadiatorSetNightTemperature ( int value) { nightTemperature = (int32_t)value; FramWrite(iNightTemperature, 4, &nightTemperature); } |
andrewboyson | 0:3c04f4b47041 | 38 | void RadiatorSetFrostTemperature ( int value) { frostTemperature = (int32_t)value; FramWrite(iFrostTemperature, 4, &frostTemperature); } |
andrewboyson | 0:3c04f4b47041 | 39 | |
andrewboyson | 0:3c04f4b47041 | 40 | static bool outputBeforeOverride = false; |
andrewboyson | 0:3c04f4b47041 | 41 | static void makeOutputBeforeOverride() |
andrewboyson | 0:3c04f4b47041 | 42 | { |
andrewboyson | 0:3c04f4b47041 | 43 | //See if the temperature is too low |
andrewboyson | 0:3c04f4b47041 | 44 | int hallTemp16ths = DS18B20ValueFromRom(hallRom); |
andrewboyson | 0:3c04f4b47041 | 45 | int nightTemp16ths = nightTemperature << 4; |
andrewboyson | 0:3c04f4b47041 | 46 | int frostTemp16ths = frostTemperature << 4; |
andrewboyson | 0:3c04f4b47041 | 47 | |
andrewboyson | 0:3c04f4b47041 | 48 | static bool tooCold = false; //This is static to ride through invalid temperature reads |
andrewboyson | 0:3c04f4b47041 | 49 | if (DS18B20IsValidValue(hallTemp16ths)) |
andrewboyson | 0:3c04f4b47041 | 50 | { |
andrewboyson | 0:3c04f4b47041 | 51 | tooCold = hallTemp16ths < frostTemp16ths; |
andrewboyson | 0:3c04f4b47041 | 52 | if (htgMode) tooCold |= hallTemp16ths < nightTemp16ths; |
andrewboyson | 0:3c04f4b47041 | 53 | } |
andrewboyson | 0:3c04f4b47041 | 54 | |
andrewboyson | 0:3c04f4b47041 | 55 | outputBeforeOverride = htgMode && ProgramTimerOutput || tooCold; |
andrewboyson | 0:3c04f4b47041 | 56 | } |
andrewboyson | 0:3c04f4b47041 | 57 | static void adjustOverride() |
andrewboyson | 0:3c04f4b47041 | 58 | { |
andrewboyson | 0:3c04f4b47041 | 59 | static bool previousOutput = false; |
andrewboyson | 0:3c04f4b47041 | 60 | if (previousOutput != outputBeforeOverride) setOverride(false); |
andrewboyson | 0:3c04f4b47041 | 61 | previousOutput = outputBeforeOverride; |
andrewboyson | 0:3c04f4b47041 | 62 | } |
andrewboyson | 0:3c04f4b47041 | 63 | bool RadiatorPump = false; |
andrewboyson | 0:3c04f4b47041 | 64 | static void makeOutputWithOverride() |
andrewboyson | 0:3c04f4b47041 | 65 | { |
andrewboyson | 0:3c04f4b47041 | 66 | RadiatorPump = htgOverride ? !outputBeforeOverride : outputBeforeOverride ; |
andrewboyson | 0:3c04f4b47041 | 67 | } |
andrewboyson | 0:3c04f4b47041 | 68 | |
andrewboyson | 0:3c04f4b47041 | 69 | void RadiatorSetMode(bool value) //Summer is false, Winter is true |
andrewboyson | 0:3c04f4b47041 | 70 | { |
andrewboyson | 0:3c04f4b47041 | 71 | if (htgMode == (char)value) return; //Ignore no change |
andrewboyson | 0:3c04f4b47041 | 72 | setMode(value); //Change to the new value |
andrewboyson | 0:3c04f4b47041 | 73 | |
andrewboyson | 0:3c04f4b47041 | 74 | bool prevOutputBeforeOverride = outputBeforeOverride; |
andrewboyson | 0:3c04f4b47041 | 75 | makeOutputBeforeOverride(); |
andrewboyson | 0:3c04f4b47041 | 76 | |
andrewboyson | 0:3c04f4b47041 | 77 | if (htgOverride) //Only deal with an override that is already set; if it wasn't set don't change it |
andrewboyson | 0:3c04f4b47041 | 78 | { |
andrewboyson | 0:3c04f4b47041 | 79 | if (htgMode) //Summer -> Winter |
andrewboyson | 0:3c04f4b47041 | 80 | { |
andrewboyson | 0:3c04f4b47041 | 81 | if (outputBeforeOverride != prevOutputBeforeOverride) setOverride(0); //Adjust the override to leave the heat as it was - off or on. |
andrewboyson | 0:3c04f4b47041 | 82 | } |
andrewboyson | 0:3c04f4b47041 | 83 | else //Winter -> Summer |
andrewboyson | 0:3c04f4b47041 | 84 | { |
andrewboyson | 0:3c04f4b47041 | 85 | setOverride(0); //turn off the heat. |
andrewboyson | 0:3c04f4b47041 | 86 | } |
andrewboyson | 0:3c04f4b47041 | 87 | } |
andrewboyson | 0:3c04f4b47041 | 88 | |
andrewboyson | 0:3c04f4b47041 | 89 | makeOutputWithOverride(); |
andrewboyson | 0:3c04f4b47041 | 90 | } |
andrewboyson | 0:3c04f4b47041 | 91 | void RadiatorSetOverride(bool value) |
andrewboyson | 0:3c04f4b47041 | 92 | { |
andrewboyson | 0:3c04f4b47041 | 93 | setOverride(value); |
andrewboyson | 0:3c04f4b47041 | 94 | makeOutputBeforeOverride(); |
andrewboyson | 0:3c04f4b47041 | 95 | makeOutputWithOverride(); } |
andrewboyson | 0:3c04f4b47041 | 96 | |
andrewboyson | 0:3c04f4b47041 | 97 | void RadiatorChgMode (){ RadiatorSetMode (!RadiatorGetMode ()); } |
andrewboyson | 0:3c04f4b47041 | 98 | void RadiatorChgOverride(){ RadiatorSetOverride(!RadiatorGetOverride()); } |
andrewboyson | 0:3c04f4b47041 | 99 | |
andrewboyson | 0:3c04f4b47041 | 100 | int RadiatorInit() |
andrewboyson | 0:3c04f4b47041 | 101 | { |
andrewboyson | 0:3c04f4b47041 | 102 | int address; |
andrewboyson | 0:3c04f4b47041 | 103 | char def1; |
andrewboyson | 0:3c04f4b47041 | 104 | int32_t def4; |
andrewboyson | 0:3c04f4b47041 | 105 | def1 = 0; address = FramLoad( 1, &htgMode, &def1); if (address < 0) return -1; iMode = address; |
andrewboyson | 0:3c04f4b47041 | 106 | def1 = 0; address = FramLoad( 1, &htgOverride, &def1); if (address < 0) return -1; iOverride = address; |
andrewboyson | 0:3c04f4b47041 | 107 | address = FramLoad( 8, hallRom, 0); if (address < 0) return -1; iHallRom = address; |
andrewboyson | 0:3c04f4b47041 | 108 | def4 = 15; address = FramLoad( 4, &nightTemperature, &def4); if (address < 0) return -1; iNightTemperature = address; |
andrewboyson | 0:3c04f4b47041 | 109 | def4 = 8; address = FramLoad( 4, &frostTemperature, &def4); if (address < 0) return -1; iFrostTemperature = address; |
andrewboyson | 0:3c04f4b47041 | 110 | |
andrewboyson | 5:82197a6997fd | 111 | RADIATOR_PUMP_DIR = 1; //Set the direction to 1 == output |
andrewboyson | 5:82197a6997fd | 112 | HALL_LED_DIR = 1; //Set the direction to 1 == output |
andrewboyson | 0:3c04f4b47041 | 113 | |
andrewboyson | 0:3c04f4b47041 | 114 | return 0; |
andrewboyson | 0:3c04f4b47041 | 115 | } |
andrewboyson | 0:3c04f4b47041 | 116 | void RadiatorMain() |
andrewboyson | 0:3c04f4b47041 | 117 | { |
andrewboyson | 0:3c04f4b47041 | 118 | //deal with pushbutton |
andrewboyson | 6:b325de442777 | 119 | static bool prevOn = false; |
andrewboyson | 7:a62134646ac8 | 120 | bool thisOn = DebounceHallPbPressed(); |
andrewboyson | 6:b325de442777 | 121 | if (thisOn && !prevOn) RadiatorChgOverride(); |
andrewboyson | 6:b325de442777 | 122 | prevOn = thisOn; |
andrewboyson | 0:3c04f4b47041 | 123 | |
andrewboyson | 0:3c04f4b47041 | 124 | //Make the radiator output |
andrewboyson | 0:3c04f4b47041 | 125 | makeOutputBeforeOverride(); |
andrewboyson | 0:3c04f4b47041 | 126 | adjustOverride(); |
andrewboyson | 0:3c04f4b47041 | 127 | makeOutputWithOverride(); |
andrewboyson | 0:3c04f4b47041 | 128 | |
andrewboyson | 0:3c04f4b47041 | 129 | //Pump output |
andrewboyson | 5:82197a6997fd | 130 | if (RadiatorPump) RADIATOR_PUMP_SET; |
andrewboyson | 5:82197a6997fd | 131 | else RADIATOR_PUMP_CLR; |
andrewboyson | 0:3c04f4b47041 | 132 | |
andrewboyson | 0:3c04f4b47041 | 133 | |
andrewboyson | 0:3c04f4b47041 | 134 | //Display the led |
andrewboyson | 5:82197a6997fd | 135 | if (RadiatorPump) HALL_LED_SET; |
andrewboyson | 5:82197a6997fd | 136 | else HALL_LED_CLR; |
andrewboyson | 0:3c04f4b47041 | 137 | } |