Working on rewriting how we acquire data from LTC chip and sending all temp data over serial for python logging

Dependencies:   MODSERIAL mbed

Fork of TCTF_Control_Main by Rivian Irvine Team

Committer:
jrodenburg
Date:
Fri Jun 15 00:50:47 2018 +0000
Revision:
17:5098d8fbb298
Parent:
16:a7fd53ab4199
Child:
18:029a1283a878
Updated the LTC2487 library to write all the addresses for a row, wait for next clock cycle of chip (15Hz), and read all the channels. This significantly sped up the thermocouple read time. Both the LTC2487 library and the read functions were changed

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 15:74a01aaeb60e 4 #define FW_VERSION "V.010"
jrodenburg 0:a28a1035c31b 5 #include "mbed.h"
jrodenburg 0:a28a1035c31b 6 #include "MODSERIAL.h"
jrodenburg 8:dbf8bd4815f8 7 #include "MCP23008.h"
jrodenburg 0:a28a1035c31b 8 #include "LTC2487.h"
jrodenburg 2:bd118a724f03 9 #include <string>
jrodenburg 0:a28a1035c31b 10
jrodenburg 0:a28a1035c31b 11 //DEFINITIVE VARIABLES
jrodenburg 16:a7fd53ab4199 12 #define DEBUG 0
jrodenburg 16:a7fd53ab4199 13 #define DEBUG1 0
jrodenburg 16:a7fd53ab4199 14 #define DEBUG2 0
jrodenburg 17:5098d8fbb298 15 #define DEBUG3 0
jrodenburg 16:a7fd53ab4199 16 #define DEBUG5 0
jrodenburg 16:a7fd53ab4199 17 #define CHN_COUNT 8
jrodenburg 16:a7fd53ab4199 18 #define MIN_TEMP 10
jrodenburg 16:a7fd53ab4199 19 #define MAX_TEMP 65
jrodenburg 16:a7fd53ab4199 20 #define TEMP_MARGIN 20
jrodenburg 16:a7fd53ab4199 21 #define HYST_LOW 0.3
jrodenburg 16:a7fd53ab4199 22 #define HYST_HIGH 1
jrodenburg 16:a7fd53ab4199 23 #define SAMPLES 5
jrodenburg 16:a7fd53ab4199 24 #define I2C_Freq 2000
jrodenburg 16:a7fd53ab4199 25 #define VALVE 1
jrodenburg 16:a7fd53ab4199 26 #define HEATER 2
jrodenburg 16:a7fd53ab4199 27 #define STATUS_GOOD 3
jrodenburg 16:a7fd53ab4199 28 #define STATUS_BAD 0
jrodenburg 16:a7fd53ab4199 29 #define sizeLUT 34
jrodenburg 16:a7fd53ab4199 30 #define BACK_THERM 0
jrodenburg 16:a7fd53ab4199 31 #define FRONT_THERM 1
jrodenburg 16:a7fd53ab4199 32 #define HEAT_FET_AMP 2
jrodenburg 16:a7fd53ab4199 33 #define VALVE_FET_AMP 3
jrodenburg 16:a7fd53ab4199 34 #define FET_ON_CURRENT 1.12
jrodenburg 16:a7fd53ab4199 35 #define ROOM_TEMP 22
jrodenburg 14:69cd53434783 36 #define MAX_HEATER_ON_TIME 25
jrodenburg 14:69cd53434783 37 #define MAX_CHILL_TIME 10 //10sec
jrodenburg 9:1cada1fe4743 38
jrodenburg 8:dbf8bd4815f8 39 // Defines for use with Serial communication
jrodenburg 8:dbf8bd4815f8 40 #pragma pack (1)
jrodenburg 8:dbf8bd4815f8 41 #define RX_SOF 0x7B
jrodenburg 8:dbf8bd4815f8 42 #define RX_EOF 0x7D
jrodenburg 8:dbf8bd4815f8 43 #define TX_SOF 0x7B
jrodenburg 8:dbf8bd4815f8 44 #define TX_EOF 0x7D
jrodenburg 8:dbf8bd4815f8 45 #define DELIMETER 0x2E
jrodenburg 8:dbf8bd4815f8 46 #define SET_TEMPERATURE 0xB0
jrodenburg 8:dbf8bd4815f8 47 #define SELECT_CHANNEL 0xB1
jrodenburg 15:74a01aaeb60e 48 #define READ_UID 0xB2
jrodenburg 15:74a01aaeb60e 49 #define RESPONSE_DATA 0xD0
jrodenburg 15:74a01aaeb60e 50 #define TEMP_DATA 0xD1
jrodenburg 15:74a01aaeb60e 51 #define UID_DATA 0xD2
jrodenburg 15:74a01aaeb60e 52 #define ERROR_DATA 0xDF
jrodenburg 9:1cada1fe4743 53
jrodenburg 0:a28a1035c31b 54
jrodenburg 8:dbf8bd4815f8 55 unsigned int chanSel_SendTemp = 0;
jrodenburg 8:dbf8bd4815f8 56 unsigned int chanSel_SetTemp = 0;
jrodenburg 8:dbf8bd4815f8 57 // Default to chan 0
jrodenburg 8:dbf8bd4815f8 58 int currChan = 0;
jrodenburg 8:dbf8bd4815f8 59 bool newTempSet = false;
jrodenburg 8:dbf8bd4815f8 60
jrodenburg 9:1cada1fe4743 61
jrodenburg 8:dbf8bd4815f8 62 //***********************************************
jrodenburg 8:dbf8bd4815f8 63 // Serial Rx Packet Format
jrodenburg 8:dbf8bd4815f8 64 //***********************************************
jrodenburg 8:dbf8bd4815f8 65 typedef struct {
jrodenburg 8:dbf8bd4815f8 66 unsigned char SOF_flag;
jrodenburg 8:dbf8bd4815f8 67 unsigned char cmd_type;
jrodenburg 8:dbf8bd4815f8 68 unsigned char len;
jrodenburg 8:dbf8bd4815f8 69 unsigned char Delim;
jrodenburg 8:dbf8bd4815f8 70 } HOST_CMD_HEADER;
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 chanID;
jrodenburg 8:dbf8bd4815f8 75 unsigned char tempDelim;
jrodenburg 8:dbf8bd4815f8 76 float setTemp;
jrodenburg 8:dbf8bd4815f8 77 unsigned char chanStatDelim;
jrodenburg 8:dbf8bd4815f8 78 unsigned char chanStat;
jrodenburg 8:dbf8bd4815f8 79 } SET_TEMPERATURE_CMD;
jrodenburg 8:dbf8bd4815f8 80
jrodenburg 8:dbf8bd4815f8 81 typedef struct {
jrodenburg 8:dbf8bd4815f8 82 HOST_CMD_HEADER cmd_header;
jrodenburg 8:dbf8bd4815f8 83 unsigned char chanIDSel;
jrodenburg 8:dbf8bd4815f8 84 unsigned char chanDelim;
jrodenburg 8:dbf8bd4815f8 85 unsigned char chanStat;
jrodenburg 8:dbf8bd4815f8 86 } SELECT_CHANNEL_CMD;
jrodenburg 8:dbf8bd4815f8 87
jrodenburg 8:dbf8bd4815f8 88 typedef struct {
jrodenburg 8:dbf8bd4815f8 89 unsigned char SOF_flag;
jrodenburg 8:dbf8bd4815f8 90 unsigned char cmd_type;
jrodenburg 8:dbf8bd4815f8 91 unsigned char len;
jrodenburg 8:dbf8bd4815f8 92 unsigned char Delim;
jrodenburg 8:dbf8bd4815f8 93 float data;
jrodenburg 8:dbf8bd4815f8 94 unsigned char EOF_flag;
jrodenburg 8:dbf8bd4815f8 95 } RESPONSE_CMD;
jrodenburg 2:bd118a724f03 96
jrodenburg 15:74a01aaeb60e 97 typedef struct {
jrodenburg 16:a7fd53ab4199 98 unsigned char SOF_flag;
jrodenburg 16:a7fd53ab4199 99 unsigned char cmd_type;
jrodenburg 16:a7fd53ab4199 100 unsigned char len;
jrodenburg 16:a7fd53ab4199 101 unsigned char Delim;
jrodenburg 16:a7fd53ab4199 102 float chTemp[CHN_COUNT];
jrodenburg 16:a7fd53ab4199 103 unsigned char EOF_flag;
jrodenburg 16:a7fd53ab4199 104 } RESPONSE_TEMP_CMD;
jrodenburg 16:a7fd53ab4199 105
jrodenburg 16:a7fd53ab4199 106 typedef struct {
jrodenburg 15:74a01aaeb60e 107 HOST_CMD_HEADER cmd_header;
jrodenburg 15:74a01aaeb60e 108 unsigned char chanIDSel;
jrodenburg 15:74a01aaeb60e 109 unsigned char chanDelim;
jrodenburg 15:74a01aaeb60e 110 unsigned char chanStat;
jrodenburg 15:74a01aaeb60e 111 } READ_UID_CMD;
jrodenburg 15:74a01aaeb60e 112
jrodenburg 15:74a01aaeb60e 113 typedef struct {
jrodenburg 15:74a01aaeb60e 114 unsigned char SOF_flag;
jrodenburg 15:74a01aaeb60e 115 unsigned char cmd_type;
jrodenburg 15:74a01aaeb60e 116 unsigned char len;
jrodenburg 15:74a01aaeb60e 117 unsigned char Delim;
jrodenburg 15:74a01aaeb60e 118 uint32_t UIDMH;
jrodenburg 15:74a01aaeb60e 119 uint32_t UIDML;
jrodenburg 15:74a01aaeb60e 120 uint32_t UIDL;
jrodenburg 15:74a01aaeb60e 121 unsigned char EOF_flag;
jrodenburg 15:74a01aaeb60e 122 } UID_RESPONSE;
jrodenburg 15:74a01aaeb60e 123
jrodenburg 2:bd118a724f03 124 //TCTF CHANNEL DATA
jrodenburg 2:bd118a724f03 125 struct CHNL_DATA{
jrodenburg 2:bd118a724f03 126 bool status;
jrodenburg 2:bd118a724f03 127 float setTemp;
jrodenburg 7:8a5e65e63e2a 128 bool error;
jrodenburg 14:69cd53434783 129 int heater_init_time;
jrodenburg 2:bd118a724f03 130 };
jrodenburg 2:bd118a724f03 131
jrodenburg 4:168a446bd0da 132 CHNL_DATA chnlStatus[] = {
jrodenburg 14:69cd53434783 133 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 134 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 135 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 136 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 137 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 138 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 139 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 140 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 141 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 142 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 143 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 144 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 145 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 146 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 147 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 148 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 149 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 150 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 151 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 152 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 153 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 154 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 155 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 156 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 157 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 158 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 159 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 160 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 161 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 162 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 163 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 164 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 165 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 166 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 167 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 168 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 169 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 170 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 171 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 172 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 173 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 174 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 175 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 176 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 177 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 178 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 179 {0, NULL, 0, 0},
jrodenburg 14:69cd53434783 180 {0, NULL, 0, 0},
jrodenburg 2:bd118a724f03 181 };
jrodenburg 2:bd118a724f03 182
jrodenburg 2:bd118a724f03 183
jrodenburg 0:a28a1035c31b 184 //I2C AADRESS LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 2:bd118a724f03 185 struct I2C_ADDR_LUT{
jrodenburg 0:a28a1035c31b 186 int adc;
jrodenburg 0:a28a1035c31b 187 int io;
jrodenburg 0:a28a1035c31b 188 };
jrodenburg 0:a28a1035c31b 189
jrodenburg 0:a28a1035c31b 190 I2C_ADDR_LUT addrLUT[] = {
jrodenburg 0:a28a1035c31b 191 {0x23, 0x10},
jrodenburg 0:a28a1035c31b 192 {0x53, 0x60},
jrodenburg 0:a28a1035c31b 193 {0x43, 0x70},
jrodenburg 0:a28a1035c31b 194 {0x73, 0x40},
jrodenburg 0:a28a1035c31b 195 {0x63, 0x50},
jrodenburg 0:a28a1035c31b 196 {0x22, 0x11},
jrodenburg 0:a28a1035c31b 197 {0x52, 0x61},
jrodenburg 0:a28a1035c31b 198 {0x42, 0x71},
jrodenburg 0:a28a1035c31b 199 {0x72, 0x41},
jrodenburg 0:a28a1035c31b 200 {0x62, 0x51},
jrodenburg 0:a28a1035c31b 201 {0x21, 0x12},
jrodenburg 0:a28a1035c31b 202 {0x51, 0x62},
jrodenburg 0:a28a1035c31b 203 {0x41, 0x72},
jrodenburg 0:a28a1035c31b 204 {0x71, 0x42},
jrodenburg 0:a28a1035c31b 205 {0x61, 0x52},
jrodenburg 0:a28a1035c31b 206 {0x20, 0x13},
jrodenburg 0:a28a1035c31b 207 {0x50, 0x63},
jrodenburg 0:a28a1035c31b 208 {0x40, 0x73},
jrodenburg 0:a28a1035c31b 209 {0x70, 0x43},
jrodenburg 0:a28a1035c31b 210 {0x60, 0x53},
jrodenburg 0:a28a1035c31b 211 {0x26, 0x14},
jrodenburg 0:a28a1035c31b 212 {0x56, 0x64},
jrodenburg 0:a28a1035c31b 213 {0x46, 0x74},
jrodenburg 0:a28a1035c31b 214 {0x76, 0x44},
jrodenburg 0:a28a1035c31b 215 {0x66, 0x54},
jrodenburg 0:a28a1035c31b 216 {0x2C, 0x1F},
jrodenburg 0:a28a1035c31b 217 {0x5C, 0x6F},
jrodenburg 0:a28a1035c31b 218 {0x4C, 0x7F},
jrodenburg 0:a28a1035c31b 219 {0x7C, 0x4F},
jrodenburg 0:a28a1035c31b 220 {0x6C, 0x5F},
jrodenburg 0:a28a1035c31b 221 {0x2D, 0x1E},
jrodenburg 0:a28a1035c31b 222 {0x5D, 0x6E},
jrodenburg 0:a28a1035c31b 223 {0x4D, 0x7E},
jrodenburg 0:a28a1035c31b 224 {0x7D, 0x4E},
jrodenburg 0:a28a1035c31b 225 {0x6D, 0x5E},
jrodenburg 0:a28a1035c31b 226 {0x2E, 0x1D},
jrodenburg 0:a28a1035c31b 227 {0x5E, 0x6D},
jrodenburg 0:a28a1035c31b 228 {0x4E, 0x7D},
jrodenburg 0:a28a1035c31b 229 {0x7E, 0x4D},
jrodenburg 0:a28a1035c31b 230 {0x6E, 0x5D},
jrodenburg 0:a28a1035c31b 231 {0x2F, 0x1C},
jrodenburg 0:a28a1035c31b 232 {0x5F, 0x6C},
jrodenburg 0:a28a1035c31b 233 {0x4F, 0x7C},
jrodenburg 0:a28a1035c31b 234 {0x7F, 0x4C},
jrodenburg 0:a28a1035c31b 235 {0x6F, 0x5C},
jrodenburg 0:a28a1035c31b 236 {0x20, 0x1B},
jrodenburg 0:a28a1035c31b 237 {0x50, 0x6B},
jrodenburg 9:1cada1fe4743 238 {0x40, 0x7B},
jrodenburg 0:a28a1035c31b 239 };
jrodenburg 0:a28a1035c31b 240
jrodenburg 0:a28a1035c31b 241 //THERMISTOR LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 2:bd118a724f03 242 struct THERM_LUT{
jrodenburg 0:a28a1035c31b 243 int adc;
jrodenburg 0:a28a1035c31b 244 int temp;
jrodenburg 0:a28a1035c31b 245 };
jrodenburg 0:a28a1035c31b 246
jrodenburg 0:a28a1035c31b 247 THERM_LUT thermLUT[] = {
jrodenburg 0:a28a1035c31b 248 {113779,-40},
jrodenburg 0:a28a1035c31b 249 {109152,-35},
jrodenburg 0:a28a1035c31b 250 {103830,-30},
jrodenburg 0:a28a1035c31b 251 {97855,-25},
jrodenburg 0:a28a1035c31b 252 {91319,-20},
jrodenburg 0:a28a1035c31b 253 {84352,-15},
jrodenburg 0:a28a1035c31b 254 {77124,-10},
jrodenburg 0:a28a1035c31b 255 {69820,-5},
jrodenburg 0:a28a1035c31b 256 {62621,0},
jrodenburg 0:a28a1035c31b 257 {55693,5},
jrodenburg 0:a28a1035c31b 258 {49169,10},
jrodenburg 0:a28a1035c31b 259 {43144,15},
jrodenburg 0:a28a1035c31b 260 {37669,20},
jrodenburg 0:a28a1035c31b 261 {32768,25},
jrodenburg 0:a28a1035c31b 262 {28429,30},
jrodenburg 0:a28a1035c31b 263 {24622,35},
jrodenburg 0:a28a1035c31b 264 {21309,40},
jrodenburg 0:a28a1035c31b 265 {18439,45},
jrodenburg 0:a28a1035c31b 266 {15962,50},
jrodenburg 0:a28a1035c31b 267 {13831,55},
jrodenburg 0:a28a1035c31b 268 {12002,60},
jrodenburg 0:a28a1035c31b 269 {10428,65},
jrodenburg 0:a28a1035c31b 270 {9080,70},
jrodenburg 0:a28a1035c31b 271 {7919,75},
jrodenburg 0:a28a1035c31b 272 {6923,80},
jrodenburg 0:a28a1035c31b 273 {6063,85},
jrodenburg 0:a28a1035c31b 274 {5323,90},
jrodenburg 0:a28a1035c31b 275 {4685,95},
jrodenburg 0:a28a1035c31b 276 {4130,100},
jrodenburg 0:a28a1035c31b 277 {3653,105},
jrodenburg 0:a28a1035c31b 278 {3234,110},
jrodenburg 0:a28a1035c31b 279 {2876,115},
jrodenburg 0:a28a1035c31b 280 {2563,120},
jrodenburg 0:a28a1035c31b 281 {2284,125}
jrodenburg 0:a28a1035c31b 282 };
jrodenburg 0:a28a1035c31b 283
jrodenburg 16:a7fd53ab4199 284 //TCTF CHANNEL TEMPERATURE
jrodenburg 16:a7fd53ab4199 285 typedef struct{
jrodenburg 16:a7fd53ab4199 286 float currTempFront;
jrodenburg 16:a7fd53ab4199 287 float currTempBack;
jrodenburg 16:a7fd53ab4199 288 }CHNL_TEMP;
jrodenburg 16:a7fd53ab4199 289
jrodenburg 16:a7fd53ab4199 290 CHNL_TEMP channelTempData[CHN_COUNT];
jrodenburg 16:a7fd53ab4199 291
jrodenburg 2:bd118a724f03 292 //SERIAL COMMUNICATION SETUP
jrodenburg 2:bd118a724f03 293 MODSERIAL pc(USBTX, USBRX);
jrodenburg 1:0182b86f9bd4 294
jrodenburg 0:a28a1035c31b 295 //DEFINE PINS
jrodenburg 0:a28a1035c31b 296 DigitalOut myled(LED2);
jrodenburg 0:a28a1035c31b 297
jrodenburg 0:a28a1035c31b 298 //I2C FOR MCP23008 (I/O Control)
jrodenburg 9:1cada1fe4743 299 MCP23008 io_control(PTC9, PTC8, 0x10, 100000); //sda, scl
jrodenburg 0:a28a1035c31b 300
jrodenburg 0:a28a1035c31b 301 //I2C FOR LTC2487 (ADC Control)
jrodenburg 9:1cada1fe4743 302 LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000); //sda, scl
jrodenburg 0:a28a1035c31b 303
jrodenburg 0:a28a1035c31b 304 //GLOBAL VARIABLES
jrodenburg 9:1cada1fe4743 305 volatile bool dataReceived = false; //used to check if data has been received
jrodenburg 8:dbf8bd4815f8 306 volatile bool dataTxReady = false;
jrodenburg 2:bd118a724f03 307 char rxBuf[50];
jrodenburg 2:bd118a724f03 308 int chnlSel;
jrodenburg 0:a28a1035c31b 309
jrodenburg 7:8a5e65e63e2a 310
jrodenburg 7:8a5e65e63e2a 311 /* Function: turnOffChannel
jrodenburg 7:8a5e65e63e2a 312 **************************************************************
jrodenburg 7:8a5e65e63e2a 313 Description: Turns off a channel
jrodenburg 7:8a5e65e63e2a 314 Recieves: chnl: channel to turn off
jrodenburg 7:8a5e65e63e2a 315 Returns: N/A
jrodenburg 7:8a5e65e63e2a 316 */
jrodenburg 7:8a5e65e63e2a 317
jrodenburg 7:8a5e65e63e2a 318 void turnOffChannel(int chnl){
jrodenburg 7:8a5e65e63e2a 319 io_control.setAddress(addrLUT[chnl].io);
jrodenburg 7:8a5e65e63e2a 320 io_control.init();
jrodenburg 7:8a5e65e63e2a 321 io_control.writeOutput(0,0,0,0);
jrodenburg 7:8a5e65e63e2a 322 }
jrodenburg 7:8a5e65e63e2a 323
jrodenburg 8:dbf8bd4815f8 324
jrodenburg 2:bd118a724f03 325 /* Function: rxInterrupt
jrodenburg 1:0182b86f9bd4 326 **************************************************************
jrodenburg 8:dbf8bd4815f8 327 Description: serial rx interupt handler
jrodenburg 8:dbf8bd4815f8 328 Receives: N/A
jrodenburg 8:dbf8bd4815f8 329 Returns: N/A
jrodenburg 2:bd118a724f03 330 */
jrodenburg 2:bd118a724f03 331
jrodenburg 8:dbf8bd4815f8 332 DigitalOut rLed(LED1);
jrodenburg 8:dbf8bd4815f8 333 DigitalOut gLed(LED2);
jrodenburg 8:dbf8bd4815f8 334
jrodenburg 15:74a01aaeb60e 335 void sendUID ();
jrodenburg 15:74a01aaeb60e 336
jrodenburg 8:dbf8bd4815f8 337 //***********************************************
jrodenburg 8:dbf8bd4815f8 338 // Rx Interrupt from serial Host interface
jrodenburg 8:dbf8bd4815f8 339 //***********************************************
jrodenburg 8:dbf8bd4815f8 340 void rxInterrupt(MODSERIAL_IRQ_INFO *info)
jrodenburg 8:dbf8bd4815f8 341 {
jrodenburg 8:dbf8bd4815f8 342 gLed = 0;
jrodenburg 8:dbf8bd4815f8 343 dataReceived = true;
jrodenburg 8:dbf8bd4815f8 344 gLed = 1;
jrodenburg 2:bd118a724f03 345 }
jrodenburg 2:bd118a724f03 346
jrodenburg 8:dbf8bd4815f8 347
jrodenburg 8:dbf8bd4815f8 348 //***************************************************************
jrodenburg 8:dbf8bd4815f8 349 // Tx Interrupt from serial Host interface
jrodenburg 8:dbf8bd4815f8 350 //***************************************************************
jrodenburg 8:dbf8bd4815f8 351 void txInterrupt(MODSERIAL_IRQ_INFO *info)
jrodenburg 8:dbf8bd4815f8 352 {
jrodenburg 8:dbf8bd4815f8 353 dataTxReady = true;
jrodenburg 8:dbf8bd4815f8 354 }
jrodenburg 8:dbf8bd4815f8 355
jrodenburg 8:dbf8bd4815f8 356
jrodenburg 2:bd118a724f03 357 /* Function: parseRXData
jrodenburg 2:bd118a724f03 358 **************************************************************
jrodenburg 9:1cada1fe4743 359 Description: The parse received data into
jrodenburg 9:1cada1fe4743 360 Receives: chn: The channel we want to put into the channel
jrodenburg 9:1cada1fe4743 361 Returns: N/A
jrodenburg 1:0182b86f9bd4 362 */
jrodenburg 0:a28a1035c31b 363
jrodenburg 8:dbf8bd4815f8 364 void parseRXData()
jrodenburg 8:dbf8bd4815f8 365 {
jrodenburg 8:dbf8bd4815f8 366 HOST_CMD_HEADER *pRxPktHdr;
jrodenburg 2:bd118a724f03 367 string data = "";
jrodenburg 8:dbf8bd4815f8 368 unsigned char *ptemp;
jrodenburg 8:dbf8bd4815f8 369 int i;
jrodenburg 8:dbf8bd4815f8 370
jrodenburg 8:dbf8bd4815f8 371 pRxPktHdr = (HOST_CMD_HEADER *)rxBuf;
jrodenburg 8:dbf8bd4815f8 372
jrodenburg 8:dbf8bd4815f8 373 #ifdef DEBUG
jrodenburg 15:74a01aaeb60e 374 pc.printf("DBG: fl=%02x, len=%02x\n", pRxPktHdr->SOF_flag, pRxPktHdr->len);
jrodenburg 8:dbf8bd4815f8 375 #endif
jrodenburg 8:dbf8bd4815f8 376
jrodenburg 8:dbf8bd4815f8 377 // Exit if the packet does not contain correct header
jrodenburg 8:dbf8bd4815f8 378 // Maybe send NAK?
jrodenburg 8:dbf8bd4815f8 379 if ((pRxPktHdr->SOF_flag != RX_SOF) || (pRxPktHdr->Delim != DELIMETER))
jrodenburg 8:dbf8bd4815f8 380 return;
jrodenburg 2:bd118a724f03 381
jrodenburg 8:dbf8bd4815f8 382 switch (pRxPktHdr->cmd_type)
jrodenburg 8:dbf8bd4815f8 383 {
jrodenburg 8:dbf8bd4815f8 384 case SET_TEMPERATURE:
jrodenburg 8:dbf8bd4815f8 385 // Process set temp for specified channel
jrodenburg 8:dbf8bd4815f8 386 {
jrodenburg 8:dbf8bd4815f8 387 SET_TEMPERATURE_CMD *pRxPkt = (SET_TEMPERATURE_CMD *)(rxBuf);
jrodenburg 15:74a01aaeb60e 388 #ifdef DEBUG
jrodenburg 15:74a01aaeb60e 389 pc.printf("DBG: SETTEMP: ch = %02x, tempSet = %f, chanStat = %02x\n",
jrodenburg 15:74a01aaeb60e 390 pRxPkt->chanID, pRxPkt->setTemp, pRxPkt->chanStat);
jrodenburg 8:dbf8bd4815f8 391 #endif
jrodenburg 8:dbf8bd4815f8 392 if ((pRxPkt->tempDelim != DELIMETER) || (pRxPkt->chanStatDelim != DELIMETER)) {
jrodenburg 8:dbf8bd4815f8 393 // Send NAK back
jrodenburg 8:dbf8bd4815f8 394 pc.printf("DBG: Error\n");
jrodenburg 2:bd118a724f03 395 }
jrodenburg 8:dbf8bd4815f8 396 else {
jrodenburg 8:dbf8bd4815f8 397 chanSel_SetTemp = pRxPkt->chanID;
jrodenburg 8:dbf8bd4815f8 398 chnlStatus[pRxPkt->chanID].status = pRxPkt->chanStat;
jrodenburg 8:dbf8bd4815f8 399 chnlStatus[pRxPkt->chanID].setTemp = pRxPkt->setTemp;
jrodenburg 9:1cada1fe4743 400 chnlStatus[pRxPkt->chanID].error = 0;
jrodenburg 14:69cd53434783 401 chnlStatus[pRxPkt->chanID].heater_init_time = 0;
jrodenburg 8:dbf8bd4815f8 402 newTempSet = true;
jrodenburg 2:bd118a724f03 403 }
jrodenburg 2:bd118a724f03 404 }
jrodenburg 8:dbf8bd4815f8 405 break;
jrodenburg 8:dbf8bd4815f8 406
jrodenburg 8:dbf8bd4815f8 407 case SELECT_CHANNEL:
jrodenburg 8:dbf8bd4815f8 408 // Select channel to send temp data to
jrodenburg 8:dbf8bd4815f8 409 {
jrodenburg 8:dbf8bd4815f8 410 SELECT_CHANNEL_CMD *pRxPkt = (SELECT_CHANNEL_CMD *)(rxBuf);
jrodenburg 8:dbf8bd4815f8 411
jrodenburg 8:dbf8bd4815f8 412 chanSel_SendTemp = pRxPkt->chanIDSel;
jrodenburg 15:74a01aaeb60e 413 #ifdef DEBUG
jrodenburg 15:74a01aaeb60e 414 pc.printf("DBG: CHAN_SEL: chan=%02x, chanStat = %02x\n", pRxPkt->chanIDSel, pRxPkt->chanStat);
jrodenburg 8:dbf8bd4815f8 415 #endif
jrodenburg 2:bd118a724f03 416 }
jrodenburg 8:dbf8bd4815f8 417 break;
jrodenburg 8:dbf8bd4815f8 418
jrodenburg 15:74a01aaeb60e 419 case READ_UID:
jrodenburg 15:74a01aaeb60e 420 {
jrodenburg 15:74a01aaeb60e 421 READ_UID_CMD *pRxPkt = (READ_UID_CMD *)(rxBuf);
jrodenburg 15:74a01aaeb60e 422 #ifdef DEBUG
jrodenburg 15:74a01aaeb60e 423 pc.printf("DBG: Read UID: chan%02x\n", pRxPkt->chanIDSel);
jrodenburg 15:74a01aaeb60e 424 #endif
jrodenburg 15:74a01aaeb60e 425 sendUID();
jrodenburg 15:74a01aaeb60e 426 }
jrodenburg 15:74a01aaeb60e 427 break;
jrodenburg 15:74a01aaeb60e 428
jrodenburg 8:dbf8bd4815f8 429 default:
jrodenburg 8:dbf8bd4815f8 430 // Error
jrodenburg 8:dbf8bd4815f8 431 break;
jrodenburg 2:bd118a724f03 432 }
jrodenburg 1:0182b86f9bd4 433 }
jrodenburg 0:a28a1035c31b 434
jrodenburg 0:a28a1035c31b 435 /* Function: get_temp
jrodenburg 0:a28a1035c31b 436 **************************************************************
jrodenburg 0:a28a1035c31b 437 Description: Retrieve data from thermistor
jrodenburg 9:1cada1fe4743 438 Receives: chn: the channel of the fixture to read temp. from
jrodenburg 5:0f38a0bd4f86 439 port: the I/O channel to read
jrodenburg 0:a28a1035c31b 440 Returns: the temperature of the fixture (front or back)
jrodenburg 0:a28a1035c31b 441 */
jrodenburg 0:a28a1035c31b 442
jrodenburg 17:5098d8fbb298 443 float get_temp(float ADC_val){
jrodenburg 1:0182b86f9bd4 444 myled = 1;
jrodenburg 17:5098d8fbb298 445 //ltc2487.setAddress(addrLUT[chn].adc);
jrodenburg 9:1cada1fe4743 446
jrodenburg 17:5098d8fbb298 447 //float ADC_val = ltc2487.readOutput(port); //(65536*1.334)/2.5
jrodenburg 9:1cada1fe4743 448
jrodenburg 0:a28a1035c31b 449 int i = 0;
jrodenburg 9:1cada1fe4743 450
jrodenburg 0:a28a1035c31b 451 while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){
jrodenburg 9:1cada1fe4743 452 i++;
jrodenburg 0:a28a1035c31b 453 } //find the temp. above therm temp
jrodenburg 9:1cada1fe4743 454
jrodenburg 9:1cada1fe4743 455 //Point slope formula extrapolation:
jrodenburg 14:69cd53434783 456 // m = (y1-y0)/(x1-x0)+ y0 , y = temp. value, x = adc value
jrodenburg 0:a28a1035c31b 457 // y1 = thermLUT[i-1].temp y0 = thermLUT[i].temp
jrodenburg 0:a28a1035c31b 458 // x1 = thermLUT[i-1].adc x0 =thermLUT[i].adc
jrodenburg 0:a28a1035c31b 459 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 460 float b = float(thermLUT[i-1].adc - thermLUT[i].adc); //slope of adc between points where therm adc is between (Amax - Amin)
jrodenburg 9:1cada1fe4743 461
jrodenburg 0:a28a1035c31b 462 float m = a/b;
jrodenburg 0:a28a1035c31b 463 float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp;
jrodenburg 9:1cada1fe4743 464
jrodenburg 17:5098d8fbb298 465 if(DEBUG3) pc.printf("DBG: ADC VAL: %f TEMP: %f \r\n", ADC_val, y);
jrodenburg 9:1cada1fe4743 466
jrodenburg 9:1cada1fe4743 467 return y;
jrodenburg 0:a28a1035c31b 468 }
jrodenburg 0:a28a1035c31b 469
jrodenburg 17:5098d8fbb298 470 void get_temp_data(){
jrodenburg 17:5098d8fbb298 471 //Write to all 8 channels selecting the front port to read
jrodenburg 17:5098d8fbb298 472 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 17:5098d8fbb298 473 ltc2487.setAddress(addrLUT[chnl].adc);
jrodenburg 17:5098d8fbb298 474 ltc2487.writePort(FRONT_THERM);
jrodenburg 17:5098d8fbb298 475 }
jrodenburg 17:5098d8fbb298 476 //wait until next clock cycle on LTC
jrodenburg 17:5098d8fbb298 477 wait(0.08);
jrodenburg 17:5098d8fbb298 478 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 17:5098d8fbb298 479 ltc2487.setAddress(addrLUT[chnl].adc);
jrodenburg 17:5098d8fbb298 480 channelTempData[chnl].currTempFront = get_temp(ltc2487.read());
jrodenburg 17:5098d8fbb298 481 }
jrodenburg 17:5098d8fbb298 482 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 17:5098d8fbb298 483 ltc2487.setAddress(addrLUT[chnl].adc);
jrodenburg 17:5098d8fbb298 484 ltc2487.writePort(BACK_THERM);
jrodenburg 17:5098d8fbb298 485 }
jrodenburg 17:5098d8fbb298 486 //wait until next clock cycle on LTC
jrodenburg 17:5098d8fbb298 487 wait(0.08);
jrodenburg 17:5098d8fbb298 488 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 17:5098d8fbb298 489 ltc2487.setAddress(addrLUT[chnl].adc);
jrodenburg 17:5098d8fbb298 490 channelTempData[chnl].currTempBack = get_temp(ltc2487.read());
jrodenburg 17:5098d8fbb298 491 }
jrodenburg 17:5098d8fbb298 492 }
jrodenburg 17:5098d8fbb298 493
jrodenburg 0:a28a1035c31b 494 /* Function: get_heater_current
jrodenburg 0:a28a1035c31b 495 **************************************************************
jrodenburg 0:a28a1035c31b 496 Description: Retrieve current into heater control MOSFET
jrodenburg 9:1cada1fe4743 497 Receives: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 498 Returns: the current into the heater control MOSFET
jrodenburg 0:a28a1035c31b 499 */
jrodenburg 0:a28a1035c31b 500
jrodenburg 6:c980535393ed 501 float get_heater_current(int chn, int port){
jrodenburg 17:5098d8fbb298 502 //return ltc2487.readOutput(port);
jrodenburg 0:a28a1035c31b 503 }
jrodenburg 0:a28a1035c31b 504
jrodenburg 0:a28a1035c31b 505 /* Function: get_valve_current
jrodenburg 0:a28a1035c31b 506 **************************************************************
jrodenburg 0:a28a1035c31b 507 Description: Retrieve current into valve control MOSFET
jrodenburg 9:1cada1fe4743 508 Receives: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 509 Returns: the current into the valve control MOSFET
jrodenburg 0:a28a1035c31b 510 */
jrodenburg 0:a28a1035c31b 511
jrodenburg 6:c980535393ed 512 float get_valve_current(int chn, int port){
jrodenburg 17:5098d8fbb298 513 //return ltc2487.readOutput(port);
jrodenburg 0:a28a1035c31b 514 }
jrodenburg 0:a28a1035c31b 515
jrodenburg 0:a28a1035c31b 516 /* Function: turn_valve_on
jrodenburg 0:a28a1035c31b 517 **************************************************************
jrodenburg 1:0182b86f9bd4 518 Description: Turn valve on and green status LED on
jrodenburg 9:1cada1fe4743 519 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 520 Returns: N/A
jrodenburg 0:a28a1035c31b 521 */
jrodenburg 0:a28a1035c31b 522
jrodenburg 1:0182b86f9bd4 523 void turn_valve_on(int chn){
jrodenburg 0:a28a1035c31b 524 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 525 io_control.init();
jrodenburg 1:0182b86f9bd4 526 io_control.writeOutput(1,0,1,0);
jrodenburg 0:a28a1035c31b 527 }
jrodenburg 0:a28a1035c31b 528
jrodenburg 0:a28a1035c31b 529 /* Function: turn_valve_off
jrodenburg 0:a28a1035c31b 530 **************************************************************
jrodenburg 1:0182b86f9bd4 531 Description: Turn valve off and green status LED on
jrodenburg 9:1cada1fe4743 532 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 533 Returns: N/A
jrodenburg 0:a28a1035c31b 534 */
jrodenburg 0:a28a1035c31b 535
jrodenburg 1:0182b86f9bd4 536 void turn_valve_off(int chn){
jrodenburg 0:a28a1035c31b 537 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 538 io_control.init();
jrodenburg 1:0182b86f9bd4 539 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 540 }
jrodenburg 0:a28a1035c31b 541
jrodenburg 0:a28a1035c31b 542 /* Function: turn_heater_on
jrodenburg 0:a28a1035c31b 543 **************************************************************
jrodenburg 1:0182b86f9bd4 544 Description: Turn heater on and green status LED on
jrodenburg 9:1cada1fe4743 545 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 546 Returns: N/A
jrodenburg 0:a28a1035c31b 547 */
jrodenburg 0:a28a1035c31b 548
jrodenburg 1:0182b86f9bd4 549 void turn_heater_on(int chn){
jrodenburg 0:a28a1035c31b 550 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 551 io_control.init();
jrodenburg 1:0182b86f9bd4 552 io_control.writeOutput(0,1,1,0);
jrodenburg 0:a28a1035c31b 553 }
jrodenburg 0:a28a1035c31b 554
jrodenburg 0:a28a1035c31b 555 /* Function: turn_heater_off
jrodenburg 0:a28a1035c31b 556 **************************************************************
jrodenburg 1:0182b86f9bd4 557 Description: Turn heater off and green status LED on
jrodenburg 9:1cada1fe4743 558 Receives: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 559 Returns: N/A
jrodenburg 0:a28a1035c31b 560 */
jrodenburg 0:a28a1035c31b 561
jrodenburg 1:0182b86f9bd4 562 void turn_heater_off(int chn){
jrodenburg 0:a28a1035c31b 563 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 564 io_control.init();
jrodenburg 1:0182b86f9bd4 565 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 566 }
jrodenburg 0:a28a1035c31b 567
jrodenburg 0:a28a1035c31b 568 /* Function: status_led
jrodenburg 0:a28a1035c31b 569 **************************************************************
jrodenburg 0:a28a1035c31b 570 Description: Turn status LED on (turns on green or red)
jrodenburg 9:1cada1fe4743 571 Receives: chn: the channel of the fixture
jrodenburg 0:a28a1035c31b 572 status: the status of channel (good (1) or bad (0))
jrodenburg 1:0182b86f9bd4 573 Returns: N/A
jrodenburg 0:a28a1035c31b 574 */
jrodenburg 0:a28a1035c31b 575
jrodenburg 1:0182b86f9bd4 576 void status_led(int chn, int status){
jrodenburg 0:a28a1035c31b 577 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 578 if(status){
jrodenburg 1:0182b86f9bd4 579 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 580 }
jrodenburg 0:a28a1035c31b 581 else{
jrodenburg 8:dbf8bd4815f8 582 //turn valve on too
jrodenburg 8:dbf8bd4815f8 583 io_control.writeOutput(1,0,0,1);
jrodenburg 0:a28a1035c31b 584 }
jrodenburg 0:a28a1035c31b 585 }
jrodenburg 0:a28a1035c31b 586
jrodenburg 1:0182b86f9bd4 587 /* Function: test_mcp23008
jrodenburg 1:0182b86f9bd4 588 **************************************************************
jrodenburg 1:0182b86f9bd4 589 Description: Test each output of the MCP23009
jrodenburg 9:1cada1fe4743 590 Receives: N/A
jrodenburg 1:0182b86f9bd4 591 Returns: N/A
jrodenburg 1:0182b86f9bd4 592 */
jrodenburg 1:0182b86f9bd4 593
jrodenburg 1:0182b86f9bd4 594 void test_mcp23008(int chn){
jrodenburg 1:0182b86f9bd4 595 turn_valve_on(chn);
jrodenburg 7:8a5e65e63e2a 596 wait(0.5);
jrodenburg 1:0182b86f9bd4 597 turn_valve_off(chn);
jrodenburg 7:8a5e65e63e2a 598 wait(0.5);
jrodenburg 1:0182b86f9bd4 599 turn_heater_on(chn);
jrodenburg 7:8a5e65e63e2a 600 wait(0.5);
jrodenburg 1:0182b86f9bd4 601 turn_heater_off(chn);
jrodenburg 7:8a5e65e63e2a 602 wait(0.5);
jrodenburg 1:0182b86f9bd4 603 status_led(chn, 0);
jrodenburg 7:8a5e65e63e2a 604 wait(0.5);
jrodenburg 1:0182b86f9bd4 605 status_led(chn, 1);
jrodenburg 1:0182b86f9bd4 606 }
jrodenburg 1:0182b86f9bd4 607
jrodenburg 2:bd118a724f03 608 /* Function: test_ltc2487
jrodenburg 2:bd118a724f03 609 **************************************************************
jrodenburg 2:bd118a724f03 610 Description: Test the reading from LTC2487
jrodenburg 9:1cada1fe4743 611 Receives: N/A
jrodenburg 2:bd118a724f03 612 Returns: N/A
jrodenburg 2:bd118a724f03 613 */
jrodenburg 2:bd118a724f03 614
jrodenburg 2:bd118a724f03 615 void test_ltc2487(int chn){
jrodenburg 17:5098d8fbb298 616 //get_temp(FRONT_THERM);
jrodenburg 17:5098d8fbb298 617 //wait(0.1);
jrodenburg 17:5098d8fbb298 618 //get_temp(BACK_THERM);
jrodenburg 17:5098d8fbb298 619 //wait(0.1);
jrodenburg 17:5098d8fbb298 620 //get_temp(VALVE_FET_AMP);
jrodenburg 17:5098d8fbb298 621 //wait(0.1);
jrodenburg 4:168a446bd0da 622 //if(DEBUG1) pc.printf("TEMPERATURE READING: %f \r\n", get_temp(chn));
jrodenburg 2:bd118a724f03 623 }
jrodenburg 2:bd118a724f03 624
jrodenburg 8:dbf8bd4815f8 625 //***************************************************************
jrodenburg 8:dbf8bd4815f8 626 // Build packet with temperature readings to send to GUI
jrodenburg 8:dbf8bd4815f8 627 //***************************************************************
jrodenburg 17:5098d8fbb298 628 void sendTempReadings(){
jrodenburg 16:a7fd53ab4199 629 RESPONSE_TEMP_CMD response;
jrodenburg 8:dbf8bd4815f8 630 unsigned char *ptr = (unsigned char *)&response;
jrodenburg 8:dbf8bd4815f8 631 int i;
jrodenburg 8:dbf8bd4815f8 632
jrodenburg 8:dbf8bd4815f8 633 response.SOF_flag = TX_SOF;
jrodenburg 15:74a01aaeb60e 634 response.cmd_type = TEMP_DATA;
jrodenburg 16:a7fd53ab4199 635 response.len = 5+(sizeof(response.chTemp));
jrodenburg 8:dbf8bd4815f8 636 response.Delim = DELIMETER;
jrodenburg 16:a7fd53ab4199 637 response.chTemp[0] = channelTempData[0].currTempBack;
jrodenburg 16:a7fd53ab4199 638 response.chTemp[1] = channelTempData[1].currTempBack;
jrodenburg 16:a7fd53ab4199 639 response.chTemp[2] = channelTempData[2].currTempBack;
jrodenburg 16:a7fd53ab4199 640 response.chTemp[3] = channelTempData[3].currTempBack;
jrodenburg 16:a7fd53ab4199 641 response.chTemp[4] = channelTempData[4].currTempBack;
jrodenburg 16:a7fd53ab4199 642 response.chTemp[5] = channelTempData[5].currTempBack;
jrodenburg 16:a7fd53ab4199 643 response.chTemp[6] = channelTempData[6].currTempBack;
jrodenburg 16:a7fd53ab4199 644 response.chTemp[7] = channelTempData[7].currTempBack;
jrodenburg 16:a7fd53ab4199 645 /*float *dst = (float *)&response.chTemp[0];
jrodenburg 16:a7fd53ab4199 646 float *src = (float *)&channelTempData[0];
jrodenburg 16:a7fd53ab4199 647 memcpy(dst, src, sizeof(channelTempData));*/
jrodenburg 8:dbf8bd4815f8 648 response.EOF_flag = TX_EOF;
jrodenburg 8:dbf8bd4815f8 649
jrodenburg 8:dbf8bd4815f8 650 // Send response to GUI
jrodenburg 8:dbf8bd4815f8 651 for (i=0; i < response.len; i++, ptr++)
jrodenburg 8:dbf8bd4815f8 652 pc.printf("%02x", *ptr);
jrodenburg 8:dbf8bd4815f8 653 pc.printf("\n");
jrodenburg 8:dbf8bd4815f8 654 }
jrodenburg 8:dbf8bd4815f8 655
jrodenburg 9:1cada1fe4743 656 //***************************************************************
jrodenburg 15:74a01aaeb60e 657 // Build packet with Board UID (Unique Identification)
jrodenburg 15:74a01aaeb60e 658 //***************************************************************
jrodenburg 15:74a01aaeb60e 659 void sendUID ()
jrodenburg 15:74a01aaeb60e 660 {
jrodenburg 15:74a01aaeb60e 661 UID_RESPONSE response;
jrodenburg 15:74a01aaeb60e 662 unsigned char *ptr = (unsigned char *)&response;
jrodenburg 15:74a01aaeb60e 663 int i;
jrodenburg 15:74a01aaeb60e 664
jrodenburg 15:74a01aaeb60e 665 response.SOF_flag = TX_SOF;
jrodenburg 15:74a01aaeb60e 666 response.cmd_type = UID_DATA;
jrodenburg 15:74a01aaeb60e 667 response.len = 17;
jrodenburg 15:74a01aaeb60e 668 response.Delim = DELIMETER;
jrodenburg 15:74a01aaeb60e 669 response.UIDMH = (uint32_t)SIM->UIDMH;
jrodenburg 15:74a01aaeb60e 670 response.UIDML = (uint32_t)SIM->UIDML;
jrodenburg 15:74a01aaeb60e 671 response.UIDL = (uint32_t)SIM->UIDL;
jrodenburg 15:74a01aaeb60e 672 response.EOF_flag = TX_EOF;
jrodenburg 15:74a01aaeb60e 673
jrodenburg 15:74a01aaeb60e 674 // Send response to GUI
jrodenburg 15:74a01aaeb60e 675 for (i=0; i < response.len; i++, ptr++)
jrodenburg 15:74a01aaeb60e 676 pc.printf("%02x", *ptr);
jrodenburg 15:74a01aaeb60e 677 pc.printf("\n");
jrodenburg 15:74a01aaeb60e 678 }
jrodenburg 15:74a01aaeb60e 679
jrodenburg 15:74a01aaeb60e 680 //***************************************************************
jrodenburg 9:1cada1fe4743 681 // Build packet with errors to send to GUI
jrodenburg 9:1cada1fe4743 682 //***************************************************************
jrodenburg 9:1cada1fe4743 683 void sendError (int chan, float error)
jrodenburg 9:1cada1fe4743 684 {
jrodenburg 9:1cada1fe4743 685 RESPONSE_CMD response;
jrodenburg 9:1cada1fe4743 686 unsigned char *ptr = (unsigned char *)&response;
jrodenburg 9:1cada1fe4743 687 int i;
jrodenburg 9:1cada1fe4743 688
jrodenburg 9:1cada1fe4743 689 response.SOF_flag = TX_SOF;
jrodenburg 9:1cada1fe4743 690 response.cmd_type = ERROR_DATA;
jrodenburg 9:1cada1fe4743 691 response.len = 9;
jrodenburg 9:1cada1fe4743 692 response.Delim = DELIMETER;
jrodenburg 9:1cada1fe4743 693 response.data = (float)error;
jrodenburg 9:1cada1fe4743 694 response.EOF_flag = TX_EOF;
jrodenburg 9:1cada1fe4743 695
jrodenburg 9:1cada1fe4743 696 // Send response to GUI
jrodenburg 9:1cada1fe4743 697 for (i=0; i < response.len; i++, ptr++)
jrodenburg 9:1cada1fe4743 698 pc.printf("%02x", *ptr);
jrodenburg 9:1cada1fe4743 699 pc.printf("\n");
jrodenburg 9:1cada1fe4743 700 }
jrodenburg 6:c980535393ed 701
jrodenburg 14:69cd53434783 702 /* Function: error_check
jrodenburg 14:69cd53434783 703 **************************************************************
jrodenburg 14:69cd53434783 704 Description: Checks for any system errors
jrodenburg 14:69cd53434783 705 Recieves: frontTemp: temp. of front thermistor
jrodenburg 14:69cd53434783 706 backTemp: temp. of back thermistor
jrodenburg 14:69cd53434783 707 currTimeMin: currentTime in minutes
jrodenburg 14:69cd53434783 708 Returns: N/A
jrodenburg 14:69cd53434783 709 */
jrodenburg 14:69cd53434783 710
jrodenburg 14:69cd53434783 711 void error_check(int chnl, float currentTempFront, float currentTempBack, int currTimeMin){
jrodenburg 14:69cd53434783 712 if((currentTempFront >= MAX_TEMP) && (currentTempBack >= MAX_TEMP)){
jrodenburg 14:69cd53434783 713 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 714 sendError(chnl, 1);
jrodenburg 14:69cd53434783 715 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 716 if(DEBUG3) pc.printf("DBG: [%d] ERROR 1 \r\n", chnl);
jrodenburg 14:69cd53434783 717 }
jrodenburg 14:69cd53434783 718 if(((currentTempFront == 0) || (currentTempBack == 0))){
jrodenburg 14:69cd53434783 719 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 720 sendError(chnl, 2);
jrodenburg 14:69cd53434783 721 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 722 if(DEBUG3) pc.printf("DBG: [%d] ERROR 2 \r\n", chnl);
jrodenburg 14:69cd53434783 723 }
jrodenburg 14:69cd53434783 724 if((abs(currentTempBack - currentTempFront) >= TEMP_MARGIN)){
jrodenburg 14:69cd53434783 725 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 726 sendError(chnl, 3);
jrodenburg 14:69cd53434783 727 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 728 if(DEBUG3) pc.printf("DBG: [%d] ERROR 3 \r\n", chnl);
jrodenburg 14:69cd53434783 729 }
jrodenburg 14:69cd53434783 730 if(((currentTempFront <= MIN_TEMP) && (currentTempBack <= MIN_TEMP))){
jrodenburg 14:69cd53434783 731 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 732 sendError(chnl, 4);
jrodenburg 14:69cd53434783 733 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 734 if(DEBUG3) pc.printf("DBG: [%d] ERROR 4 \r\n", chnl);
jrodenburg 14:69cd53434783 735 }
jrodenburg 14:69cd53434783 736 if(chnlStatus[chnl].heater_init_time != 0){
jrodenburg 14:69cd53434783 737 int init_time = chnlStatus[chnl].heater_init_time;
jrodenburg 15:74a01aaeb60e 738 int on_time_heater = currTimeMin - init_time;
jrodenburg 14:69cd53434783 739 //account for 0 crossover
jrodenburg 14:69cd53434783 740 if(init_time > currTimeMin){
jrodenburg 14:69cd53434783 741 on_time_heater = (60 - init_time)+currTimeMin;
jrodenburg 14:69cd53434783 742 }
jrodenburg 14:69cd53434783 743
jrodenburg 14:69cd53434783 744 if(on_time_heater > MAX_HEATER_ON_TIME){
jrodenburg 14:69cd53434783 745 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 746 sendError(chnl, 5);
jrodenburg 14:69cd53434783 747 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 748 chnlStatus[chnl].heater_init_time = 0;
jrodenburg 14:69cd53434783 749 if(DEBUG3) pc.printf("DBG: [%d] ERROR 5 \r\n", chnl);
jrodenburg 14:69cd53434783 750 }
jrodenburg 14:69cd53434783 751 }
jrodenburg 14:69cd53434783 752 if(chnlStatus[chnl].error == 1){
jrodenburg 14:69cd53434783 753 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 754 chnlStatus[chnl].error = 1;
jrodenburg 14:69cd53434783 755 }
jrodenburg 14:69cd53434783 756 }
jrodenburg 14:69cd53434783 757
jrodenburg 6:c980535393ed 758
jrodenburg 6:c980535393ed 759 /*************************************************************/
jrodenburg 6:c980535393ed 760 /* MAIN FUNCTION */
jrodenburg 6:c980535393ed 761 /*************************************************************/
jrodenburg 0:a28a1035c31b 762
jrodenburg 9:1cada1fe4743 763 int main() {
jrodenburg 14:69cd53434783 764 //variables for controlling board
jrodenburg 9:1cada1fe4743 765 Timer t;
jrodenburg 14:69cd53434783 766 Timer t_cool;
jrodenburg 14:69cd53434783 767 int time_min = 0;
jrodenburg 14:69cd53434783 768 int init_heat_time = 0;
jrodenburg 15:74a01aaeb60e 769 uint32_t UIDMH, UIDML, UIDL;
jrodenburg 8:dbf8bd4815f8 770
jrodenburg 8:dbf8bd4815f8 771 // Setup serial port
jrodenburg 8:dbf8bd4815f8 772 // Look for RX_EOF
jrodenburg 8:dbf8bd4815f8 773 pc.baud(9600);
jrodenburg 8:dbf8bd4815f8 774 pc.autoDetectChar(RX_EOF);
jrodenburg 8:dbf8bd4815f8 775 pc.attach(&rxInterrupt, MODSERIAL::RxAutoDetect);
jrodenburg 8:dbf8bd4815f8 776
jrodenburg 8:dbf8bd4815f8 777 myled = 1;
jrodenburg 9:1cada1fe4743 778 rLed = 1;
jrodenburg 9:1cada1fe4743 779 gLed = 0;
jrodenburg 9:1cada1fe4743 780
jrodenburg 8:dbf8bd4815f8 781 t.start();
jrodenburg 9:1cada1fe4743 782
jrodenburg 15:74a01aaeb60e 783 sendUID();
jrodenburg 15:74a01aaeb60e 784
jrodenburg 15:74a01aaeb60e 785 UIDMH = (uint32_t)SIM->UIDMH;
jrodenburg 15:74a01aaeb60e 786 UIDML = (uint32_t)SIM->UIDML;
jrodenburg 15:74a01aaeb60e 787 UIDL = (uint32_t)SIM->UIDL;
jrodenburg 15:74a01aaeb60e 788
jrodenburg 0:a28a1035c31b 789 while(1) {
jrodenburg 17:5098d8fbb298 790 //test_mcp23008(0);
jrodenburg 15:74a01aaeb60e 791 if(DEBUG3) {
jrodenburg 15:74a01aaeb60e 792 pc.printf("DBG: <%f>[UID: %04X%08X%08X] PSTARTED\n", t.read(), UIDMH, UIDML, UIDL);
jrodenburg 15:74a01aaeb60e 793 }
jrodenburg 9:1cada1fe4743 794
jrodenburg 16:a7fd53ab4199 795 //SETUP TIMER USED TO TRACK HOW LONG THE HEATER IS ON
jrodenburg 14:69cd53434783 796 float time_sec = t.read();
jrodenburg 14:69cd53434783 797
jrodenburg 13:604e6933366f 798 if(time_sec >= 60){
jrodenburg 14:69cd53434783 799 time_min++;
jrodenburg 14:69cd53434783 800 //time_sec = 0;
jrodenburg 13:604e6933366f 801 t.reset();
jrodenburg 13:604e6933366f 802 }
jrodenburg 14:69cd53434783 803
jrodenburg 14:69cd53434783 804 if(time_min >= 60){
jrodenburg 14:69cd53434783 805 time_min = 0;
jrodenburg 14:69cd53434783 806 }
jrodenburg 17:5098d8fbb298 807
jrodenburg 17:5098d8fbb298 808 //GET TEMPERATURE DATA
jrodenburg 17:5098d8fbb298 809 get_temp_data();
jrodenburg 14:69cd53434783 810
jrodenburg 16:a7fd53ab4199 811 //SEND TEMPERATURE DATA FOR LOGGING
jrodenburg 16:a7fd53ab4199 812 sendTempReadings();
jrodenburg 17:5098d8fbb298 813
jrodenburg 17:5098d8fbb298 814 time_sec = t.read();
jrodenburg 17:5098d8fbb298 815 pc.printf("\r TIME: %f s\r\n", time_sec);
jrodenburg 13:604e6933366f 816
jrodenburg 16:a7fd53ab4199 817 //CONTROL LOOP: LOOPS THROUGH EVERY CHANNEL AND CONTROLS THE FIXTURE IN RESPONSE
jrodenburg 5:0f38a0bd4f86 818 for(int chnl = 0; chnl < CHN_COUNT; chnl++){
jrodenburg 9:1cada1fe4743 819
jrodenburg 8:dbf8bd4815f8 820 //check if we received data/need to update TCTF data
jrodenburg 8:dbf8bd4815f8 821 if(dataReceived){
jrodenburg 8:dbf8bd4815f8 822 dataReceived = false;
jrodenburg 15:74a01aaeb60e 823 #ifdef DEBUG
jrodenburg 9:1cada1fe4743 824 pc.printf("DBG: %d bytes Rx'd\n", pc.rxBufferGetCount());
jrodenburg 15:74a01aaeb60e 825 #endif
jrodenburg 8:dbf8bd4815f8 826 pc.move(rxBuf, 50);
jrodenburg 8:dbf8bd4815f8 827 parseRXData();
jrodenburg 8:dbf8bd4815f8 828 pc.rxBufferFlush();
jrodenburg 9:1cada1fe4743 829 }
jrodenburg 8:dbf8bd4815f8 830
jrodenburg 17:5098d8fbb298 831 //update channel temperature data
jrodenburg 17:5098d8fbb298 832 float currentTempFront = channelTempData[chnl].currTempFront;
jrodenburg 17:5098d8fbb298 833 float currentTempBack = channelTempData[chnl].currTempBack;
jrodenburg 17:5098d8fbb298 834
jrodenburg 17:5098d8fbb298 835 //float currentTempBack = get_temp(chnl, BACK_THERM);
jrodenburg 13:604e6933366f 836 float currentTemp = currentTempBack;
jrodenburg 13:604e6933366f 837
jrodenburg 14:69cd53434783 838 //Error check on fixture
jrodenburg 14:69cd53434783 839 error_check(chnl, currentTempFront, currentTempBack, time_min);
jrodenburg 9:1cada1fe4743 840
jrodenburg 16:a7fd53ab4199 841 pc.printf("TEMPERATURE: [%d] Temp: F: %f B: %f\r\n", chnl, currentTempFront, currentTempBack);
jrodenburg 16:a7fd53ab4199 842
jrodenburg 2:bd118a724f03 843 //CONTROL LOOP:
jrodenburg 4:168a446bd0da 844 if(chnlStatus[chnl].status == 1){
jrodenburg 15:74a01aaeb60e 845 if(DEBUG) pc.printf("DBG: [%d] Temp: F: %f B: %f\r\n", chnl, currentTempFront, currentTempBack);
jrodenburg 14:69cd53434783 846 //main loop for channels
jrodenburg 7:8a5e65e63e2a 847 if(chnlStatus[chnl].error == 0){
jrodenburg 9:1cada1fe4743 848 if(currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)){
jrodenburg 16:a7fd53ab4199 849 if(DEBUG) pc.printf("DBG: [%d] Chiller ON \r\n", chnl);
jrodenburg 14:69cd53434783 850 //reset heater on time
jrodenburg 14:69cd53434783 851 chnlStatus[chnl].heater_init_time = 0;
jrodenburg 14:69cd53434783 852 //reset cooling timer
jrodenburg 14:69cd53434783 853 float time_sec = 0;
jrodenburg 13:604e6933366f 854 //check if the temp. diff. is small and can't wait for an entire period (~4.18) to turn valve off
jrodenburg 13:604e6933366f 855 if(abs(currentTemp - (chnlStatus[chnl].setTemp)) < 5){
jrodenburg 14:69cd53434783 856 //start timer
jrodenburg 14:69cd53434783 857 t_cool.start();
jrodenburg 14:69cd53434783 858 //turn chiller on
jrodenburg 13:604e6933366f 859 turn_valve_on(chnl);
jrodenburg 14:69cd53434783 860 //read timer
jrodenburg 14:69cd53434783 861 time_sec = t_cool.read();
jrodenburg 14:69cd53434783 862 while((currentTemp > ((chnlStatus[chnl].setTemp)+HYST_HIGH)) && (time_sec < MAX_CHILL_TIME)){
jrodenburg 17:5098d8fbb298 863 //currentTemp = get_temp(chnl, BACK_THERM);
jrodenburg 14:69cd53434783 864 time_sec = t_cool.read();
jrodenburg 14:69cd53434783 865 if(DEBUG5) pc.printf("DBG: [%d] TIME: %f \r\n", chnl, time_sec);
jrodenburg 13:604e6933366f 866 }
jrodenburg 14:69cd53434783 867 time_sec = 0;
jrodenburg 14:69cd53434783 868 t_cool.stop();
jrodenburg 14:69cd53434783 869 t_cool.reset();
jrodenburg 13:604e6933366f 870 turn_valve_off(chnl);
jrodenburg 13:604e6933366f 871 }
jrodenburg 13:604e6933366f 872 else{
jrodenburg 13:604e6933366f 873 //Turn chiller on
jrodenburg 14:69cd53434783 874 turn_valve_on(chnl);
jrodenburg 13:604e6933366f 875 }
jrodenburg 7:8a5e65e63e2a 876 }
jrodenburg 8:dbf8bd4815f8 877 else if (currentTemp < ((chnlStatus[chnl].setTemp)-HYST_LOW)){
jrodenburg 16:a7fd53ab4199 878 if(DEBUG) pc.printf("DBG: [%d] Heater ON \r\n", chnl);
jrodenburg 14:69cd53434783 879 //establish starting time for heater
jrodenburg 14:69cd53434783 880 if(chnlStatus[chnl].heater_init_time == 0){ //check if it is the first time that heater has turned on
jrodenburg 14:69cd53434783 881 //make sure time isn't zero, otherwise keeping looping until we are not at 0
jrodenburg 14:69cd53434783 882 if(time_min != 0){
jrodenburg 14:69cd53434783 883 chnlStatus[chnl].heater_init_time = time_min;
jrodenburg 14:69cd53434783 884 }
jrodenburg 14:69cd53434783 885 }
jrodenburg 14:69cd53434783 886 if(DEBUG5) pc.printf("DBG: TIME ON: %d %d %f %d %d\r\n", chnlStatus[chnl].heater_init_time, time_min, time_sec, init_heat_time, (chnlStatus[chnl].heater_init_time == NULL));
jrodenburg 7:8a5e65e63e2a 887 //Turn heater on
jrodenburg 7:8a5e65e63e2a 888 turn_heater_on(chnl);
jrodenburg 7:8a5e65e63e2a 889 }
jrodenburg 7:8a5e65e63e2a 890 else{
jrodenburg 16:a7fd53ab4199 891 if(DEBUG) pc.printf("DBG: [%d] All OFF \r\n", chnl);
jrodenburg 14:69cd53434783 892 //reset heater on time
jrodenburg 14:69cd53434783 893 chnlStatus[chnl].heater_init_time = 0;
jrodenburg 7:8a5e65e63e2a 894 //turn off chiller
jrodenburg 7:8a5e65e63e2a 895 turn_valve_off(chnl);
jrodenburg 9:1cada1fe4743 896 //turn off heater
jrodenburg 7:8a5e65e63e2a 897 turn_heater_off(chnl);
jrodenburg 7:8a5e65e63e2a 898 //turn on green LED status light
jrodenburg 9:1cada1fe4743 899 status_led(chnl, 1);
jrodenburg 9:1cada1fe4743 900 }
jrodenburg 9:1cada1fe4743 901 }
jrodenburg 2:bd118a724f03 902 }
jrodenburg 13:604e6933366f 903 else{
jrodenburg 14:69cd53434783 904 if(chnlStatus[chnl].error == 0){
jrodenburg 15:74a01aaeb60e 905 //turn off chiller
jrodenburg 15:74a01aaeb60e 906 turn_valve_off(chnl);
jrodenburg 15:74a01aaeb60e 907 //turn off heater
jrodenburg 15:74a01aaeb60e 908 turn_heater_off(chnl);
jrodenburg 15:74a01aaeb60e 909 //turn on green LED status light
jrodenburg 15:74a01aaeb60e 910 status_led(chnl, 1);
jrodenburg 17:5098d8fbb298 911 }
jrodenburg 14:69cd53434783 912 else{
jrodenburg 14:69cd53434783 913 status_led(chnl, STATUS_BAD);
jrodenburg 14:69cd53434783 914 }
jrodenburg 14:69cd53434783 915 //reset heater on time
jrodenburg 14:69cd53434783 916 chnlStatus[chnl].heater_init_time = 0;
jrodenburg 13:604e6933366f 917 }
jrodenburg 2:bd118a724f03 918 }
jrodenburg 0:a28a1035c31b 919 }
jrodenburg 9:1cada1fe4743 920 }