Version 3.0: Switching to newer LDC1614 which is placed on the same PCB.

Dependencies:   Bob DS1825 LDC1614 LDC1101 SDFileSystem mbed

Fork of Inductive_Sensor by Bob Giesberts

Revision:
21:b96a3de88002
Parent:
20:1c5ea04a3b2a
Child:
22:ac26f52ebaf8
--- a/main.cpp	Sun Sep 11 15:09:14 2016 +0000
+++ b/main.cpp	Wed Sep 21 14:26:45 2016 +0000
@@ -27,13 +27,15 @@
 
 
 bool DEBUG              = false;
-
+// Serial pc( USBTX, USBRX );  
 
 
 // SETTINGS
-float C                 = 120E-12;      // Capacitor in F
-int sensors             = 1;            // number of attached sensors
-uint16_t INTERVAL_FIRST = 3600;         // First sampling time in seconds.  60:00 minutes = 3600 sec
+uint8_t C               = 120;          // Capacitor (in pF)
+uint8_t f_CLKIN         = 40;           // Oscillator frequency (in MHz)
+const uint8_t sensors   = 2;            // number of attached sensors
+
+uint16_t INTERVAL_FIRST = 900;         // First sampling time in seconds.  60:00 minutes = 3600 sec
 uint16_t INTERVAL_OFF   = 870;          // Waiting interval in seconds.     14:30 minutes = 870 sec (14*60+30)
 uint16_t INTERVAL_ON    = 30;           // Sampling interval in seconds.     0:30 minutes =  30 sec
 
@@ -64,7 +66,7 @@
 
 // load libraries
 Bob bob( _LED_PROCESS, _LED_ERROR, _BUTTON, _ENABLE, _SD_PRESENT, _BATTERY );
-Serial pc( USBTX, USBRX );
+// Serial pc( USBTX, USBRX );
 
 // timer variables
 uint32_t now = 0, next = 0, prev = 0;
@@ -80,18 +82,20 @@
 const char *fn;
 
 // temperature variables
-uint8_t Tdata[9];
-int16_t T_bin;
 float T;
 
+// LDC variables
+uint32_t L;
+
 // temporal storage for data samples
-const uint8_t package_size = 60;    // ±12-13 Hz, so 60 would take about 5 sec.
+const uint8_t package_size = 60;            // ±12-13 Hz, so 60 would take about 5 sec.
 struct mydata {
-    uint32_t    t[package_size];    // time (s)
-    uint32_t    L[package_size];    // LHR_DATA
+    uint32_t    t[package_size];            // time (s)
+    uint32_t    L[sensors][package_size];   // LHR_DATA
 } collected;
 uint8_t counter = 0;
-uint8_t status;
+uint8_t SKIPPING = 3;
+uint8_t skip = 0;
 
 // function to write all data to the SD card
 void storeit( float T )
@@ -102,15 +106,25 @@
     // V     8-bit  |   3.10          4.20        4x8-bit |          FF   2x8
     // T    16-bit  | -20.0000       85.0000      7x8-bit |        FFFF   4x8
    
+    
+    bob.processing();
+    
     // write data to SD card
-    bob.processing();
     fp = fopen( fn, "a" );      // open file (append)
     for( int i = 0; i < counter; i++ )
-       fprintf( fp, "%.2f;%d;%.2f;%.4f\r\n", (float) collected.t[i]/100.0, collected.L[i], bob.battery(), T ); // write to file
+    {
+        fprintf( fp, "%.2f;", (float) collected.t[i]/100.0); // write to file
+        for(int sensor = 0; sensor < sensors; sensor++)
+            fprintf( fp, "%d;", collected.L[sensor][i]); // write to file
+        fprintf( fp, "%.2f;%.4f\r\n", bob.battery(), T ); // write to file
+    }
     fclose( fp );               // close file
     
     // write to screen
-    pc.printf( "%.2f;%d;%.2f;%.4f\r\n", (float) collected.t[counter-1]/100.0, collected.L[counter-1], bob.battery(), T ); // write to screen
+    // pc.printf( "%.2f;", (float) collected.t[counter-1]/100.0); // write to file
+    // for(int sensor = 0; sensor < sensors; sensor++)
+    //      pc.printf( "%d;", collected.L[sensor][counter-1]); // write to file
+    // pc.printf( "%.2f;%.4f\r\n", bob.battery(), T ); // write to file
     
     bob.no_processing();
                     
