Library to communicate with LDC1614

Dependencies:   SHTx

Dependents:   Inductive_Sensor_3

Fork of LDC1101 by Bob Giesberts

Revision:
16:07d0e43c2d12
Child:
17:a5cf2b4bec13
diff -r 8a09279a05eb -r 07d0e43c2d12 LDC1101.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LDC1101.cpp	Thu Dec 10 15:12:16 2015 +0000
@@ -0,0 +1,136 @@
+/**
+* @file LDC1101.cpp
+* @brief this C++ file contains all required
+* functions to interface with Texas
+* Instruments' LDC1101.
+*
+* @author Victor Sluiter
+*
+* @date 2015-12-09
+*/
+
+#include "LDC1101.h"
+
+ 
+LDC1101::LDC1101(PinName mosi, PinName miso, PinName sck, PinName cs, float capacitor, float f_external, PinName clock_out) : _spiport(mosi,miso,sck, NC), _cs_pin(cs), _clock(clock_out,1)
+{
+
+    cap = capacitor;
+    _spiport.format(8,3);
+    _spiport.frequency(1E6);
+    setFrequency(f_external);
+    
+    // ---CSB must go low before accessing first address???
+    _cs_pin.write(1);
+    wait_us(100);
+    
+    init();
+}
+
+void LDC1101::init()
+{
+    // configuration
+    mode(LDC_MODE_STANDBY);     // 0x01 naar 0x0B  
+    
+    wait(0.1);
+    wait_us(10);
+
+    setResponseTime(LDC_RESPONSE_6144);
+
+    // L-Only Measurement
+    writeSPIregister(0x05, 0x03);   // ALT_CONFIG:  clock config >> we get 0x00 if this line is disabled and the cable is reconnected 
+    writeSPIregister(0x0C, 0x01);   // D_CONF:      Register 0x0C enables a function that can improve L measurements while disabling RP measurements 
+
+    // High Resolution Reference Count
+    // LHR_COUNT_LSB:   0xff naar 0x30 ???
+    // LHR_COUNT_MSB:   0xff naar 0x31 ???
+
+    // Start measuring
+    mode(LDC_MODE_ACTIVE);      // 0x00 naar 0x0B
+}
+
+void LDC1101::setResponseTime(LDC_RESPONSE responsetime)
+{
+    _responsetime = responsetime;
+    writeSPIregister(0x04, responsetime);
+}
+
+void LDC1101::setFrequency(float frequency)
+{
+    _frequency = frequency;
+    _clock.period(1.0/frequency);
+    _clock.pulsewidth(0.5/frequency);
+}
+
+float LDC1101::getInductance()
+{
+    uint16_t resp[] = {0,0,192, 384, 768, 1536, 3072, 6144};
+    _raw_l = readRawCounts();
+    
+    // f_CLKIN * RESP_TIME
+    // -------------------
+    //      3 * L_DATA
+    _fsensor = (_frequency/(_raw_l*3.0))*resp[(uint8_t)(_responsetime)];
+    
+    //            1
+    // ---------------------        --> p. 31
+    // C * (2*PI*f_sensor)^2
+    return 1./(cap*pow(2*PI*_fsensor,2));
+};
+
+
+uint32_t LDC1101::readRawCounts(void)
+{
+    uint8_t val[5];
+    
+    // 0x38 + 0x39 + 0x3A
+    readSPI(val, 0x38, 5);
+    uint32_t combinedbytes = (val[4]<<16)| (val[3]<<8) | val[2];  // combine the content of the 3 bytes from registers 23, 24 and 25 
+    return combinedbytes;
+}
+
+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++)
+    {
+        data[i] = _spiport.write(0xFF);
+    }
+    // CSB up
+    _cs_pin.write(1);
+}
+
+void LDC1101::writeSPI(uint8_t *data, uint8_t address, uint8_t num_bytes)
+{
+    // CSB down
+    _cs_pin.write(0);
+    
+    _spiport.write(address); 
+    for(int i=0; i < num_bytes ; i++)
+    {
+        _spiport.write(data[i]);
+    }
+    // CSB up
+    _cs_pin.write(1);
+}
+
+
+// EXTRA test: Get&print values of all variables to verify (to calculate the induction)
+// The data will be printed on the screen using RealTerm: baud 9600.
+// Begin ***********************************************************
+    float LDC1101::get_raw_l()          {_raw_l = readRawCounts(); 
+                                        return _raw_l;};        
+    float LDC1101::get_fsensor()        {
+    uint16_t resp[] = {0, 0, 192, 384, 768, 1536, 3072, 6144};
+    _raw_l = readRawCounts();
+    _fsensor = (_frequency/(_raw_l*3.0))*resp[(uint8_t)(_responsetime)];                
+        return _fsensor;};        
+    
+    float LDC1101::get_frequency()      {return _frequency;};    
+    float LDC1101::get_responsetime()   {return _responsetime;};    
+    float LDC1101::get_cap()            {return cap;};
+// END ***********************************************************
\ No newline at end of file