Communication with LDC1101. Working version, ready for extensive calibration tests for resolution, linearity, etc.

Dependencies:   Bob DS1825 LDC1101 SDFileSystem mbed

Fork of Inductive_Sensor by Bob Giesberts

Revision:
7:6c4cac1ec122
Parent:
6:ff39d60061ca
Child:
8:8cc1960467ae
--- a/main.cpp	Mon Jan 18 15:43:07 2016 +0000
+++ b/main.cpp	Mon Jan 25 09:13:11 2016 +0000
@@ -26,8 +26,8 @@
 // SETTINGS
 bool DEBUG = false;
 float C = 120E-12;      // pF
-int INTERVAL_ON  = 30;  //   30 =       30 sec
-int INTERVAL_OFF = 1770;  // 1770 = 29*60+30 sec
+int INTERVAL_ON  = 5;  //   30 =       30 sec
+int INTERVAL_OFF = 5;  // 1770 = 29*60+30 sec
 
 // load libraries
 Bob bob(PTB0, PTB1, PTC3, PTE0, PTC2, PTE30); // red led, green led, sd_enable, sd_present, batt, 3V3_DET
@@ -45,15 +45,28 @@
 const char *fn;
 
 // temporal storage for data samples
-vector < float > Svector;
-float S;
+vector < uint32_t > Svector;
+uint32_t S;
 vector < uint32_t > Lvector;
 uint32_t L;
 float batt;
 
+uint8_t bla;
 
 int main(void){
 
+    if( DEBUG ){
+        LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
+        
+        while(1){
+            while( !ldc->is_New_LHR_data() ){ } // wait until data is ready
+            
+            pc.printf( "%d\r\n", ldc->get_LHR_Data() );
+        }
+    }
+
+
+
     bob.flash(2);
 
     // Load SD File system
@@ -108,48 +121,44 @@
         mkdir("/sd", 0777); // select folder  
         
         // clock
-        now  = (uint32_t) clock(); 
+        now  = (uint32_t) clock();
         next = (uint32_t) now + INTERVAL_ON*100;
         
         // Take samples for INTERVAL_ON seconds
         while( (uint32_t) clock() < next )
         {
             // Collect a package 16 data points
-            bob.green();
             while( Lvector.size() < 16 ) // Write is per 512 bits: 16 * 32 = 512
             {
                 // wait for new data to be ready
-                while( !ldc->is_New_LHR_data() ) { } // does not seem to work?
+                while( !ldc->is_New_LHR_data() ) { }
                 
                 if( !ldc->is_Oscillation_Error() ){ // sensor not overloaded
                     // time
                     prev = now;
                     now = (uint32_t) clock();
                     if( now < prev ) t_high++;
-                    S = ((float) now/100.0) + 4294.967296*(float)t_high + lost + 4294.967296*(float)lost_high;
+                    S = now + 429496.7296*t_high + lost + 429496.7296*lost_high;
                     Svector.push_back( S );
                     
                     // induction
                     L = ldc->get_LHR_Data();
-                    Lvector.push_back( L );
-                    
-                    // set sample rate
-                    wait_ms(50);    // ~20 Hz
+                    Lvector.push_back( L );                    
                 }
             }
             // battery level
             batt = bob.battery();
-            bob.greenoff();
     
             // Store the package of 16 data points
-            // pc.printf( "[%d] Writing %dx data to SD card: %.2f;%d;%.4f\r\n", lost, Lvector.size(), Svector[0], Lvector[0], batt );
-            // bob.red();
+            pc.printf( "Writing %d samples of data to SD card: Time = %.2f; Inductance = %d; Battery = %.4f\r\n", Lvector.size(), (float) Svector.at(15)/100.0, Lvector.at(15), batt );
+            
+            bob.green();
             fp = fopen( fn, "a" ); // open file
             // if( fp == NULL ) { pc.printf("error (append)...\r\n"); }
             for( int i = 0; i < Lvector.size(); i++ )
-                fprintf( fp, "%.2f;%d;%.4f\r\n", Svector.at(i), Lvector.at(i), batt ); // write to file
+                fprintf( fp, "%.2f;%d;%.4f\r\n", (float) Svector.at(i)/100.0, Lvector.at(i), batt ); // write to file
             fclose( fp ); // close file
-            // bob.redoff();
+            bob.greenoff();
             
             // Release data
             Lvector.clear();
@@ -172,12 +181,14 @@
         DigitalOut *senP4 = new DigitalOut(PTC6); senP4->write(0); delete senP4; senP4 = NULL; // mosi
         DigitalOut *senP5 = new DigitalOut(PTC4); senP5->write(0); delete senP5; senP5 = NULL; // cs
         
+        pc.printf( "zzz...\r\n\r\n" );
+        
         // Sleep for INTERVAL_OFF samples
         bob.sleep( (float) INTERVAL_OFF - (clock() - next)/100.0 );
         
         // Add lost time to the counter
         lost_prev = lost;
-        lost += (INTERVAL_OFF);
+        lost += (INTERVAL_OFF)*100;
         if( lost < lost_prev ) lost_high++;
     }