@@ -122,6 +136,39 @@
 
 int main(void)
 {
+    if( DEBUG )
+    {      
+        Serial pc( USBTX, USBRX );
+        bob.wakeup_periphery( );
+        LDC1614 ldc( _LDC_SDA, _LDC_SCL, _LDC_SD, f_CLKIN, sensors, C );  
+        DS1825 thermometer( _Tpin );
+        
+        while( 1 ) {          
+        
+            prev = now;
+            now = (uint32_t) clock();
+            
+            bob.processing();
+            for(int sensor = 0; sensor < sensors; sensor++)
+            {
+                // wait for a new conversion to be ready          
+                while( !ldc.is_ready( sensor ) ) {  }
+
+                // write to screen
+                L = ldc.get_Data( sensor );
+                if ( !ldc.get_error( sensor ) )
+                {
+                    pc.printf( "[%.2f Hz] 0x%08X, %.6f MHz, Vbatt = %.2f V, T = %.2f C\r\n", 1/((now-prev)/100.0), L, ldc.get_fsensor( L )/1000000, bob.battery(), thermometer.getTemperature() );
+                    bob.no_error();
+                }else{
+                    bob.error();
+                }
+            }
+            bob.no_processing();
+        }   
+    }
+
+
 
     // TODO:
     // - implement this data conversion in writing to SD!!
@@ -142,6 +189,9 @@
     // This turns enable on, i.e. it powers the SD card system, the crystal and the LDC
     bob.wakeup_periphery();
     
+    // check if both leds work
+    bob.flash( 1 );
+    
     // load SD File System
     SDFileSystem *sd = new SDFileSystem( _SD_MOSI, _SD_MISO, _SD_SCLK, _SD_CS, "sd" );
 
@@ -159,71 +209,27 @@
             {
                 fn = filename.c_str();
                 fclose( fp );
+                bob.processing( 1 );
                 break;
             } else {
-                bob.error();  // error: unable to create new file
+                bob.error( 3 );  // error: unable to create new file
             }
         } else { // file already exists
             fclose( fp );
         }
-    }
+    }    
     if( fp == NULL )
     {
-        bob.error();  // error: file 00 - 99 already exists
+        // THIS ERROR IS NOT CATCHED PROPERLY!!!
+        bob.error( 4 );  // error: file 00 - 99 already exists
     }
 
-
-
-    if( DEBUG )
-    {        
-        bob.wakeup_periphery( );
-        LDC1614 ldc( _LDC_SDA, _LDC_SCL, _LDC_SD, 16E6, sensors, C );  
-
-        while( 1 ) {          
-
-            for(int sensor = 0; sensor < sensors; sensor++)
-            {
-                // wait for a new conversion to be ready          
-                while( !ldc.is_ready( sensor ) ) {  }
-
-                // get error information from ldc.get_Data because 
-                // it is not stored in ldc.get_status as soon as 
-                // ldc.is_ready == true
-
-                bob.processing();
-                
-                // write to SD card
-                fp = fopen( fn, "a" );      // open file (append)
-                fprintf( fp, "%d\r\n", ldc.get_Data( sensor ) ); // write to file
-                fclose( fp );               // close file
-                
-                // write to screen
-                pc.printf( "0x%08X\r\n", ldc.get_Data( sensor ) );
-                
-                bob.no_processing();
-        
-                // write data to screen
-                
-            }
-            // wait(0.5);
-
-        }   
-    }
-
-
-
-
-
-
-
-
-
-
-
     // Unload SD File system
     delete sd;
     sd = NULL;
 
