Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Committer:
jrodenburg
Date:
Wed Mar 28 01:25:07 2018 +0000
Revision:
8:dbf8bd4815f8
Parent:
7:8a5e65e63e2a
Child:
9:8cd14861dc63
Child:
12:1cada1fe4743
Code before adding in Huyens Code

Who changed what in which revision?

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