Simple demo for exercising the board

Dependencies:   MAX11300 MAX4822 OneWire Terminal ds3231 mbed

Revision:
8:8e36fe48b351
Parent:
6:18a831a757b5
Child:
9:a5fd406b24a6
--- a/main.cpp	Fri Aug 12 18:44:39 2016 +0000
+++ b/main.cpp	Mon Aug 22 23:10:57 2016 +0000
@@ -44,10 +44,9 @@
 void get_time_date(Terminal & term, Ds3231 & rtc);
 void search_ow_bus(Terminal & term, DS2484 & owm);
 void print_rom_id(Terminal & term, RomId & romId);
-void calibrate_420_io(Terminal & term, MAX11300 & pixi, 
-float & slope_out, float & offset_out, 
-float & slope_in, float & offset_in);
+bool calibrate_420_io(Terminal & term, MAX11300 & pixi, DS2484 & owm, uint16_t *calData, bool loopAlreadyCal);
 void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector);
+bool check_420_cal(Terminal & term, DS2484 & owm, uint16_t *calData);
 
 int main(void)
 {
@@ -95,17 +94,30 @@
     }
     term.printf("OneWire master initialized\n\n");
     
+    float aio_slope = 0;
+    float aio_offset = 0;
+    float aii_slope = 0;
+    float aii_offset = 0;
+    uint16_t calData[4];
+    
+    bool currentLoopCal = false;
+    if(check_420_cal(term, owm, calData))
+    {
+        aio_slope = (((calData[1]*1.0) - (calData[0]*1.0))/16.0);
+        aio_offset = ((-1.0*aio_slope*4.0) + calData[0]);
+        
+        aii_slope = (16.0/((calData[3]*1.0) - (calData[2]*1.0)));
+        aii_offset = ((-1.0*aii_slope*calData[2]) + 4.0);
+    
+        currentLoopCal = true;
+    }
+    
     int32_t user_entry = 0;
     int32_t relay = 0;
     
     uint16_t analog_out_ch, analog_val;
     float avo_volts, aio_mA, aii_mA;
     
-    float aio_slope = 130.0;
-    float aio_offset = -2.0;
-    float aii_slope = 7.71e-3;
-    float aii_offset = 3.86e-3;
-    
     const int32_t QUIT = 15;
     
     MAX11300::CmdResult pixi_result;
@@ -214,31 +226,56 @@
             break;
             
             case 11:
-                aio_mA = term.get_float("Please enter a current from 4.0 to 20.0 mA: ", 3.9, 20.1);
-                analog_val = static_cast<uint16_t>((aio_slope * aio_mA) + aio_offset);
-                
-                pixi_result = pixi.single_ended_dac_write(MAX11300::PORT8, analog_val);
-                if(pixi_result != MAX11300::Success)
+                if(currentLoopCal)
                 {
-                    term.printf("Failed to set 4-20 out\n");
+                    aio_mA = term.get_float("Please enter a current from 4.0 to 20.0 mA: ", 3.9, 20.1);
+                    analog_val = static_cast<uint16_t>((aio_slope * aio_mA) + aio_offset);
+                    
+                    pixi_result = pixi.single_ended_dac_write(MAX11300::PORT8, analog_val);
+                    if(pixi_result != MAX11300::Success)
+                    {
+                        term.printf("Failed to set 4-20 out\n");
+                    }
+                }
+                else
+                {
+                    term.printf("Please calibrate 4-20mA loop first, option 13.\n");
                 }
             break;
             
             case 12:
-                pixi_result = pixi.single_ended_adc_read(MAX11300::PORT12, analog_val);
-                aii_mA = ((aii_slope*analog_val) + aii_offset);
-                if(pixi_result == MAX11300::Success)
+                if(currentLoopCal)
                 {
-                    term.printf("4-20 in = %.2fmA\n", aii_mA);
+                    pixi_result = pixi.single_ended_adc_read(MAX11300::PORT12, analog_val);
+                    aii_mA = ((aii_slope*analog_val) + aii_offset);
+                    if(pixi_result == MAX11300::Success)
+                    {
+                        term.printf("4-20 in = %.2fmA\n", aii_mA);
+                    }
+                    else
+                    {
+                        term.printf("Failed to read 4-20 in\n");
+                    }
                 }
                 else
                 {
-                    term.printf("Failed to read 4-20 in\n");
+                    term.printf("Please calibrate 4-20mA loop first, option 13.\n");
                 }
             break;
             
             case 13:
-                calibrate_420_io(term, pixi, aio_slope, aio_offset, aii_slope, aii_offset);
+                if(calibrate_420_io(term, pixi, owm, calData, currentLoopCal))
+                {
+                    aio_slope = (((calData[1]*1.0) - (calData[0]*1.0))/16.0);
+                    aio_offset = ((-1.0*aio_slope*4.0) + calData[0]);
+                    
+                    aii_slope = (16.0/((calData[3]*1.0) - (calData[2]*1.0)));
+                    aii_offset = ((-1.0*aii_slope*calData[2]) + 4.0);
+                
+                    currentLoopCal = true;
+                    
+                    term.printf("Calibration data saved to 1-Wire EEPROM\n");
+                }
             break;
             
             case (QUIT - 1):
@@ -381,14 +418,18 @@
 }
 
 //*********************************************************************
