Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Revision:
1:0182b86f9bd4
Parent:
0:a28a1035c31b
Child:
2:bd118a724f03
--- a/main.cpp	Mon Jan 22 20:11:52 2018 +0000
+++ b/main.cpp	Mon Feb 05 06:41:23 2018 +0000
@@ -8,17 +8,17 @@
 #include "LTC2487.h"
 
 //DEFINITIVE VARIABLES
-#define DEBUG         0
+#define DEBUG         1
 #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 VALVE         1
+#define HEATER        2
+#define STATUS_GOOD   3
+#define STATUS_BAD    4
 #define sizeLUT       34
 #define FRONT_THERM   0
 #define BACK_THERM    1
@@ -125,27 +125,36 @@
     {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);
+MCP23008 io_control(PTC9, PTC8, 0x10, 100000);
 MODSERIAL pc(USBTX, USBRX);
 
 //I2C FOR LTC2487 (ADC Control)
 //I2C i2c_ADC(PTC11, PTC10); //sda, scl
-LTC2487 ltc2487(PTC11, PTC10, 0x23, 3000);
+LTC2487 ltc2487(PTC11, PTC10, 0x23, 100000);
 
 //GLOBAL VARIABLES
-//channel status varaibles: init. to off and 0 degrees
+//channel status variables: init. to off and 0 degrees
 float chTemps[CHN_COUNT] = {};
 float chGoalTemps[CHN_COUNT] = {};  
 int chStatus[CHN_COUNT] = {};
 
 
+/* Function: set_io_status
+   **************************************************************
+   Description: Update i/o channels status for this TCTF channel
+   Recieves: chn: the channel of the fixture to read temp. from
+   Returns: N/A     
+*/
 
-
+void set_io_status(int chn){
+    
+}
 
 /* Function: get_temp
    **************************************************************
@@ -155,11 +164,10 @@
 */
 
 float get_temp(int chn){
+    myled = 1;
     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);
+    float ADC_val = ltc2487.readOutput(chn); //(65536*1.334)/2.5;
     
     int i = 0;
     
@@ -177,7 +185,10 @@
     float m = a/b;
     float y = (m*(ADC_val-thermLUT[i].adc))+thermLUT[i].temp;
     
+    pc.printf("ADC VAL: %f TEMP: %f \r\n", ADC_val, y);
+    
     return y;  
+    
 }
 
 
@@ -189,7 +200,7 @@
 */
 
 float get_heater_current(int chn){
-    
+    return 0.0;
 }
 
 /* Function: get_valve_current
@@ -200,59 +211,59 @@
 */
 
 float get_valve_current(int chn){
-    
+    return 0.0;
 }
 
 /* Function: turn_valve_on
    **************************************************************
-   Description: Turn valve on
+   Description: Turn valve on and green status LED on
    Recieves: chn: the channel of the fixture 
-   Returns: nothing
+   Returns: N/A
 */
 
-float turn_valve_on(int chn){
+void turn_valve_on(int chn){
     io_control.setAddress(addrLUT[chn].io);
     io_control.init();
-    io_control.writeOutput(VALVE, 1);
+    io_control.writeOutput(1,0,1,0);
 }
 
 /* Function: turn_valve_off
    **************************************************************
-   Description: Turn valve off
+   Description: Turn valve off and green status LED on
    Recieves: chn: the channel of the fixture 
-   Returns: nothing
+   Returns: N/A
 */
 
-float turn_valve_off(int chn){
+void turn_valve_off(int chn){
     io_control.setAddress(addrLUT[chn].io);
     io_control.init();
-    io_control.writeOutput(VALVE, 0);
+    io_control.writeOutput(0,0,1,0);
 }
 
 /* Function: turn_heater_on
    **************************************************************
-   Description: Turn heater on
+   Description: Turn heater on and green status LED on
    Recieves: chn: the channel of the fixture 
-   Returns: nothing
+   Returns: N/A
 */
 
-float turn_heater_on(int chn){
+void turn_heater_on(int chn){
     io_control.setAddress(addrLUT[chn].io);
     io_control.init();
-    io_control.writeOutput(HEATER, 1);
+    io_control.writeOutput(0,1,1,0);
 }
 
 /* Function: turn_heater_off
    **************************************************************
-   Description: Turn heater off
+   Description: Turn heater off and green status LED on
    Recieves: chn: the channel of the fixture 
-   Returns: nothing
+   Returns: N/A
 */
 
-float turn_heater_off(int chn){
+void turn_heater_off(int chn){
     io_control.setAddress(addrLUT[chn].io);
     io_control.init();
-    io_control.writeOutput(HEATER, 0);
+    io_control.writeOutput(0,0,1,0);
 }
 
 /* Function: status_led
@@ -260,79 +271,56 @@
    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
+   Returns: N/A
 */
 
-float status_led(int chn, int status){
+void 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);    
+        io_control.writeOutput(0,0,1,0);
     }
     else{
-        io_control.writeOutput(STATUS_BAD, 1);
-        io_control.writeOutput(STATUS_GOOD, 1);
+        io_control.writeOutput(0,0,0,1);
     }
 }
 
+/* Function: test_mcp23008
+   **************************************************************
+   Description: Test each output of the MCP23009
+   Recieves: N/A
+   Returns: N/A
+*/
+
+void test_mcp23008(int chn){
+    turn_valve_on(chn);
+    wait(1);
+    turn_valve_off(chn);
+    wait(1);
+    turn_heater_on(chn);
+    wait(1);
+    turn_heater_off(chn);
+    wait(1);
+    status_led(chn, 0);
+    wait(1);
+    status_led(chn, 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);*/
+    
+    //ADC TEST
     
-    
-    //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);
+    //get_temp(FRONT_THERM);
     
     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);*/
+        //if(DEBUG) pc.printf("FSTART \r\n");
+        //get_temp(1);
+        test_mcp23008(1);
+        test_mcp23008(0);
+        //wait(0.5);
+       // myled = 0;
     }
 }