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:
17:a4700b1c3c37
Parent:
16:b53721840a38
Child:
19:c0707d20bada
--- a/main.cpp	Wed Aug 24 10:31:32 2016 +0000
+++ b/main.cpp	Wed Sep 07 09:59:16 2016 +0000
@@ -24,8 +24,13 @@
 #include <string>
 using namespace std;
 
+
+
+bool DEBUG              = true;
+
+
+
 // SETTINGS
-bool DEBUG              = false;
 float C                 = 120E-12;      // Capacitor in F
 int sensors             = 2;            // number of attached sensors
 uint16_t INTERVAL_FIRST = 3600;         // First sampling time in seconds.  60:00 minutes = 3600 sec
@@ -45,7 +50,6 @@
 PinName _LDC_SD      = PTC6; // Shutdown
 
 // SD File system
-PinName _SD_ENABLE   = PTC3;
 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
@@ -53,14 +57,14 @@
 PinName _SD_CS       = PTD4; // cs          // old = new: PTD4
 
 // Other components
-PinName _OSCILLATOR  = PTC3;
+PinName _ENABLE      = PTC3;   // enables SD card and Crystal
 PinName _BATTERY     = PTC2;   // voltage   // old = new: PTC2
 PinName _BUTTON      = PTA4;
 
 
 // load libraries
-Bob bob( _LED_PROCESS, _LED_ERROR, _BUTTON, _SD_PRESENT, _BATTERY );
-Serial pc(USBTX, USBRX);
+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;
@@ -101,7 +105,7 @@
 
     // construct data
     /*
-    bob.ledon();
+    bob.processing();
     // fp = fopen( fn, "a" );      // open file (append)
     char buffer[16]; memset(buffer, 0, sizeof(buffer)); 
     char towrite[64]; memset(towrite, 0, sizeof(towrite)); 
@@ -143,11 +147,11 @@
         //pc.printf( "%s", towrite);
     }   
     //fclose( fp ); // close file
-    bob.ledoff();
+    bob.no_processing();
     */   
     
     // write data to SD card
-    bob.ledon();
+    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
@@ -156,7 +160,7 @@
     // 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
     
-    bob.ledoff();
+    bob.no_processing();
                     
     // Reset data
     memset(collected.t, 0, counter);
@@ -167,33 +171,44 @@
 int main(void)
 {
     if( DEBUG )
-    {
-        Serial pc( USBTX, USBRX );
-
-        LDC1614 ldc( _LDC_SDA, _LDC_SCL, _LDC_SD, _OSCILLATOR, 16E6, sensors, C );
+    {        
+        bob.wakeup_periphery( );
+        wait(1);
+        // LDC1614 ldc( _LDC_SDA, _LDC_SCL, _LDC_SD, 16E6, sensors, C );    // error: pinmap not found for peripheral
+        // LDC1614 ldc( PTC5, PTC4, PTC6, 16E6, sensors, C );               // error: pinmap not found for peripheral
+        // I2C _i2c( PTC5, PTC4 );                                          // error: pinmap not found for peripheral
+        // I2CX _i2c( I2C_0, PTC5, PTC4 );                                  // error: library doesn't work
         
-        uint8_t status;
-        
+        // KL25Z has standard allocated I2C pins that are not present in our package (32-pins)
+        // Dynamic allocation of pinnames...
+                        
         while( 1 ) {
+            
+            pc.printf( "works!\r\n" );
+                        
+            wait(1);
+            
+            /* 
             // check the sensor
-            status = ldc.get_status();
+            status = ldc.get_status();            
 
             // if there is an error turn on the red led
             if( ldc.is_error( status ) )
             {
                 // error
-                // bob.error();
+                bob.error();
             }else{
                 if( ldc.is_ready( status ) )
                 {
-                    //bob.no_error();
+                    bob.no_error();
                     pc.printf( "sensor 1: %d | sensor 2: %d\r\n", ldc.get_Data( 0 ), ldc.get_Data( 1 ) );
                 }
             }
+            */
+            
         }   
-    }    
-    
-    
+    }
+
 
     // TODO:
     // - implement this data conversion in writing to SD!!
@@ -210,7 +225,11 @@
     //       Didn't get a response from the disk
     //       Set 512-byte block timed out"
     // TODO: buy 'better' SD cards from IAPC / the Stores (Kingston ...)
-    bob.wakeup();
+    
+    // This turns enable on, i.e. it powers the SD card system, the crystal and the LDC
+    bob.wakeup_periphery();
+    
+    // load SD File System
     SDFileSystem *sd = new SDFileSystem( _SD_MOSI, _SD_MISO, _SD_SCLK, _SD_CS, "sd" );
 
     // Create a new data file (data00.txt)
@@ -229,7 +248,7 @@
                 fclose( fp );
                 break;
             } else {
-                bob.ledon();  // error: unable to create new file
+                bob.error();  // error: unable to create new file
             }
         } else { // file already exists
             fclose( fp );
@@ -237,39 +256,37 @@
     }
     if( fp == NULL )
     {
-        bob.ledon();  // error: file 00 - 99 already exists
+        bob.error();  // error: file 00 - 99 already exists
         // pc.printf( "/sd/data00.txt t/m /sd/data99.txt already exist!\n" );
     }
 
     // Unload SD File system
     delete sd;
     sd = NULL;
+
    
     while(1)
     {
-        // Wakeup: SD + LDC1101 on
-        bob.wakeup();
-
         // what time is it now?
         prev = now;                                                     // 0 -> 429 496     --> (4 294,96 s)        (71 min)
         now = (uint32_t) clock();                                       // 0 -> 429 496     --> (4 294,96 s)        (71 min)
         if( now < prev )
             t_high++;                                                   // 0 -> 255         --> (255*4 294,96 s)    (12 days)
         t = now + 429496.7296*t_high + t_lost + 429496.7296*lost_high;  // 0 -> 219 901 952 --> (2 199 019,52 s)    (25 days)
+
+        // How long should we takes samples?
+        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)
+        bob.wakeup_periphery();
         
         // load libraries to take control over the communication
-        LDC1614 *ldc = new LDC1614(_LDC_SDA, _LDC_SCL, _LDC_SD, _OSCILLATOR, 16E6, sensors, C);        
+        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 system
+        SDFileSystem *sd = new SDFileSystem(_SD_MOSI, _SD_MISO, _SD_SCLK, _SD_CS, "sd");  // load SD File System library
             mkdir("/sd", 0777); // select folder
 
-        // How long should we takes samples?
-        if( next == 0 )
-        {
-            next = t + INTERVAL_FIRST*100;
-        } else {
-            next = t + INTERVAL_ON*100;                                 // 0 -> 219 904 952 --> (2 199 049,52 s)    (25 days)
-        }
 
         // Take samples for INTERVAL_ON seconds
         while( t < next )
@@ -287,10 +304,10 @@
             if( ldc->is_error(status) )
             {
                 // red led on
-                // bob.error();
+                bob.error();
             }else{  
                 // red led off
-                // bob.no_error();
+                bob.no_error();
             
                 if( ldc->is_ready(status) ) // data from all sensors is ready
                 {
@@ -312,17 +329,17 @@
         if( counter > 0 )
             storeit( thermometer->getTemperature() );
 
-        // prepare for sleep, power down the SD and the LDC1101
+        // 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
         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.beforesleep();
+        bob.shutdown_periphery();
         
         // 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.ledon();  // error: battery empty
+            bob.error();  // error: battery empty
             // pc.printf( "Battery empty (%.1f V), shutting down.\n", bob.battery() );
             exit(0);
         }