Bob Giesberts / LDC1614

Dependencies:   SHTx

Dependents:   Inductive_Sensor_3

Fork of LDC1101 by Bob Giesberts

Files at this revision

API Documentation at this revision

Comitter:
bobgiesberts
Date:
Thu Dec 10 15:12:16 2015 +0000
Parent:
15:8a09279a05eb
Child:
17:a5cf2b4bec13
Commit message:
Started new library for LDC1101, forked from LDC1000

Changed in this revision

FastPWM.lib Show annotated file Show diff for this revision Revisions of this file
LDC1000.cpp Show diff for this revision Revisions of this file
LDC1000.h Show diff for this revision Revisions of this file
LDC1101.cpp Show annotated file Show diff for this revision Revisions of this file
LDC1101.h Show annotated file Show diff for this revision Revisions of this file
--- a/FastPWM.lib	Tue Aug 18 17:46:35 2015 +0000
+++ b/FastPWM.lib	Thu Dec 10 15:12:16 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/Sissors/code/FastPWM/#1f451660d8c0
+http://developer.mbed.org/users/Sissors/code/FastPWM/#8b1bf34c72aa
--- a/LDC1000.cpp	Tue Aug 18 17:46:35 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,125 +0,0 @@
-/**
-* @file LDC1000.h
-* @brief this C++ file wcontains all required
-* functions to interface with Texas
-* Instruments' LDC1000.
-*
-* @author Victor Sluiter
-*
-* @date 2015-04-01
-*/
-
-#include "LDC1000.h"
-
-LDC1000::LDC1000(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);
-    _cs_pin.write(1);
-    wait_us(100);
-    mode(LDC_MODE_STANDBY);
-    setFrequency(f_external);
-    wait(0.1);
-    wait_us(10);
-
-    setWatchdog(5000);
-    setResponseTime(LDC_RESPONSE_6144);
-    setOutputPower(LDC_AMPLITUDE_4V);
-
-    writeSPIregister(0x05,0x00);   // clock config >> we get 0x00 if this line is disabled and the cable is reconnected 
-    writeSPIregister(0x0C,0x01);   // Register 0x0C enables a function that can improve L measurements while disabling RP measurements 
-
-    mode(LDC_MODE_ACTIVE);
-}
-
-void LDC1000::setOutputPower(LDC_AMPLITUDE amplitude)
-{
-    uint8_t buffer;
-    _amplitude  = amplitude;
-    readSPI(&buffer, 0x04);
-    buffer &= 0xE7; //clear amplitude bits
-    buffer |= (amplitude<<3) & 0x18;
-    writeSPI(&buffer,0x04);
-}
-
-void LDC1000::setWatchdog(float frequency)
-{
-    uint8_t buffer;
-    buffer = 68.94*log(frequency/2500);
-    writeSPI(&buffer,0x03);
-}
-
-void LDC1000::setResponseTime(LDC_RESPONSE responsetime)
-{
-    uint8_t buffer;
-    _responsetime = responsetime;
-    readSPI(&buffer, 0x04);
-    buffer &= 0xF8; //clear responsetime bits
-    buffer |= responsetime & 0x07;
-    //writeSPIregister(0x04,buffer);
-    writeSPI(&buffer,0x04);
-}
-
-void LDC1000::setFrequency(float frequency)
-{
-    _frequency = frequency;
-    _clock.period(1.0/frequency);
-    _clock.pulsewidth(0.5/frequency);
-}
-
-float LDC1000::getInductance()
-{
-    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 1./(cap*pow(2*PI*_fsensor,2));
-};
-
-
-uint32_t LDC1000::readRawCounts(void)
-{
-    uint8_t val[5];
-    readSPI(val,0x21,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 LDC1000::readSPI(uint8_t *data, uint8_t address, uint8_t num_bytes)
-{
-    _cs_pin.write(0);
-    _spiport.write(address | 0x80); //read flag 
-    for(int i=0; i < num_bytes ; i++)
-    {
-        data[i] = _spiport.write(0xFF);
-    }
-    _cs_pin.write(1);
-}
-
-void LDC1000::writeSPI(uint8_t *data, uint8_t address, uint8_t num_bytes)
-{
-    _cs_pin.write(0);
-    _spiport.write(address); 
-    for(int i=0; i < num_bytes ; i++)
-    {
-        _spiport.write(data[i]);
-    }
-    _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 LDC1000::get_raw_l()          {_raw_l = readRawCounts(); 
-                                        return _raw_l;};        
-    float LDC1000::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 LDC1000::get_frequency()      {return _frequency;};    
-    float LDC1000::get_responsetime()   {return _responsetime;};    
-    float LDC1000::get_cap()            {return cap;};
-// END ***********************************************************
\ No newline at end of file
--- a/LDC1000.h	Tue Aug 18 17:46:35 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,127 +0,0 @@
-#ifndef _LDC1000_H_
-#define _LDC1000_H_
-
-#include "FastPWM.h"
-/**
-* @file LDC1000.h
-* @brief this header file will contain all required
-* definitions for the functions to interface with Texas
-* Instruments' LDC1000.
-*
-* @author Victor Sluiter
-*
-* @date 2015-04-01
-*/
-
-#include "mbed.h"
-
-#ifndef PI
-#define PI 3.14
-#endif
-
-typedef enum {  LDC_RESPONSE_192=2,\
-                LDC_RESPONSE_384= 3,  \
-                LDC_RESPONSE_768,  \
-                LDC_RESPONSE_1536, \
-                LDC_RESPONSE_3072, \
-                LDC_RESPONSE_6144} LDC_RESPONSE;
-
-typedef enum {  LDC_AMPLITUDE_1V=0,\
-                LDC_AMPLITUDE_2V,  \
-                LDC_AMPLITUDE_4V} LDC_AMPLITUDE;
-
-typedef enum { LDC_MODE_STANDBY = 0, LDC_MODE_ACTIVE = 1} LDC_MODE;
-
-/**
-* Class for the LDC1000.
-* @author Victor Sluiter
-* @date 2015-04-01
-*/
-class LDC1000
-{
-    public:
-    /**
-    * @brief Create a new Class to interface to an LDC1000
-    **/
-    LDC1000(PinName mosi, PinName miso, PinName sck, PinName cs, float capacitor, float f_external, PinName clock_out=NC);
-    /**
-    * @brief Set power mode.
-    * The constructor sets the LDC1000 in Active mode.
-    * @param mode choose from LDC_MODE_ACTIVE or LDC_MODE STANDBY
-    **/
-    void mode(LDC_MODE mode){writeSPI((uint8_t *)(&mode), 0x0B);};
-    /**
-    * @brief get the calculated inductance value
-    **/
-    float getInductance(void);
-
-
-    // EXTRA test get variables values to verify (to calculate the induction)
-    float get_raw_l(void);
-    float get_fsensor(void);
-    float get_frequency(void);
-    float get_responsetime(void);
-    float get_cap(void);
-    
-
-    /**
-    * @brief Set the value of the external capacitor
-    * This is needed for the calculation of the inductance.
-    **/
-    void  setCapacitor(float c){cap = c;};
-    /**
-    * @brief set the value of the external clock
-    * If PWMout is used to generate a clock signal, this will update the output frequency.s
-    **/
-    void  setFrequency(float frequency);
-    /**
-    * @brief Read the raw 24-bit inductance value.
-    * This is needed for the calculation of the inductance.
-    **/
-    uint32_t readRawL(void){_raw_l = readRawCounts(); return _raw_l;};
-
-
-    /**
-    * @brief Set the Response Time parameters. 
-    * @param responsetime 
-    * Larger value increases accuracy, but slows down the output data rate. Choose one of these values:
-    * - LDC_RESPONSE_192
-    * - LDC_RESPONSE_384
-    * - LDC_RESPONSE_768
-    * - LDC_RESPONSE_1536
-    * - LDC_RESPONSE_3072
-    * - LDC_RESPONSE_6144
-    **/
-    void setResponseTime(LDC_RESPONSE responsetime);
-    /**
-    * @brief Set the oscilation amplitude.
-    * Use one of these values:
-    * - LDC_AMPLITUDE_1V
-    * - LDC_AMPLITUDE_2V
-    * - LDC_AMPLITUDE_4V
-    **/
-    void setOutputPower(LDC_AMPLITUDE amplitude);
-    
-    /** set Watchdog timer **/
-    void setWatchdog(float frequency);
-    
-    private:
-    void readSPI(uint8_t *data, uint8_t address, uint8_t num_bytes = 1);
-    void writeSPI(uint8_t *data, uint8_t address, uint8_t num_bytes = 1);
-    void writeSPIregister(uint8_t reg, uint8_t value){writeSPI(&value,reg);};
-    uint32_t readRawCounts(void);
-    uint32_t readINTB(void); // EXTRA UNTB Read register
-    LDC_RESPONSE _responsetime;
-    LDC_AMPLITUDE _amplitude;
-    float _fsensor;
-    float _inductance;
-    float _frequency; //frequency of external clock
-    float cap;    
-    uint32_t _raw_l;
-    uint32_t INTB; // extra: read register INTB
-    SPI _spiport;
-    DigitalOut _cs_pin;
-    FastPWM _clock;
-};
-
-#endif
\ No newline at end of file
--- /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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LDC1101.h	Thu Dec 10 15:12:16 2015 +0000
@@ -0,0 +1,123 @@
+#ifndef _LDC1101_H_
+#define _LDC1101_H_
+
+#include "FastPWM.h"
+/**
+* @file LDC1101.h
+* @brief this header file will contain all required
+* definitions for the functions to interface with Texas
+* Instruments' LDC1101.
+*
+* @author Victor Sluiter
+*
+* @date 2015-12-09
+*/
+
+#include "mbed.h"
+
+#ifndef PI
+#define PI 3.14
+#endif
+
+typedef enum {  LDC_RESPONSE_192 = 2, \
+                LDC_RESPONSE_384 = 3, \
+                LDC_RESPONSE_768 = 4,  \
+                LDC_RESPONSE_1536= 5, \
+                LDC_RESPONSE_3072= 6, \
+                LDC_RESPONSE_6144= 7} LDC_RESPONSE;
+
+typedef enum {  LDC_MODE_ACTIVE = 0, \
+                LDC_MODE_STANDBY = 1, \
+                LDC_MODE_SHUTDOWN = 2} LDC_MODE;
+
+/**
+* Class for the LDC1101.
+* @author Victor Sluiter
+* @date 2015-12-09
+*/
+class LDC1101
+{
+    public:
+        /**
+        * @brief Create a new Class to interface to an LDC1101
+        **/
+        LDC1101(PinName mosi, PinName miso, PinName sck, PinName cs, float capacitor, float f_external, PinName clock_out=NC);
+        
+        /**
+        * @brief Set power mode.
+        * The constructor sets the LDC1101 in Active mode.
+        * @param mode choose from LDC_MODE_ACTIVE, LDC_MODE STANDBY or LDC_MODE_SHUTDOWN
+        **/
+        void mode(LDC_MODE mode) { writeSPI((uint8_t *)(&mode), 0x0B); };
+    
+        /**
+        * @brief initial configurations
+        **/
+        void init(void);
+        
+        /**
+        * @brief get the calculated inductance value
+        **/
+        float getInductance(void);
+    
+    
+        // EXTRA test get variables values to verify (to calculate the induction)
+        float get_raw_l(void);
+        float get_fsensor(void);
+        float get_frequency(void);
+        float get_responsetime(void);
+        float get_cap(void);
+        
+    
+        /**
+        * @brief Set the value of the external capacitor
+        * This is needed for the calculation of the inductance.
+        **/
+        void  setCapacitor(float c){cap = c;};
+        /**
+        * @brief set the value of the external clock
+        * If PWMout is used to generate a clock signal, this will update the output frequency.s
+        **/
+        void  setFrequency(float frequency);
+        /**
+        * @brief Read the raw 24-bit inductance value.
+        * This is needed for the calculation of the inductance.
+        **/
+        uint32_t readRawL(void){_raw_l = readRawCounts(); return _raw_l;};
+    
+    
+        /**
+        * @brief Set the Response Time parameters. 
+        * @param responsetime 
+        * Larger value increases accuracy, but slows down the output data rate. Choose one of these values:
+        * - LDC_RESPONSE_192
+        * - LDC_RESPONSE_384
+        * - LDC_RESPONSE_768
+        * - LDC_RESPONSE_1536
+        * - LDC_RESPONSE_3072
+        * - LDC_RESPONSE_6144
+        **/
+        void setResponseTime(LDC_RESPONSE responsetime);
+        
+    private:
+        void readSPI(uint8_t *data, uint8_t address, uint8_t num_bytes = 1);
+        void writeSPI(uint8_t *data, uint8_t address, uint8_t num_bytes = 1);
+        void writeSPIregister(uint8_t reg, uint8_t value){writeSPI(&value,reg);}; // VERKEERD OM?!
+        
+        uint32_t readRawCounts(void);
+        uint32_t readINTB(void); // EXTRA UNTB Read register
+        LDC_RESPONSE _responsetime;
+        float _fsensor;
+        float _inductance;
+        float _frequency; //frequency of external clock
+        float cap;    
+        uint32_t _raw_l;
+        uint32_t INTB; // extra: read register INTB
+        
+        SPI _spiport;
+        DigitalOut _cs_pin;
+       
+        FastPWM _clock;
+};
+
+#endif
\ No newline at end of file