Library to communicate with LDC1101

Dependents:   Inductive_Sensor Inductive_Sensor_Jasper Inductive_Sensor_3

Fork of LDC1000 by First Last

Revision:
20:8e1b1efdbb49
Parent:
19:e205ab9142d8
Child:
21:7c9e04e7d34f
--- a/LDC1101.cpp	Wed Dec 16 16:25:33 2015 +0000
+++ b/LDC1101.cpp	Fri Dec 18 15:49:12 2015 +0000
@@ -4,7 +4,7 @@
 * functions to interface with Texas
 * Instruments' LDC1101.
 *
-* @author Victor Sluiter
+* @author Victor Sluiter & Bob Giesberts
 *
 * @date 2015-12-09
 */
@@ -26,49 +26,58 @@
     init();
 }
 
+void LDC1101::func_mode(LDC_MODE mode) { writeSPI((uint8_t *)(&mode), 0x0B); wait(0.1); }
+void LDC1101::sleep(void) { /* stop toggling the CLKIN pin input and drive the CLKIN pin Low */ func_mode(LDC_MODE_SHUTDOWN); }
+void LDC1101::wakeup(void) { /* start toggling the clock input on the CLKIN pin */ init(); }
+
+
 void LDC1101::init()
 {
+    /********* SETTINGS *****************
+    ** C_sensor     = 120 pF
+    ** L_sensor     =   5 uH
+    ** Rs           = ??? Ohm
+    ** Rp_min       = ??? Ohm
+    **
+    ** RCount       = 65535
+    ** Samplerate   =    15.3 Hz
+    ** t_conv       =    65.5 ms
+    **
+    ** f_sensor_min =   6.4 MHz (d = inf)
+    ** f_sensor_max =  10   MHz (d = 0)
+    ** divider      = (4*f_sensor_max)/f_CLKIN = 4*10/16 = 2,5 --> 2
+    ************************************/
+    
+    
     // Set LDC1101 in configuration modus
-    func_mode(LDC_MODE_STANDBY);     // STANDBY = 0x01 naar 0x0B  
-    wait(0.1);
+    func_mode( LDC_MODE_STANDBY );     // STANDBY = 0x01 naar 0x0B  
 
-    // - initialise LHR mode
+    // - initialise LHR mode & enable SHUTDOWN mode
     setLHRmode();
 
     // - set ResponseTime to 6144
-    // (This setting MUST be applied, leaving it to default does not work)
-    setResponseTime(LDC_RESPONSE_6144); // 6144 = 0x07 naar 0x04
+    setResponseTime( LDC_RESPONSE_6144 );
     
-    // - set Reference Count to 8192 (13 ENOB - 2^13)
-    setReferenceCount(0x8192);   //0xffff
+    // - set Reference Count to highest resolution
+    setReferenceCount( 0xffff );
+    
+    // - set calibrated value for f_sensor_min (d = inf, no target)
+    setf_sensorMin( 6.4 ); // 6.4 MHz
     
     // - disable RP_MAX
     // - set RP_MIN to 3 kOhm
-    setRPsettings(1, RPMIN_12);
+    setRPsettings( 1, RPMIN_12 );
 
     //  - set Divider to 2
-    setDivider(DIVIDER_2);  
+    setDivider( DIVIDER_2 );  
 
     // Done configuring settings, set LDC1101 in measuring modus
-    func_mode(LDC_MODE_ACTIVE);      // ACTIVE = 0x00 naar 0x0B
+    func_mode( LDC_MODE_ACTIVE );
 }
 
