Library to communicate with LDC1614

Dependencies:   SHTx

Dependents:   Inductive_Sensor_3

Fork of LDC1101 by Bob Giesberts

Committer:
bobgiesberts
Date:
Sat Sep 10 12:46:36 2016 +0000
Revision:
32:9712c9bdaf44
Parent:
31:ab4354a71996
Child:
34:b03d7bb9010c
Read / Write functions established, good communication now! Working version to get data from the LDC1614

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bobgiesberts 28:76a2fc42f888 1 #ifndef _LDC1614_H_
bobgiesberts 28:76a2fc42f888 2 #define _LDC1614_H_
bobgiesberts 16:07d0e43c2d12 3
bobgiesberts 16:07d0e43c2d12 4 #include "mbed.h"
bobgiesberts 31:ab4354a71996 5 #include "register_values.h"
bobgiesberts 32:9712c9bdaf44 6 #include "i2c.hpp"
bobgiesberts 16:07d0e43c2d12 7
bobgiesberts 16:07d0e43c2d12 8 #ifndef PI
bobgiesberts 16:07d0e43c2d12 9 #define PI 3.14
bobgiesberts 16:07d0e43c2d12 10 #endif
bobgiesberts 16:07d0e43c2d12 11
bobgiesberts 29:41815fd13822 12
bobgiesberts 29:41815fd13822 13 /*
bobgiesberts 28:76a2fc42f888 14 int min(int a, int b) { return (a<b) ? a : b; }
bobgiesberts 28:76a2fc42f888 15 float min(float a, float b) { return (a<b) ? a : b; }
bobgiesberts 28:76a2fc42f888 16 int max(int a, int b) { return (a>b) ? a : b; }
bobgiesberts 28:76a2fc42f888 17 float max(float a, float b) { return (a>b) ? a : b; }
bobgiesberts 29:41815fd13822 18 */
bobgiesberts 28:76a2fc42f888 19
bobgiesberts 16:07d0e43c2d12 20
bobgiesberts 30:95c53d244f91 21 /** Class for the LDC1614.
bobgiesberts 30:95c53d244f91 22 * @author Bob Giesberts
bobgiesberts 30:95c53d244f91 23 * @date 2016-08-09
bobgiesberts 30:95c53d244f91 24 * @file LDC1614.h
bobgiesberts 30:95c53d244f91 25 * @brief this header file will contain all required
bobgiesberts 30:95c53d244f91 26 * definitions for the functions to interface with Texas
bobgiesberts 30:95c53d244f91 27 * Instruments' LDC1614.
bobgiesberts 30:95c53d244f91 28 */
bobgiesberts 30:95c53d244f91 29 class LDC1614 {
bobgiesberts 16:07d0e43c2d12 30 public:
bobgiesberts 30:95c53d244f91 31 /** Create a new LDC1614 class
bobgiesberts 30:95c53d244f91 32 * @brief Create a new Class to interface to an LDC1614
bobgiesberts 30:95c53d244f91 33 * @param sda I2C SDA pin connected to LDC1614
bobgiesberts 30:95c53d244f91 34 * @param scl I2C SCL pin connected to LDC1614
bobgiesberts 30:95c53d244f91 35 * @param sd Shutdown pin connected to LDC1614
bobgiesberts 30:95c53d244f91 36 * @param f_CLKIN f_CLKIN (Hz)
bobgiesberts 30:95c53d244f91 37 * @param sensors number of sensors
bobgiesberts 30:95c53d244f91 38 * @param capacitor Used capacitor on all connected sensors
bobgiesberts 16:07d0e43c2d12 39 **/
bobgiesberts 31:ab4354a71996 40 LDC1614(PinName sda, PinName scl, PinName sd, float f_CLKIN, int sensors, float capacitor);
bobgiesberts 28:76a2fc42f888 41 ~LDC1614();
bobgiesberts 16:07d0e43c2d12 42
bobgiesberts 16:07d0e43c2d12 43 /**
bobgiesberts 30:95c53d244f91 44 * @brief Set power mode.
bobgiesberts 30:95c53d244f91 45 * The constructor sets the LDC1614 in Active mode.
bobgiesberts 30:95c53d244f91 46 * @param mode choose from:
bobgiesberts 30:95c53d244f91 47 * - LDC_MODE_ACTIVE
bobgiesberts 30:95c53d244f91 48 * - LDC_MODE_SLEEP
bobgiesberts 30:95c53d244f91 49 * - LDC_MODE_SHUTDOWN
bobgiesberts 16:07d0e43c2d12 50 **/
bobgiesberts 20:8e1b1efdbb49 51 void func_mode(LDC_MODE mode);
bobgiesberts 30:95c53d244f91 52
bobgiesberts 30:95c53d244f91 53 /// @brief Put the LDC in configuration modus
bobgiesberts 20:8e1b1efdbb49 54 void sleep(void);
bobgiesberts 30:95c53d244f91 55 /// @brief Get LDC1614 to work for you again
bobgiesberts 20:8e1b1efdbb49 56 void wakeup(void);
bobgiesberts 30:95c53d244f91 57 /// @brief Put the LDC in its lowest power modus
bobgiesberts 29:41815fd13822 58 void shutdown(void);
bobgiesberts 30:95c53d244f91 59 /// @brief initial configurations
bobgiesberts 16:07d0e43c2d12 60 void init(void);
bobgiesberts 19:e205ab9142d8 61
bobgiesberts 16:07d0e43c2d12 62
bobgiesberts 19:e205ab9142d8 63 /**
bobgiesberts 30:95c53d244f91 64 * @brief Set the Reference Count parameter.
bobgiesberts 30:95c53d244f91 65 * @param channel the channel (CH0, CH1, CH2, CH3)
bobgiesberts 30:95c53d244f91 66 * @param RCOUNT For LHR mode, the conversion time is set by the reference count CHx_RCOUNT
bobgiesberts 30:95c53d244f91 67 * The conversion time represents the number of clock cycles used to measure the sensor frequency.
bobgiesberts 30:95c53d244f91 68 * Higher values for CHx_RCOUNT have a higher effective measurement resolution but a lower sample rate.
bobgiesberts 30:95c53d244f91 69 * The maximum setting (0xffff) is required for full resolution
bobgiesberts 30:95c53d244f91 70 * t_conv = (CHx_RCOUNT x 16) / f_REFx
bobgiesberts 30:95c53d244f91 71 * CHx_RCount = (f_CLKIN/sample rate - 55)/16
bobgiesberts 28:76a2fc42f888 72 **/
bobgiesberts 28:76a2fc42f888 73 void setReferenceCount( uint8_t channel, uint16_t rcount );
bobgiesberts 28:76a2fc42f888 74
bobgiesberts 28:76a2fc42f888 75 /**
bobgiesberts 30:95c53d244f91 76 * @brief Sensor offset (p.13)
bobgiesberts 30:95c53d244f91 77 * The sensor might reach a value > 2^28. To prevent this, set an offset.
bobgiesberts 30:95c53d244f91 78 * @param channel the channel (CH0, CH1, CH2, CH3)
bobgiesberts 30:95c53d244f91 79 * @param offset 16 bit value that should be substracted from the current sensor value
bobgiesberts 30:95c53d244f91 80 * f_offsetx = (CHx_OFFSET / 2^16)*f_REFx
bobgiesberts 28:76a2fc42f888 81 **/
bobgiesberts 32:9712c9bdaf44 82 void setOffset( uint8_t channel, uint16_t offset );
bobgiesberts 28:76a2fc42f888 83
bobgiesberts 28:76a2fc42f888 84 /**
bobgiesberts 30:95c53d244f91 85 * @brief sensor settling time
bobgiesberts 30:95c53d244f91 86 * CHx_SETTLECOUNT >= Q * f_REFx / (16 * f_SENSORx)
bobgiesberts 30:95c53d244f91 87 * t_settle = (CHx_SETTLECOUNT x 16) / f_REFx
bobgiesberts 30:95c53d244f91 88 * @param channel the channel (CH0, CH1, CH2, CH3)
bobgiesberts 30:95c53d244f91 89 * @param settlecount CHx_SETTLECOUNT (0 - 65 535)
bobgiesberts 28:76a2fc42f888 90 **/
bobgiesberts 28:76a2fc42f888 91 void setSettlecount( uint8_t channel, uint16_t settlecount);
bobgiesberts 28:76a2fc42f888 92
bobgiesberts 28:76a2fc42f888 93 /**
bobgiesberts 30:95c53d244f91 94 * @brief clock divider
bobgiesberts 30:95c53d244f91 95 * Sensor divider (p.14)
bobgiesberts 30:95c53d244f91 96 * f_REF = f_CLKIN / REF_DIVIDER
bobgiesberts 30:95c53d244f91 97 * f_IN = f_SENSOR / IN_DIVIDER
bobgiesberts 30:95c53d244f91 98 * Ideally f_REF > 4*f_IN
bobgiesberts 30:95c53d244f91 99 * @param channel the channel (CH0, CH1, CH2, CH3)
bobgiesberts 30:95c53d244f91 100 * @param divIN CHx_FIN_DIVIDER (4bit: 1 - 15)
bobgiesberts 30:95c53d244f91 101 * @param divREF CHx_FREF_DIVIDER (8 bit: 1 - 255)
bobgiesberts 20:8e1b1efdbb49 102 **/
bobgiesberts 28:76a2fc42f888 103 void setDivider( uint8_t channel, uint8_t divIN, uint8_t divREF );
bobgiesberts 28:76a2fc42f888 104
bobgiesberts 28:76a2fc42f888 105
bobgiesberts 28:76a2fc42f888 106 /**
bobgiesberts 30:95c53d244f91 107 * @brief Configuration of the LDC1614
bobgiesberts 30:95c53d244f91 108 * This function can be used to set all settings (p.31)
bobgiesberts 30:95c53d244f91 109 * @param addr address of the setting catgory
bobgiesberts 30:95c53d244f91 110 * - ERROR_CONFIG
bobgiesberts 30:95c53d244f91 111 * - CONFIG
bobgiesberts 30:95c53d244f91 112 * - MUX_CONFIG
bobgiesberts 30:95c53d244f91 113 * @param setting for ERROR_CONFIG
bobgiesberts 30:95c53d244f91 114 * - UR_ERR2OUT
bobgiesberts 30:95c53d244f91 115 * - OR_ERR2OUT
bobgiesberts 30:95c53d244f91 116 * - WD_ERR2OUT
bobgiesberts 30:95c53d244f91 117 * - AH_ERR2OUT
bobgiesberts 30:95c53d244f91 118 * - AL_ERR2OUT
bobgiesberts 30:95c53d244f91 119 * - UR_ERR2INT
bobgiesberts 30:95c53d244f91 120 * - OR_ERR2INT
bobgiesberts 30:95c53d244f91 121 * - WD_ERR2INT
bobgiesberts 30:95c53d244f91 122 * - AH_ERR2INT
bobgiesberts 30:95c53d244f91 123 * - AL_ERR2INT
bobgiesberts 30:95c53d244f91 124 * - ZC_ERR2INT
bobgiesberts 30:95c53d244f91 125 * - DRDY_2INT
bobgiesberts 30:95c53d244f91 126 * for CONFIG (0x1A)
bobgiesberts 30:95c53d244f91 127 * - ACTIVE_CHAN : 0 (CH0) | 1 (CH1) | 10, 11 (CHx)
bobgiesberts 30:95c53d244f91 128 * - SLEEP_MODE_EN : 0 (ACTIVE) | 1 (SLEEP)
bobgiesberts 30:95c53d244f91 129 * - RP_OVERRIDE_EN : 0 (OFF) | 1 (Rp override on)
bobgiesberts 30:95c53d244f91 130 * - SENSOR_ACTIVATE_SEL : 0 (full current) | 1 (low power)
bobgiesberts 30:95c53d244f91 131 * - AUTO_AMP_DIS : 0 (enabled) | 1 (disabled)
bobgiesberts 30:95c53d244f91 132 * - REF_CLK_SRC : 0 (internal) | 1 (external)
bobgiesberts 30:95c53d244f91 133 * - INTB_DIS : 0 (enabled) | 1 (disabled)
bobgiesberts 30:95c53d244f91 134 * - HIGH_CURRENT_DRV : 0 (1.5 mA) | 1 (> 1.5 mA)
bobgiesberts 30:95c53d244f91 135 * for MUX_CONFIG (0x1B)
bobgiesberts 30:95c53d244f91 136 * - AUTOSCAN_EN : 0 (1 channel) | 1 (multichannel)
bobgiesberts 30:95c53d244f91 137 * - RR_SEQUENCE : 00 (CH0, CH1) | 01 (CH0, CH1, CH2) | 10 (CH0, CH1, CH2, CH3)
bobgiesberts 30:95c53d244f91 138 * - DEGLITCH : 001 (1.0 MHz) | 100 (3.3 MHz) | 101 (10 MHz) | 111 (33 MHz)
bobgiesberts 30:95c53d244f91 139 * @param value 0 | 1 | ...
bobgiesberts 30:95c53d244f91 140 * for MUX_CONFIG.DEGLITCH
bobgiesberts 30:95c53d244f91 141 * f_deglitch > f_sensor_max (p. 16)
bobgiesberts 30:95c53d244f91 142 * DEGLITCH_1M - 1.0 MHz
bobgiesberts 30:95c53d244f91 143 * DEGLITCH_3M - 3.3 MHz
bobgiesberts 30:95c53d244f91 144 * DEGLITCH_10M - 10.0 MHz
bobgiesberts 30:95c53d244f91 145 * DEGLITCH_33M - 33.0 MHz
bobgiesberts 28:76a2fc42f888 146 **/
bobgiesberts 28:76a2fc42f888 147 void set( ADDR addr, SETTING setting, uint8_t value );
bobgiesberts 28:76a2fc42f888 148
bobgiesberts 28:76a2fc42f888 149
bobgiesberts 29:41815fd13822 150 uint8_t get( ADDR addr, SETTING setting, uint8_t mask = 1 );
bobgiesberts 28:76a2fc42f888 151
bobgiesberts 32:9712c9bdaf44 152 uint16_t get_config();
bobgiesberts 32:9712c9bdaf44 153 uint16_t get_error_config();
bobgiesberts 28:76a2fc42f888 154
bobgiesberts 28:76a2fc42f888 155
bobgiesberts 28:76a2fc42f888 156 /** !!!!!!
bobgiesberts 30:95c53d244f91 157 * @brief Set the drive current
bobgiesberts 30:95c53d244f91 158 * Exact functioning still unknown (p.14)
bobgiesberts 30:95c53d244f91 159 * @param idrive 5-bit value
bobgiesberts 30:95c53d244f91 160 * b00000 ( 0) - 16 uA
bobgiesberts 30:95c53d244f91 161 * b11111 (31) - 1563 uA
bobgiesberts 28:76a2fc42f888 162 **/
bobgiesberts 28:76a2fc42f888 163 void setDriveCurrent( uint8_t channel, uint8_t idrive );
bobgiesberts 28:76a2fc42f888 164
bobgiesberts 28:76a2fc42f888 165
bobgiesberts 19:e205ab9142d8 166 /**
bobgiesberts 19:e205ab9142d8 167 * @brief Set the value of the external capacitor
bobgiesberts 19:e205ab9142d8 168 * This is needed for the calculation of the inductance.
bobgiesberts 19:e205ab9142d8 169 **/
bobgiesberts 19:e205ab9142d8 170 void setCapacitor(float c){_cap = c;};
bobgiesberts 19:e205ab9142d8 171 /**
bobgiesberts 19:e205ab9142d8 172 * @brief set the value of the external clock
bobgiesberts 19:e205ab9142d8 173 **/
bobgiesberts 19:e205ab9142d8 174 void setFrequency(float frequency){_fCLKIN = frequency;};
bobgiesberts 19:e205ab9142d8 175
bobgiesberts 19:e205ab9142d8 176
bobgiesberts 20:8e1b1efdbb49 177
bobgiesberts 20:8e1b1efdbb49 178
bobgiesberts 20:8e1b1efdbb49 179
bobgiesberts 19:e205ab9142d8 180 /**
bobgiesberts 30:95c53d244f91 181 * @brief Read Data, the raw 28-bit inductance value.
bobgiesberts 30:95c53d244f91 182 * This is needed for the calculation of the inductance.
bobgiesberts 30:95c53d244f91 183 * @param channel the channel (CH0, CH1, CH2, CH3)
bobgiesberts 19:e205ab9142d8 184 **/
bobgiesberts 28:76a2fc42f888 185 uint32_t get_Data( uint8_t channel );
bobgiesberts 19:e205ab9142d8 186 /**
bobgiesberts 30:95c53d244f91 187 * @brief get the calculated value for f_sensor (Hz)
bobgiesberts 30:95c53d244f91 188 * @param Ldata the obtained Data, if omitted, get_Data(0) will be used
bobgiesberts 19:e205ab9142d8 189 **/
bobgiesberts 28:76a2fc42f888 190 float get_fsensor( uint32_t Ldata = 0 );
bobgiesberts 19:e205ab9142d8 191 /**
bobgiesberts 30:95c53d244f91 192 * @brief get the calculated inductance value
bobgiesberts 30:95c53d244f91 193 * @param Ldata the obtained Data, if omitted, get_Data(0) will be used
bobgiesberts 19:e205ab9142d8 194 **/
bobgiesberts 28:76a2fc42f888 195 float get_Inductance( uint32_t Ldata = 0 );
bobgiesberts 28:76a2fc42f888 196
bobgiesberts 28:76a2fc42f888 197
bobgiesberts 28:76a2fc42f888 198
bobgiesberts 30:95c53d244f91 199 /// @brief get the reference frequency (f_CLKIN)
bobgiesberts 19:e205ab9142d8 200 float get_fCLKIN(void);
bobgiesberts 30:95c53d244f91 201 /// @brief get the reference count
bobgiesberts 28:76a2fc42f888 202 uint16_t get_Rcount( uint8_t channel );
bobgiesberts 30:95c53d244f91 203 /// @brief get the divider
bobgiesberts 28:76a2fc42f888 204 uint8_t get_dividerIN(void);
bobgiesberts 30:95c53d244f91 205 /// @brief get the divider
bobgiesberts 28:76a2fc42f888 206 uint8_t get_dividerREF(void);
bobgiesberts 30:95c53d244f91 207 /// @brief get _Offset
bobgiesberts 32:9712c9bdaf44 208 uint16_t get_Offset(void);
bobgiesberts 30:95c53d244f91 209 /// @brief get the capacitance
bobgiesberts 19:e205ab9142d8 210 float get_cap(void);
bobgiesberts 28:76a2fc42f888 211
bobgiesberts 30:95c53d244f91 212 /// @brief get the status of the sensors (p.28)
bobgiesberts 28:76a2fc42f888 213 uint16_t get_status(void);
bobgiesberts 19:e205ab9142d8 214
bobgiesberts 30:95c53d244f91 215 /**
bobgiesberts 30:95c53d244f91 216 * @brief is data ready?
bobgiesberts 30:95c53d244f91 217 * returns true when data is ready for all sensors
bobgiesberts 30:95c53d244f91 218 * returns false if not
bobgiesberts 30:95c53d244f91 219 * @param status the obtained value from get_status()
bobgiesberts 30:95c53d244f91 220 * if omitted, get_status() will be used to get a value
bobgiesberts 28:76a2fc42f888 221 **/
bobgiesberts 32:9712c9bdaf44 222 bool is_ready( uint8_t channel );
bobgiesberts 29:41815fd13822 223
bobgiesberts 29:41815fd13822 224 /**
bobgiesberts 30:95c53d244f91 225 * @brief is there an error?
bobgiesberts 30:95c53d244f91 226 * returns true when there is an error
bobgiesberts 30:95c53d244f91 227 * returns false if not
bobgiesberts 30:95c53d244f91 228 * @param status the obtained value from get_status()
bobgiesberts 30:95c53d244f91 229 * if omitted, get_status() will be used to get a value
bobgiesberts 29:41815fd13822 230 **/
bobgiesberts 29:41815fd13822 231 bool is_error( uint8_t status = 17 );
bobgiesberts 29:41815fd13822 232
bobgiesberts 32:9712c9bdaf44 233 uint8_t what_error( uint8_t channel );
bobgiesberts 19:e205ab9142d8 234
bobgiesberts 32:9712c9bdaf44 235 uint16_t get_ID( void );
bobgiesberts 32:9712c9bdaf44 236 uint16_t get_manufacturer_ID( void );
bobgiesberts 19:e205ab9142d8 237
bobgiesberts 16:07d0e43c2d12 238 private:
bobgiesberts 28:76a2fc42f888 239 void readI2C( uint16_t *data, uint8_t address, uint8_t length = 1);
bobgiesberts 28:76a2fc42f888 240 void writeI2C( uint16_t *data, uint8_t address, uint8_t length = 1);
bobgiesberts 28:76a2fc42f888 241 void writeI2Cregister( uint8_t reg, uint16_t value);
bobgiesberts 28:76a2fc42f888 242 void regchange( uint8_t addr, uint8_t setting, uint8_t value, uint8_t mask = 1 );
bobgiesberts 28:76a2fc42f888 243
bobgiesberts 22:8da965ce5af3 244 void suicide(void *obj) {delete obj;};
bobgiesberts 16:07d0e43c2d12 245
bobgiesberts 28:76a2fc42f888 246 uint8_t _channels; // number of sensors
bobgiesberts 28:76a2fc42f888 247 uint8_t _dividerIN; // CHx_FIN_DIVIDER
bobgiesberts 28:76a2fc42f888 248 uint8_t _dividerREF; // CHx_FREF_DIVIDER
bobgiesberts 32:9712c9bdaf44 249 uint16_t _Offset; // LHR_OFFSET
bobgiesberts 28:76a2fc42f888 250
bobgiesberts 28:76a2fc42f888 251 uint16_t _Rcount; // CHx_RCOUNT
bobgiesberts 28:76a2fc42f888 252 uint16_t _SettleCount; // CHx_SETTLECOUNT
bobgiesberts 28:76a2fc42f888 253 uint8_t _DriveCurrent; // CHx_IDRIVE
bobgiesberts 16:07d0e43c2d12 254
bobgiesberts 30:95c53d244f91 255 float _fsensor; // f_sensor (Hz): the calculated frequency of the sensor
bobgiesberts 30:95c53d244f91 256 float _fCLKIN; // f_CLKIN (Hz): frequency of external clock: 16MHz
bobgiesberts 28:76a2fc42f888 257
bobgiesberts 28:76a2fc42f888 258 float _inductance; // the calculated inductance
bobgiesberts 30:95c53d244f91 259 float _cap; // capacitor (F): 120 pF
bobgiesberts 19:e205ab9142d8 260
bobgiesberts 28:76a2fc42f888 261
bobgiesberts 32:9712c9bdaf44 262 SHTx::I2C _i2c;
bobgiesberts 28:76a2fc42f888 263 DigitalOut _shutdown_pin;
bobgiesberts 16:07d0e43c2d12 264 };
bobgiesberts 16:07d0e43c2d12 265
bobgiesberts 16:07d0e43c2d12 266 #endif