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:
22:ac26f52ebaf8
Parent:
21:b96a3de88002
--- a/main.cpp	Wed Sep 21 14:26:45 2016 +0000
+++ b/main.cpp	Wed Oct 05 08:15:57 2016 +0000
@@ -26,25 +26,27 @@
 
 
 
-bool DEBUG              = false;
-// Serial pc( USBTX, USBRX );  
+bool DEBUG              = true;
+
 
 
 // SETTINGS
 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
+const uint8_t sensors   = 1;            // number of attached sensors
 
-uint16_t INTERVAL_FIRST = 900;         // First sampling time in seconds.  60:00 minutes = 3600 sec
+uint16_t INTERVAL_FIRST = 3600;         // 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
 
+uint8_t SKIPPING        = 2;            // The first how many measurements should be skipped?
+
 // Leds
-PinName _LED_PROCESS = PTB0; // Green led 
-PinName _LED_ERROR   = PTB1; // Red led
+PinName _LED_PROCESS = PTB1; // Green led 
+PinName _LED_ERROR   = PTB0; // Red led
 
 // Thermometer
-PinName _Tpin        = PTC1; // OneWire system (PTB1 for the first sensor) // old = PTB0, new: PTC1
+PinName _Tpin        = PTC1; // OneWire system (PTB1 for the first sensor)
 
 // LDC1614
 PinName _LDC_SDA     = PTC5; // I2C: SDA
@@ -52,21 +54,21 @@
 PinName _LDC_SD      = PTC6; // Shutdown
 
 // SD File system
-PinName _SD_PRESENT  = PTE0; // card detect // old = new: PTE0
-PinName _SD_MOSI     = PTD6; // mosi        // old = new: PTD6
-PinName _SD_MISO     = PTD7; // miso        // old = new: PTD7
-PinName _SD_SCLK     = PTD5; // sclk        // old = new: PTD5
-PinName _SD_CS       = PTD4; // cs          // old = new: PTD4
+PinName _SD_PRESENT  = PTE0; // card detect
+PinName _SD_MOSI     = PTD6; // mosi 
+PinName _SD_MISO     = PTD7; // miso 
+PinName _SD_SCLK     = PTD5; // sclk 
+PinName _SD_CS       = PTD4; // cs
 
 // Other components
-PinName _ENABLE      = PTC3;   // enables SD card and Crystal
-PinName _BATTERY     = PTC2;   // voltage   // old = new: PTC2
+PinName _ENABLE      = PTC3; // enables SD card and Crystal
+PinName _BATTERY     = PTC2; // voltage
 PinName _BUTTON      = PTA4;
 
 
 // load libraries
 Bob bob( _LED_PROCESS, _LED_ERROR, _BUTTON, _ENABLE, _SD_PRESENT, _BATTERY );
-// Serial pc( USBTX, USBRX );
+
 
 // timer variables
 uint32_t now = 0, next = 0, prev = 0;
@@ -81,9 +83,6 @@
 string filename = "/sd/data00.txt";
 const char *fn;
 
-// temperature variables
-float T;
-
 // LDC variables
 uint32_t L;
 
@@ -91,58 +90,68 @@
 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[sensors][package_size];   // LHR_DATA
+    uint32_t    L[sensors][package_size];   // DATA
+    float T;                                // Temperature
 } collected;
 uint8_t counter = 0;
-uint8_t SKIPPING = 3;
 uint8_t skip = 0;
 
+
+
 // function to write all data to the SD card
