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:
10:3cab80866536
Parent:
9:47f1b1c0ef8b
Child:
11:599ca9982e45
--- a/main.cpp	Wed Feb 24 09:39:07 2016 +0000
+++ b/main.cpp	Wed Feb 24 16:26:37 2016 +0000
@@ -1,19 +1,10 @@
-#include "mbed.h"
-#include "LDC1101.h"
-#include "SDFileSystem.h"
-#include "Bob.h"
-#include <iostream>
-#include <vector>
-#include <string>
-using namespace std;
-
 /**
 * @file main.cpp
 * @brief This file programs the processor for the inductive force sensor
 * using the library LDC1101.h and LDC1101.cpp.
 * - Red Led: processing communication with LDC1101
 * - Green Led: processing SD card
-* 
+*
 * Log protocol:
 * -  1 minute  at 20 Hz
 * - 29 minutes rest
@@ -22,12 +13,26 @@
 *
 * @date 2015-12-17
 */
+#include "mbed.h"
+#include "LDC1101.h"
+#include "SDFileSystem.h"
+#include "Bob.h"
+
+#include "OneWire.h"
+
+#include <iostream>
+#include <vector>
+#include <string>
+using namespace std;
+
+
+
 
 // SETTINGS
 bool DEBUG = false;
 float C = 120E-12;      // pF
-int INTERVAL_ON  = 1;  //   30 =       30 sec
-int INTERVAL_OFF = 60;  // 1770 = 29*60+30 sec
+int INTERVAL_ON  = 30;  //   30 =       30 sec
+int INTERVAL_OFF = 870;  // 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
@@ -54,24 +59,62 @@
 
 float f;
 
