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