-void LDC1101::setResponseTime(LDC_RESPONSE responsetime)
-{
-    uint16_t resps[] = {0, 0, 192, 384, 768, 1536, 3072, 6144};
-    _responsetime = resps[responsetime];
-    writeSPIregister(0x04, responsetime);
-}
-
-void LDC1101::setReferenceCount(uint16_t rcount)
-{
-    _Rcount = rcount;
-    
-    uint8_t LHR_RCOUNT_LSB = (rcount & 0x00ff);
-    uint8_t LHR_RCOUNT_MSB = ((rcount & 0xff00) >> 8);
-    
-    writeSPIregister(0x30, LHR_RCOUNT_LSB);   //LSB
-    writeSPIregister(0x31, LHR_RCOUNT_MSB);   //MSB
+void LDC1101::setLHRmode( void ){
+    writeSPIregister( 0x05, 0x03 ); // ALT_CONFIG:  0000 0011 --> LHR modus + Shutdown enabled
+    writeSPIregister( 0x0C, 0x01 ); // D_CONFIG:    Enables LHR modus, disables RP 
 }
 
 void LDC1101::setRPsettings(bool RP_MAX_DIS, RPMIN rpmin)
@@ -83,13 +92,43 @@
     uint8_t divs[] = {1, 2, 4, 8};
     _divider = divs[div];    
     writeSPIregister(0x34, div);
-}   
+} 
+
+void LDC1101::setResponseTime(LDC_RESPONSE responsetime)
+{
+    uint16_t resps[] = {0, 0, 192, 384, 768, 1536, 3072, 6144};
+    _responsetime = resps[responsetime];
+    uint8_t buffer[1];    
+    readSPI(buffer, 0x04, 1);
+    writeSPIregister(0x04, (buffer[0] & 0xF8) + responsetime);
+}
+
+void LDC1101::setReferenceCount(uint16_t rcount)
+{
+    _Rcount = rcount;
+    uint8_t LHR_RCOUNT_LSB = (rcount & 0x00ff);
+    uint8_t LHR_RCOUNT_MSB = ((rcount & 0xff00) >> 8);
+    writeSPIregister(0x30, LHR_RCOUNT_LSB);   //LSB
+    writeSPIregister(0x31, LHR_RCOUNT_MSB);   //MSB
+}
+
+void LDC1101::setSampleRate(float samplerate){ setReferenceCount( ((_fCLKIN/samplerate)-55)/16 ); }
 
 
-float LDC1101::get_Q(void)
+void LDC1101::setf_sensorMin(float f_sensor_min)
 {
-    return _RPmin * sqrt(_cap/_inductance*1000000); 
-}  
+    uint8_t buffer[1];    
+    readSPI(buffer, 0x04, 1);
+    uint8_t MIN_FREQ = 16 - (8 / f_sensor_min);
+    writeSPIregister(0x04, ((buffer[0] & 0x0F) + (MIN_FREQ << 4)));
+}
+
+
+
+  
+
+
+float LDC1101::get_Q(void){ return _RPmin * sqrt(_cap/_inductance*1000000); }  
 
 
 float LDC1101::get_fsensor(void)
@@ -103,30 +142,25 @@
 float LDC1101::get_Inductance(void)
 {  
     _fsensor = get_fsensor();
-    
     //               1
     // L = ---------------------        --> p. 34
     //     C * (2*PI*f_sensor)^2
-    _inductance = 1./(_cap * 4*PI*PI*_fsensor*_fsensor);
+    _inductance = 1./(_cap * 4*PI*PI*_fsensor*_fsensor); // (p.34)
     return _inductance;
 }
 
 
 uint32_t LDC1101::get_LHR_Data(void)
 {
-    // LHR_DATA (p.26 & p.27)
     uint8_t LHR_DATA[3];
     readSPI(LHR_DATA, 0x38, 3);     // 0x38 + 0x39 + 0x3A
-    uint32_t combinedbytes = (LHR_DATA[2]<<16) | (LHR_DATA[1]<<8) | LHR_DATA[0];
-    return combinedbytes;
+    return (LHR_DATA[2]<<16) | (LHR_DATA[1]<<8) | LHR_DATA[0];
 }
 
 void LDC1101::readSPI(uint8_t *data, uint8_t address, uint8_t num_bytes)
 {
     // CSB down
     _cs_pin.write(0);
-    
-    // makes sure the address starts with 1... Why?
     _spiport.write(address | 0x80); //read flag 
     for(int i=0; i < num_bytes ; i++)
     {
@@ -155,8 +189,6 @@
 // The data will be printed on the screen using RealTerm: baud 9600.
 // Begin ***********************************************************
     float LDC1101::get_fCLKIN()             {return _fCLKIN;};    
-    uint16_t LDC1101::get_responsetime()    {return _responsetime;};    
-    uint16_t LDC1101::get_Rcount()          {return _Rcount;};
     uint8_t LDC1101::get_divider()          {return _divider;};
     float LDC1101::get_RPmin()              {return _RPmin;};
     float LDC1101::get_cap()                {return _cap;};