Library to communicate with LDC1614

Dependencies:   SHTx

Dependents:   Inductive_Sensor_3

Fork of LDC1101 by Bob Giesberts

Committer:
bobgiesberts
Date:
Wed Oct 05 08:19:35 2016 +0000
Revision:
36:6a4e28dadae6
Parent:
35:00c9c01f0c0f
new function to automatic determine DriveCurrent

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