Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Committer:
jrodenburg
Date:
Tue Feb 13 20:30:59 2018 +0000
Revision:
4:168a446bd0da
Parent:
3:0c9476da0cad
Child:
5:0f38a0bd4f86
Code with the control loop updated.;

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 0:a28a1035c31b 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 4:168a446bd0da 11 #define DEBUG 0
jrodenburg 4:168a446bd0da 12 #define DEBUG1 1
jrodenburg 0:a28a1035c31b 13 #define CHN_COUNT 48
jrodenburg 0:a28a1035c31b 14 #define MIN_TEMP 15
jrodenburg 0:a28a1035c31b 15 #define MAX_TEMP 60
jrodenburg 0:a28a1035c31b 16 #define HYST 0.2
jrodenburg 0:a28a1035c31b 17 #define SAMPLES 5
jrodenburg 0:a28a1035c31b 18 #define I2C_Freq 2000
jrodenburg 1:0182b86f9bd4 19 #define VALVE 1
jrodenburg 1:0182b86f9bd4 20 #define HEATER 2
jrodenburg 1:0182b86f9bd4 21 #define STATUS_GOOD 3
jrodenburg 1:0182b86f9bd4 22 #define STATUS_BAD 4
jrodenburg 0:a28a1035c31b 23 #define sizeLUT 34
jrodenburg 0:a28a1035c31b 24 #define FRONT_THERM 0
jrodenburg 0:a28a1035c31b 25 #define BACK_THERM 1
jrodenburg 0:a28a1035c31b 26 #define HEAT_FET_AMP 2
jrodenburg 0:a28a1035c31b 27 #define VALV_FET_AMP 3
jrodenburg 0:a28a1035c31b 28
jrodenburg 2:bd118a724f03 29
jrodenburg 2:bd118a724f03 30 //TCTF CHANNEL DATA
jrodenburg 2:bd118a724f03 31 struct CHNL_DATA{
jrodenburg 2:bd118a724f03 32 bool status;
jrodenburg 2:bd118a724f03 33 float setTemp;
jrodenburg 2:bd118a724f03 34 };
jrodenburg 2:bd118a724f03 35
jrodenburg 4:168a446bd0da 36 CHNL_DATA chnlStatus[] = {
jrodenburg 2:bd118a724f03 37 {0, NULL},
jrodenburg 2:bd118a724f03 38 {0, NULL},
jrodenburg 2:bd118a724f03 39 {0, NULL},
jrodenburg 2:bd118a724f03 40 {0, NULL},
jrodenburg 2:bd118a724f03 41 {0, NULL},
jrodenburg 2:bd118a724f03 42 {0, NULL},
jrodenburg 2:bd118a724f03 43 {0, NULL},
jrodenburg 2:bd118a724f03 44 {0, NULL},
jrodenburg 2:bd118a724f03 45 {0, NULL},
jrodenburg 2:bd118a724f03 46 {0, NULL},
jrodenburg 2:bd118a724f03 47 {0, NULL},
jrodenburg 2:bd118a724f03 48 {0, NULL},
jrodenburg 2:bd118a724f03 49 {0, NULL},
jrodenburg 2:bd118a724f03 50 {0, NULL},
jrodenburg 2:bd118a724f03 51 {0, NULL},
jrodenburg 2:bd118a724f03 52 {0, NULL},
jrodenburg 2:bd118a724f03 53 {0, NULL},
jrodenburg 2:bd118a724f03 54 {0, NULL},
jrodenburg 2:bd118a724f03 55 {0, NULL},
jrodenburg 2:bd118a724f03 56 {0, NULL},
jrodenburg 2:bd118a724f03 57 {0, NULL},
jrodenburg 2:bd118a724f03 58 {0, NULL},
jrodenburg 2:bd118a724f03 59 {0, NULL},
jrodenburg 2:bd118a724f03 60 {0, NULL},
jrodenburg 2:bd118a724f03 61 {0, NULL},
jrodenburg 2:bd118a724f03 62 {0, NULL},
jrodenburg 2:bd118a724f03 63 {0, NULL},
jrodenburg 2:bd118a724f03 64 {0, NULL},
jrodenburg 2:bd118a724f03 65 {0, NULL},
jrodenburg 2:bd118a724f03 66 {0, NULL},
jrodenburg 2:bd118a724f03 67 {0, NULL},
jrodenburg 2:bd118a724f03 68 {0, NULL},
jrodenburg 2:bd118a724f03 69 {0, NULL},
jrodenburg 2:bd118a724f03 70 {0, NULL},
jrodenburg 2:bd118a724f03 71 {0, NULL},
jrodenburg 2:bd118a724f03 72 {0, NULL},
jrodenburg 2:bd118a724f03 73 {0, NULL},
jrodenburg 2:bd118a724f03 74 {0, NULL},
jrodenburg 2:bd118a724f03 75 {0, NULL},
jrodenburg 2:bd118a724f03 76 {0, NULL},
jrodenburg 2:bd118a724f03 77 {0, NULL},
jrodenburg 2:bd118a724f03 78 {0, NULL},
jrodenburg 2:bd118a724f03 79 {0, NULL},
jrodenburg 2:bd118a724f03 80 {0, NULL},
jrodenburg 2:bd118a724f03 81 {0, NULL},
jrodenburg 2:bd118a724f03 82 {0, NULL},
jrodenburg 2:bd118a724f03 83 {0, NULL},
jrodenburg 2:bd118a724f03 84 {0, NULL},
jrodenburg 2:bd118a724f03 85 };
jrodenburg 2:bd118a724f03 86
jrodenburg 2:bd118a724f03 87
jrodenburg 0:a28a1035c31b 88 //I2C AADRESS LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 2:bd118a724f03 89 struct I2C_ADDR_LUT{
jrodenburg 0:a28a1035c31b 90 int adc;
jrodenburg 0:a28a1035c31b 91 int io;
jrodenburg 0:a28a1035c31b 92 };
jrodenburg 0:a28a1035c31b 93
jrodenburg 0:a28a1035c31b 94 I2C_ADDR_LUT addrLUT[] = {
jrodenburg 0:a28a1035c31b 95 {0x23, 0x10},
jrodenburg 0:a28a1035c31b 96 {0x53, 0x60},
jrodenburg 0:a28a1035c31b 97 {0x43, 0x70},
jrodenburg 0:a28a1035c31b 98 {0x73, 0x40},
jrodenburg 0:a28a1035c31b 99 {0x63, 0x50},
jrodenburg 0:a28a1035c31b 100 {0x22, 0x11},
jrodenburg 0:a28a1035c31b 101 {0x52, 0x61},
jrodenburg 0:a28a1035c31b 102 {0x42, 0x71},
jrodenburg 0:a28a1035c31b 103 {0x72, 0x41},
jrodenburg 0:a28a1035c31b 104 {0x62, 0x51},
jrodenburg 0:a28a1035c31b 105 {0x21, 0x12},
jrodenburg 0:a28a1035c31b 106 {0x51, 0x62},
jrodenburg 0:a28a1035c31b 107 {0x41, 0x72},
jrodenburg 0:a28a1035c31b 108 {0x71, 0x42},
jrodenburg 0:a28a1035c31b 109 {0x61, 0x52},
jrodenburg 0:a28a1035c31b 110 {0x20, 0x13},
jrodenburg 0:a28a1035c31b 111 {0x50, 0x63},
jrodenburg 0:a28a1035c31b 112 {0x40, 0x73},
jrodenburg 0:a28a1035c31b 113 {0x70, 0x43},
jrodenburg 0:a28a1035c31b 114 {0x60, 0x53},
jrodenburg 0:a28a1035c31b 115 {0x26, 0x14},
jrodenburg 0:a28a1035c31b 116 {0x56, 0x64},
jrodenburg 0:a28a1035c31b 117 {0x46, 0x74},
jrodenburg 0:a28a1035c31b 118 {0x76, 0x44},
jrodenburg 0:a28a1035c31b 119 {0x66, 0x54},
jrodenburg 0:a28a1035c31b 120 {0x2C, 0x1F},
jrodenburg 0:a28a1035c31b 121 {0x5C, 0x6F},
jrodenburg 0:a28a1035c31b 122 {0x4C, 0x7F},
jrodenburg 0:a28a1035c31b 123 {0x7C, 0x4F},
jrodenburg 0:a28a1035c31b 124 {0x6C, 0x5F},
jrodenburg 0:a28a1035c31b 125 {0x2D, 0x1E},
jrodenburg 0:a28a1035c31b 126 {0x5D, 0x6E},
jrodenburg 0:a28a1035c31b 127 {0x4D, 0x7E},
jrodenburg 0:a28a1035c31b 128 {0x7D, 0x4E},
jrodenburg 0:a28a1035c31b 129 {0x6D, 0x5E},
jrodenburg 0:a28a1035c31b 130 {0x2E, 0x1D},
jrodenburg 0:a28a1035c31b 131 {0x5E, 0x6D},
jrodenburg 0:a28a1035c31b 132 {0x4E, 0x7D},
jrodenburg 0:a28a1035c31b 133 {0x7E, 0x4D},
jrodenburg 0:a28a1035c31b 134 {0x6E, 0x5D},
jrodenburg 0:a28a1035c31b 135 {0x2F, 0x1C},
jrodenburg 0:a28a1035c31b 136 {0x5F, 0x6C},
jrodenburg 0:a28a1035c31b 137 {0x4F, 0x7C},
jrodenburg 0:a28a1035c31b 138 {0x7F, 0x4C},
jrodenburg 0:a28a1035c31b 139 {0x6F, 0x5C},
jrodenburg 0:a28a1035c31b 140 {0x20, 0x1B},
jrodenburg 0:a28a1035c31b 141 {0x50, 0x6B},
jrodenburg 0:a28a1035c31b 142 {0x40, 0x7B},
jrodenburg 0:a28a1035c31b 143 };
jrodenburg 0:a28a1035c31b 144
jrodenburg 0:a28a1035c31b 145 //THERMISTOR LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 2:bd118a724f03 146 struct THERM_LUT{
jrodenburg 0:a28a1035c31b 147 int adc;
jrodenburg 0:a28a1035c31b 148 int temp;
jrodenburg 0:a28a1035c31b 149 };
jrodenburg 0:a28a1035c31b 150
jrodenburg 0:a28a1035c31b 151 THERM_LUT thermLUT[] = {
jrodenburg 0:a28a1035c31b 152 {113779,-40},
jrodenburg 0:a28a1035c31b 153 {109152,-35},
jrodenburg 0:a28a1035c31b 154 {103830,-30},
jrodenburg 0:a28a1035c31b 155 {97855,-25},
jrodenburg 0:a28a1035c31b 156 {91319,-20},
jrodenburg 0:a28a1035c31b 157 {84352,-15},
jrodenburg 0:a28a1035c31b 158 {77124,-10},
jrodenburg 0:a28a1035c31b 159 {69820,-5},
jrodenburg 0:a28a1035c31b 160 {62621,0},
jrodenburg 0:a28a1035c31b 161 {55693,5},
jrodenburg 0:a28a1035c31b 162 {49169,10},
jrodenburg 0:a28a1035c31b 163 {43144,15},
jrodenburg 0:a28a1035c31b 164 {37669,20},
jrodenburg 0:a28a1035c31b 165 {32768,25},
jrodenburg 0:a28a1035c31b 166 {28429,30},
jrodenburg 0:a28a1035c31b 167 {24622,35},
jrodenburg 0:a28a1035c31b 168 {21309,40},
jrodenburg 0:a28a1035c31b 169 {18439,45},
jrodenburg 0:a28a1035c31b 170 {15962,50},
jrodenburg 0:a28a1035c31b 171 {13831,55},
jrodenburg 0:a28a1035c31b 172 {12002,60},
jrodenburg 0:a28a1035c31b 173 {10428,65},
jrodenburg 0:a28a1035c31b 174 {9080,70},
jrodenburg 0:a28a1035c31b 175 {7919,75},
jrodenburg 0:a28a1035c31b 176 {6923,80},
jrodenburg 0:a28a1035c31b 177 {6063,85},
jrodenburg 0:a28a1035c31b 178 {5323,90},
jrodenburg 0:a28a1035c31b 179 {4685,95},
jrodenburg 0:a28a1035c31b 180 {4130,100},
jrodenburg 0:a28a1035c31b 181 {3653,105},
jrodenburg 0:a28a1035c31b 182 {3234,110},
jrodenburg 0:a28a1035c31b 183 {2876,115},
jrodenburg 0:a28a1035c31b 184 {2563,120},
jrodenburg 0:a28a1035c31b 185 {2284,125}
jrodenburg 0:a28a1035c31b 186 };
jrodenburg 0:a28a1035c31b 187
jrodenburg 2:bd118a724f03 188 //SERIAL COMMUNICATION SETUP
jrodenburg 2:bd118a724f03 189 MODSERIAL pc(USBTX, USBRX);
jrodenburg 1:0182b86f9bd4 190
jrodenburg 0:a28a1035c31b 191 //DEFINE PINS
jrodenburg 0:a28a1035c31b 192 DigitalOut myled(LED2);
jrodenburg 0:a28a1035c31b 193
jrodenburg 0:a28a1035c31b 194 //I2C FOR MCP23008 (I/O Control)
jrodenburg 2:bd118a724f03 195 MCP23008 io_control(PTC9, PTC8, 0x10, 100000); //sda, scl
jrodenburg 0:a28a1035c31b 196
jrodenburg 0:a28a1035c31b 197 //I2C FOR LTC2487 (ADC Control)
jrodenburg 2:bd118a724f03 198 LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000); //sda, scl
jrodenburg 0:a28a1035c31b 199
jrodenburg 0:a28a1035c31b 200 //GLOBAL VARIABLES
jrodenburg 2:bd118a724f03 201 volatile bool dataRecieved = false; //used to check if data has been recieved
jrodenburg 2:bd118a724f03 202 char rxBuf[50];
jrodenburg 2:bd118a724f03 203 int chnlSel;
jrodenburg 0:a28a1035c31b 204
jrodenburg 2:bd118a724f03 205 /* Function: rxInterrupt
jrodenburg 1:0182b86f9bd4 206 **************************************************************
jrodenburg 2:bd118a724f03 207 Description: serial rx interupt handler
jrodenburg 2:bd118a724f03 208 Recieves: N/A
jrodenburg 2:bd118a724f03 209 Returns: N/A
jrodenburg 2:bd118a724f03 210 */
jrodenburg 2:bd118a724f03 211
jrodenburg 2:bd118a724f03 212 void rxInterrupt(MODSERIAL_IRQ_INFO *info){
jrodenburg 2:bd118a724f03 213 dataRecieved = true;
jrodenburg 2:bd118a724f03 214 if(DEBUG) pc.printf("DATA RECIEVED \r\n");
jrodenburg 2:bd118a724f03 215 }
jrodenburg 2:bd118a724f03 216
jrodenburg 2:bd118a724f03 217 /* Function: parseRXData
jrodenburg 2:bd118a724f03 218 **************************************************************
jrodenburg 2:bd118a724f03 219 Description: The parse recieved data into
jrodenburg 2:bd118a724f03 220 Recieves: chn: The channel we want to put into the channel
jrodenburg 1:0182b86f9bd4 221 Returns: N/A
jrodenburg 1:0182b86f9bd4 222 */
jrodenburg 0:a28a1035c31b 223
jrodenburg 2:bd118a724f03 224 void parseRXData(){
jrodenburg 2:bd118a724f03 225 int pCount = 0; //count data collected
jrodenburg 2:bd118a724f03 226 int i = 0;
jrodenburg 2:bd118a724f03 227 int chnl;
jrodenburg 2:bd118a724f03 228 string data = "";
jrodenburg 2:bd118a724f03 229
jrodenburg 2:bd118a724f03 230 if(DEBUG) pc.printf("buff1 = <%s> \r\n", rxBuf);
jrodenburg 1:0182b86f9bd4 231
jrodenburg 2:bd118a724f03 232 while(pCount < 3){
jrodenburg 2:bd118a724f03 233 if(rxBuf[i] != '.'){
jrodenburg 2:bd118a724f03 234 data += rxBuf[i];
jrodenburg 2:bd118a724f03 235 }
jrodenburg 2:bd118a724f03 236 else{
jrodenburg 2:bd118a724f03 237 pCount++;
jrodenburg 2:bd118a724f03 238 if(pCount == 1){ //get channel
jrodenburg 2:bd118a724f03 239 if((atoi(data.c_str()) < 0) || (atoi(data.c_str()) > 15)){
jrodenburg 2:bd118a724f03 240 //check if channel is out of array index
jrodenburg 2:bd118a724f03 241 if(DEBUG) pc.printf("ERROR: INDEX OUT OF RANGE! \r\n", rxBuf);
jrodenburg 2:bd118a724f03 242 }
jrodenburg 2:bd118a724f03 243 else{
jrodenburg 2:bd118a724f03 244 chnl = atoi(data.c_str());
jrodenburg 2:bd118a724f03 245 chnl = chnl-1; //fix for array indexing
jrodenburg 2:bd118a724f03 246 chnlSel = chnl; //chnl selected by user in GUI
jrodenburg 2:bd118a724f03 247 }
jrodenburg 2:bd118a724f03 248 }
jrodenburg 2:bd118a724f03 249 else if(pCount == 2){ //get channel temperature
jrodenburg 4:168a446bd0da 250 chnlStatus[chnl].setTemp = atoi(data.c_str());
jrodenburg 4:168a446bd0da 251 if(DEBUG) pc.printf("CHANNEL TEMP: %f \r\n", chnlStatus[chnl].setTemp);
jrodenburg 2:bd118a724f03 252 }
jrodenburg 2:bd118a724f03 253 else if(pCount == 3){ //get channel status
jrodenburg 4:168a446bd0da 254 chnlStatus[chnl].status = atoi(data.c_str());
jrodenburg 4:168a446bd0da 255 if(DEBUG) pc.printf("CHANNEL STATUS: %f \r\n", chnlStatus[chnl].status);
jrodenburg 2:bd118a724f03 256 }
jrodenburg 2:bd118a724f03 257 data = "";
jrodenburg 2:bd118a724f03 258 }
jrodenburg 2:bd118a724f03 259 i++;
jrodenburg 2:bd118a724f03 260 }
jrodenburg 1:0182b86f9bd4 261 }
jrodenburg 0:a28a1035c31b 262
jrodenburg 0:a28a1035c31b 263 /* Function: get_temp
jrodenburg 0:a28a1035c31b 264 **************************************************************
jrodenburg 0:a28a1035c31b 265 Description: Retrieve data from thermistor
jrodenburg 0:a28a1035c31b 266 Recieves: chn: the channel of the fixture to read temp. from
jrodenburg 0:a28a1035c31b 267 Returns: the temperature of the fixture (front or back)
jrodenburg 0:a28a1035c31b 268 */
jrodenburg 0:a28a1035c31b 269
jrodenburg 0:a28a1035c31b 270 float get_temp(int chn){
jrodenburg 1:0182b86f9bd4 271 myled = 1;
jrodenburg 0:a28a1035c31b 272 ltc2487.setAddress(addrLUT[chn].adc);
jrodenburg 0:a28a1035c31b 273
jrodenburg 1:0182b86f9bd4 274 float ADC_val = ltc2487.readOutput(chn); //(65536*1.334)/2.5;
jrodenburg 0:a28a1035c31b 275
jrodenburg 0:a28a1035c31b 276 int i = 0;
jrodenburg 0:a28a1035c31b 277
jrodenburg 0:a28a1035c31b 278 while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){
jrodenburg 0:a28a1035c31b 279 i++;
jrodenburg 0:a28a1035c31b 280 } //find the temp. above therm temp
jrodenburg 0:a28a1035c31b 281
jrodenburg 0:a28a1035c31b 282 //Point slope formula extrapolation:
jrodenburg 0:a28a1035c31b 283 // y1 = m (x1-x0)+ y0 , y = temp. value, x = adc value
jrodenburg 0:a28a1035c31b 284 // y1 = thermLUT[i-1].temp y0 = thermLUT[i].temp
jrodenburg 0:a28a1035c31b 285 // x1 = thermLUT[i-1].adc x0 =thermLUT[i].adc
jrodenburg 0:a28a1035c31b 286 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 287 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 288
jrodenburg 0:a28a1035c31b 289 float m = a/b;
jrodenburg 0:a28a1035c31b 290 float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp;
jrodenburg 0:a28a1035c31b 291
jrodenburg 2:bd118a724f03 292 if(DEBUG) pc.printf("ADC VAL: %f TEMP: %f \r\n", ADC_val, y);
jrodenburg 1:0182b86f9bd4 293
jrodenburg 4:168a446bd0da 294 //if(chn == chnlSel)
jrodenburg 4:168a446bd0da 295 pc.printf("%f \r\n", y);
jrodenburg 4:168a446bd0da 296
jrodenburg 0:a28a1035c31b 297 return y;
jrodenburg 0:a28a1035c31b 298 }
jrodenburg 0:a28a1035c31b 299
jrodenburg 0:a28a1035c31b 300 /* Function: get_heater_current
jrodenburg 0:a28a1035c31b 301 **************************************************************
jrodenburg 0:a28a1035c31b 302 Description: Retrieve current into heater control MOSFET
jrodenburg 0:a28a1035c31b 303 Recieves: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 304 Returns: the current into the heater control MOSFET
jrodenburg 0:a28a1035c31b 305 */
jrodenburg 0:a28a1035c31b 306
jrodenburg 0:a28a1035c31b 307 float get_heater_current(int chn){
jrodenburg 1:0182b86f9bd4 308 return 0.0;
jrodenburg 0:a28a1035c31b 309 }
jrodenburg 0:a28a1035c31b 310
jrodenburg 0:a28a1035c31b 311 /* Function: get_valve_current
jrodenburg 0:a28a1035c31b 312 **************************************************************
jrodenburg 0:a28a1035c31b 313 Description: Retrieve current into valve control MOSFET
jrodenburg 0:a28a1035c31b 314 Recieves: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 315 Returns: the current into the valve control MOSFET
jrodenburg 0:a28a1035c31b 316 */
jrodenburg 0:a28a1035c31b 317
jrodenburg 0:a28a1035c31b 318 float get_valve_current(int chn){
jrodenburg 1:0182b86f9bd4 319 return 0.0;
jrodenburg 0:a28a1035c31b 320 }
jrodenburg 0:a28a1035c31b 321
jrodenburg 0:a28a1035c31b 322 /* Function: turn_valve_on
jrodenburg 0:a28a1035c31b 323 **************************************************************
jrodenburg 1:0182b86f9bd4 324 Description: Turn valve on and green status LED on
jrodenburg 0:a28a1035c31b 325 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 326 Returns: N/A
jrodenburg 0:a28a1035c31b 327 */
jrodenburg 0:a28a1035c31b 328
jrodenburg 1:0182b86f9bd4 329 void turn_valve_on(int chn){
jrodenburg 0:a28a1035c31b 330 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 331 io_control.init();
jrodenburg 1:0182b86f9bd4 332 io_control.writeOutput(1,0,1,0);
jrodenburg 0:a28a1035c31b 333 }
jrodenburg 0:a28a1035c31b 334
jrodenburg 0:a28a1035c31b 335 /* Function: turn_valve_off
jrodenburg 0:a28a1035c31b 336 **************************************************************
jrodenburg 1:0182b86f9bd4 337 Description: Turn valve off and green status LED on
jrodenburg 0:a28a1035c31b 338 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 339 Returns: N/A
jrodenburg 0:a28a1035c31b 340 */
jrodenburg 0:a28a1035c31b 341
jrodenburg 1:0182b86f9bd4 342 void turn_valve_off(int chn){
jrodenburg 0:a28a1035c31b 343 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 344 io_control.init();
jrodenburg 1:0182b86f9bd4 345 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 346 }
jrodenburg 0:a28a1035c31b 347
jrodenburg 0:a28a1035c31b 348 /* Function: turn_heater_on
jrodenburg 0:a28a1035c31b 349 **************************************************************
jrodenburg 1:0182b86f9bd4 350 Description: Turn heater on and green status LED on
jrodenburg 0:a28a1035c31b 351 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 352 Returns: N/A
jrodenburg 0:a28a1035c31b 353 */
jrodenburg 0:a28a1035c31b 354
jrodenburg 1:0182b86f9bd4 355 void turn_heater_on(int chn){
jrodenburg 0:a28a1035c31b 356 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 357 io_control.init();
jrodenburg 1:0182b86f9bd4 358 io_control.writeOutput(0,1,1,0);
jrodenburg 0:a28a1035c31b 359 }
jrodenburg 0:a28a1035c31b 360
jrodenburg 0:a28a1035c31b 361 /* Function: turn_heater_off
jrodenburg 0:a28a1035c31b 362 **************************************************************
jrodenburg 1:0182b86f9bd4 363 Description: Turn heater off and green status LED on
jrodenburg 0:a28a1035c31b 364 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 365 Returns: N/A
jrodenburg 0:a28a1035c31b 366 */
jrodenburg 0:a28a1035c31b 367
jrodenburg 1:0182b86f9bd4 368 void turn_heater_off(int chn){
jrodenburg 0:a28a1035c31b 369 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 370 io_control.init();
jrodenburg 1:0182b86f9bd4 371 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 372 }
jrodenburg 0:a28a1035c31b 373
jrodenburg 0:a28a1035c31b 374 /* Function: status_led
jrodenburg 0:a28a1035c31b 375 **************************************************************
jrodenburg 0:a28a1035c31b 376 Description: Turn status LED on (turns on green or red)
jrodenburg 0:a28a1035c31b 377 Recieves: chn: the channel of the fixture
jrodenburg 0:a28a1035c31b 378 status: the status of channel (good (1) or bad (0))
jrodenburg 1:0182b86f9bd4 379 Returns: N/A
jrodenburg 0:a28a1035c31b 380 */
jrodenburg 0:a28a1035c31b 381
jrodenburg 1:0182b86f9bd4 382 void status_led(int chn, int status){
jrodenburg 0:a28a1035c31b 383 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 384 if(status){
jrodenburg 1:0182b86f9bd4 385 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 386 }
jrodenburg 0:a28a1035c31b 387 else{
jrodenburg 1:0182b86f9bd4 388 io_control.writeOutput(0,0,0,1);
jrodenburg 0:a28a1035c31b 389 }
jrodenburg 0:a28a1035c31b 390 }
jrodenburg 0:a28a1035c31b 391
jrodenburg 1:0182b86f9bd4 392 /* Function: test_mcp23008
jrodenburg 1:0182b86f9bd4 393 **************************************************************
jrodenburg 1:0182b86f9bd4 394 Description: Test each output of the MCP23009
jrodenburg 1:0182b86f9bd4 395 Recieves: N/A
jrodenburg 1:0182b86f9bd4 396 Returns: N/A
jrodenburg 1:0182b86f9bd4 397 */
jrodenburg 1:0182b86f9bd4 398
jrodenburg 1:0182b86f9bd4 399 void test_mcp23008(int chn){
jrodenburg 1:0182b86f9bd4 400 turn_valve_on(chn);
jrodenburg 1:0182b86f9bd4 401 wait(1);
jrodenburg 1:0182b86f9bd4 402 turn_valve_off(chn);
jrodenburg 1:0182b86f9bd4 403 wait(1);
jrodenburg 1:0182b86f9bd4 404 turn_heater_on(chn);
jrodenburg 1:0182b86f9bd4 405 wait(1);
jrodenburg 1:0182b86f9bd4 406 turn_heater_off(chn);
jrodenburg 1:0182b86f9bd4 407 wait(1);
jrodenburg 1:0182b86f9bd4 408 status_led(chn, 0);
jrodenburg 1:0182b86f9bd4 409 wait(1);
jrodenburg 1:0182b86f9bd4 410 status_led(chn, 1);
jrodenburg 1:0182b86f9bd4 411 }
jrodenburg 1:0182b86f9bd4 412
jrodenburg 2:bd118a724f03 413 /* Function: test_ltc2487
jrodenburg 2:bd118a724f03 414 **************************************************************
jrodenburg 2:bd118a724f03 415 Description: Test the reading from LTC2487
jrodenburg 2:bd118a724f03 416 Recieves: N/A
jrodenburg 2:bd118a724f03 417 Returns: N/A
jrodenburg 2:bd118a724f03 418 */
jrodenburg 2:bd118a724f03 419
jrodenburg 2:bd118a724f03 420 void test_ltc2487(int chn){
jrodenburg 4:168a446bd0da 421 get_temp(chn);
jrodenburg 4:168a446bd0da 422 //if(DEBUG1) pc.printf("TEMPERATURE READING: %f \r\n", get_temp(chn));
jrodenburg 2:bd118a724f03 423 }
jrodenburg 2:bd118a724f03 424
jrodenburg 0:a28a1035c31b 425
jrodenburg 0:a28a1035c31b 426
jrodenburg 0:a28a1035c31b 427 int main() {
jrodenburg 0:a28a1035c31b 428
jrodenburg 4:168a446bd0da 429 pc.baud(9600);
jrodenburg 4:168a446bd0da 430 pc.autoDetectChar('E');
jrodenburg 4:168a446bd0da 431 pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect);
jrodenburg 4:168a446bd0da 432
jrodenburg 0:a28a1035c31b 433 myled = 1;
jrodenburg 4:168a446bd0da 434 /*
jrodenburg 4:168a446bd0da 435 //test_mcp23008(1);
jrodenburg 4:168a446bd0da 436 while(1){
jrodenburg 4:168a446bd0da 437 test_ltc2487(1);
jrodenburg 4:168a446bd0da 438 }*/
jrodenburg 0:a28a1035c31b 439
jrodenburg 0:a28a1035c31b 440 while(1) {
jrodenburg 2:bd118a724f03 441 if(DEBUG) pc.printf("THE PROGRAM STARTED \r\n");
jrodenburg 2:bd118a724f03 442
jrodenburg 2:bd118a724f03 443 //check if we recieved data/need to update TCTF data
jrodenburg 2:bd118a724f03 444 if(dataRecieved){
jrodenburg 2:bd118a724f03 445 dataRecieved = false;
jrodenburg 2:bd118a724f03 446 pc.autoDetectChar('\n');
jrodenburg 2:bd118a724f03 447 pc.move(rxBuf, 50);
jrodenburg 2:bd118a724f03 448 pc.rxBufferFlush();
jrodenburg 2:bd118a724f03 449 parseRXData();
jrodenburg 2:bd118a724f03 450 }
jrodenburg 2:bd118a724f03 451
jrodenburg 2:bd118a724f03 452 for(int chnl = 0; chnl <= CHN_COUNT; chnl++){
jrodenburg 2:bd118a724f03 453 float currentTemp = get_temp(chnl);
jrodenburg 2:bd118a724f03 454
jrodenburg 2:bd118a724f03 455 //CONTROL LOOP:
jrodenburg 4:168a446bd0da 456 if(chnlStatus[chnl].status == 1){
jrodenburg 4:168a446bd0da 457 if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST)){
jrodenburg 2:bd118a724f03 458 //Turn chiller on
jrodenburg 2:bd118a724f03 459 turn_valve_on(chnl);
jrodenburg 2:bd118a724f03 460 //Turn heater off
jrodenburg 2:bd118a724f03 461 turn_heater_off(chnl);
jrodenburg 2:bd118a724f03 462 }
jrodenburg 4:168a446bd0da 463 else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST)){
jrodenburg 2:bd118a724f03 464 //Turn chiller off
jrodenburg 2:bd118a724f03 465 turn_valve_off(chnl);
jrodenburg 2:bd118a724f03 466 //Turn heater on
jrodenburg 2:bd118a724f03 467 turn_heater_on(chnl);
jrodenburg 2:bd118a724f03 468 }
jrodenburg 2:bd118a724f03 469 else{
jrodenburg 2:bd118a724f03 470 //turn off chiller
jrodenburg 2:bd118a724f03 471 turn_valve_off(chnl);
jrodenburg 2:bd118a724f03 472 //turn off heater
jrodenburg 2:bd118a724f03 473 turn_heater_off(chnl);
jrodenburg 2:bd118a724f03 474 //turn on green LED status light
jrodenburg 2:bd118a724f03 475 status_led(chnl, 1);
jrodenburg 2:bd118a724f03 476 }
jrodenburg 2:bd118a724f03 477 }
jrodenburg 2:bd118a724f03 478 }
jrodenburg 0:a28a1035c31b 479 }
jrodenburg 0:a28a1035c31b 480 }