Library to communicate with LDC1101
Dependents: Inductive_Sensor Inductive_Sensor_Jasper Inductive_Sensor_3
Fork of LDC1000 by
LDC1101.h@19:e205ab9142d8, 2015-12-16 (annotated)
- Committer:
- bobgiesberts
- Date:
- Wed Dec 16 16:25:33 2015 +0000
- Revision:
- 19:e205ab9142d8
- Parent:
- 18:fc9bb81a631f
- Child:
- 20:8e1b1efdbb49
Tweaking the internal settings (RP_Min, Responsetime, divider, etc.) to find the optimal configuration (= highest resolution, highest accuracy, linearity, etc.).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bobgiesberts | 16:07d0e43c2d12 | 1 | #ifndef _LDC1101_H_ |
bobgiesberts | 16:07d0e43c2d12 | 2 | #define _LDC1101_H_ |
bobgiesberts | 16:07d0e43c2d12 | 3 | |
bobgiesberts | 16:07d0e43c2d12 | 4 | #include "FastPWM.h" |
bobgiesberts | 16:07d0e43c2d12 | 5 | /** |
bobgiesberts | 16:07d0e43c2d12 | 6 | * @file LDC1101.h |
bobgiesberts | 16:07d0e43c2d12 | 7 | * @brief this header file will contain all required |
bobgiesberts | 16:07d0e43c2d12 | 8 | * definitions for the functions to interface with Texas |
bobgiesberts | 16:07d0e43c2d12 | 9 | * Instruments' LDC1101. |
bobgiesberts | 16:07d0e43c2d12 | 10 | * |
bobgiesberts | 16:07d0e43c2d12 | 11 | * @author Victor Sluiter |
bobgiesberts | 16:07d0e43c2d12 | 12 | * |
bobgiesberts | 16:07d0e43c2d12 | 13 | * @date 2015-12-09 |
bobgiesberts | 16:07d0e43c2d12 | 14 | */ |
bobgiesberts | 16:07d0e43c2d12 | 15 | |
bobgiesberts | 16:07d0e43c2d12 | 16 | #include "mbed.h" |
bobgiesberts | 16:07d0e43c2d12 | 17 | |
bobgiesberts | 16:07d0e43c2d12 | 18 | #ifndef PI |
bobgiesberts | 16:07d0e43c2d12 | 19 | #define PI 3.14 |
bobgiesberts | 16:07d0e43c2d12 | 20 | #endif |
bobgiesberts | 16:07d0e43c2d12 | 21 | |
bobgiesberts | 16:07d0e43c2d12 | 22 | typedef enum { LDC_RESPONSE_192 = 2, \ |
bobgiesberts | 16:07d0e43c2d12 | 23 | LDC_RESPONSE_384 = 3, \ |
bobgiesberts | 19:e205ab9142d8 | 24 | LDC_RESPONSE_768 = 4, \ |
bobgiesberts | 16:07d0e43c2d12 | 25 | LDC_RESPONSE_1536= 5, \ |
bobgiesberts | 16:07d0e43c2d12 | 26 | LDC_RESPONSE_3072= 6, \ |
bobgiesberts | 16:07d0e43c2d12 | 27 | LDC_RESPONSE_6144= 7} LDC_RESPONSE; |
bobgiesberts | 16:07d0e43c2d12 | 28 | |
bobgiesberts | 19:e205ab9142d8 | 29 | typedef enum { LDC_MODE_ACTIVE = 0, \ |
bobgiesberts | 19:e205ab9142d8 | 30 | LDC_MODE_STANDBY = 1, \ |
bobgiesberts | 16:07d0e43c2d12 | 31 | LDC_MODE_SHUTDOWN = 2} LDC_MODE; |
bobgiesberts | 16:07d0e43c2d12 | 32 | |
bobgiesberts | 17:a5cf2b4bec13 | 33 | typedef enum { DIVIDER_1 = 0, \ |
bobgiesberts | 17:a5cf2b4bec13 | 34 | DIVIDER_2 = 1, \ |
bobgiesberts | 17:a5cf2b4bec13 | 35 | DIVIDER_4 = 2, \ |
bobgiesberts | 17:a5cf2b4bec13 | 36 | DIVIDER_8 = 3} DIVIDER; |
bobgiesberts | 17:a5cf2b4bec13 | 37 | |
bobgiesberts | 19:e205ab9142d8 | 38 | typedef enum { RPMIN_96 = 0, \ |
bobgiesberts | 19:e205ab9142d8 | 39 | RPMIN_48 = 1, \ |
bobgiesberts | 19:e205ab9142d8 | 40 | RPMIN_24 = 2, \ |
bobgiesberts | 19:e205ab9142d8 | 41 | RPMIN_12 = 3, \ |
bobgiesberts | 19:e205ab9142d8 | 42 | RPMIN_6 = 4, \ |
bobgiesberts | 19:e205ab9142d8 | 43 | RPMIN_3 = 5, \ |
bobgiesberts | 19:e205ab9142d8 | 44 | RPMIN_1 = 6, \ |
bobgiesberts | 19:e205ab9142d8 | 45 | RPMIN_0 = 7} RPMIN; |
bobgiesberts | 17:a5cf2b4bec13 | 46 | |
bobgiesberts | 16:07d0e43c2d12 | 47 | /** |
bobgiesberts | 16:07d0e43c2d12 | 48 | * Class for the LDC1101. |
bobgiesberts | 16:07d0e43c2d12 | 49 | * @author Victor Sluiter |
bobgiesberts | 16:07d0e43c2d12 | 50 | * @date 2015-12-09 |
bobgiesberts | 16:07d0e43c2d12 | 51 | */ |
bobgiesberts | 16:07d0e43c2d12 | 52 | class LDC1101 |
bobgiesberts | 16:07d0e43c2d12 | 53 | { |
bobgiesberts | 16:07d0e43c2d12 | 54 | public: |
bobgiesberts | 16:07d0e43c2d12 | 55 | /** |
bobgiesberts | 16:07d0e43c2d12 | 56 | * @brief Create a new Class to interface to an LDC1101 |
bobgiesberts | 16:07d0e43c2d12 | 57 | **/ |
bobgiesberts | 18:fc9bb81a631f | 58 | LDC1101(PinName mosi, PinName miso, PinName sck, PinName cs, float capacitor, float f_CLKIN, PinName clock_out=NC); |
bobgiesberts | 16:07d0e43c2d12 | 59 | |
bobgiesberts | 16:07d0e43c2d12 | 60 | /** |
bobgiesberts | 16:07d0e43c2d12 | 61 | * @brief Set power mode. |
bobgiesberts | 16:07d0e43c2d12 | 62 | * The constructor sets the LDC1101 in Active mode. |
bobgiesberts | 16:07d0e43c2d12 | 63 | * @param mode choose from LDC_MODE_ACTIVE, LDC_MODE STANDBY or LDC_MODE_SHUTDOWN |
bobgiesberts | 16:07d0e43c2d12 | 64 | **/ |
bobgiesberts | 19:e205ab9142d8 | 65 | void func_mode(LDC_MODE mode) { writeSPI((uint8_t *)(&mode), 0x0B); }; |
bobgiesberts | 16:07d0e43c2d12 | 66 | |
bobgiesberts | 16:07d0e43c2d12 | 67 | /** |
bobgiesberts | 16:07d0e43c2d12 | 68 | * @brief initial configurations |
bobgiesberts | 16:07d0e43c2d12 | 69 | **/ |
bobgiesberts | 16:07d0e43c2d12 | 70 | void init(void); |
bobgiesberts | 16:07d0e43c2d12 | 71 | |
bobgiesberts | 16:07d0e43c2d12 | 72 | /** |
bobgiesberts | 19:e205ab9142d8 | 73 | * @brief initialises LHR mode |
bobgiesberts | 19:e205ab9142d8 | 74 | * Also enables shutdown modus |
bobgiesberts | 19:e205ab9142d8 | 75 | **/ |
bobgiesberts | 19:e205ab9142d8 | 76 | void setLHRmode(void) |
bobgiesberts | 19:e205ab9142d8 | 77 | { |
bobgiesberts | 19:e205ab9142d8 | 78 | writeSPIregister(0x05, 0x03); // ALT_CONFIG: 0000 0011 --> LHR modus + Shutdown enabled |
bobgiesberts | 19:e205ab9142d8 | 79 | writeSPIregister(0x0C, 0x01); // D_CONFIG: Enables a function that can improve L measurements while disabling RP measurements |
bobgiesberts | 19:e205ab9142d8 | 80 | }; |
bobgiesberts | 19:e205ab9142d8 | 81 | |
bobgiesberts | 19:e205ab9142d8 | 82 | /** |
bobgiesberts | 19:e205ab9142d8 | 83 | * @brief Settings for RP |
bobgiesberts | 19:e205ab9142d8 | 84 | * @param RPMAX_DIS [7] |
bobgiesberts | 19:e205ab9142d8 | 85 | * 0 - not disabled: RP_MAX is driven |
bobgiesberts | 19:e205ab9142d8 | 86 | * 1 - disabled: RP_MAX is ignored, current drive is off. |
bobgiesberts | 19:e205ab9142d8 | 87 | * @param RPMIN [2:0] |
bobgiesberts | 19:e205ab9142d8 | 88 | * pi * V_amp |
bobgiesberts | 19:e205ab9142d8 | 89 | * R_p = ----------- |
bobgiesberts | 19:e205ab9142d8 | 90 | * 4 * I_drive |
bobgiesberts | 19:e205ab9142d8 | 91 | * This setting can be calibrated with the target closest to the sensor: R_p(d = 0mm) |
bobgiesberts | 19:e205ab9142d8 | 92 | * RPMIN < 0.8 x R_p(d = 0mm) |
bobgiesberts | 19:e205ab9142d8 | 93 | * If R_p < 750 Ohm --> increase distance to target |
bobgiesberts | 19:e205ab9142d8 | 94 | * - 000: RPMIN = 96 kOhm | I_drive = 4.7 uA |
bobgiesberts | 19:e205ab9142d8 | 95 | * - 001: RPMIN = 48 kOhm | I_drive = 9.4 uA |
bobgiesberts | 19:e205ab9142d8 | 96 | * - 010: RPMIN = 24 kOhm | I_drive = 18.7 uA |
bobgiesberts | 19:e205ab9142d8 | 97 | * - 011: RPMIN = 12 kOhm | I_drive = 37.5 uA |
bobgiesberts | 19:e205ab9142d8 | 98 | * - 100: RPMIN = 6 kOhm | I_drive = 75 uA |
bobgiesberts | 19:e205ab9142d8 | 99 | * - 101: RPMIN = 3 kOhm | I_drive = 150 uA |
bobgiesberts | 19:e205ab9142d8 | 100 | * - 110: RPMIN = 1.5 kOhm | I_drive = 300 uA |
bobgiesberts | 19:e205ab9142d8 | 101 | * - 111: RPMIN = 0.75 kOhm | I_drive = 600 uA (default) |
bobgiesberts | 19:e205ab9142d8 | 102 | **/ |
bobgiesberts | 19:e205ab9142d8 | 103 | void setRPsettings(bool RPMAX_DIS, RPMIN rpmin); |
bobgiesberts | 19:e205ab9142d8 | 104 | |
bobgiesberts | 19:e205ab9142d8 | 105 | /** |
bobgiesberts | 17:a5cf2b4bec13 | 106 | * @brief Sensor divider (p.26) |
bobgiesberts | 19:e205ab9142d8 | 107 | * Sensor input divider (p.35) |
bobgiesberts | 19:e205ab9142d8 | 108 | * Because f_CLKIN > 4*f_sensor is not realisable for higher frequencies, so there is a divider |
bobgiesberts | 19:e205ab9142d8 | 109 | * f_CLKIN > 4 * f_sensor / SENSOR_DIV |
bobgiesberts | 17:a5cf2b4bec13 | 110 | * @param div |
bobgiesberts | 17:a5cf2b4bec13 | 111 | * - DIVIDER_1 |
bobgiesberts | 17:a5cf2b4bec13 | 112 | * - DIVIDER_2 |
bobgiesberts | 17:a5cf2b4bec13 | 113 | * - DIVIDER_4 |
bobgiesberts | 17:a5cf2b4bec13 | 114 | * - DIVIDER_8 |
bobgiesberts | 17:a5cf2b4bec13 | 115 | **/ |
bobgiesberts | 17:a5cf2b4bec13 | 116 | void setDivider(DIVIDER div); |
bobgiesberts | 19:e205ab9142d8 | 117 | |
bobgiesberts | 16:07d0e43c2d12 | 118 | /** |
bobgiesberts | 19:e205ab9142d8 | 119 | * @brief Set the Response Time parameters. Does not apply in LHR mode (p.17) |
bobgiesberts | 16:07d0e43c2d12 | 120 | * @param responsetime |
bobgiesberts | 16:07d0e43c2d12 | 121 | * Larger value increases accuracy, but slows down the output data rate. Choose one of these values: |
bobgiesberts | 16:07d0e43c2d12 | 122 | * - LDC_RESPONSE_192 |
bobgiesberts | 16:07d0e43c2d12 | 123 | * - LDC_RESPONSE_384 |
bobgiesberts | 16:07d0e43c2d12 | 124 | * - LDC_RESPONSE_768 |
bobgiesberts | 16:07d0e43c2d12 | 125 | * - LDC_RESPONSE_1536 |
bobgiesberts | 16:07d0e43c2d12 | 126 | * - LDC_RESPONSE_3072 |
bobgiesberts | 16:07d0e43c2d12 | 127 | * - LDC_RESPONSE_6144 |
bobgiesberts | 19:e205ab9142d8 | 128 | * ResponseTime |
bobgiesberts | 19:e205ab9142d8 | 129 | * t_conv (s) = ------------ |
bobgiesberts | 19:e205ab9142d8 | 130 | * 3 x f_sensor |
bobgiesberts | 16:07d0e43c2d12 | 131 | **/ |
bobgiesberts | 16:07d0e43c2d12 | 132 | void setResponseTime(LDC_RESPONSE responsetime); |
bobgiesberts | 16:07d0e43c2d12 | 133 | |
bobgiesberts | 19:e205ab9142d8 | 134 | /** |
bobgiesberts | 19:e205ab9142d8 | 135 | * @brief Set the Reference Count parameter. |
bobgiesberts | 19:e205ab9142d8 | 136 | * @param LHR_Count |
bobgiesberts | 19:e205ab9142d8 | 137 | * For LHR mode, the conversion time is set by the reference count LHR_RCOUNT (0x30 & 0x31) (p.34) |
bobgiesberts | 19:e205ab9142d8 | 138 | * The conversion time represents the number of clock cycles used to measure the sensor frequency. |
bobgiesberts | 19:e205ab9142d8 | 139 | * Higher values for LHR_RCOUNT have a higher effective measurement resolution but a lower sample rate. (p.34) |
bobgiesberts | 19:e205ab9142d8 | 140 | * The maximum setting (0xffff) is required for full resolution (p. 35) |
bobgiesberts | 19:e205ab9142d8 | 141 | * 1) LHR_Count = 1/sample rate [kS/s] - 55/f_CLKIN |
bobgiesberts | 19:e205ab9142d8 | 142 | * 2) The reference count value must be chosen to support the required number of effective bits (ENOB). |
bobgiesberts | 19:e205ab9142d8 | 143 | * e.g. ENOB 13 bits --> minimum converstion time 2^13 = 8192 clock cycles required. 8192 = 0x2000 = RCOUNT. |
bobgiesberts | 19:e205ab9142d8 | 144 | **/ |
bobgiesberts | 19:e205ab9142d8 | 145 | void setReferenceCount(uint16_t LHR_Count); |
bobgiesberts | 19:e205ab9142d8 | 146 | /** |
bobgiesberts | 19:e205ab9142d8 | 147 | * @brief Set the value of the external capacitor |
bobgiesberts | 19:e205ab9142d8 | 148 | * This is needed for the calculation of the inductance. |
bobgiesberts | 19:e205ab9142d8 | 149 | **/ |
bobgiesberts | 19:e205ab9142d8 | 150 | void setCapacitor(float c){_cap = c;}; |
bobgiesberts | 19:e205ab9142d8 | 151 | /** |
bobgiesberts | 19:e205ab9142d8 | 152 | * @brief set the value of the external clock |
bobgiesberts | 19:e205ab9142d8 | 153 | * If PWMout is used to generate a clock signal, this will update the output frequency.s |
bobgiesberts | 19:e205ab9142d8 | 154 | **/ |
bobgiesberts | 19:e205ab9142d8 | 155 | void setFrequency(float frequency){_fCLKIN = frequency;}; |
bobgiesberts | 19:e205ab9142d8 | 156 | |
bobgiesberts | 19:e205ab9142d8 | 157 | |
bobgiesberts | 19:e205ab9142d8 | 158 | /** |
bobgiesberts | 19:e205ab9142d8 | 159 | * @brief Read LHR_Data, the raw 24-bit inductance value. |
bobgiesberts | 19:e205ab9142d8 | 160 | * This is needed for the calculation of the inductance. |
bobgiesberts | 19:e205ab9142d8 | 161 | * It reads from addresses 0x38, 0x39 & 0x3A. |
bobgiesberts | 19:e205ab9142d8 | 162 | **/ |
bobgiesberts | 19:e205ab9142d8 | 163 | uint32_t get_LHR_Data(void); |
bobgiesberts | 19:e205ab9142d8 | 164 | /** |
bobgiesberts | 19:e205ab9142d8 | 165 | * @brief get the calculated value for f_sensor |
bobgiesberts | 19:e205ab9142d8 | 166 | **/ |
bobgiesberts | 19:e205ab9142d8 | 167 | float get_fsensor(void); |
bobgiesberts | 19:e205ab9142d8 | 168 | /** |
bobgiesberts | 19:e205ab9142d8 | 169 | * @brief get the calculated inductance value |
bobgiesberts | 19:e205ab9142d8 | 170 | **/ |
bobgiesberts | 19:e205ab9142d8 | 171 | float get_Inductance(void); |
bobgiesberts | 19:e205ab9142d8 | 172 | /** |
bobgiesberts | 19:e205ab9142d8 | 173 | * @brief get the reference frequency (f_CLKIN) |
bobgiesberts | 19:e205ab9142d8 | 174 | **/ |
bobgiesberts | 19:e205ab9142d8 | 175 | float get_fCLKIN(void); |
bobgiesberts | 19:e205ab9142d8 | 176 | /** |
bobgiesberts | 19:e205ab9142d8 | 177 | * @brief get the responsetime |
bobgiesberts | 19:e205ab9142d8 | 178 | **/ |
bobgiesberts | 19:e205ab9142d8 | 179 | uint16_t get_responsetime(void); |
bobgiesberts | 19:e205ab9142d8 | 180 | /** |
bobgiesberts | 19:e205ab9142d8 | 181 | * @brief get RPmin |
bobgiesberts | 19:e205ab9142d8 | 182 | **/ |
bobgiesberts | 19:e205ab9142d8 | 183 | float get_RPmin(void); |
bobgiesberts | 19:e205ab9142d8 | 184 | /** |
bobgiesberts | 19:e205ab9142d8 | 185 | * @brief get the reference count |
bobgiesberts | 19:e205ab9142d8 | 186 | **/ |
bobgiesberts | 19:e205ab9142d8 | 187 | uint16_t get_Rcount(void); |
bobgiesberts | 19:e205ab9142d8 | 188 | /** |
bobgiesberts | 19:e205ab9142d8 | 189 | * @brief get the divider |
bobgiesberts | 19:e205ab9142d8 | 190 | **/ |
bobgiesberts | 19:e205ab9142d8 | 191 | uint8_t get_divider(void); |
bobgiesberts | 19:e205ab9142d8 | 192 | /** |
bobgiesberts | 19:e205ab9142d8 | 193 | * @brief get the capacitance |
bobgiesberts | 19:e205ab9142d8 | 194 | **/ |
bobgiesberts | 19:e205ab9142d8 | 195 | float get_cap(void); |
bobgiesberts | 19:e205ab9142d8 | 196 | /** |
bobgiesberts | 19:e205ab9142d8 | 197 | * @brief get the quality |
bobgiesberts | 19:e205ab9142d8 | 198 | **/ |
bobgiesberts | 19:e205ab9142d8 | 199 | float get_Q(void); |
bobgiesberts | 19:e205ab9142d8 | 200 | |
bobgiesberts | 19:e205ab9142d8 | 201 | |
bobgiesberts | 19:e205ab9142d8 | 202 | |
bobgiesberts | 19:e205ab9142d8 | 203 | |
bobgiesberts | 19:e205ab9142d8 | 204 | |
bobgiesberts | 19:e205ab9142d8 | 205 | |
bobgiesberts | 16:07d0e43c2d12 | 206 | private: |
bobgiesberts | 16:07d0e43c2d12 | 207 | void readSPI(uint8_t *data, uint8_t address, uint8_t num_bytes = 1); |
bobgiesberts | 16:07d0e43c2d12 | 208 | void writeSPI(uint8_t *data, uint8_t address, uint8_t num_bytes = 1); |
bobgiesberts | 16:07d0e43c2d12 | 209 | void writeSPIregister(uint8_t reg, uint8_t value){writeSPI(&value,reg);}; // VERKEERD OM?! |
bobgiesberts | 16:07d0e43c2d12 | 210 | |
bobgiesberts | 16:07d0e43c2d12 | 211 | uint32_t readINTB(void); // EXTRA UNTB Read register |
bobgiesberts | 16:07d0e43c2d12 | 212 | uint32_t INTB; // extra: read register INTB |
bobgiesberts | 16:07d0e43c2d12 | 213 | |
bobgiesberts | 19:e205ab9142d8 | 214 | uint16_t _responsetime; // Response Time |
bobgiesberts | 19:e205ab9142d8 | 215 | uint8_t _divider; // divider |
bobgiesberts | 19:e205ab9142d8 | 216 | float _RPmin; // RP_MIN |
bobgiesberts | 19:e205ab9142d8 | 217 | float _fsensor; // f_sensor: the calculated frequency of the sensor |
bobgiesberts | 19:e205ab9142d8 | 218 | float _inductance; // the calculated inductance |
bobgiesberts | 19:e205ab9142d8 | 219 | float _fCLKIN; // f_CLKIN: frequency of external clock: 16MHz |
bobgiesberts | 19:e205ab9142d8 | 220 | float _cap; // capacitor: 120 pF |
bobgiesberts | 19:e205ab9142d8 | 221 | uint32_t _L_data; // The raw measured data for inductance |
bobgiesberts | 19:e205ab9142d8 | 222 | uint16_t _Rcount; // The reference count |
bobgiesberts | 19:e205ab9142d8 | 223 | |
bobgiesberts | 19:e205ab9142d8 | 224 | |
bobgiesberts | 16:07d0e43c2d12 | 225 | SPI _spiport; |
bobgiesberts | 16:07d0e43c2d12 | 226 | DigitalOut _cs_pin; |
bobgiesberts | 16:07d0e43c2d12 | 227 | |
bobgiesberts | 17:a5cf2b4bec13 | 228 | //FastPWM _clock; |
bobgiesberts | 16:07d0e43c2d12 | 229 | }; |
bobgiesberts | 16:07d0e43c2d12 | 230 | |
bobgiesberts | 16:07d0e43c2d12 | 231 | #endif |