-void storeit( float T )
-{   
-    // TODO: writing to SD card more efficient!
-    // t    32-bit  |   0.00    2000000.00     3-10x8-bit |    FFFFFFFF   8x8
-    // L    24-bit  |   0       1677216        6- 7x8-bit |      FFFFFF   6x8
-    // V     8-bit  |   3.10          4.20        4x8-bit |          FF   2x8
-    // T    16-bit  | -20.0000       85.0000      7x8-bit |        FFFF   4x8
-   
-    
+void storeit( void )
+{       
     bob.processing();
     
-    // write data to SD card
-    fp = fopen( fn, "a" );      // open file (append)
-    for( int i = 0; i < counter; i++ )
-    {
-        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;", (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
+        // write data to SD card
+        fp = fopen( fn, "a" );      // open file (append)
+        for( int i = 0; i < counter; i++ )
+        {
+            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, "%.4f;%.4f\r\n", bob.battery(), collected.T ); // write to file
+        }
+        fclose( fp );               // close file
     
     bob.no_processing();
+    
+    // write data to screen
+    // debug( "Writing to SD: " );
+    // debug( "%.2f;", (float) collected.t[0]/100.0); // write to file
+    // for(int sensor = 0; sensor < sensors; sensor++)
+    //     debug( "%d;", collected.L[sensor][0]); // write to file
+    // debug( "%.4f;%.4f\r\n", bob.battery(), collected.T ); // write to file
                     
     // Reset data
     memset(collected.t, 0, counter);
     memset(collected.L, 0, counter);
+    collected.T = 0.0;
     counter = 0;    
 }
 
+
+
 int main(void)
 {
     if( DEBUG )
-    {      
-        Serial pc( USBTX, USBRX );
+    {           
+        while ( 1 ) {
+        
+            // 5 sec awake
+            wait( 5 );
+        
+            // 5 sec asleep
+            bob.sleep( 5 );
+        }
+        
+        /*
         bob.wakeup_periphery( );
         LDC1614 ldc( _LDC_SDA, _LDC_SCL, _LDC_SD, f_CLKIN, sensors, C );  
         DS1825 thermometer( _Tpin );
         
+        wait(0.5);
+        
         while( 1 ) {          
         
             prev = now;
@@ -151,6 +160,8 @@
             bob.processing();
             for(int sensor = 0; sensor < sensors; sensor++)
             {
+                // ldc.autoDriveCurrent( sensor );
+                
                 // wait for a new conversion to be ready          
                 while( !ldc.is_ready( sensor ) ) {  }
 
@@ -158,33 +169,34 @@
                 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() );
+                    debug( "[%.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{
+                    debug( "%d\r\n", L );
                     bob.error();
                 }
             }
+            debug( "\r\n", L );
+            
             bob.no_processing();
-        }   
+            
+            wait(0.5);
+        }
+        */   
     }
-
-
-
-    // TODO:
-    // - implement this data conversion in writing to SD!!
-    // - use T as uint16_t, not float!
-    // - use V as uint16_t, not float!
-    // - write blocks of 512 bit (4x 32 + 32 + 16 + 16)
-    //
-    // float temp_a = 127.9375;
-    // pc.printf( "A: %.4f (%08x); \r\n", temp_a, * (uint32_t *) &temp_a);
-
-    // Load SD File system
-    // - the Raspberry Pie SD cards give a warning here that might just be ignored:
-    //      "Not in idle state after sending CMD8 (not an SD card?)
-    //       Didn't get a response from the disk
-    //       Set 512-byte block timed out"
-    // TODO: buy 'better' SD cards from IAPC / the Stores (Kingston ...)
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
     
     // This turns enable on, i.e. it powers the SD card system, the crystal and the LDC
     bob.wakeup_periphery();
@@ -217,19 +229,23 @@
         } else { // file already exists
             fclose( fp );
         }
-    }    
+    }   
+    // THIS ERROR IS NOT CATCHED PROPERLY!!! 
     if( fp == NULL )
     {
-        // THIS ERROR IS NOT CATCHED PROPERLY!!!
         bob.error( 4 );  // error: file 00 - 99 already exists
     }
 
+
+
+
+
+
+
+
     // 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)
     {
@@ -248,8 +264,8 @@
         bob.wakeup_periphery();
         
         // load libraries to take control over the communication
-        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
+        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
 
@@ -282,13 +298,16 @@
                         collected.L[sensor][counter] = L;
                         
                         // only store the time once
-                        if ( sensor == 0 )
+                        // do this after the last sensor
+                        if ( sensor == sensors-1 )
                         {
                             collected.t[counter] = t;
                             counter++;
                         }                        
                     }else{
-                        skip++;
+                        // count the skipper in pairs, so after the last one
+                        if ( sensor == sensors-1 )
+                            skip++;
                     }
                 }else{
                     bob.error();
@@ -298,17 +317,23 @@
             // Write a full package of data points to the SD card
             if( counter >= package_size-1  )
             {
-                storeit( thermometer->getTemperature() );
+                // No need to measure temperature with 10+ Hz
+                // only calculate the temperature once every time data is stored
+                collected.T = thermometer->getTemperature();
+                storeit(  );
             }
             
             bob.no_error();
         }
+        
+        // Reset the skipper
         skip = 0;
         
         // Write remaining data to the SD card
         if( counter > 0 )
         {
-            storeit( thermometer->getTemperature() );
+            collected.T = thermometer->getTemperature();   
+            storeit(  );
         }
 
         // prepare for sleep: delete and power down the periphery (SD, crystal and LDC1614)
@@ -319,7 +344,7 @@
         delete ldc;         ldc = NULL;             // unload library to be able to power down completely
         bob.shutdown_pin( _LDC_SDA );
         bob.shutdown_pin( _LDC_SCL );
-        bob.shutdown_pin( _LDC_SD );
+        bob.shutdown_pin( _LDC_SD  );
         
         // SD SPI
         delete sd;          sd  = NULL;             // unload library to be able to power down completely
@@ -334,6 +359,7 @@
         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
@@ -349,7 +375,6 @@
         if( bob.battery() < 3.10f )
         {
             bob.error( 5 );  // error: battery empty
-            // pc.printf( "Battery empty (%.1f V), shutting down.\n", bob.battery() );
             exit(0);
         }