Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Committer:
jrodenburg
Date:
Mon Feb 05 06:41:23 2018 +0000
Revision:
1:0182b86f9bd4
Parent:
0:a28a1035c31b
Child:
2:bd118a724f03
The ADC and GPIO chips are working;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jrodenburg 0:a28a1035c31b 1 // MBED SCRIPT FOR CONTROLLING THE TEMPERATURE CONTROLLED TEST FIXTURE
jrodenburg 0:a28a1035c31b 2 // AUTHOR: JUSTIN RODENBURG
jrodenburg 0:a28a1035c31b 3 // DATE: SEPTEMBER 2017
jrodenburg 0:a28a1035c31b 4
jrodenburg 0:a28a1035c31b 5 #include "mbed.h"
jrodenburg 0:a28a1035c31b 6 #include "MODSERIAL.h"
jrodenburg 0:a28a1035c31b 7 #include "MCP23008.h"
jrodenburg 0:a28a1035c31b 8 #include "LTC2487.h"
jrodenburg 0:a28a1035c31b 9
jrodenburg 0:a28a1035c31b 10 //DEFINITIVE VARIABLES
jrodenburg 1:0182b86f9bd4 11 #define DEBUG 1
jrodenburg 0:a28a1035c31b 12 #define CHN_COUNT 48
jrodenburg 0:a28a1035c31b 13 #define MIN_TEMP 15
jrodenburg 0:a28a1035c31b 14 #define MAX_TEMP 60
jrodenburg 0:a28a1035c31b 15 #define HYST 0.2
jrodenburg 0:a28a1035c31b 16 #define SAMPLES 5
jrodenburg 0:a28a1035c31b 17 #define I2C_Freq 2000
jrodenburg 1:0182b86f9bd4 18 #define VALVE 1
jrodenburg 1:0182b86f9bd4 19 #define HEATER 2
jrodenburg 1:0182b86f9bd4 20 #define STATUS_GOOD 3
jrodenburg 1:0182b86f9bd4 21 #define STATUS_BAD 4
jrodenburg 0:a28a1035c31b 22 #define sizeLUT 34
jrodenburg 0:a28a1035c31b 23 #define FRONT_THERM 0
jrodenburg 0:a28a1035c31b 24 #define BACK_THERM 1
jrodenburg 0:a28a1035c31b 25 #define HEAT_FET_AMP 2
jrodenburg 0:a28a1035c31b 26 #define VALV_FET_AMP 3
jrodenburg 0:a28a1035c31b 27
jrodenburg 0:a28a1035c31b 28 //I2C AADRESS LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 0:a28a1035c31b 29 const struct I2C_ADDR_LUT{
jrodenburg 0:a28a1035c31b 30 int adc;
jrodenburg 0:a28a1035c31b 31 int io;
jrodenburg 0:a28a1035c31b 32 };
jrodenburg 0:a28a1035c31b 33
jrodenburg 0:a28a1035c31b 34 I2C_ADDR_LUT addrLUT[] = {
jrodenburg 0:a28a1035c31b 35 {0x23, 0x10},
jrodenburg 0:a28a1035c31b 36 {0x53, 0x60},
jrodenburg 0:a28a1035c31b 37 {0x43, 0x70},
jrodenburg 0:a28a1035c31b 38 {0x73, 0x40},
jrodenburg 0:a28a1035c31b 39 {0x63, 0x50},
jrodenburg 0:a28a1035c31b 40 {0x22, 0x11},
jrodenburg 0:a28a1035c31b 41 {0x52, 0x61},
jrodenburg 0:a28a1035c31b 42 {0x42, 0x71},
jrodenburg 0:a28a1035c31b 43 {0x72, 0x41},
jrodenburg 0:a28a1035c31b 44 {0x62, 0x51},
jrodenburg 0:a28a1035c31b 45 {0x21, 0x12},
jrodenburg 0:a28a1035c31b 46 {0x51, 0x62},
jrodenburg 0:a28a1035c31b 47 {0x41, 0x72},
jrodenburg 0:a28a1035c31b 48 {0x71, 0x42},
jrodenburg 0:a28a1035c31b 49 {0x61, 0x52},
jrodenburg 0:a28a1035c31b 50 {0x20, 0x13},
jrodenburg 0:a28a1035c31b 51 {0x50, 0x63},
jrodenburg 0:a28a1035c31b 52 {0x40, 0x73},
jrodenburg 0:a28a1035c31b 53 {0x70, 0x43},
jrodenburg 0:a28a1035c31b 54 {0x60, 0x53},
jrodenburg 0:a28a1035c31b 55 {0x26, 0x14},
jrodenburg 0:a28a1035c31b 56 {0x56, 0x64},
jrodenburg 0:a28a1035c31b 57 {0x46, 0x74},
jrodenburg 0:a28a1035c31b 58 {0x76, 0x44},
jrodenburg 0:a28a1035c31b 59 {0x66, 0x54},
jrodenburg 0:a28a1035c31b 60 {0x2C, 0x1F},
jrodenburg 0:a28a1035c31b 61 {0x5C, 0x6F},
jrodenburg 0:a28a1035c31b 62 {0x4C, 0x7F},
jrodenburg 0:a28a1035c31b 63 {0x7C, 0x4F},
jrodenburg 0:a28a1035c31b 64 {0x6C, 0x5F},
jrodenburg 0:a28a1035c31b 65 {0x2D, 0x1E},
jrodenburg 0:a28a1035c31b 66 {0x5D, 0x6E},
jrodenburg 0:a28a1035c31b 67 {0x4D, 0x7E},
jrodenburg 0:a28a1035c31b 68 {0x7D, 0x4E},
jrodenburg 0:a28a1035c31b 69 {0x6D, 0x5E},
jrodenburg 0:a28a1035c31b 70 {0x2E, 0x1D},
jrodenburg 0:a28a1035c31b 71 {0x5E, 0x6D},
jrodenburg 0:a28a1035c31b 72 {0x4E, 0x7D},
jrodenburg 0:a28a1035c31b 73 {0x7E, 0x4D},
jrodenburg 0:a28a1035c31b 74 {0x6E, 0x5D},
jrodenburg 0:a28a1035c31b 75 {0x2F, 0x1C},
jrodenburg 0:a28a1035c31b 76 {0x5F, 0x6C},
jrodenburg 0:a28a1035c31b 77 {0x4F, 0x7C},
jrodenburg 0:a28a1035c31b 78 {0x7F, 0x4C},
jrodenburg 0:a28a1035c31b 79 {0x6F, 0x5C},
jrodenburg 0:a28a1035c31b 80 {0x20, 0x1B},
jrodenburg 0:a28a1035c31b 81 {0x50, 0x6B},
jrodenburg 0:a28a1035c31b 82 {0x40, 0x7B},
jrodenburg 0:a28a1035c31b 83 };
jrodenburg 0:a28a1035c31b 84
jrodenburg 0:a28a1035c31b 85 //THERMISTOR LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP)
jrodenburg 0:a28a1035c31b 86 const struct THERM_LUT{
jrodenburg 0:a28a1035c31b 87 int adc;
jrodenburg 0:a28a1035c31b 88 int temp;
jrodenburg 0:a28a1035c31b 89 };
jrodenburg 0:a28a1035c31b 90
jrodenburg 0:a28a1035c31b 91 THERM_LUT thermLUT[] = {
jrodenburg 0:a28a1035c31b 92 {113779,-40},
jrodenburg 0:a28a1035c31b 93 {109152,-35},
jrodenburg 0:a28a1035c31b 94 {103830,-30},
jrodenburg 0:a28a1035c31b 95 {97855,-25},
jrodenburg 0:a28a1035c31b 96 {91319,-20},
jrodenburg 0:a28a1035c31b 97 {84352,-15},
jrodenburg 0:a28a1035c31b 98 {77124,-10},
jrodenburg 0:a28a1035c31b 99 {69820,-5},
jrodenburg 0:a28a1035c31b 100 {62621,0},
jrodenburg 0:a28a1035c31b 101 {55693,5},
jrodenburg 0:a28a1035c31b 102 {49169,10},
jrodenburg 0:a28a1035c31b 103 {43144,15},
jrodenburg 0:a28a1035c31b 104 {37669,20},
jrodenburg 0:a28a1035c31b 105 {32768,25},
jrodenburg 0:a28a1035c31b 106 {28429,30},
jrodenburg 0:a28a1035c31b 107 {24622,35},
jrodenburg 0:a28a1035c31b 108 {21309,40},
jrodenburg 0:a28a1035c31b 109 {18439,45},
jrodenburg 0:a28a1035c31b 110 {15962,50},
jrodenburg 0:a28a1035c31b 111 {13831,55},
jrodenburg 0:a28a1035c31b 112 {12002,60},
jrodenburg 0:a28a1035c31b 113 {10428,65},
jrodenburg 0:a28a1035c31b 114 {9080,70},
jrodenburg 0:a28a1035c31b 115 {7919,75},
jrodenburg 0:a28a1035c31b 116 {6923,80},
jrodenburg 0:a28a1035c31b 117 {6063,85},
jrodenburg 0:a28a1035c31b 118 {5323,90},
jrodenburg 0:a28a1035c31b 119 {4685,95},
jrodenburg 0:a28a1035c31b 120 {4130,100},
jrodenburg 0:a28a1035c31b 121 {3653,105},
jrodenburg 0:a28a1035c31b 122 {3234,110},
jrodenburg 0:a28a1035c31b 123 {2876,115},
jrodenburg 0:a28a1035c31b 124 {2563,120},
jrodenburg 0:a28a1035c31b 125 {2284,125}
jrodenburg 0:a28a1035c31b 126 };
jrodenburg 0:a28a1035c31b 127
jrodenburg 1:0182b86f9bd4 128
jrodenburg 0:a28a1035c31b 129 //DEFINE PINS
jrodenburg 0:a28a1035c31b 130 DigitalOut myled(LED2);
jrodenburg 0:a28a1035c31b 131
jrodenburg 0:a28a1035c31b 132 //I2C FOR MCP23008 (I/O Control)
jrodenburg 0:a28a1035c31b 133 //I2C i2c_IO(PTC9, PTC8); //sda, scl
jrodenburg 1:0182b86f9bd4 134 MCP23008 io_control(PTC9, PTC8, 0x10, 100000);
jrodenburg 0:a28a1035c31b 135 MODSERIAL pc(USBTX, USBRX);
jrodenburg 0:a28a1035c31b 136
jrodenburg 0:a28a1035c31b 137 //I2C FOR LTC2487 (ADC Control)
jrodenburg 0:a28a1035c31b 138 //I2C i2c_ADC(PTC11, PTC10); //sda, scl
jrodenburg 1:0182b86f9bd4 139 LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000);
jrodenburg 0:a28a1035c31b 140
jrodenburg 0:a28a1035c31b 141 //GLOBAL VARIABLES
jrodenburg 1:0182b86f9bd4 142 //channel status variables: init. to off and 0 degrees
jrodenburg 0:a28a1035c31b 143 float chTemps[CHN_COUNT] = {};
jrodenburg 0:a28a1035c31b 144 float chGoalTemps[CHN_COUNT] = {};
jrodenburg 0:a28a1035c31b 145 int chStatus[CHN_COUNT] = {};
jrodenburg 0:a28a1035c31b 146
jrodenburg 0:a28a1035c31b 147
jrodenburg 1:0182b86f9bd4 148 /* Function: set_io_status
jrodenburg 1:0182b86f9bd4 149 **************************************************************
jrodenburg 1:0182b86f9bd4 150 Description: Update i/o channels status for this TCTF channel
jrodenburg 1:0182b86f9bd4 151 Recieves: chn: the channel of the fixture to read temp. from
jrodenburg 1:0182b86f9bd4 152 Returns: N/A
jrodenburg 1:0182b86f9bd4 153 */
jrodenburg 0:a28a1035c31b 154
jrodenburg 1:0182b86f9bd4 155 void set_io_status(int chn){
jrodenburg 1:0182b86f9bd4 156
jrodenburg 1:0182b86f9bd4 157 }
jrodenburg 0:a28a1035c31b 158
jrodenburg 0:a28a1035c31b 159 /* Function: get_temp
jrodenburg 0:a28a1035c31b 160 **************************************************************
jrodenburg 0:a28a1035c31b 161 Description: Retrieve data from thermistor
jrodenburg 0:a28a1035c31b 162 Recieves: chn: the channel of the fixture to read temp. from
jrodenburg 0:a28a1035c31b 163 Returns: the temperature of the fixture (front or back)
jrodenburg 0:a28a1035c31b 164 */
jrodenburg 0:a28a1035c31b 165
jrodenburg 0:a28a1035c31b 166 float get_temp(int chn){
jrodenburg 1:0182b86f9bd4 167 myled = 1;
jrodenburg 0:a28a1035c31b 168 ltc2487.setAddress(addrLUT[chn].adc);
jrodenburg 0:a28a1035c31b 169
jrodenburg 1:0182b86f9bd4 170 float ADC_val = ltc2487.readOutput(chn); //(65536*1.334)/2.5;
jrodenburg 0:a28a1035c31b 171
jrodenburg 0:a28a1035c31b 172 int i = 0;
jrodenburg 0:a28a1035c31b 173
jrodenburg 0:a28a1035c31b 174 while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){
jrodenburg 0:a28a1035c31b 175 i++;
jrodenburg 0:a28a1035c31b 176 } //find the temp. above therm temp
jrodenburg 0:a28a1035c31b 177
jrodenburg 0:a28a1035c31b 178 //Point slope formula extrapolation:
jrodenburg 0:a28a1035c31b 179 // y1 = m (x1-x0)+ y0 , y = temp. value, x = adc value
jrodenburg 0:a28a1035c31b 180 // y1 = thermLUT[i-1].temp y0 = thermLUT[i].temp
jrodenburg 0:a28a1035c31b 181 // x1 = thermLUT[i-1].adc x0 =thermLUT[i].adc
jrodenburg 0:a28a1035c31b 182 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 183 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 184
jrodenburg 0:a28a1035c31b 185 float m = a/b;
jrodenburg 0:a28a1035c31b 186 float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp;
jrodenburg 0:a28a1035c31b 187
jrodenburg 1:0182b86f9bd4 188 pc.printf("ADC VAL: %f TEMP: %f \r\n", ADC_val, y);
jrodenburg 1:0182b86f9bd4 189
jrodenburg 0:a28a1035c31b 190 return y;
jrodenburg 1:0182b86f9bd4 191
jrodenburg 0:a28a1035c31b 192 }
jrodenburg 0:a28a1035c31b 193
jrodenburg 0:a28a1035c31b 194
jrodenburg 0:a28a1035c31b 195 /* Function: get_heater_current
jrodenburg 0:a28a1035c31b 196 **************************************************************
jrodenburg 0:a28a1035c31b 197 Description: Retrieve current into heater control MOSFET
jrodenburg 0:a28a1035c31b 198 Recieves: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 199 Returns: the current into the heater control MOSFET
jrodenburg 0:a28a1035c31b 200 */
jrodenburg 0:a28a1035c31b 201
jrodenburg 0:a28a1035c31b 202 float get_heater_current(int chn){
jrodenburg 1:0182b86f9bd4 203 return 0.0;
jrodenburg 0:a28a1035c31b 204 }
jrodenburg 0:a28a1035c31b 205
jrodenburg 0:a28a1035c31b 206 /* Function: get_valve_current
jrodenburg 0:a28a1035c31b 207 **************************************************************
jrodenburg 0:a28a1035c31b 208 Description: Retrieve current into valve control MOSFET
jrodenburg 0:a28a1035c31b 209 Recieves: chn: the channel of the fixture to read current from
jrodenburg 0:a28a1035c31b 210 Returns: the current into the valve control MOSFET
jrodenburg 0:a28a1035c31b 211 */
jrodenburg 0:a28a1035c31b 212
jrodenburg 0:a28a1035c31b 213 float get_valve_current(int chn){
jrodenburg 1:0182b86f9bd4 214 return 0.0;
jrodenburg 0:a28a1035c31b 215 }
jrodenburg 0:a28a1035c31b 216
jrodenburg 0:a28a1035c31b 217 /* Function: turn_valve_on
jrodenburg 0:a28a1035c31b 218 **************************************************************
jrodenburg 1:0182b86f9bd4 219 Description: Turn valve on and green status LED on
jrodenburg 0:a28a1035c31b 220 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 221 Returns: N/A
jrodenburg 0:a28a1035c31b 222 */
jrodenburg 0:a28a1035c31b 223
jrodenburg 1:0182b86f9bd4 224 void turn_valve_on(int chn){
jrodenburg 0:a28a1035c31b 225 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 226 io_control.init();
jrodenburg 1:0182b86f9bd4 227 io_control.writeOutput(1,0,1,0);
jrodenburg 0:a28a1035c31b 228 }
jrodenburg 0:a28a1035c31b 229
jrodenburg 0:a28a1035c31b 230 /* Function: turn_valve_off
jrodenburg 0:a28a1035c31b 231 **************************************************************
jrodenburg 1:0182b86f9bd4 232 Description: Turn valve off and green status LED on
jrodenburg 0:a28a1035c31b 233 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 234 Returns: N/A
jrodenburg 0:a28a1035c31b 235 */
jrodenburg 0:a28a1035c31b 236
jrodenburg 1:0182b86f9bd4 237 void turn_valve_off(int chn){
jrodenburg 0:a28a1035c31b 238 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 239 io_control.init();
jrodenburg 1:0182b86f9bd4 240 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 241 }
jrodenburg 0:a28a1035c31b 242
jrodenburg 0:a28a1035c31b 243 /* Function: turn_heater_on
jrodenburg 0:a28a1035c31b 244 **************************************************************
jrodenburg 1:0182b86f9bd4 245 Description: Turn heater on and green status LED on
jrodenburg 0:a28a1035c31b 246 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 247 Returns: N/A
jrodenburg 0:a28a1035c31b 248 */
jrodenburg 0:a28a1035c31b 249
jrodenburg 1:0182b86f9bd4 250 void turn_heater_on(int chn){
jrodenburg 0:a28a1035c31b 251 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 252 io_control.init();
jrodenburg 1:0182b86f9bd4 253 io_control.writeOutput(0,1,1,0);
jrodenburg 0:a28a1035c31b 254 }
jrodenburg 0:a28a1035c31b 255
jrodenburg 0:a28a1035c31b 256 /* Function: turn_heater_off
jrodenburg 0:a28a1035c31b 257 **************************************************************
jrodenburg 1:0182b86f9bd4 258 Description: Turn heater off and green status LED on
jrodenburg 0:a28a1035c31b 259 Recieves: chn: the channel of the fixture
jrodenburg 1:0182b86f9bd4 260 Returns: N/A
jrodenburg 0:a28a1035c31b 261 */
jrodenburg 0:a28a1035c31b 262
jrodenburg 1:0182b86f9bd4 263 void turn_heater_off(int chn){
jrodenburg 0:a28a1035c31b 264 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 265 io_control.init();
jrodenburg 1:0182b86f9bd4 266 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 267 }
jrodenburg 0:a28a1035c31b 268
jrodenburg 0:a28a1035c31b 269 /* Function: status_led
jrodenburg 0:a28a1035c31b 270 **************************************************************
jrodenburg 0:a28a1035c31b 271 Description: Turn status LED on (turns on green or red)
jrodenburg 0:a28a1035c31b 272 Recieves: chn: the channel of the fixture
jrodenburg 0:a28a1035c31b 273 status: the status of channel (good (1) or bad (0))
jrodenburg 1:0182b86f9bd4 274 Returns: N/A
jrodenburg 0:a28a1035c31b 275 */
jrodenburg 0:a28a1035c31b 276
jrodenburg 1:0182b86f9bd4 277 void status_led(int chn, int status){
jrodenburg 0:a28a1035c31b 278 io_control.setAddress(addrLUT[chn].io);
jrodenburg 0:a28a1035c31b 279 if(status){
jrodenburg 1:0182b86f9bd4 280 io_control.writeOutput(0,0,1,0);
jrodenburg 0:a28a1035c31b 281 }
jrodenburg 0:a28a1035c31b 282 else{
jrodenburg 1:0182b86f9bd4 283 io_control.writeOutput(0,0,0,1);
jrodenburg 0:a28a1035c31b 284 }
jrodenburg 0:a28a1035c31b 285 }
jrodenburg 0:a28a1035c31b 286
jrodenburg 1:0182b86f9bd4 287 /* Function: test_mcp23008
jrodenburg 1:0182b86f9bd4 288 **************************************************************
jrodenburg 1:0182b86f9bd4 289 Description: Test each output of the MCP23009
jrodenburg 1:0182b86f9bd4 290 Recieves: N/A
jrodenburg 1:0182b86f9bd4 291 Returns: N/A
jrodenburg 1:0182b86f9bd4 292 */
jrodenburg 1:0182b86f9bd4 293
jrodenburg 1:0182b86f9bd4 294 void test_mcp23008(int chn){
jrodenburg 1:0182b86f9bd4 295 turn_valve_on(chn);
jrodenburg 1:0182b86f9bd4 296 wait(1);
jrodenburg 1:0182b86f9bd4 297 turn_valve_off(chn);
jrodenburg 1:0182b86f9bd4 298 wait(1);
jrodenburg 1:0182b86f9bd4 299 turn_heater_on(chn);
jrodenburg 1:0182b86f9bd4 300 wait(1);
jrodenburg 1:0182b86f9bd4 301 turn_heater_off(chn);
jrodenburg 1:0182b86f9bd4 302 wait(1);
jrodenburg 1:0182b86f9bd4 303 status_led(chn, 0);
jrodenburg 1:0182b86f9bd4 304 wait(1);
jrodenburg 1:0182b86f9bd4 305 status_led(chn, 1);
jrodenburg 1:0182b86f9bd4 306 }
jrodenburg 1:0182b86f9bd4 307
jrodenburg 0:a28a1035c31b 308
jrodenburg 0:a28a1035c31b 309
jrodenburg 0:a28a1035c31b 310 int main() {
jrodenburg 0:a28a1035c31b 311 //GPIO TEST
jrodenburg 1:0182b86f9bd4 312
jrodenburg 1:0182b86f9bd4 313 //ADC TEST
jrodenburg 0:a28a1035c31b 314
jrodenburg 0:a28a1035c31b 315 myled = 1;
jrodenburg 1:0182b86f9bd4 316 //get_temp(FRONT_THERM);
jrodenburg 0:a28a1035c31b 317
jrodenburg 0:a28a1035c31b 318 while(1) {
jrodenburg 1:0182b86f9bd4 319 //if(DEBUG) pc.printf("FSTART \r\n");
jrodenburg 1:0182b86f9bd4 320 //get_temp(1);
jrodenburg 1:0182b86f9bd4 321 test_mcp23008(1);
jrodenburg 1:0182b86f9bd4 322 test_mcp23008(0);
jrodenburg 1:0182b86f9bd4 323 //wait(0.5);
jrodenburg 1:0182b86f9bd4 324 // myled = 0;
jrodenburg 0:a28a1035c31b 325 }
jrodenburg 0:a28a1035c31b 326 }