![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Control Code with I/O and ADC working
Diff: main.cpp
- Revision:
- 0:a28a1035c31b
- Child:
- 1:0182b86f9bd4
diff -r 000000000000 -r a28a1035c31b main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jan 22 20:11:52 2018 +0000 @@ -0,0 +1,338 @@ +// MBED SCRIPT FOR CONTROLLING THE TEMPERATURE CONTROLLED TEST FIXTURE +// AUTHOR: JUSTIN RODENBURG +// DATE: SEPTEMBER 2017 + +#include "mbed.h" +#include "MODSERIAL.h" +#include "MCP23008.h" +#include "LTC2487.h" + +//DEFINITIVE VARIABLES +#define DEBUG 0 +#define CHN_COUNT 48 +#define MIN_TEMP 15 +#define MAX_TEMP 60 +#define HYST 0.2 +#define SAMPLES 5 +#define I2C_Freq 2000 +#define VALVE 0 +#define HEATER 1 +#define STATUS_GOOD 2 +#define STATUS_BAD 3 +#define sizeLUT 34 +#define FRONT_THERM 0 +#define BACK_THERM 1 +#define HEAT_FET_AMP 2 +#define VALV_FET_AMP 3 + +//I2C AADRESS LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP) +const struct I2C_ADDR_LUT{ + int adc; + int io; +}; + +I2C_ADDR_LUT addrLUT[] = { + {0x23, 0x10}, + {0x53, 0x60}, + {0x43, 0x70}, + {0x73, 0x40}, + {0x63, 0x50}, + {0x22, 0x11}, + {0x52, 0x61}, + {0x42, 0x71}, + {0x72, 0x41}, + {0x62, 0x51}, + {0x21, 0x12}, + {0x51, 0x62}, + {0x41, 0x72}, + {0x71, 0x42}, + {0x61, 0x52}, + {0x20, 0x13}, + {0x50, 0x63}, + {0x40, 0x73}, + {0x70, 0x43}, + {0x60, 0x53}, + {0x26, 0x14}, + {0x56, 0x64}, + {0x46, 0x74}, + {0x76, 0x44}, + {0x66, 0x54}, + {0x2C, 0x1F}, + {0x5C, 0x6F}, + {0x4C, 0x7F}, + {0x7C, 0x4F}, + {0x6C, 0x5F}, + {0x2D, 0x1E}, + {0x5D, 0x6E}, + {0x4D, 0x7E}, + {0x7D, 0x4E}, + {0x6D, 0x5E}, + {0x2E, 0x1D}, + {0x5E, 0x6D}, + {0x4E, 0x7D}, + {0x7E, 0x4D}, + {0x6E, 0x5D}, + {0x2F, 0x1C}, + {0x5F, 0x6C}, + {0x4F, 0x7C}, + {0x7F, 0x4C}, + {0x6F, 0x5C}, + {0x20, 0x1B}, + {0x50, 0x6B}, + {0x40, 0x7B}, +}; + +//THERMISTOR LOOK UP TABLE, CREATED IN EXCEL SHEET (COUNT, TEMP) +const struct THERM_LUT{ + int adc; + int temp; +}; + +THERM_LUT thermLUT[] = { + {113779,-40}, + {109152,-35}, + {103830,-30}, + {97855,-25}, + {91319,-20}, + {84352,-15}, + {77124,-10}, + {69820,-5}, + {62621,0}, + {55693,5}, + {49169,10}, + {43144,15}, + {37669,20}, + {32768,25}, + {28429,30}, + {24622,35}, + {21309,40}, + {18439,45}, + {15962,50}, + {13831,55}, + {12002,60}, + {10428,65}, + {9080,70}, + {7919,75}, + {6923,80}, + {6063,85}, + {5323,90}, + {4685,95}, + {4130,100}, + {3653,105}, + {3234,110}, + {2876,115}, + {2563,120}, + {2284,125} +}; + +//DEFINE PINS +DigitalOut myled(LED2); + +//I2C FOR MCP23008 (I/O Control) +//I2C i2c_IO(PTC9, PTC8); //sda, scl +MCP23008 io_control(PTC9, PTC8, 0x10, 2000); +MODSERIAL pc(USBTX, USBRX); + +//I2C FOR LTC2487 (ADC Control) +//I2C i2c_ADC(PTC11, PTC10); //sda, scl +LTC2487 ltc2487(PTC11, PTC10, 0x23, 3000); + +//GLOBAL VARIABLES +//channel status varaibles: init. to off and 0 degrees +float chTemps[CHN_COUNT] = {}; +float chGoalTemps[CHN_COUNT] = {}; +int chStatus[CHN_COUNT] = {}; + + + + + +/* Function: get_temp + ************************************************************** + Description: Retrieve data from thermistor + Recieves: chn: the channel of the fixture to read temp. from + Returns: the temperature of the fixture (front or back) +*/ + +float get_temp(int chn){ + ltc2487.setAddress(addrLUT[chn].adc); + + float ADC_val = (65536*1.334)/2.5;//ltc2487.readOutput(chn); + + //pc.printf("ADC VAL: %f %i\r\n", ADC_val); + + int i = 0; + + while((i < sizeLUT) && (thermLUT[i].adc > ADC_val)){ + i++; + } //find the temp. above therm temp + + //Point slope formula extrapolation: + // y1 = m (x1-x0)+ y0 , y = temp. value, x = adc value + // y1 = thermLUT[i-1].temp y0 = thermLUT[i].temp + // x1 = thermLUT[i-1].adc x0 =thermLUT[i].adc + float a = float(thermLUT[i-1].temp - thermLUT[i].temp); //slope of temp between points where therm temp is between (Tmax - Tmin) + float b = float(thermLUT[i-1].adc - thermLUT[i].adc); //slope of adc between points where therm adc is between (Amax - Amin) + + float m = a/b; + float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp; + + return y; +} + + +/* Function: get_heater_current + ************************************************************** + Description: Retrieve current into heater control MOSFET + Recieves: chn: the channel of the fixture to read current from + Returns: the current into the heater control MOSFET +*/ + +float get_heater_current(int chn){ + +} + +/* Function: get_valve_current + ************************************************************** + Description: Retrieve current into valve control MOSFET + Recieves: chn: the channel of the fixture to read current from + Returns: the current into the valve control MOSFET +*/ + +float get_valve_current(int chn){ + +} + +/* Function: turn_valve_on + ************************************************************** + Description: Turn valve on + Recieves: chn: the channel of the fixture + Returns: nothing +*/ + +float turn_valve_on(int chn){ + io_control.setAddress(addrLUT[chn].io); + io_control.init(); + io_control.writeOutput(VALVE, 1); +} + +/* Function: turn_valve_off + ************************************************************** + Description: Turn valve off + Recieves: chn: the channel of the fixture + Returns: nothing +*/ + +float turn_valve_off(int chn){ + io_control.setAddress(addrLUT[chn].io); + io_control.init(); + io_control.writeOutput(VALVE, 0); +} + +/* Function: turn_heater_on + ************************************************************** + Description: Turn heater on + Recieves: chn: the channel of the fixture + Returns: nothing +*/ + +float turn_heater_on(int chn){ + io_control.setAddress(addrLUT[chn].io); + io_control.init(); + io_control.writeOutput(HEATER, 1); +} + +/* Function: turn_heater_off + ************************************************************** + Description: Turn heater off + Recieves: chn: the channel of the fixture + Returns: nothing +*/ + +float turn_heater_off(int chn){ + io_control.setAddress(addrLUT[chn].io); + io_control.init(); + io_control.writeOutput(HEATER, 0); +} + +/* Function: status_led + ************************************************************** + Description: Turn status LED on (turns on green or red) + Recieves: chn: the channel of the fixture + status: the status of channel (good (1) or bad (0)) + Returns: nothing +*/ + +float status_led(int chn, int status){ + io_control.setAddress(addrLUT[chn].io); + if(status){ + io_control.writeOutput(STATUS_GOOD, 1); + io_control.writeOutput(STATUS_BAD, 0); + } + else{ + io_control.writeOutput(STATUS_BAD, 1); + io_control.writeOutput(STATUS_GOOD, 1); + } +} + + + +int main() { + //GPIO TEST + /*io_control.init(); + turn_heater_on(0); + wait(1); + io_control.writeOutput(VALVE, 1); + wait(1); + io_control.writeOutput(HEATER, 1); + wait(1); + io_control.writeOutput(STATUS_GOOD, 1); + wait(1); + io_control.writeOutput(STATUS_BAD, 1); + wait(1); + io_control.writeOutput(VALVE, 0); + wait(1); + io_control.writeOutput(HEATER, 0); + wait(1); + io_control.writeOutput(STATUS_GOOD, 0); + wait(1); + io_control.writeOutput(STATUS_GOOD, 0); + wait(1); + io_control.writeOutput(STATUS_BAD, 0); + wait(1);*/ + + + //ADCD TEST + //io_control.init(); + //io_control.writeOutput(STAT_GOOD_ON, 1); + //pc.printf("HERE\r\n"); + //float resulttt = ltc2487.readOutput(0); + //pc.printf("STATE: %f \r\n", get_temp(FRONT_THERM)); + + io_control.init(); + //turn_heater_on(1); + //turn_valve_on(0); + myled = 1; + //wait(2); + //turn_heater_on(0); + //io_control.writeOutput(BACK_THERM, 1); + + while(1) { + turn_heater_on(0); + turn_heater_on(1); + turn_heater_on(2); + //turn_heater_on(1); + /*turn_heater_on(1); + wait(1); + turn_valve_on(0); + wait(1); + turn_heater_off(1); + wait(1); + turn_valve_off(0); + wait(1); + myled = 1; + wait(0.2); + myled = 0; + wait(0.2);*/ + } +}