![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Control Code with I/O and ADC working
main.cpp@9:8cd14861dc63, 2018-04-02 (annotated)
- Committer:
- jrodenburg
- Date:
- Mon Apr 02 00:00:04 2018 +0000
- Revision:
- 9:8cd14861dc63
- Parent:
- 8:dbf8bd4815f8
- Child:
- 11:fe6c5aac5d89
Code before adding other RX/TX channels
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jrodenburg | 4:168a446bd0da | 1 | // MBED SCRIPT FOR CONTROLLING THE TEMPERATURE CONTROLLED TEST FIXTURE (TCTF) |
jrodenburg | 0:a28a1035c31b | 2 | // DATE: SEPTEMBER 2017 |
jrodenburg | 0:a28a1035c31b | 3 | |
jrodenburg | 0:a28a1035c31b | 4 | #include "mbed.h" |
jrodenburg | 0:a28a1035c31b | 5 | #include "MODSERIAL.h" |
jrodenburg | 8:dbf8bd4815f8 | 6 | #include "MCP23008.h" |
jrodenburg | 0:a28a1035c31b | 7 | #include "LTC2487.h" |
jrodenburg | 2:bd118a724f03 | 8 | #include <string> |
jrodenburg | 0:a28a1035c31b | 9 | |
jrodenburg | 0:a28a1035c31b | 10 | //DEFINITIVE VARIABLES |
jrodenburg | 8:dbf8bd4815f8 | 11 | #define DEBUG 0 |
jrodenburg | 9:8cd14861dc63 | 12 | #define DEBUG1 0 |
jrodenburg | 9:8cd14861dc63 | 13 | #define DEBUG2 0 |
jrodenburg | 8:dbf8bd4815f8 | 14 | #define DEBUG3 1 |
jrodenburg | 8:dbf8bd4815f8 | 15 | #define CHN_COUNT 8 |
jrodenburg | 8:dbf8bd4815f8 | 16 | #define MIN_TEMP 15 |
jrodenburg | 8:dbf8bd4815f8 | 17 | #define MAX_TEMP 60 |
jrodenburg | 8:dbf8bd4815f8 | 18 | #define TEMP_MARGIN 5 |
jrodenburg | 8:dbf8bd4815f8 | 19 | #define HYST_LOW 0.3 |
jrodenburg | 8:dbf8bd4815f8 | 20 | #define HYST_HIGH 1 |
jrodenburg | 8:dbf8bd4815f8 | 21 | #define SAMPLES 5 |
jrodenburg | 8:dbf8bd4815f8 | 22 | #define I2C_Freq 2000 |
jrodenburg | 8:dbf8bd4815f8 | 23 | #define VALVE 1 |
jrodenburg | 8:dbf8bd4815f8 | 24 | #define HEATER 2 |
jrodenburg | 8:dbf8bd4815f8 | 25 | #define STATUS_GOOD 3 |
jrodenburg | 8:dbf8bd4815f8 | 26 | #define STATUS_BAD 4 |
jrodenburg | 8:dbf8bd4815f8 | 27 | #define sizeLUT 34 |
jrodenburg | 8:dbf8bd4815f8 | 28 | #define FRONT_THERM 0 |
jrodenburg | 8:dbf8bd4815f8 | 29 | #define BACK_THERM 1 |
jrodenburg | 8:dbf8bd4815f8 | 30 | #define HEAT_FET_AMP 2 |
jrodenburg | 7:8a5e65e63e2a | 31 | #define VALVE_FET_AMP 3 |
jrodenburg | 8:dbf8bd4815f8 | 32 | #define FET_ON_CURRENT 1.12 |
jrodenburg | 9:8cd14861dc63 | 33 | #define ROOM_TEMP 22 |
jrodenburg | 9:8cd14861dc63 | 34 | |
jrodenburg | 8:dbf8bd4815f8 | 35 | // Defines for use with Serial communication |
jrodenburg | 8:dbf8bd4815f8 | 36 | #pragma pack (1) |
jrodenburg | 8:dbf8bd4815f8 | 37 | #define RX_SOF 0x7B |
jrodenburg | 8:dbf8bd4815f8 | 38 | #define RX_EOF 0x7D |
jrodenburg | 8:dbf8bd4815f8 | 39 | #define TX_SOF 0x7B |
jrodenburg | 8:dbf8bd4815f8 | 40 | #define TX_EOF 0x7D |
jrodenburg | 8:dbf8bd4815f8 | 41 | #define DELIMETER 0x2E |
jrodenburg | 8:dbf8bd4815f8 | 42 | #define SET_TEMPERATURE 0xB0 |
jrodenburg | 8:dbf8bd4815f8 | 43 | #define SELECT_CHANNEL 0xB1 |
jrodenburg | 8:dbf8bd4815f8 | 44 | #define CMD_RESPONSE 0xD0 |
jrodenburg | 8:dbf8bd4815f8 | 45 | #define CMD_DATA 0xD1 |
jrodenburg | 0:a28a1035c31b | 46 | |
jrodenburg | 9:8cd14861dc63 | 47 | |
jrodenburg | 8:dbf8bd4815f8 | 48 | unsigned int chanSel_SendTemp = 0; |
jrodenburg | 8:dbf8bd4815f8 | 49 | unsigned int chanSel_SetTemp = 0; |
jrodenburg | 8:dbf8bd4815f8 | 50 | // Default to chan 0 |
jrodenburg | 8:dbf8bd4815f8 | 51 | int currChan = 0; |
jrodenburg | 8:dbf8bd4815f8 | 52 | bool newTempSet = false; |
jrodenburg | 8:dbf8bd4815f8 | 53 | |
jrodenburg | 9:8cd14861dc63 | 54 | |
jrodenburg | 8:dbf8bd4815f8 | 55 | //*********************************************** |
jrodenburg | 8:dbf8bd4815f8 | 56 | // Serial Rx Packet Format |
jrodenburg | 8:dbf8bd4815f8 | 57 | //*********************************************** |
jrodenburg | 8:dbf8bd4815f8 | 58 | typedef struct { |
jrodenburg | 8:dbf8bd4815f8 | 59 | unsigned char SOF_flag; |
jrodenburg | 8:dbf8bd4815f8 | 60 | unsigned char cmd_type; |
jrodenburg | 8:dbf8bd4815f8 | 61 | unsigned char len; |
jrodenburg | 8:dbf8bd4815f8 | 62 | unsigned char Delim; |
jrodenburg | 8:dbf8bd4815f8 | 63 | } HOST_CMD_HEADER; |
jrodenburg | 8:dbf8bd4815f8 | 64 | |
jrodenburg | 8:dbf8bd4815f8 | 65 | typedef struct { |
jrodenburg | 8:dbf8bd4815f8 | 66 | HOST_CMD_HEADER cmd_header; |
jrodenburg | 8:dbf8bd4815f8 | 67 | unsigned char chanID; |
jrodenburg | 8:dbf8bd4815f8 | 68 | unsigned char tempDelim; |
jrodenburg | 8:dbf8bd4815f8 | 69 | float setTemp; |
jrodenburg | 8:dbf8bd4815f8 | 70 | unsigned char chanStatDelim; |
jrodenburg | 8:dbf8bd4815f8 | 71 | unsigned char chanStat; |
jrodenburg | 8:dbf8bd4815f8 | 72 | } SET_TEMPERATURE_CMD; |
jrodenburg | 8:dbf8bd4815f8 | 73 | |
jrodenburg | 8:dbf8bd4815f8 | 74 | typedef struct { |
jrodenburg | 8:dbf8bd4815f8 | 75 | HOST_CMD_HEADER cmd_header; |
jrodenburg | 8:dbf8bd4815f8 | 76 | unsigned char chanIDSel; |
jrodenburg | 8:dbf8bd4815f8 | 77 | unsigned char chanDelim; |
jrodenburg | 8:dbf8bd4815f8 | 78 | unsigned char chanStat; |
jrodenburg | 8:dbf8bd4815f8 | 79 | } SELECT_CHANNEL_CMD; |
jrodenburg | 8:dbf8bd4815f8 | 80 | |
jrodenburg | 8:dbf8bd4815f8 | 81 | typedef struct { |
jrodenburg | 8:dbf8bd4815f8 | 82 | unsigned char SOF_flag; |
jrodenburg | 8:dbf8bd4815f8 | 83 | unsigned char cmd_type; |
jrodenburg | 8:dbf8bd4815f8 | 84 | unsigned char len; |
jrodenburg | 8:dbf8bd4815f8 | 85 | unsigned char Delim; |
jrodenburg | 8:dbf8bd4815f8 | 86 | float data; |
jrodenburg | 8:dbf8bd4815f8 | 87 | unsigned char EOF_flag; |
jrodenburg | 8:dbf8bd4815f8 | 88 | } RESPONSE_CMD; |
jrodenburg | 2:bd118a724f03 | 89 | |
jrodenburg | 2:bd118a724f03 | 90 | //TCTF CHANNEL DATA |
jrodenburg | 2:bd118a724f03 | 91 | struct CHNL_DATA{ |
jrodenburg | 2:bd118a724f03 | 92 | bool status; |
jrodenburg | 2:bd118a724f03 | 93 | float setTemp; |
jrodenburg | 7:8a5e65e63e2a | 94 | bool error; |
jrodenburg | 2:bd118a724f03 | 95 | }; |
jrodenburg | 2:bd118a724f03 | 96 | |
jrodenburg | 4:168a446bd0da | 97 | CHNL_DATA chnlStatus[] = { |
jrodenburg | 8:dbf8bd4815f8 | 98 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 99 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 100 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 101 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 102 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 103 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 104 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 105 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 106 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 107 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 108 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 109 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 110 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 111 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 112 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 113 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 114 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 115 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 116 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 117 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 118 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 119 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 120 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 121 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 122 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 123 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 124 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 125 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 126 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 127 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 128 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 129 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 130 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 131 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 132 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 133 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 134 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 135 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 136 | {0, NULL, 0}, |
jrodenburg | 7:8a5e65e63e2a | 137 | {0, NULL, 0}, |
jrodenburg | 9:8cd14861dc63 | 138 | {0, NULL, 0}, |
jrodenburg | 9:8cd14861dc63 | 139 | {0, NULL, 0}, |
jrodenburg | 9:8cd14861dc63 | 140 | {0, NULL, 0}, |
jrodenburg | 9:8cd14861dc63 | 141 | {0, NULL, 0}, |
jrodenburg | 9:8cd14861dc63 | 142 | {0, NULL, 0}, |
jrodenburg | 9:8cd14861dc63 | 143 | {0, NULL, 0}, |
jrodenburg | 9:8cd14861dc63 | 144 | {0, NULL, 0}, |
jrodenburg | 9:8cd14861dc63 | 145 | {0, NULL, 0}, |
jrodenburg | 2:bd118a724f03 | 146 | }; |
jrodenburg | 2:bd118a724f03 | 147 | |
jrodenburg | 2:bd118a724f03 | 148 | |
jrodenburg | 0:a28a1035c31b | 149 | //I2C AADRESS LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP) |
jrodenburg | 2:bd118a724f03 | 150 | struct I2C_ADDR_LUT{ |
jrodenburg | 0:a28a1035c31b | 151 | int adc; |
jrodenburg | 0:a28a1035c31b | 152 | int io; |
jrodenburg | 0:a28a1035c31b | 153 | }; |
jrodenburg | 0:a28a1035c31b | 154 | |
jrodenburg | 0:a28a1035c31b | 155 | I2C_ADDR_LUT addrLUT[] = { |
jrodenburg | 0:a28a1035c31b | 156 | {0x23, 0x10}, |
jrodenburg | 0:a28a1035c31b | 157 | {0x53, 0x60}, |
jrodenburg | 0:a28a1035c31b | 158 | {0x43, 0x70}, |
jrodenburg | 0:a28a1035c31b | 159 | {0x73, 0x40}, |
jrodenburg | 0:a28a1035c31b | 160 | {0x63, 0x50}, |
jrodenburg | 0:a28a1035c31b | 161 | {0x22, 0x11}, |
jrodenburg | 0:a28a1035c31b | 162 | {0x52, 0x61}, |
jrodenburg | 0:a28a1035c31b | 163 | {0x42, 0x71}, |
jrodenburg | 0:a28a1035c31b | 164 | {0x72, 0x41}, |
jrodenburg | 0:a28a1035c31b | 165 | {0x62, 0x51}, |
jrodenburg | 0:a28a1035c31b | 166 | {0x21, 0x12}, |
jrodenburg | 0:a28a1035c31b | 167 | {0x51, 0x62}, |
jrodenburg | 0:a28a1035c31b | 168 | {0x41, 0x72}, |
jrodenburg | 0:a28a1035c31b | 169 | {0x71, 0x42}, |
jrodenburg | 0:a28a1035c31b | 170 | {0x61, 0x52}, |
jrodenburg | 0:a28a1035c31b | 171 | {0x20, 0x13}, |
jrodenburg | 0:a28a1035c31b | 172 | {0x50, 0x63}, |
jrodenburg | 0:a28a1035c31b | 173 | {0x40, 0x73}, |
jrodenburg | 0:a28a1035c31b | 174 | {0x70, 0x43}, |
jrodenburg | 0:a28a1035c31b | 175 | {0x60, 0x53}, |
jrodenburg | 0:a28a1035c31b | 176 | {0x26, 0x14}, |
jrodenburg | 0:a28a1035c31b | 177 | {0x56, 0x64}, |
jrodenburg | 0:a28a1035c31b | 178 | {0x46, 0x74}, |
jrodenburg | 0:a28a1035c31b | 179 | {0x76, 0x44}, |
jrodenburg | 0:a28a1035c31b | 180 | {0x66, 0x54}, |
jrodenburg | 0:a28a1035c31b | 181 | {0x2C, 0x1F}, |
jrodenburg | 0:a28a1035c31b | 182 | {0x5C, 0x6F}, |
jrodenburg | 0:a28a1035c31b | 183 | {0x4C, 0x7F}, |
jrodenburg | 0:a28a1035c31b | 184 | {0x7C, 0x4F}, |
jrodenburg | 0:a28a1035c31b | 185 | {0x6C, 0x5F}, |
jrodenburg | 0:a28a1035c31b | 186 | {0x2D, 0x1E}, |
jrodenburg | 0:a28a1035c31b | 187 | {0x5D, 0x6E}, |
jrodenburg | 0:a28a1035c31b | 188 | {0x4D, 0x7E}, |
jrodenburg | 0:a28a1035c31b | 189 | {0x7D, 0x4E}, |
jrodenburg | 0:a28a1035c31b | 190 | {0x6D, 0x5E}, |
jrodenburg | 0:a28a1035c31b | 191 | {0x2E, 0x1D}, |
jrodenburg | 0:a28a1035c31b | 192 | {0x5E, 0x6D}, |
jrodenburg | 0:a28a1035c31b | 193 | {0x4E, 0x7D}, |
jrodenburg | 0:a28a1035c31b | 194 | {0x7E, 0x4D}, |
jrodenburg | 0:a28a1035c31b | 195 | {0x6E, 0x5D}, |
jrodenburg | 0:a28a1035c31b | 196 | {0x2F, 0x1C}, |
jrodenburg | 0:a28a1035c31b | 197 | {0x5F, 0x6C}, |
jrodenburg | 0:a28a1035c31b | 198 | {0x4F, 0x7C}, |
jrodenburg | 0:a28a1035c31b | 199 | {0x7F, 0x4C}, |
jrodenburg | 0:a28a1035c31b | 200 | {0x6F, 0x5C}, |
jrodenburg | 0:a28a1035c31b | 201 | {0x20, 0x1B}, |
jrodenburg | 0:a28a1035c31b | 202 | {0x50, 0x6B}, |
jrodenburg | 9:8cd14861dc63 | 203 | {0x40, 0x7B}, |
jrodenburg | 0:a28a1035c31b | 204 | }; |
jrodenburg | 0:a28a1035c31b | 205 | |
jrodenburg | 0:a28a1035c31b | 206 | //THERMISTOR LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP) |
jrodenburg | 2:bd118a724f03 | 207 | struct THERM_LUT{ |
jrodenburg | 0:a28a1035c31b | 208 | int adc; |
jrodenburg | 0:a28a1035c31b | 209 | int temp; |
jrodenburg | 0:a28a1035c31b | 210 | }; |
jrodenburg | 0:a28a1035c31b | 211 | |
jrodenburg | 0:a28a1035c31b | 212 | THERM_LUT thermLUT[] = { |
jrodenburg | 0:a28a1035c31b | 213 | {113779,-40}, |
jrodenburg | 0:a28a1035c31b | 214 | {109152,-35}, |
jrodenburg | 0:a28a1035c31b | 215 | {103830,-30}, |
jrodenburg | 0:a28a1035c31b | 216 | {97855,-25}, |
jrodenburg | 0:a28a1035c31b | 217 | {91319,-20}, |
jrodenburg | 0:a28a1035c31b | 218 | {84352,-15}, |
jrodenburg | 0:a28a1035c31b | 219 | {77124,-10}, |
jrodenburg | 0:a28a1035c31b | 220 | {69820,-5}, |
jrodenburg | 0:a28a1035c31b | 221 | {62621,0}, |
jrodenburg | 0:a28a1035c31b | 222 | {55693,5}, |
jrodenburg | 0:a28a1035c31b | 223 | {49169,10}, |
jrodenburg | 0:a28a1035c31b | 224 | {43144,15}, |
jrodenburg | 0:a28a1035c31b | 225 | {37669,20}, |
jrodenburg | 0:a28a1035c31b | 226 | {32768,25}, |
jrodenburg | 0:a28a1035c31b | 227 | {28429,30}, |
jrodenburg | 0:a28a1035c31b | 228 | {24622,35}, |
jrodenburg | 0:a28a1035c31b | 229 | {21309,40}, |
jrodenburg | 0:a28a1035c31b | 230 | {18439,45}, |
jrodenburg | 0:a28a1035c31b | 231 | {15962,50}, |
jrodenburg | 0:a28a1035c31b | 232 | {13831,55}, |
jrodenburg | 0:a28a1035c31b | 233 | {12002,60}, |
jrodenburg | 0:a28a1035c31b | 234 | {10428,65}, |
jrodenburg | 0:a28a1035c31b | 235 | {9080,70}, |
jrodenburg | 0:a28a1035c31b | 236 | {7919,75}, |
jrodenburg | 0:a28a1035c31b | 237 | {6923,80}, |
jrodenburg | 0:a28a1035c31b | 238 | {6063,85}, |
jrodenburg | 0:a28a1035c31b | 239 | {5323,90}, |
jrodenburg | 0:a28a1035c31b | 240 | {4685,95}, |
jrodenburg | 0:a28a1035c31b | 241 | {4130,100}, |
jrodenburg | 0:a28a1035c31b | 242 | {3653,105}, |
jrodenburg | 0:a28a1035c31b | 243 | {3234,110}, |
jrodenburg | 0:a28a1035c31b | 244 | {2876,115}, |
jrodenburg | 0:a28a1035c31b | 245 | {2563,120}, |
jrodenburg | 0:a28a1035c31b | 246 | {2284,125} |
jrodenburg | 0:a28a1035c31b | 247 | }; |
jrodenburg | 0:a28a1035c31b | 248 | |
jrodenburg | 2:bd118a724f03 | 249 | //SERIAL COMMUNICATION SETUP |
jrodenburg | 2:bd118a724f03 | 250 | MODSERIAL pc(USBTX, USBRX); |
jrodenburg | 9:8cd14861dc63 | 251 | MODSERIAL chain(PTA2, PTA1); |
jrodenburg | 1:0182b86f9bd4 | 252 | |
jrodenburg | 0:a28a1035c31b | 253 | //DEFINE PINS |
jrodenburg | 0:a28a1035c31b | 254 | DigitalOut myled(LED2); |
jrodenburg | 0:a28a1035c31b | 255 | |
jrodenburg | 0:a28a1035c31b | 256 | //I2C FOR MCP23008 (I/O Control) |
jrodenburg | 9:8cd14861dc63 | 257 | MCP23008 io_control(PTC9, PTC8, 0x10, 100000); //sda, scl |
jrodenburg | 0:a28a1035c31b | 258 | |
jrodenburg | 0:a28a1035c31b | 259 | //I2C FOR LTC2487 (ADC Control) |
jrodenburg | 9:8cd14861dc63 | 260 | LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000); //sda, scl |
jrodenburg | 0:a28a1035c31b | 261 | |
jrodenburg | 0:a28a1035c31b | 262 | //GLOBAL VARIABLES |
jrodenburg | 9:8cd14861dc63 | 263 | volatile bool dataReceived = false; //used to check if data has been received |
jrodenburg | 8:dbf8bd4815f8 | 264 | volatile bool dataTxReady = false; |
jrodenburg | 2:bd118a724f03 | 265 | char rxBuf[50]; |
jrodenburg | 2:bd118a724f03 | 266 | int chnlSel; |
jrodenburg | 0:a28a1035c31b | 267 | |
jrodenburg | 7:8a5e65e63e2a | 268 | |
jrodenburg | 7:8a5e65e63e2a | 269 | /* Function: turnOffChannel |
jrodenburg | 7:8a5e65e63e2a | 270 | ************************************************************** |
jrodenburg | 7:8a5e65e63e2a | 271 | Description: Turns off a channel |
jrodenburg | 7:8a5e65e63e2a | 272 | Recieves: chnl: channel to turn off |
jrodenburg | 7:8a5e65e63e2a | 273 | Returns: N/A |
jrodenburg | 7:8a5e65e63e2a | 274 | */ |
jrodenburg | 7:8a5e65e63e2a | 275 | |
jrodenburg | 7:8a5e65e63e2a | 276 | void turnOffChannel(int chnl){ |
jrodenburg | 7:8a5e65e63e2a | 277 | io_control.setAddress(addrLUT[chnl].io); |
jrodenburg | 7:8a5e65e63e2a | 278 | io_control.init(); |
jrodenburg | 7:8a5e65e63e2a | 279 | io_control.writeOutput(0,0,0,0); |
jrodenburg | 7:8a5e65e63e2a | 280 | } |
jrodenburg | 7:8a5e65e63e2a | 281 | |
jrodenburg | 8:dbf8bd4815f8 | 282 | |
jrodenburg | 2:bd118a724f03 | 283 | /* Function: rxInterrupt |
jrodenburg | 1:0182b86f9bd4 | 284 | ************************************************************** |
jrodenburg | 8:dbf8bd4815f8 | 285 | Description: serial rx interupt handler |
jrodenburg | 8:dbf8bd4815f8 | 286 | Receives: N/A |
jrodenburg | 8:dbf8bd4815f8 | 287 | Returns: N/A |
jrodenburg | 2:bd118a724f03 | 288 | */ |
jrodenburg | 2:bd118a724f03 | 289 | |
jrodenburg | 8:dbf8bd4815f8 | 290 | DigitalOut rLed(LED1); |
jrodenburg | 8:dbf8bd4815f8 | 291 | DigitalOut gLed(LED2); |
jrodenburg | 8:dbf8bd4815f8 | 292 | |
jrodenburg | 8:dbf8bd4815f8 | 293 | //*********************************************** |
jrodenburg | 8:dbf8bd4815f8 | 294 | // Rx Interrupt from serial Host interface |
jrodenburg | 8:dbf8bd4815f8 | 295 | //*********************************************** |
jrodenburg | 8:dbf8bd4815f8 | 296 | void rxInterrupt(MODSERIAL_IRQ_INFO *info) |
jrodenburg | 8:dbf8bd4815f8 | 297 | { |
jrodenburg | 8:dbf8bd4815f8 | 298 | gLed = 0; |
jrodenburg | 8:dbf8bd4815f8 | 299 | dataReceived = true; |
jrodenburg | 8:dbf8bd4815f8 | 300 | gLed = 1; |
jrodenburg | 2:bd118a724f03 | 301 | } |
jrodenburg | 2:bd118a724f03 | 302 | |
jrodenburg | 8:dbf8bd4815f8 | 303 | |
jrodenburg | 8:dbf8bd4815f8 | 304 | //*************************************************************** |
jrodenburg | 8:dbf8bd4815f8 | 305 | // Tx Interrupt from serial Host interface |
jrodenburg | 8:dbf8bd4815f8 | 306 | //*************************************************************** |
jrodenburg | 8:dbf8bd4815f8 | 307 | void txInterrupt(MODSERIAL_IRQ_INFO *info) |
jrodenburg | 8:dbf8bd4815f8 | 308 | { |
jrodenburg | 8:dbf8bd4815f8 | 309 | dataTxReady = true; |
jrodenburg | 8:dbf8bd4815f8 | 310 | } |
jrodenburg | 8:dbf8bd4815f8 | 311 | |
jrodenburg | 8:dbf8bd4815f8 | 312 | |
jrodenburg | 2:bd118a724f03 | 313 | /* Function: parseRXData |
jrodenburg | 2:bd118a724f03 | 314 | ************************************************************** |
jrodenburg | 9:8cd14861dc63 | 315 | Description: The parse received data into |
jrodenburg | 9:8cd14861dc63 | 316 | Receives: chn: The channel we want to put into the channel |
jrodenburg | 9:8cd14861dc63 | 317 | Returns: N/A |
jrodenburg | 1:0182b86f9bd4 | 318 | */ |
jrodenburg | 0:a28a1035c31b | 319 | |
jrodenburg | 8:dbf8bd4815f8 | 320 | void parseRXData() |
jrodenburg | 8:dbf8bd4815f8 | 321 | { |
jrodenburg | 8:dbf8bd4815f8 | 322 | HOST_CMD_HEADER *pRxPktHdr; |
jrodenburg | 2:bd118a724f03 | 323 | string data = ""; |
jrodenburg | 8:dbf8bd4815f8 | 324 | unsigned char *ptemp; |
jrodenburg | 8:dbf8bd4815f8 | 325 | int i; |
jrodenburg | 8:dbf8bd4815f8 | 326 | |
jrodenburg | 8:dbf8bd4815f8 | 327 | pRxPktHdr = (HOST_CMD_HEADER *)rxBuf; |
jrodenburg | 8:dbf8bd4815f8 | 328 | |
jrodenburg | 8:dbf8bd4815f8 | 329 | #ifdef DEBUG |
jrodenburg | 8:dbf8bd4815f8 | 330 | pc.printf("DBG: fl = %02x\n", pRxPktHdr->SOF_flag); |
jrodenburg | 8:dbf8bd4815f8 | 331 | pc.printf("DBG: len = %02x\n", pRxPktHdr->len); |
jrodenburg | 8:dbf8bd4815f8 | 332 | #endif |
jrodenburg | 8:dbf8bd4815f8 | 333 | |
jrodenburg | 8:dbf8bd4815f8 | 334 | // Exit if the packet does not contain correct header |
jrodenburg | 8:dbf8bd4815f8 | 335 | // Maybe send NAK? |
jrodenburg | 8:dbf8bd4815f8 | 336 | if ((pRxPktHdr->SOF_flag != RX_SOF) || (pRxPktHdr->Delim != DELIMETER)) |
jrodenburg | 8:dbf8bd4815f8 | 337 | return; |
jrodenburg | 2:bd118a724f03 | 338 | |
jrodenburg | 8:dbf8bd4815f8 | 339 | switch (pRxPktHdr->cmd_type) |
jrodenburg | 8:dbf8bd4815f8 | 340 | { |
jrodenburg | 8:dbf8bd4815f8 | 341 | case SET_TEMPERATURE: |
jrodenburg | 8:dbf8bd4815f8 | 342 | // Process set temp for specified channel |
jrodenburg | 8:dbf8bd4815f8 | 343 | { |
jrodenburg | 8:dbf8bd4815f8 | 344 | SET_TEMPERATURE_CMD *pRxPkt = (SET_TEMPERATURE_CMD *)(rxBuf); |
jrodenburg | 8:dbf8bd4815f8 | 345 | #if 1 //def DEBUG |
jrodenburg | 8:dbf8bd4815f8 | 346 | pc.printf("DBG: ch = %02x\n", pRxPkt->chanID); |
jrodenburg | 8:dbf8bd4815f8 | 347 | pc.printf("DBG: tempSet = %f\n", pRxPkt->setTemp); |
jrodenburg | 8:dbf8bd4815f8 | 348 | pc.printf("DBG: chanStat = %02x\n", pRxPkt->chanStat); |
jrodenburg | 8:dbf8bd4815f8 | 349 | #endif |
jrodenburg | 8:dbf8bd4815f8 | 350 | if ((pRxPkt->tempDelim != DELIMETER) || (pRxPkt->chanStatDelim != DELIMETER)) { |
jrodenburg | 8:dbf8bd4815f8 | 351 | // Send NAK back |
jrodenburg | 8:dbf8bd4815f8 | 352 | pc.printf("DBG: Error\n"); |
jrodenburg | 2:bd118a724f03 | 353 | } |
jrodenburg | 8:dbf8bd4815f8 | 354 | else { |
jrodenburg | 8:dbf8bd4815f8 | 355 | chanSel_SetTemp = pRxPkt->chanID; |
jrodenburg | 8:dbf8bd4815f8 | 356 | chnlStatus[pRxPkt->chanID].status = pRxPkt->chanStat; |
jrodenburg | 8:dbf8bd4815f8 | 357 | chnlStatus[pRxPkt->chanID].setTemp = pRxPkt->setTemp; |
jrodenburg | 8:dbf8bd4815f8 | 358 | newTempSet = true; |
jrodenburg | 2:bd118a724f03 | 359 | } |
jrodenburg | 2:bd118a724f03 | 360 | } |
jrodenburg | 8:dbf8bd4815f8 | 361 | break; |
jrodenburg | 8:dbf8bd4815f8 | 362 | |
jrodenburg | 8:dbf8bd4815f8 | 363 | case SELECT_CHANNEL: |
jrodenburg | 8:dbf8bd4815f8 | 364 | // Select channel to send temp data to |
jrodenburg | 8:dbf8bd4815f8 | 365 | { |
jrodenburg | 8:dbf8bd4815f8 | 366 | SELECT_CHANNEL_CMD *pRxPkt = (SELECT_CHANNEL_CMD *)(rxBuf); |
jrodenburg | 8:dbf8bd4815f8 | 367 | |
jrodenburg | 8:dbf8bd4815f8 | 368 | chanSel_SendTemp = pRxPkt->chanIDSel; |
jrodenburg | 8:dbf8bd4815f8 | 369 | #if 1 //def DEBUG |
jrodenburg | 8:dbf8bd4815f8 | 370 | pc.printf("DBG: chanSel = %02x\n", pRxPkt->chanIDSel); |
jrodenburg | 8:dbf8bd4815f8 | 371 | pc.printf("DBG: chanStat = %02x\n", pRxPkt->chanStat); |
jrodenburg | 8:dbf8bd4815f8 | 372 | #endif |
jrodenburg | 2:bd118a724f03 | 373 | } |
jrodenburg | 8:dbf8bd4815f8 | 374 | break; |
jrodenburg | 8:dbf8bd4815f8 | 375 | |
jrodenburg | 8:dbf8bd4815f8 | 376 | default: |
jrodenburg | 8:dbf8bd4815f8 | 377 | // Error |
jrodenburg | 8:dbf8bd4815f8 | 378 | break; |
jrodenburg | 2:bd118a724f03 | 379 | } |
jrodenburg | 1:0182b86f9bd4 | 380 | } |
jrodenburg | 0:a28a1035c31b | 381 | |
jrodenburg | 0:a28a1035c31b | 382 | /* Function: get_temp |
jrodenburg | 0:a28a1035c31b | 383 | ************************************************************** |
jrodenburg | 0:a28a1035c31b | 384 | Description: Retrieve data from thermistor |
jrodenburg | 9:8cd14861dc63 | 385 | Receives: chn: the channel of the fixture to read temp. from |
jrodenburg | 5:0f38a0bd4f86 | 386 | port: the I/O channel to read |
jrodenburg | 0:a28a1035c31b | 387 | Returns: the temperature of the fixture (front or back) |
jrodenburg | 0:a28a1035c31b | 388 | */ |
jrodenburg | 0:a28a1035c31b | 389 | |
jrodenburg | 5:0f38a0bd4f86 | 390 | float get_temp(int chn, int port){ |
jrodenburg | 1:0182b86f9bd4 | 391 | myled = 1; |
jrodenburg | 0:a28a1035c31b | 392 | ltc2487.setAddress(addrLUT[chn].adc); |
jrodenburg | 9:8cd14861dc63 | 393 | |
jrodenburg | 5:0f38a0bd4f86 | 394 | float ADC_val = ltc2487.readOutput(port); //(65536*1.334)/2.5 |
jrodenburg | 9:8cd14861dc63 | 395 | |
jrodenburg | 0:a28a1035c31b | 396 | int i = 0; |
jrodenburg | 9:8cd14861dc63 | 397 | |
jrodenburg | 0:a28a1035c31b | 398 | while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){ |
jrodenburg | 9:8cd14861dc63 | 399 | i++; |
jrodenburg | 0:a28a1035c31b | 400 | } //find the temp. above therm temp |
jrodenburg | 9:8cd14861dc63 | 401 | |
jrodenburg | 9:8cd14861dc63 | 402 | //Point slope formula extrapolation: |
jrodenburg | 0:a28a1035c31b | 403 | // y1 = m (x1-x0)+ y0 , y = temp. value, x = adc value |
jrodenburg | 0:a28a1035c31b | 404 | // y1 = thermLUT[i-1].temp y0 = thermLUT[i].temp |
jrodenburg | 0:a28a1035c31b | 405 | // x1 = thermLUT[i-1].adc x0 =thermLUT[i].adc |
jrodenburg | 0:a28a1035c31b | 406 | float a = float(thermLUT[i-1].temp - thermLUT[i].temp); //slope of temp between points where therm temp is between (Tmax - Tmin) |
jrodenburg | 0:a28a1035c31b | 407 | float b = float(thermLUT[i-1].adc - thermLUT[i].adc); //slope of adc between points where therm adc is between (Amax - Amin) |
jrodenburg | 9:8cd14861dc63 | 408 | |
jrodenburg | 0:a28a1035c31b | 409 | float m = a/b; |
jrodenburg | 0:a28a1035c31b | 410 | float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp; |
jrodenburg | 9:8cd14861dc63 | 411 | |
jrodenburg | 9:8cd14861dc63 | 412 | if(DEBUG2) pc.printf("DBG: CHAN: %i PORT: %i ADC VAL: %f TEMP: %f \r\n", chn, port, ADC_val, y); |
jrodenburg | 9:8cd14861dc63 | 413 | |
jrodenburg | 9:8cd14861dc63 | 414 | return y; |
jrodenburg | 0:a28a1035c31b | 415 | } |
jrodenburg | 0:a28a1035c31b | 416 | |
jrodenburg | 0:a28a1035c31b | 417 | /* Function: get_heater_current |
jrodenburg | 0:a28a1035c31b | 418 | ************************************************************** |
jrodenburg | 0:a28a1035c31b | 419 | Description: Retrieve current into heater control MOSFET |
jrodenburg | 9:8cd14861dc63 | 420 | Receives: chn: the channel of the fixture to read current from |
jrodenburg | 0:a28a1035c31b | 421 | Returns: the current into the heater control MOSFET |
jrodenburg | 0:a28a1035c31b | 422 | */ |
jrodenburg | 0:a28a1035c31b | 423 | |
jrodenburg | 6:c980535393ed | 424 | float get_heater_current(int chn, int port){ |
jrodenburg | 6:c980535393ed | 425 | return ltc2487.readOutput(port); |
jrodenburg | 0:a28a1035c31b | 426 | } |
jrodenburg | 0:a28a1035c31b | 427 | |
jrodenburg | 0:a28a1035c31b | 428 | /* Function: get_valve_current |
jrodenburg | 0:a28a1035c31b | 429 | ************************************************************** |
jrodenburg | 0:a28a1035c31b | 430 | Description: Retrieve current into valve control MOSFET |
jrodenburg | 9:8cd14861dc63 | 431 | Receives: chn: the channel of the fixture to read current from |
jrodenburg | 0:a28a1035c31b | 432 | Returns: the current into the valve control MOSFET |
jrodenburg | 0:a28a1035c31b | 433 | */ |
jrodenburg | 0:a28a1035c31b | 434 | |
jrodenburg | 6:c980535393ed | 435 | float get_valve_current(int chn, int port){ |
jrodenburg | 6:c980535393ed | 436 | return ltc2487.readOutput(port); |
jrodenburg | 0:a28a1035c31b | 437 | } |
jrodenburg | 0:a28a1035c31b | 438 | |
jrodenburg | 0:a28a1035c31b | 439 | /* Function: turn_valve_on |
jrodenburg | 0:a28a1035c31b | 440 | ************************************************************** |
jrodenburg | 1:0182b86f9bd4 | 441 | Description: Turn valve on and green status LED on |
jrodenburg | 9:8cd14861dc63 | 442 | Receives: chn: the channel of the fixture |
jrodenburg | 1:0182b86f9bd4 | 443 | Returns: N/A |
jrodenburg | 0:a28a1035c31b | 444 | */ |
jrodenburg | 0:a28a1035c31b | 445 | |
jrodenburg | 1:0182b86f9bd4 | 446 | void turn_valve_on(int chn){ |
jrodenburg | 0:a28a1035c31b | 447 | io_control.setAddress(addrLUT[chn].io); |
jrodenburg | 0:a28a1035c31b | 448 | io_control.init(); |
jrodenburg | 1:0182b86f9bd4 | 449 | io_control.writeOutput(1,0,1,0); |
jrodenburg | 0:a28a1035c31b | 450 | } |
jrodenburg | 0:a28a1035c31b | 451 | |
jrodenburg | 0:a28a1035c31b | 452 | /* Function: turn_valve_off |
jrodenburg | 0:a28a1035c31b | 453 | ************************************************************** |
jrodenburg | 1:0182b86f9bd4 | 454 | Description: Turn valve off and green status LED on |
jrodenburg | 9:8cd14861dc63 | 455 | Receives: chn: the channel of the fixture |
jrodenburg | 1:0182b86f9bd4 | 456 | Returns: N/A |
jrodenburg | 0:a28a1035c31b | 457 | */ |
jrodenburg | 0:a28a1035c31b | 458 | |
jrodenburg | 1:0182b86f9bd4 | 459 | void turn_valve_off(int chn){ |
jrodenburg | 0:a28a1035c31b | 460 | io_control.setAddress(addrLUT[chn].io); |
jrodenburg | 0:a28a1035c31b | 461 | io_control.init(); |
jrodenburg | 1:0182b86f9bd4 | 462 | io_control.writeOutput(0,0,1,0); |
jrodenburg | 0:a28a1035c31b | 463 | } |
jrodenburg | 0:a28a1035c31b | 464 | |
jrodenburg | 0:a28a1035c31b | 465 | /* Function: turn_heater_on |
jrodenburg | 0:a28a1035c31b | 466 | ************************************************************** |
jrodenburg | 1:0182b86f9bd4 | 467 | Description: Turn heater on and green status LED on |
jrodenburg | 9:8cd14861dc63 | 468 | Receives: chn: the channel of the fixture |
jrodenburg | 1:0182b86f9bd4 | 469 | Returns: N/A |
jrodenburg | 0:a28a1035c31b | 470 | */ |
jrodenburg | 0:a28a1035c31b | 471 | |
jrodenburg | 1:0182b86f9bd4 | 472 | void turn_heater_on(int chn){ |
jrodenburg | 0:a28a1035c31b | 473 | io_control.setAddress(addrLUT[chn].io); |
jrodenburg | 0:a28a1035c31b | 474 | io_control.init(); |
jrodenburg | 1:0182b86f9bd4 | 475 | io_control.writeOutput(0,1,1,0); |
jrodenburg | 0:a28a1035c31b | 476 | } |
jrodenburg | 0:a28a1035c31b | 477 | |
jrodenburg | 0:a28a1035c31b | 478 | /* Function: turn_heater_off |
jrodenburg | 0:a28a1035c31b | 479 | ************************************************************** |
jrodenburg | 1:0182b86f9bd4 | 480 | Description: Turn heater off and green status LED on |
jrodenburg | 9:8cd14861dc63 | 481 | Receives: chn: the channel of the fixture |
jrodenburg | 1:0182b86f9bd4 | 482 | Returns: N/A |
jrodenburg | 0:a28a1035c31b | 483 | */ |
jrodenburg | 0:a28a1035c31b | 484 | |
jrodenburg | 1:0182b86f9bd4 | 485 | void turn_heater_off(int chn){ |
jrodenburg | 0:a28a1035c31b | 486 | io_control.setAddress(addrLUT[chn].io); |
jrodenburg | 0:a28a1035c31b | 487 | io_control.init(); |
jrodenburg | 1:0182b86f9bd4 | 488 | io_control.writeOutput(0,0,1,0); |
jrodenburg | 0:a28a1035c31b | 489 | } |
jrodenburg | 0:a28a1035c31b | 490 | |
jrodenburg | 0:a28a1035c31b | 491 | /* Function: status_led |
jrodenburg | 0:a28a1035c31b | 492 | ************************************************************** |
jrodenburg | 0:a28a1035c31b | 493 | Description: Turn status LED on (turns on green or red) |
jrodenburg | 9:8cd14861dc63 | 494 | Receives: chn: the channel of the fixture |
jrodenburg | 0:a28a1035c31b | 495 | status: the status of channel (good (1) or bad (0)) |
jrodenburg | 1:0182b86f9bd4 | 496 | Returns: N/A |
jrodenburg | 0:a28a1035c31b | 497 | */ |
jrodenburg | 0:a28a1035c31b | 498 | |
jrodenburg | 1:0182b86f9bd4 | 499 | void status_led(int chn, int status){ |
jrodenburg | 0:a28a1035c31b | 500 | io_control.setAddress(addrLUT[chn].io); |
jrodenburg | 0:a28a1035c31b | 501 | if(status){ |
jrodenburg | 1:0182b86f9bd4 | 502 | io_control.writeOutput(0,0,1,0); |
jrodenburg | 0:a28a1035c31b | 503 | } |
jrodenburg | 0:a28a1035c31b | 504 | else{ |
jrodenburg | 8:dbf8bd4815f8 | 505 | //turn valve on too |
jrodenburg | 8:dbf8bd4815f8 | 506 | io_control.writeOutput(1,0,0,1); |
jrodenburg | 0:a28a1035c31b | 507 | } |
jrodenburg | 0:a28a1035c31b | 508 | } |
jrodenburg | 0:a28a1035c31b | 509 | |
jrodenburg | 1:0182b86f9bd4 | 510 | /* Function: test_mcp23008 |
jrodenburg | 1:0182b86f9bd4 | 511 | ************************************************************** |
jrodenburg | 1:0182b86f9bd4 | 512 | Description: Test each output of the MCP23009 |
jrodenburg | 9:8cd14861dc63 | 513 | Receives: N/A |
jrodenburg | 1:0182b86f9bd4 | 514 | Returns: N/A |
jrodenburg | 1:0182b86f9bd4 | 515 | */ |
jrodenburg | 1:0182b86f9bd4 | 516 | |
jrodenburg | 1:0182b86f9bd4 | 517 | void test_mcp23008(int chn){ |
jrodenburg | 1:0182b86f9bd4 | 518 | turn_valve_on(chn); |
jrodenburg | 7:8a5e65e63e2a | 519 | wait(0.5); |
jrodenburg | 1:0182b86f9bd4 | 520 | turn_valve_off(chn); |
jrodenburg | 7:8a5e65e63e2a | 521 | wait(0.5); |
jrodenburg | 1:0182b86f9bd4 | 522 | turn_heater_on(chn); |
jrodenburg | 7:8a5e65e63e2a | 523 | wait(0.5); |
jrodenburg | 1:0182b86f9bd4 | 524 | turn_heater_off(chn); |
jrodenburg | 7:8a5e65e63e2a | 525 | wait(0.5); |
jrodenburg | 1:0182b86f9bd4 | 526 | status_led(chn, 0); |
jrodenburg | 7:8a5e65e63e2a | 527 | wait(0.5); |
jrodenburg | 1:0182b86f9bd4 | 528 | status_led(chn, 1); |
jrodenburg | 1:0182b86f9bd4 | 529 | } |
jrodenburg | 1:0182b86f9bd4 | 530 | |
jrodenburg | 2:bd118a724f03 | 531 | /* Function: test_ltc2487 |
jrodenburg | 2:bd118a724f03 | 532 | ************************************************************** |
jrodenburg | 2:bd118a724f03 | 533 | Description: Test the reading from LTC2487 |
jrodenburg | 9:8cd14861dc63 | 534 | Receives: N/A |
jrodenburg | 2:bd118a724f03 | 535 | Returns: N/A |
jrodenburg | 2:bd118a724f03 | 536 | */ |
jrodenburg | 2:bd118a724f03 | 537 | |
jrodenburg | 2:bd118a724f03 | 538 | void test_ltc2487(int chn){ |
jrodenburg | 5:0f38a0bd4f86 | 539 | get_temp(chn, FRONT_THERM); |
jrodenburg | 5:0f38a0bd4f86 | 540 | wait(0.1); |
jrodenburg | 6:c980535393ed | 541 | get_temp(chn, BACK_THERM); |
jrodenburg | 6:c980535393ed | 542 | wait(0.1); |
jrodenburg | 8:dbf8bd4815f8 | 543 | get_temp(chn, VALVE_FET_AMP); |
jrodenburg | 8:dbf8bd4815f8 | 544 | wait(0.1); |
jrodenburg | 4:168a446bd0da | 545 | //if(DEBUG1) pc.printf("TEMPERATURE READING: %f \r\n", get_temp(chn)); |
jrodenburg | 2:bd118a724f03 | 546 | } |
jrodenburg | 2:bd118a724f03 | 547 | |
jrodenburg | 6:c980535393ed | 548 | /* Function: error_check |
jrodenburg | 6:c980535393ed | 549 | ************************************************************** |
jrodenburg | 6:c980535393ed | 550 | Description: Checks for any system errors |
jrodenburg | 7:8a5e65e63e2a | 551 | Recieves: frontTemp: temp. of front thermistor |
jrodenburg | 7:8a5e65e63e2a | 552 | backTemp: temp. of back thermistor |
jrodenburg | 7:8a5e65e63e2a | 553 | valveFet: current through valve FET |
jrodenburg | 7:8a5e65e63e2a | 554 | heaterFet: current through heater FET |
jrodenburg | 6:c980535393ed | 555 | Returns: N/A |
jrodenburg | 6:c980535393ed | 556 | */ |
jrodenburg | 6:c980535393ed | 557 | |
jrodenburg | 6:c980535393ed | 558 | void error_check(int chn, float frontTemp, float backTemp, float valveFet, float heaterFet){ |
jrodenburg | 6:c980535393ed | 559 | float setTemp = chnlStatus[chn].setTemp; |
jrodenburg | 9:8cd14861dc63 | 560 | |
jrodenburg | 7:8a5e65e63e2a | 561 | //CHECK IF THERMISTOR READINGS ARE OFF (> 5 DEGREES) |
jrodenburg | 7:8a5e65e63e2a | 562 | if(abs(frontTemp-backTemp) > 5){ |
jrodenburg | 6:c980535393ed | 563 | //ERROR 6: Thermistor reading off |
jrodenburg | 7:8a5e65e63e2a | 564 | chnlStatus[chn].error = 1; |
jrodenburg | 9:8cd14861dc63 | 565 | } |
jrodenburg | 9:8cd14861dc63 | 566 | |
jrodenburg | 6:c980535393ed | 567 | //CHECK IF HEATER'S STUCK ON OR CELL OVERHEATING |
jrodenburg | 6:c980535393ed | 568 | if((frontTemp >= (setTemp+TEMP_MARGIN)) || (backTemp >= (setTemp+TEMP_MARGIN))){ |
jrodenburg | 6:c980535393ed | 569 | //ERROR 0 |
jrodenburg | 7:8a5e65e63e2a | 570 | chnlStatus[chn].error = 1; |
jrodenburg | 6:c980535393ed | 571 | status_led(chn, STATUS_BAD); |
jrodenburg | 6:c980535393ed | 572 | if(heaterFet >= FET_ON_CURRENT){ |
jrodenburg | 9:8cd14861dc63 | 573 | //ERROR 1: Heater FET stuck on |
jrodenburg | 6:c980535393ed | 574 | } |
jrodenburg | 6:c980535393ed | 575 | if(valveFet <= FET_ON_CURRENT){ |
jrodenburg | 9:8cd14861dc63 | 576 | //ERROR 2: valve FET stuck off |
jrodenburg | 6:c980535393ed | 577 | } |
jrodenburg | 6:c980535393ed | 578 | } |
jrodenburg | 9:8cd14861dc63 | 579 | |
jrodenburg | 6:c980535393ed | 580 | //CHECK IF VALVE STUCK ON OR CELL OVERHEATING |
jrodenburg | 6:c980535393ed | 581 | if((frontTemp <= (setTemp-TEMP_MARGIN)) || (backTemp <= (setTemp-TEMP_MARGIN))){ |
jrodenburg | 6:c980535393ed | 582 | //ERROR 0 |
jrodenburg | 7:8a5e65e63e2a | 583 | chnlStatus[chn].error = 1; |
jrodenburg | 6:c980535393ed | 584 | status_led(chn, STATUS_BAD); |
jrodenburg | 6:c980535393ed | 585 | if(heaterFet <= FET_ON_CURRENT){ |
jrodenburg | 9:8cd14861dc63 | 586 | //ERROR 2: Heater FET stuck off |
jrodenburg | 6:c980535393ed | 587 | } |
jrodenburg | 6:c980535393ed | 588 | else if(valveFet >= FET_ON_CURRENT){ |
jrodenburg | 9:8cd14861dc63 | 589 | //ERROR 3: Chiller FET stuck on |
jrodenburg | 6:c980535393ed | 590 | } |
jrodenburg | 9:8cd14861dc63 | 591 | } |
jrodenburg | 5:0f38a0bd4f86 | 592 | } |
jrodenburg | 5:0f38a0bd4f86 | 593 | |
jrodenburg | 8:dbf8bd4815f8 | 594 | //*************************************************************** |
jrodenburg | 8:dbf8bd4815f8 | 595 | // Build packet with temperature readings to send to GUI |
jrodenburg | 8:dbf8bd4815f8 | 596 | //*************************************************************** |
jrodenburg | 8:dbf8bd4815f8 | 597 | void sendTempReadings (int chan, float currentTemp) |
jrodenburg | 8:dbf8bd4815f8 | 598 | { |
jrodenburg | 8:dbf8bd4815f8 | 599 | RESPONSE_CMD response; |
jrodenburg | 8:dbf8bd4815f8 | 600 | unsigned char *ptr = (unsigned char *)&response; |
jrodenburg | 8:dbf8bd4815f8 | 601 | int i; |
jrodenburg | 8:dbf8bd4815f8 | 602 | |
jrodenburg | 8:dbf8bd4815f8 | 603 | response.SOF_flag = TX_SOF; |
jrodenburg | 8:dbf8bd4815f8 | 604 | response.cmd_type = CMD_DATA; |
jrodenburg | 8:dbf8bd4815f8 | 605 | response.len = 9; |
jrodenburg | 8:dbf8bd4815f8 | 606 | response.Delim = DELIMETER; |
jrodenburg | 8:dbf8bd4815f8 | 607 | response.data = (float)currentTemp; |
jrodenburg | 8:dbf8bd4815f8 | 608 | response.EOF_flag = TX_EOF; |
jrodenburg | 8:dbf8bd4815f8 | 609 | |
jrodenburg | 8:dbf8bd4815f8 | 610 | // Send response to GUI |
jrodenburg | 8:dbf8bd4815f8 | 611 | for (i=0; i < response.len; i++, ptr++) |
jrodenburg | 8:dbf8bd4815f8 | 612 | pc.printf("%02x", *ptr); |
jrodenburg | 8:dbf8bd4815f8 | 613 | pc.printf("\n"); |
jrodenburg | 8:dbf8bd4815f8 | 614 | } |
jrodenburg | 8:dbf8bd4815f8 | 615 | |
jrodenburg | 6:c980535393ed | 616 | |
jrodenburg | 6:c980535393ed | 617 | /*************************************************************/ |
jrodenburg | 6:c980535393ed | 618 | /* MAIN FUNCTION */ |
jrodenburg | 6:c980535393ed | 619 | /*************************************************************/ |
jrodenburg | 0:a28a1035c31b | 620 | |
jrodenburg | 9:8cd14861dc63 | 621 | int main() { |
jrodenburg | 0:a28a1035c31b | 622 | |
jrodenburg | 9:8cd14861dc63 | 623 | Timer t; |
jrodenburg | 8:dbf8bd4815f8 | 624 | |
jrodenburg | 8:dbf8bd4815f8 | 625 | // Setup serial port |
jrodenburg | 8:dbf8bd4815f8 | 626 | // Look for RX_EOF |
jrodenburg | 8:dbf8bd4815f8 | 627 | pc.baud(9600); |
jrodenburg | 8:dbf8bd4815f8 | 628 | pc.autoDetectChar(RX_EOF); |
jrodenburg | 8:dbf8bd4815f8 | 629 | pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect); |
jrodenburg | 8:dbf8bd4815f8 | 630 | |
jrodenburg | 8:dbf8bd4815f8 | 631 | myled = 1; |
jrodenburg | 8:dbf8bd4815f8 | 632 | rLed = 0; |
jrodenburg | 8:dbf8bd4815f8 | 633 | gLed = 1; |
jrodenburg | 4:168a446bd0da | 634 | |
jrodenburg | 8:dbf8bd4815f8 | 635 | t.start(); |
jrodenburg | 9:8cd14861dc63 | 636 | |
jrodenburg | 0:a28a1035c31b | 637 | while(1) { |
jrodenburg | 9:8cd14861dc63 | 638 | |
jrodenburg | 9:8cd14861dc63 | 639 | //if(DEBUG3) |
jrodenburg | 9:8cd14861dc63 | 640 | // pc.printf("DBG: PROGRAM STARTED \r\n"); |
jrodenburg | 9:8cd14861dc63 | 641 | |
jrodenburg | 5:0f38a0bd4f86 | 642 | for(int chnl = 0; chnl < CHN_COUNT; chnl++){ |
jrodenburg | 5:0f38a0bd4f86 | 643 | float currentTempFront = get_temp(chnl, FRONT_THERM); |
jrodenburg | 6:c980535393ed | 644 | float currentTempBack = get_temp(chnl, BACK_THERM); |
jrodenburg | 7:8a5e65e63e2a | 645 | float valveCurrent = get_valve_current(chnl, VALVE_FET_AMP); |
jrodenburg | 7:8a5e65e63e2a | 646 | float heaterCurrent = get_heater_current(chnl, HEAT_FET_AMP); |
jrodenburg | 8:dbf8bd4815f8 | 647 | float currentTemp = currentTempFront; |
jrodenburg | 9:8cd14861dc63 | 648 | |
jrodenburg | 8:dbf8bd4815f8 | 649 | if(currentTempFront >= currentTempBack){ |
jrodenburg | 8:dbf8bd4815f8 | 650 | currentTemp = currentTempFront; |
jrodenburg | 8:dbf8bd4815f8 | 651 | } |
jrodenburg | 8:dbf8bd4815f8 | 652 | else{ |
jrodenburg | 8:dbf8bd4815f8 | 653 | currentTemp = currentTempBack; |
jrodenburg | 8:dbf8bd4815f8 | 654 | } |
jrodenburg | 8:dbf8bd4815f8 | 655 | |
jrodenburg | 8:dbf8bd4815f8 | 656 | //float currentTemp = (currentTempFront + currentTempBack)/2; |
jrodenburg | 8:dbf8bd4815f8 | 657 | |
jrodenburg | 8:dbf8bd4815f8 | 658 | //check if we received data/need to update TCTF data |
jrodenburg | 8:dbf8bd4815f8 | 659 | if(dataReceived){ |
jrodenburg | 8:dbf8bd4815f8 | 660 | dataReceived = false; |
jrodenburg | 9:8cd14861dc63 | 661 | pc.printf("DBG: %d bytes Rx'd\n", pc.rxBufferGetCount()); |
jrodenburg | 8:dbf8bd4815f8 | 662 | pc.move(rxBuf, 50); |
jrodenburg | 8:dbf8bd4815f8 | 663 | parseRXData(); |
jrodenburg | 8:dbf8bd4815f8 | 664 | pc.rxBufferFlush(); |
jrodenburg | 8:dbf8bd4815f8 | 665 | |
jrodenburg | 8:dbf8bd4815f8 | 666 | } |
jrodenburg | 8:dbf8bd4815f8 | 667 | |
jrodenburg | 8:dbf8bd4815f8 | 668 | if (chnl == chanSel_SendTemp){ |
jrodenburg | 8:dbf8bd4815f8 | 669 | // Send temp readings for selected chan to GUI |
jrodenburg | 8:dbf8bd4815f8 | 670 | sendTempReadings(currChan, currentTemp); |
jrodenburg | 8:dbf8bd4815f8 | 671 | } |
jrodenburg | 9:8cd14861dc63 | 672 | |
jrodenburg | 2:bd118a724f03 | 673 | //CONTROL LOOP: |
jrodenburg | 4:168a446bd0da | 674 | if(chnlStatus[chnl].status == 1){ |
jrodenburg | 9:8cd14861dc63 | 675 | if(DEBUG3) pc.printf("DBG: [%d] Temp: %f \r\n", chnl, currentTempFront); |
jrodenburg | 7:8a5e65e63e2a | 676 | |
jrodenburg | 7:8a5e65e63e2a | 677 | //Error check on fixture |
jrodenburg | 8:dbf8bd4815f8 | 678 | //error_check(chnl, currentTempFront, currentTempBack, valveCurrent, heaterCurrent); |
jrodenburg | 8:dbf8bd4815f8 | 679 | if((currentTempFront >= 70) || (currentTempBack >= 70)){ |
jrodenburg | 8:dbf8bd4815f8 | 680 | status_led(chnl, STATUS_BAD); |
jrodenburg | 8:dbf8bd4815f8 | 681 | } |
jrodenburg | 7:8a5e65e63e2a | 682 | |
jrodenburg | 7:8a5e65e63e2a | 683 | if(chnlStatus[chnl].error == 0){ |
jrodenburg | 9:8cd14861dc63 | 684 | if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)){ |
jrodenburg | 9:8cd14861dc63 | 685 | if(DEBUG3) pc.printf("DBG: [%d] Chiller ON \r\n", chnl); |
jrodenburg | 7:8a5e65e63e2a | 686 | //Turn chiller on |
jrodenburg | 9:8cd14861dc63 | 687 | turn_valve_on(chnl); |
jrodenburg | 7:8a5e65e63e2a | 688 | } |
jrodenburg | 8:dbf8bd4815f8 | 689 | else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST_LOW)){ |
jrodenburg | 9:8cd14861dc63 | 690 | if(DEBUG3) pc.printf("DBG: [%d] Heater ON \r\n", chnl); |
jrodenburg | 7:8a5e65e63e2a | 691 | //Turn heater on |
jrodenburg | 7:8a5e65e63e2a | 692 | turn_heater_on(chnl); |
jrodenburg | 7:8a5e65e63e2a | 693 | } |
jrodenburg | 7:8a5e65e63e2a | 694 | else{ |
jrodenburg | 9:8cd14861dc63 | 695 | if(DEBUG3) pc.printf("DBG: [%d] All OFF \r\n", chnl); |
jrodenburg | 7:8a5e65e63e2a | 696 | //turn off chiller |
jrodenburg | 7:8a5e65e63e2a | 697 | turn_valve_off(chnl); |
jrodenburg | 9:8cd14861dc63 | 698 | //turn off heater |
jrodenburg | 7:8a5e65e63e2a | 699 | turn_heater_off(chnl); |
jrodenburg | 7:8a5e65e63e2a | 700 | //turn on green LED status light |
jrodenburg | 9:8cd14861dc63 | 701 | status_led(chnl, 1); |
jrodenburg | 9:8cd14861dc63 | 702 | } |
jrodenburg | 9:8cd14861dc63 | 703 | } |
jrodenburg | 2:bd118a724f03 | 704 | } |
jrodenburg | 2:bd118a724f03 | 705 | } |
jrodenburg | 0:a28a1035c31b | 706 | } |
jrodenburg | 0:a28a1035c31b | 707 | } |