-void calibrate_420_io(Terminal & term, MAX11300 & pixi, 
-float & slope_out, float & offset_out, 
-float & slope_in, float & offset_in)
+bool calibrate_420_io(Terminal & term, MAX11300 & pixi, DS2484 & owm, uint16_t *calData, bool loopAlreadyCal)
 {
     char user_entry;
     //initial vals for aio determined imperically on one pcb
     static uint16_t aio_4mA = 518;
     static uint16_t aio_20mA = 2592;
+    
+    if(loopAlreadyCal)
+    {
+        aio_4mA = calData[0];
+        aio_20mA = calData[1];
+    }
     uint16_t aii_4mA, aii_20mA;
     
     term.cls();
@@ -448,8 +489,8 @@
     }
     while(user_entry != 'q');
     
-    slope_out = (((aio_20mA*1.0) - (aio_4mA*1.0))/16.0);
-    offset_out = ((-1.0*slope_out*4.0) + aio_4mA);
+    calData[0] = aio_4mA;
+    calData[1] = aio_20mA;
     
     uint8_t idx;
     //cal aii
@@ -461,6 +502,8 @@
     }
     pixi.single_ended_adc_read(MAX11300::PORT12, aii_4mA);
     
+    calData[2] = aii_4mA;
+    
     pixi.single_ended_dac_write(MAX11300::PORT8, aio_20mA);
     for(idx = 0; idx < 10; idx++)
     {
@@ -469,12 +512,57 @@
     }
     pixi.single_ended_adc_read(MAX11300::PORT12, aii_20mA);
     
+    calData[3] = aii_20mA;
     
-    slope_in = (16.0/((aii_20mA*1.0) - (aii_4mA*1.0)));
-    offset_in = ((-1.0*slope_in*aii_4mA) + 4.0);
+    
+    bool rtn_val = false;
+    
+    //Write cal data to eeprom
+    OneWireMaster::CmdResult result = owm.OWReset();
+    MultidropRomIterator selector(owm);
+    SearchState search_state;
+    
+    DS2431 eeprom(selector);
     
-    term.printf("\nCal done.\n\n");
-    wait(1.0);
+    search_state.findFamily(0x2D);
+    result = OWNext(owm, search_state);
+    if(result == OneWireMaster::Success)
+    {
+        eeprom.setRomId(search_state.romId);
+        
+        OneWireSlave::CmdResult slaveResult = eeprom.writeMemory(0, "CalTrue!", 8);
+        if(slaveResult == OneWireSlave::Success)
+        {
+            uint8_t writeCalData[8];
+            //cal data stored MSB first;
+            writeCalData[0] = ((aio_4mA >> 8) & 0xFF);
+            writeCalData[1] = (aio_4mA & 0xFF);
+            writeCalData[2] = ((aio_20mA >> 8) & 0xFF);
+            writeCalData[3] = (aio_20mA & 0xFF);
+            writeCalData[4] = ((aii_4mA >> 8) & 0xFF);
+            writeCalData[5] = (aii_4mA & 0xFF);
+            writeCalData[6] = ((aii_20mA >> 8) & 0xFF);
+            writeCalData[7] = (aii_20mA & 0xFF);
+            
+            slaveResult = eeprom.writeMemory(8, writeCalData, 8);
+            if(slaveResult == OneWireSlave::Success)
+            {
+                rtn_val = true;
+                term.printf("\nCal done.\n\n");
+                wait(1.0);
+            }
+            else
+            {
+                term.printf("Failed to write cal data.\n\n");
+            }
+        }
+        else
+        {
+            term.printf("Failed to write cal message.\n\n");
+        }
+    }
+    
+    return rtn_val;
 }
 
 //*********************************************************************
@@ -606,3 +694,59 @@
         }
     }
 }
+
+//*********************************************************************
+bool check_420_cal(Terminal & term, DS2484 & owm, uint16_t *calData)
+{
+    OneWireMaster::CmdResult result = owm.OWReset();
+    MultidropRomIterator selector(owm);
+    SearchState search_state;
+    
+    DS2431 eeprom(selector);
+    
+    bool rtn_val = false;
+    
+    term.printf("\n\nPlease remove any 1-Wire devices connected to bus on start up and calibrartion of 4-20mA loop.\n\n");
+    wait(2.5);
+    
+    search_state.findFamily(0x2D);
+    result = OWNext(owm, search_state);
+    if(result == OneWireMaster::Success)
+    {
+        eeprom.setRomId(search_state.romId);
+        uint8_t temp_data[8];
+        
+        OneWireSlave::CmdResult slaveResult = eeprom.readMemory(0, temp_data, 8);
+        
+        if((temp_data[0] == 'C') && (slaveResult == OneWireSlave::Success))
+        {
+            slaveResult = eeprom.readMemory(8, temp_data, 8);
+            if(slaveResult == OneWireSlave::Success)
+            {
+                calData[0] = ((temp_data[0] << 8) | temp_data[1]);
+                calData[1] = ((temp_data[2] << 8) | temp_data[3]);
+                calData[2] = ((temp_data[4] << 8) | temp_data[5]);
+                calData[3] = ((temp_data[6] << 8) | temp_data[7]);
+                
+                rtn_val = true;
+            }
+            else
+            {
+                term.printf("Failed to read row 1 of EEPROM\n\n");
+            }
+        }
+        else
+        {
+            if(slaveResult == OneWireSlave::Success)
+            {
+                term.printf("No cal data stored, please calibrate 4-20mA loop\n\n");
+            }
+            else
+            {
+                term.printf("Failed to read row 0 of EEPROM\n\n");
+            }
+        }
+    }
+    
+    return rtn_val;
+}