+
+    // LDC1614 *ldc = new LDC1614(_LDC_SDA, _LDC_SCL, _LDC_SD, f_CLKIN, sensors, C);        // load LDC1614 libray 
    
     while(1)
     {
@@ -238,16 +244,15 @@
         next = t + ( (next == 0) ? INTERVAL_FIRST : INTERVAL_ON )*100; // 0 -> 219 904 952 --> (2 199 049,52 s)    (25 days)
 
 
-        // Wakeup the periphery (SD, crystal and LDC1614 on)
+        // Wakeup the periphery (3v3 on. Powers SD, crystal and LDC1614)
         bob.wakeup_periphery();
         
         // load libraries to take control over the communication
-        LDC1614 *ldc = new LDC1614(_LDC_SDA, _LDC_SCL, _LDC_SD, 16E6, sensors, C);        // load LDC1614 libray 
-        // DS1825 *thermometer = new DS1825( _Tpin );                                        // load thermometer (DS1825) library
-        SDFileSystem *sd = new SDFileSystem(_SD_MOSI, _SD_MISO, _SD_SCLK, _SD_CS, "sd");  // load SD File System library
+        LDC1614 *ldc        = new LDC1614(_LDC_SDA, _LDC_SCL, _LDC_SD, f_CLKIN, sensors, C);  // load LDC1614 libray 
+        DS1825 *thermometer = new DS1825( _Tpin );                                     // load thermometer (DS1825) library
+        SDFileSystem *sd    = new SDFileSystem(_SD_MOSI, _SD_MISO, _SD_SCLK, _SD_CS, "sd");  // load SD File System library
             mkdir("/sd", 0777); // select folder
 
-
         // Take samples for INTERVAL_ON seconds
         while( t < next )
         {
@@ -258,40 +263,92 @@
                 t_high++;
             t = now + 429496.7296*t_high + t_lost + 429496.7296*lost_high;
 
-            // wait for a new conversion to be ready          
-            while( !ldc->is_ready( 0 ) ) {  }
+            // assess all sensors
+            for ( int sensor = 0; sensor < sensors; sensor++ )
+            {
+                // wait for a new conversion to be ready          
+                while( !ldc->is_ready( sensor ) ) {  }
+    
+                // collect data
+                L = ldc->get_Data( sensor );
+                
+                // catch errors (sensor broken / not connected)
+                if ( !ldc->get_error( sensor ) )
+                {
+                    // skip the first few measurements (they are too low)
+                    if ( skip >= SKIPPING )
+                    {
+                        // store data
+                        collected.L[sensor][counter] = L;
                         
-            // Store data in temporal memory
-            collected.t[counter] = t;
-            collected.L[counter] = ldc->get_Data( 0 );
-            counter++;
+                        // only store the time once
+                        if ( sensor == 0 )
+                        {
+                            collected.t[counter] = t;
+                            counter++;
+                        }                        
+                    }else{
+                        skip++;
+                    }
+                }else{
+                    bob.error();
+                }
+            }
             
             // Write a full package of data points to the SD card
             if( counter >= package_size-1  )
             {
-                // storeit( thermometer->getTemperature() );
-                storeit( 0.5 );
+                storeit( thermometer->getTemperature() );
             }
+            
+            bob.no_error();
         }
+        skip = 0;
         
         // Write remaining data to the SD card
         if( counter > 0 )
         {
-            // storeit( thermometer->getTemperature() );
-            storeit( 1.5 );
+            storeit( thermometer->getTemperature() );
         }
 
         // prepare for sleep: delete and power down the periphery (SD, crystal and LDC1614)
-        delete sd;          sd  = NULL;             // unload library to be able to power down completely
+        ldc->shutdown();                // _LDC_SD --> 1
+        bob.shutdown_periphery();       // 3V3     --> 0
+
+        // LDC I2C
         delete ldc;         ldc = NULL;             // unload library to be able to power down completely
-        // delete thermometer; thermometer = NULL;     // unload library to be able to power down completely
-        bob.shutdown_periphery();
+        bob.shutdown_pin( _LDC_SDA );
+        bob.shutdown_pin( _LDC_SCL );
+        bob.shutdown_pin( _LDC_SD );
+        
+        // SD SPI
+        delete sd;          sd  = NULL;             // unload library to be able to power down completely
+        bob.shutdown_pin( _SD_PRESENT ); 
+        bob.shutdown_pin( _SD_CS );
+        bob.shutdown_pin( _SD_MOSI );
+        bob.shutdown_pin( _SD_SCLK );
+        bob.shutdown_pin( _SD_MISO );
+        
+        // PC ports (SWD / UARTO)
+        bob.shutdown_pin( PTA0 );   // SWD_CLK
+        bob.shutdown_pin( PTA1 );   // UARTO_RX
+        bob.shutdown_pin( PTA2 );   // UARTO_TX
+        bob.shutdown_pin( PTA3 );   // SWD_DIO
+        // bob.shutdown_pin( PTA20, 1 ); // RESET (does not work properly)
+        
+        // Thermometer
+        delete thermometer; thermometer = NULL;     // unload library to be able to power down completely
+        bob.shutdown_pin( _Tpin );
+        
+        // Button
+        bob.shutdown_pin( _BUTTON );
+
         
         // if the battery is almost empty (Vbatt < 3.10 V), the thermometer stops 
         // working well and L can not be corrected. So just shut down...
         if( bob.battery() < 3.10f )
         {
-            bob.error();  // error: battery empty
+            bob.error( 5 );  // error: battery empty
             // pc.printf( "Battery empty (%.1f V), shutting down.\n", bob.battery() );
             exit(0);
         }
@@ -317,9 +374,6 @@
         bob.sleep( t_sleep * 10 );
 
     }
-
+}
 
 
-
-}
-