Library to work with the LDC1000 from Texas Instruments

Dependencies:   FastPWM

Dependents:   LDC1000_test

LDC1000

This library was written to interface to Texas Instruments' LDC1000 in order to perform inductance measurement. This libary needs a SPI peripheral on your mbed device to talk to the LDC1000.

Clock

The LDC1000 needs a high speed clock for its internal frequency counter. In order to provide this clock, the FastPWM library is used. This may change the behaviour of other PWM channels, please be aware of that, and read the FastPWM documentation to understand the implications.

Unsupported

Not supported (yet):

  1. Setting the RpMAX and RpMIN values
  2. Setting the interrupt pin functionality

Files at this revision

API Documentation at this revision

Comitter:
vsluiter
Date:
Sun Apr 05 18:19:12 2015 +0000
Child:
1:a88df80e7664
Commit message:
Initial commit

Changed in this revision

LDC1000.cpp Show annotated file Show diff for this revision Revisions of this file
LDC1000.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LDC1000.cpp	Sun Apr 05 18:19:12 2015 +0000
@@ -0,0 +1,92 @@
+/**
+* @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)
+{
+    cap = capacitor;
+    _spiport.format(8,3);
+    _spiport.frequency(1E6);
+    _cs_pin.write(1);
+    setFrequency(f_external);
+    setResponseTime(LDC_RESPONSE_384);
+    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) & 0xE7;
+    writeSPI(&buffer,0x04);
+
+}
+
+void LDC1000::setResponseTime(LDC_RESPONSE responsetime)
+{
+    uint8_t buffer;
+    _responsetime = responsetime;
+    readSPI(&buffer, 0x04);
+    buffer &= 0xF8; //clear responsetime bits
+    buffer |= responsetime & 0xF8;
+    writeSPI(&buffer,0x04);
+}
+
+void LDC1000::setFrequency(float frequency)
+{
+    _frequency = frequency;
+    _clock.period(1.0/frequency);
+}
+
+float LDC1000::getInductance()
+{
+    uint16_t resp[] = {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)
+{
+    union
+    {
+        uint8_t buf[4];
+        uint32_t value;
+    } val;
+    val.value = 0;
+    readSPI(val.buf,0x23,3);
+    return val.value;
+}
+
+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++)
+    {
+        _spiport.write(data[i]);
+    }
+    _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++)
+    {
+        *data = _spiport.write(0xFF);
+    }
+    _cs_pin.write(1);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LDC1000.h	Sun Apr 05 18:19:12 2015 +0000
@@ -0,0 +1,109 @@
+#ifndef _LDC1000_H_
+#define _LDC1000_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,  \
+                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(&mode, 0x0B);};
+    /**
+    * @brief get the calculated inductance value
+    **/
+    float getInductance(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
+    **/
+    enable power
+    void setOutputPower(LDC_AMPLITUDE amplitude);
+    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);
+    uint32_t readRawCounts(void);
+    LDC_RESPONSE _responsetime;
+    LDC_AMPLITUDE _amplitude;
+    float _fsensor;
+    float _inductance;
+    float _frequency; //frequency of external clock
+    float cap;    
+    uint32_t _raw_l;
+    SPI _spiport;
+    DigitalOut _cs_pin;
+    PwmOut _clock;
+};
+
+#endif
\ No newline at end of file