Library to communicate with LDC1614

Dependencies:   SHTx

Dependents:   Inductive_Sensor_3

Fork of LDC1101 by Bob Giesberts

LDC1101.h

Committer:
bobgiesberts
Date:
2015-12-16
Revision:
18:fc9bb81a631f
Parent:
17:a5cf2b4bec13
Child:
19:e205ab9142d8

File content as of revision 18:fc9bb81a631f:

#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;

typedef enum {  DIVIDER_1 = 0, \
                DIVIDER_2 = 1, \
                DIVIDER_4 = 2, \
                DIVIDER_8 = 3} DIVIDER;


/**
* 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_CLKIN, 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 Sensor divider (p.26)
        * @param div 
        * Divides the sensor by a certain amount
        * - DIVIDER_1
        * - DIVIDER_2
        * - DIVIDER_4
        * - DIVIDER_8
        **/
        void setDivider(DIVIDER div);
        
        
        /**
        * @brief get the calculated value for f_sensor
        **/        
        float get_fsensor(void);
        
        
        /**
        * @brief get the calculated inductance value
        **/
        float get_Inductance(void);
    
    
        // EXTRA test get variables values to verify (to calculate the induction)
        float get_fCLKIN(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 LHR_Data, the raw 24-bit inductance value.
        * This is needed for the calculation of the inductance.
        * It reads from addresses 0x38, 0x39 & 0x3A.
        **/
        uint32_t get_LHR_Data(void);
    
    
        /**
        * @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 readINTB(void); // EXTRA UNTB Read register
        LDC_RESPONSE _responsetime_;
        DIVIDER _divider_;
        float _responsetime;
        float _divider;
        float _fsensor;
        float _inductance;
        float _fCLKIN; //frequency of external clock: 16MHz
        float cap;    
        uint32_t _L_data;
        uint32_t INTB; // extra: read register INTB
        
        SPI _spiport;
        DigitalOut _cs_pin;
       
        //FastPWM _clock;
};

#endif