-int main(void){
+float T;
+BYTE address[8];
+BYTE Tdata[9];
+
+int main(void)
+{
 
-    if( DEBUG ){
+
+
+    if( DEBUG ) {
         LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
-        
+        OneWire *onewire = new OneWire( PTB1 );
+
+
         wait(1);
-        f = ldc->get_fsensor();
-        
-        while(1){
-            while( !ldc->is_New_LHR_data() ){ } // wait until data is ready
+
+
+        while(1) {
+            while( !ldc->is_New_LHR_data() ) { } // wait until data is ready
+
+            L = ldc->get_LHR_Data();
+
+            // initialization of the Thermometer
+            /*
+            onewire.reset();                   
+            onewire.write( 0x33 );  // READ ROM
+            for (int i = 0; i < 8; i++) 
+                address[i] = onewire.read();
+            if( address[7] != onewire.crc8(address, 7) )
+                pc.printf("Address CRC not valid!\r\n");           
+            pc.printf("\r\n");
+            // wait_ms( 750 );
+            */
             
-            L = ldc->get_LHR_Data();
-            pc.printf( "%d", L );
+            // Temperature conversion
+            onewire->reset();
+            onewire->write( 0xCC );
+            onewire->write( 0x44, 1 );
+            wait_ms( 750 );
+            
+            // read scratchpad
+            onewire->reset();
+            onewire->write( 0xCC );
+            onewire->write( 0xBE );
+            for (int i = 0; i < 9; i++) Tdata[i] = onewire->read();
+            if( Tdata[8] != onewire->crc8(Tdata, 8) ) pc.printf("Data CRC not valid!\r\n");
+            
+            T = ((Tdata[1] << 8) | Tdata[0]) / 16.0;
+            
+            pc.printf("L: %d; T: %.4f C", L, T);
+            //pc.printf( "%d", L );
             //pc.printf( "%.3f MHz", ldc->get_fsensor()/1000000 );
             //pc.printf( "%.3f KHz", (f - ldc->get_fsensor())/1000 );
-            
+
             pc.printf( "\r\n" );
-            wait( 10 ); // 20 Hz
+            wait( 1 ); // 20 Hz
         }
     }
 
@@ -80,140 +123,180 @@
     bob.flash(2);
     pc.printf( "check sd: %d\r\n", bob.checkSD() );
     // Load SD File system
-    
-    
+
+
     // bob.wakeup();
     bob.SDon();
     SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
-    
+
     // Create a new data file (data00.txt)
-    mkdir("/sd", 0777);   
+    mkdir("/sd", 0777);
     for(uint8_t i = 0; i < 100; i++) {
         filename[8] = i/10 + '0';
         filename[9] = i%10 + '0';
         fp = fopen( filename.c_str() , "r" );
         if( fp == NULL ) { // read failed so file does not exist
             fp = fopen( filename.c_str(), "w" ); // create it
-            if( fp != NULL ){
+            if( fp != NULL ) {
                 fn = filename.c_str();
                 fclose( fp );
                 break;
-            }else{
+            } else {
                 bob.red();
             }
-        }else{ // file already exists
+        } else { // file already exists
             fclose( fp );
         }
     }
-        
+
     // Unload SD File system
-    delete sd; sd = NULL;
+    delete sd;
+    sd = NULL;
 
 
-    while(1)
-    {        
+    while(1) {
         // SD on + sensor on
         bob.SDon();
 
-        // Sensor on        
+        // Sensor on
         LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
-        
+
         // SD on
-        SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd"); 
-        mkdir("/sd", 0777); // select folder  
-        
+        SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
+        mkdir("/sd", 0777); // select folder
+
         // time
         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)
         S = now + 429496.7296*t_high + lost + 429496.7296*lost_high;    // 0 -> 219 901 952 --> (2 199 019,52 s)    (25 days)
-        
+
         // first time take a comfortably long period
-        if( next == 0 ){
+        if( next == 0 ) {
             next = S + (INTERVAL_ON + INTERVAL_OFF)*100;
-        }else{
+        } else {
             next = S + INTERVAL_ON*100;                                 // 0 -> 219 904 952 --> (2 199 049,52 s)    (25 days)
         }
-        
+
         // Take samples for INTERVAL_ON seconds
-        while( S < next )
-        {
+        while( S < next ) {
             // Collect a package 16 data points
-            while( Lvector.size() < 16 ) // Write is per 512 bits: 16 * 32 = 512
-            {
+            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() ) { }
-                
-                if( !ldc->is_Oscillation_Error() ){ // sensor not overloaded
+
+                if( !ldc->is_Oscillation_Error() ) { // sensor not overloaded
                     // time
                     prev = now;
                     now = (uint32_t) clock();
                     if( now < prev ) t_high++;
                     S = now + 429496.7296*t_high + lost + 429496.7296*lost_high;
-                    
+
                     // induction
                     L = ldc->get_LHR_Data();
 
                     // Store data in temporal memory
                     Svector.push_back( S );
-                    Lvector.push_back( L );                    
+                    Lvector.push_back( L );
                 }
             }
-            
-            pc.printf("%d; %02x\r\n", L, ldc->get_LHR_status() );
-            
+
             // battery level
             batt = bob.battery();
-    
+            
+            // temperature
+            OneWire *onewire = new OneWire( PTB1 );
+            onewire->reset();
+            onewire->write( 0xCC );
+            onewire->write( 0x44, 1 );
+            wait_ms( 750 );
+            onewire->reset();
+            onewire->write( 0xCC );
+            onewire->write( 0xBE );
+            for (int i = 0; i < 9; i++) Tdata[i] = onewire->read();
+            if( Tdata[8] != onewire->crc8(Tdata, 8) ) pc.printf("Data CRC not valid!\r\n");
+            T = ((Tdata[1] << 8) | Tdata[0]) / 16.0;
+            onewire->depower();
+            delete onewire;
+            onewire = NULL;
+
             // Store the package of 16 data points
-            bob.green();
+            bob.red();
             fp = fopen( fn, "a" ); // open file
             for( int i = 0; i < Lvector.size(); i++ )
-                fprintf( fp, "%.2f;%d;%.4f\r\n", (float) Svector.at(i)/100.0, Lvector.at(i), batt ); // write to file
+                fprintf( fp, "%.2f;%d;%.4f;%.4f\r\n", (float) Svector.at(i)/100.0, Lvector.at(i), batt, T ); // write to file
             fclose( fp ); // close file
-            bob.greenoff();
+            bob.redoff();
 
-                       
+            pc.printf("%.2f;%d;%.4f;%.4f\r\n", (float) Svector.at(0)/100.0, Lvector.at(0), batt, T ); // write to console
+
+
             // Release data
             Lvector.clear();
             Svector.clear();
-            
-        }               
-        
+
+        }
+
         // SD off
-        delete sd; sd = NULL;
+        delete sd;
+        sd = NULL;
         bob.SDoff();
-        DigitalOut *sdP2 = new DigitalOut(PTD4); sdP2->write(0); delete sdP2; sdP2 = NULL;// cs
-        DigitalOut *sdP3 = new DigitalOut(PTD6); sdP3->write(0); delete sdP3; sdP3 = NULL;// mosi
-        DigitalOut *sdP5 = new DigitalOut(PTD5); sdP5->write(0); delete sdP5; sdP5 = NULL;// sck
-        DigitalOut *sdP7 = new DigitalOut(PTD7); sdP7->write(0); delete sdP7; sdP7 = NULL;// miso 
-        
+        DigitalOut *sdP2 = new DigitalOut(PTD4);
+        sdP2->write(0);
+        delete sdP2;
+        sdP2 = NULL;// cs
+        DigitalOut *sdP3 = new DigitalOut(PTD6);
+        sdP3->write(0);
+        delete sdP3;
+        sdP3 = NULL;// mosi
+        DigitalOut *sdP5 = new DigitalOut(PTD5);
+        sdP5->write(0);
+        delete sdP5;
+        sdP5 = NULL;// sck
+        DigitalOut *sdP7 = new DigitalOut(PTD7);
+        sdP7->write(0);
+        delete sdP7;
+        sdP7 = NULL;// miso
+
         // Sensor off
-        delete ldc; ldc = NULL;
-        DigitalOut *senP2 = new DigitalOut(PTC7); senP2->write(0); delete senP2; senP2 = NULL; // miso
-        DigitalOut *senP3 = new DigitalOut(PTC5); senP3->write(0); delete senP3; senP3 = NULL; // sck
-        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
-        
-        
-        
+        delete ldc;
+        ldc = NULL;
+        DigitalOut *senP2 = new DigitalOut(PTC7);
+        senP2->write(0);
+        delete senP2;
+        senP2 = NULL; // miso
+        DigitalOut *senP3 = new DigitalOut(PTC5);
+        senP3->write(0);
+        delete senP3;
+        senP3 = NULL; // sck
+        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
+
+
+
         // Calculate sleeping time (correction to INTERVAL_OFF)
         prev = now;
         now = (uint32_t) clock();
         if( now < prev ) t_high++;
         S = now + 429496.7296*t_high + lost + 429496.7296*lost_high;
-        
+
         t_sleep = ( INTERVAL_OFF*100 - (S - next) );
 
         // Add lost time to the counter
         lost_prev = lost;
         lost += t_sleep;
         if( lost < lost_prev ) lost_high++;
-        
+
         // Sleep now...
         // pc.printf( "zzz... (%f) \r\n\r\n", (float) (t_sleep / 100.0) );
         bob.sleep( (float) ( t_sleep / 100.0) );
-        
+
     }
 
-} 
+}