Library to communicate with LDC1614

Dependencies:   SHTx

Dependents:   Inductive_Sensor_3

Fork of LDC1101 by Bob Giesberts

Committer:
bobgiesberts
Date:
Wed Sep 07 09:58:32 2016 +0000
Revision:
31:ab4354a71996
Parent:
30:95c53d244f91
Child:
32:9712c9bdaf44
No big changes

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