Library to communicate with LDC1614
Dependents: Inductive_Sensor_3
Fork of LDC1101 by
LDC1101.cpp@26:1ef9172cd355, 2016-03-30 (annotated)
- Committer:
- bobgiesberts
- Date:
- Wed Mar 30 12:00:10 2016 +0000
- Revision:
- 26:1ef9172cd355
- Parent:
- 25:ae111662ee03
- Child:
- 27:05dd145c7997
In this improved version:; - Solved the timing issue (error with converting floats to int in sleep); - Shutdown when Vbatt < 3,10 V; - Arrays instead of vectors;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bobgiesberts | 16:07d0e43c2d12 | 1 | /** |
bobgiesberts | 16:07d0e43c2d12 | 2 | * @file LDC1101.cpp |
bobgiesberts | 16:07d0e43c2d12 | 3 | * @brief this C++ file contains all required |
bobgiesberts | 16:07d0e43c2d12 | 4 | * functions to interface with Texas |
bobgiesberts | 16:07d0e43c2d12 | 5 | * Instruments' LDC1101. |
bobgiesberts | 16:07d0e43c2d12 | 6 | * |
bobgiesberts | 20:8e1b1efdbb49 | 7 | * @author Victor Sluiter & Bob Giesberts |
bobgiesberts | 16:07d0e43c2d12 | 8 | * |
bobgiesberts | 16:07d0e43c2d12 | 9 | * @date 2015-12-09 |
bobgiesberts | 16:07d0e43c2d12 | 10 | */ |
bobgiesberts | 16:07d0e43c2d12 | 11 | |
bobgiesberts | 16:07d0e43c2d12 | 12 | #include "LDC1101.h" |
bobgiesberts | 16:07d0e43c2d12 | 13 | |
bobgiesberts | 16:07d0e43c2d12 | 14 | |
bobgiesberts | 22:8da965ce5af3 | 15 | LDC1101::LDC1101(PinName mosi, PinName miso, PinName sck, PinName cs, float capacitor, float f_CLKIN, PinName clock_out) : _spiport(mosi,miso,sck, NC), _cs_pin(cs) //, _clock(clock_out,1) |
bobgiesberts | 16:07d0e43c2d12 | 16 | { |
bobgiesberts | 18:fc9bb81a631f | 17 | // settings |
bobgiesberts | 19:e205ab9142d8 | 18 | _cap = capacitor; |
bobgiesberts | 25:ae111662ee03 | 19 | _LHRoffset = 0; |
bobgiesberts | 25:ae111662ee03 | 20 | _f_sensor_min = 6.4; // MHz |
bobgiesberts | 25:ae111662ee03 | 21 | _Rcount = 0xffff; // max |
bobgiesberts | 22:8da965ce5af3 | 22 | |
bobgiesberts | 16:07d0e43c2d12 | 23 | _spiport.format(8,3); |
bobgiesberts | 16:07d0e43c2d12 | 24 | _spiport.frequency(1E6); |
bobgiesberts | 18:fc9bb81a631f | 25 | setFrequency(f_CLKIN); |
bobgiesberts | 16:07d0e43c2d12 | 26 | |
bobgiesberts | 16:07d0e43c2d12 | 27 | _cs_pin.write(1); |
bobgiesberts | 16:07d0e43c2d12 | 28 | wait_us(100); |
bobgiesberts | 16:07d0e43c2d12 | 29 | |
bobgiesberts | 16:07d0e43c2d12 | 30 | init(); |
bobgiesberts | 16:07d0e43c2d12 | 31 | } |
bobgiesberts | 16:07d0e43c2d12 | 32 | |
bobgiesberts | 26:1ef9172cd355 | 33 | LDC1101::~LDC1101() |
bobgiesberts | 26:1ef9172cd355 | 34 | { |
bobgiesberts | 26:1ef9172cd355 | 35 | // THERE IS A LEAK HERE!!! |
bobgiesberts | 26:1ef9172cd355 | 36 | // _sd_enable is connected to both the SD card and the LDC1101 |
bobgiesberts | 26:1ef9172cd355 | 37 | // this line should put 0.0 V on the SD card and the LDC1101 |
bobgiesberts | 26:1ef9172cd355 | 38 | // but in reality there still is 0.8 V on both the SD card and the LDC1101 |
bobgiesberts | 26:1ef9172cd355 | 39 | |
bobgiesberts | 26:1ef9172cd355 | 40 | |
bobgiesberts | 26:1ef9172cd355 | 41 | // For all SPI communication pinouts: |
bobgiesberts | 26:1ef9172cd355 | 42 | // 1) Create a new handle to access the pinout |
bobgiesberts | 26:1ef9172cd355 | 43 | // 2) Set it to 0 V |
bobgiesberts | 26:1ef9172cd355 | 44 | // 3) delete and remove the class |
bobgiesberts | 26:1ef9172cd355 | 45 | |
bobgiesberts | 26:1ef9172cd355 | 46 | // SPI communication with the SD card --> THIS SHOULD BE PART OF THE SDFileSystem CLASS!!! |
bobgiesberts | 26:1ef9172cd355 | 47 | DigitalOut *sdP2 = new DigitalOut(PTD4); |
bobgiesberts | 26:1ef9172cd355 | 48 | sdP2->write(0); |
bobgiesberts | 26:1ef9172cd355 | 49 | delete sdP2; |
bobgiesberts | 26:1ef9172cd355 | 50 | sdP2 = NULL;// SPI: cs |
bobgiesberts | 26:1ef9172cd355 | 51 | DigitalOut *sdP3 = new DigitalOut(PTD6); |
bobgiesberts | 26:1ef9172cd355 | 52 | sdP3->write(0); |
bobgiesberts | 26:1ef9172cd355 | 53 | delete sdP3; |
bobgiesberts | 26:1ef9172cd355 | 54 | sdP3 = NULL;// SPI: mosi |
bobgiesberts | 26:1ef9172cd355 | 55 | DigitalOut *sdP5 = new DigitalOut(PTD5); |
bobgiesberts | 26:1ef9172cd355 | 56 | sdP5->write(0); |
bobgiesberts | 26:1ef9172cd355 | 57 | delete sdP5; |
bobgiesberts | 26:1ef9172cd355 | 58 | sdP5 = NULL;// SPI: sck |
bobgiesberts | 26:1ef9172cd355 | 59 | DigitalOut *sdP7 = new DigitalOut(PTD7); |
bobgiesberts | 26:1ef9172cd355 | 60 | sdP7->write(0); |
bobgiesberts | 26:1ef9172cd355 | 61 | delete sdP7; |
bobgiesberts | 26:1ef9172cd355 | 62 | sdP7 = NULL;// SPI: miso |
bobgiesberts | 26:1ef9172cd355 | 63 | |
bobgiesberts | 26:1ef9172cd355 | 64 | // SPI communication with the LDC1101 |
bobgiesberts | 26:1ef9172cd355 | 65 | DigitalOut *senP2 = new DigitalOut(PTC7); |
bobgiesberts | 26:1ef9172cd355 | 66 | senP2->write(0); |
bobgiesberts | 26:1ef9172cd355 | 67 | delete senP2; |
bobgiesberts | 26:1ef9172cd355 | 68 | senP2 = NULL; // SPI: miso |
bobgiesberts | 26:1ef9172cd355 | 69 | DigitalOut *senP3 = new DigitalOut(PTC5); |
bobgiesberts | 26:1ef9172cd355 | 70 | senP3->write(0); |
bobgiesberts | 26:1ef9172cd355 | 71 | delete senP3; |
bobgiesberts | 26:1ef9172cd355 | 72 | senP3 = NULL; // SPI: sck |
bobgiesberts | 26:1ef9172cd355 | 73 | DigitalOut *senP4 = new DigitalOut(PTC6); |
bobgiesberts | 26:1ef9172cd355 | 74 | senP4->write(0); |
bobgiesberts | 26:1ef9172cd355 | 75 | delete senP4; |
bobgiesberts | 26:1ef9172cd355 | 76 | senP4 = NULL; // SPI: mosi |
bobgiesberts | 26:1ef9172cd355 | 77 | DigitalOut *senP5 = new DigitalOut(PTC4); |
bobgiesberts | 26:1ef9172cd355 | 78 | senP5->write(0); |
bobgiesberts | 26:1ef9172cd355 | 79 | delete senP5; |
bobgiesberts | 26:1ef9172cd355 | 80 | senP5 = NULL; // SPI: cs |
bobgiesberts | 26:1ef9172cd355 | 81 | } |
bobgiesberts | 26:1ef9172cd355 | 82 | |
bobgiesberts | 26:1ef9172cd355 | 83 | |
bobgiesberts | 26:1ef9172cd355 | 84 | void LDC1101::func_mode(LDC_MODE mode) |
bobgiesberts | 26:1ef9172cd355 | 85 | { |
bobgiesberts | 26:1ef9172cd355 | 86 | writeSPI((uint8_t *)(&mode), 0x0B); |
bobgiesberts | 26:1ef9172cd355 | 87 | wait_ms(0.8); |
bobgiesberts | 26:1ef9172cd355 | 88 | } |
bobgiesberts | 22:8da965ce5af3 | 89 | |
bobgiesberts | 22:8da965ce5af3 | 90 | void LDC1101::sleep(void) |
bobgiesberts | 22:8da965ce5af3 | 91 | { |
bobgiesberts | 22:8da965ce5af3 | 92 | /* stop toggling the CLKIN pin input and drive the CLKIN pin Low */ |
bobgiesberts | 22:8da965ce5af3 | 93 | func_mode( LDC_MODE_SHUTDOWN ); |
bobgiesberts | 22:8da965ce5af3 | 94 | suicide( this ); |
bobgiesberts | 22:8da965ce5af3 | 95 | } |
bobgiesberts | 26:1ef9172cd355 | 96 | |
bobgiesberts | 22:8da965ce5af3 | 97 | void LDC1101::wakeup(void) { |
bobgiesberts | 22:8da965ce5af3 | 98 | /* start toggling the clock input on the CLKIN pin */ |
bobgiesberts | 22:8da965ce5af3 | 99 | init(); |
bobgiesberts | 22:8da965ce5af3 | 100 | wait(0.5); |
bobgiesberts | 22:8da965ce5af3 | 101 | } |
bobgiesberts | 20:8e1b1efdbb49 | 102 | |
bobgiesberts | 20:8e1b1efdbb49 | 103 | |
bobgiesberts | 16:07d0e43c2d12 | 104 | void LDC1101::init() |
bobgiesberts | 16:07d0e43c2d12 | 105 | { |
bobgiesberts | 20:8e1b1efdbb49 | 106 | /********* SETTINGS ***************** |
bobgiesberts | 25:ae111662ee03 | 107 | ** C_sensor = 120 pF |
bobgiesberts | 25:ae111662ee03 | 108 | ** L_sensor = 5 uH |
bobgiesberts | 25:ae111662ee03 | 109 | ** Rp_min = 1500 Ohm |
bobgiesberts | 20:8e1b1efdbb49 | 110 | ** |
bobgiesberts | 25:ae111662ee03 | 111 | ** RCount = 65535 (max) |
bobgiesberts | 20:8e1b1efdbb49 | 112 | ** Samplerate = 15.3 Hz |
bobgiesberts | 20:8e1b1efdbb49 | 113 | ** t_conv = 65.5 ms |
bobgiesberts | 20:8e1b1efdbb49 | 114 | ** |
bobgiesberts | 25:ae111662ee03 | 115 | ** f_sensor_min = 6.4 MHz (d = inf) |
bobgiesberts | 25:ae111662ee03 | 116 | ** f_sensor_max = 10 MHz (d = 0) |
bobgiesberts | 25:ae111662ee03 | 117 | ** divider = 1 |
bobgiesberts | 20:8e1b1efdbb49 | 118 | ************************************/ |
bobgiesberts | 20:8e1b1efdbb49 | 119 | |
bobgiesberts | 20:8e1b1efdbb49 | 120 | |
bobgiesberts | 18:fc9bb81a631f | 121 | // Set LDC1101 in configuration modus |
bobgiesberts | 20:8e1b1efdbb49 | 122 | func_mode( LDC_MODE_STANDBY ); // STANDBY = 0x01 naar 0x0B |
bobgiesberts | 18:fc9bb81a631f | 123 | |
bobgiesberts | 20:8e1b1efdbb49 | 124 | // - initialise LHR mode & enable SHUTDOWN mode |
bobgiesberts | 25:ae111662ee03 | 125 | setLHRmode(); // LHR mode |
bobgiesberts | 25:ae111662ee03 | 126 | // setRPmode(); // RP+L mode |
bobgiesberts | 16:07d0e43c2d12 | 127 | |
bobgiesberts | 19:e205ab9142d8 | 128 | // - set ResponseTime to 6144 |
bobgiesberts | 20:8e1b1efdbb49 | 129 | setResponseTime( LDC_RESPONSE_6144 ); |
bobgiesberts | 19:e205ab9142d8 | 130 | |
bobgiesberts | 20:8e1b1efdbb49 | 131 | // - set Reference Count to highest resolution |
bobgiesberts | 25:ae111662ee03 | 132 | setReferenceCount( _Rcount ); |
bobgiesberts | 20:8e1b1efdbb49 | 133 | |
bobgiesberts | 20:8e1b1efdbb49 | 134 | // - set calibrated value for f_sensor_min (d = inf, no target) |
bobgiesberts | 25:ae111662ee03 | 135 | set_fsensor_min( _f_sensor_min ); // 6.4 MHz |
bobgiesberts | 18:fc9bb81a631f | 136 | |
bobgiesberts | 19:e205ab9142d8 | 137 | // - disable RP_MAX |
bobgiesberts | 22:8da965ce5af3 | 138 | // - set RP_MIN to 1,5 kOhm (RPMIN_1) |
bobgiesberts | 25:ae111662ee03 | 139 | setRPsettings( 1, RPMAX_96, RPMIN_1 ); // LHR mode |
bobgiesberts | 25:ae111662ee03 | 140 | // setRPsettings( 0, RPMAX_96, RPMIN_1 ); // RP+L mode |
bobgiesberts | 16:07d0e43c2d12 | 141 | |
bobgiesberts | 25:ae111662ee03 | 142 | // - set Divider to 1 (for large range / ENOB / resolution) |
bobgiesberts | 25:ae111662ee03 | 143 | setDivider( DIVIDER_1 ); |
bobgiesberts | 25:ae111662ee03 | 144 | |
bobgiesberts | 25:ae111662ee03 | 145 | // - shift the signal down a bit |
bobgiesberts | 25:ae111662ee03 | 146 | setLHRoffset( _LHRoffset ); |
bobgiesberts | 18:fc9bb81a631f | 147 | |
bobgiesberts | 18:fc9bb81a631f | 148 | // Done configuring settings, set LDC1101 in measuring modus |
bobgiesberts | 20:8e1b1efdbb49 | 149 | func_mode( LDC_MODE_ACTIVE ); |
bobgiesberts | 16:07d0e43c2d12 | 150 | } |
bobgiesberts | 16:07d0e43c2d12 | 151 | |
bobgiesberts | 20:8e1b1efdbb49 | 152 | void LDC1101::setLHRmode( void ){ |
bobgiesberts | 20:8e1b1efdbb49 | 153 | writeSPIregister( 0x05, 0x03 ); // ALT_CONFIG: 0000 0011 --> LHR modus + Shutdown enabled |
bobgiesberts | 20:8e1b1efdbb49 | 154 | writeSPIregister( 0x0C, 0x01 ); // D_CONFIG: Enables LHR modus, disables RP |
bobgiesberts | 19:e205ab9142d8 | 155 | } |
bobgiesberts | 19:e205ab9142d8 | 156 | |
bobgiesberts | 25:ae111662ee03 | 157 | void LDC1101::setRPmode( void ){ |
bobgiesberts | 25:ae111662ee03 | 158 | writeSPIregister( 0x05, 0x02 ); // ALT_CONFIG: 0000 0010 --> RP modus + Shutdown enabled |
bobgiesberts | 25:ae111662ee03 | 159 | writeSPIregister( 0x0C, 0x00 ); // D_CONFIG: Enables LHR modus, disables RP |
bobgiesberts | 25:ae111662ee03 | 160 | } |
bobgiesberts | 25:ae111662ee03 | 161 | |
bobgiesberts | 25:ae111662ee03 | 162 | void LDC1101::setRPsettings(bool RP_MAX_DIS, RPMAX rpmax, RPMIN rpmin) |
bobgiesberts | 19:e205ab9142d8 | 163 | { |
bobgiesberts | 25:ae111662ee03 | 164 | float rps[] = {96, 48, 24, 12, 6, 3, 1.5, 0.75}; |
bobgiesberts | 25:ae111662ee03 | 165 | _RPmin = rps[rpmin]; |
bobgiesberts | 25:ae111662ee03 | 166 | _RPmax = rps[rpmax]; |
bobgiesberts | 25:ae111662ee03 | 167 | writeSPIregister(0x01, (RP_MAX_DIS << 7) | (rpmax << 4) | rpmin); |
bobgiesberts | 19:e205ab9142d8 | 168 | } |
bobgiesberts | 19:e205ab9142d8 | 169 | |
bobgiesberts | 17:a5cf2b4bec13 | 170 | void LDC1101::setDivider(DIVIDER div) |
bobgiesberts | 17:a5cf2b4bec13 | 171 | { |
bobgiesberts | 19:e205ab9142d8 | 172 | uint8_t divs[] = {1, 2, 4, 8}; |
bobgiesberts | 19:e205ab9142d8 | 173 | _divider = divs[div]; |
bobgiesberts | 17:a5cf2b4bec13 | 174 | writeSPIregister(0x34, div); |
bobgiesberts | 20:8e1b1efdbb49 | 175 | } |
bobgiesberts | 20:8e1b1efdbb49 | 176 | |
bobgiesberts | 25:ae111662ee03 | 177 | void LDC1101::setLHRoffset( uint32_t offset ) |
bobgiesberts | 25:ae111662ee03 | 178 | { |
bobgiesberts | 25:ae111662ee03 | 179 | _LHRoffset = offset; |
bobgiesberts | 25:ae111662ee03 | 180 | uint16_t LHR_OFFSET = offset / 256; |
bobgiesberts | 25:ae111662ee03 | 181 | writeSPIregister(0x32, (uint8_t) (LHR_OFFSET & 0x00ff) ); // LSB |
bobgiesberts | 25:ae111662ee03 | 182 | writeSPIregister(0x33, (uint8_t) ((LHR_OFFSET & 0xff00) >> 8) ); // MSB |
bobgiesberts | 25:ae111662ee03 | 183 | } |
bobgiesberts | 25:ae111662ee03 | 184 | |
bobgiesberts | 20:8e1b1efdbb49 | 185 | void LDC1101::setResponseTime(LDC_RESPONSE responsetime) |
bobgiesberts | 20:8e1b1efdbb49 | 186 | { |
bobgiesberts | 20:8e1b1efdbb49 | 187 | uint16_t resps[] = {0, 0, 192, 384, 768, 1536, 3072, 6144}; |
bobgiesberts | 20:8e1b1efdbb49 | 188 | _responsetime = resps[responsetime]; |
bobgiesberts | 25:ae111662ee03 | 189 | uint8_t DIG_CONF[1]; |
bobgiesberts | 25:ae111662ee03 | 190 | readSPI(DIG_CONF, 0x04, 1); |
bobgiesberts | 25:ae111662ee03 | 191 | writeSPIregister(0x04, (DIG_CONF[0] & 0xF8) + responsetime); |
bobgiesberts | 20:8e1b1efdbb49 | 192 | } |
bobgiesberts | 20:8e1b1efdbb49 | 193 | |
bobgiesberts | 20:8e1b1efdbb49 | 194 | void LDC1101::setReferenceCount(uint16_t rcount) |
bobgiesberts | 20:8e1b1efdbb49 | 195 | { |
bobgiesberts | 20:8e1b1efdbb49 | 196 | _Rcount = rcount; |
bobgiesberts | 20:8e1b1efdbb49 | 197 | uint8_t LHR_RCOUNT_LSB = (rcount & 0x00ff); |
bobgiesberts | 20:8e1b1efdbb49 | 198 | uint8_t LHR_RCOUNT_MSB = ((rcount & 0xff00) >> 8); |
bobgiesberts | 20:8e1b1efdbb49 | 199 | writeSPIregister(0x30, LHR_RCOUNT_LSB); //LSB |
bobgiesberts | 20:8e1b1efdbb49 | 200 | writeSPIregister(0x31, LHR_RCOUNT_MSB); //MSB |
bobgiesberts | 20:8e1b1efdbb49 | 201 | } |
bobgiesberts | 20:8e1b1efdbb49 | 202 | |
bobgiesberts | 20:8e1b1efdbb49 | 203 | void LDC1101::setSampleRate(float samplerate){ setReferenceCount( ((_fCLKIN/samplerate)-55)/16 ); } |
bobgiesberts | 17:a5cf2b4bec13 | 204 | |
bobgiesberts | 17:a5cf2b4bec13 | 205 | |
bobgiesberts | 25:ae111662ee03 | 206 | void LDC1101::set_fsensor_min(float f_sensor_min) |
bobgiesberts | 16:07d0e43c2d12 | 207 | { |
bobgiesberts | 25:ae111662ee03 | 208 | uint8_t DIG_CONF[1]; |
bobgiesberts | 25:ae111662ee03 | 209 | readSPI(DIG_CONF, 0x04, 1); |
bobgiesberts | 25:ae111662ee03 | 210 | uint8_t MIN_FREQ = 16.0f - (8.0f / f_sensor_min); |
bobgiesberts | 25:ae111662ee03 | 211 | writeSPIregister(0x04, ((MIN_FREQ << 4) + (DIG_CONF[0] & 0x0f))); |
bobgiesberts | 20:8e1b1efdbb49 | 212 | } |
bobgiesberts | 20:8e1b1efdbb49 | 213 | |
bobgiesberts | 25:ae111662ee03 | 214 | float LDC1101::get_fsensor_min() |
bobgiesberts | 22:8da965ce5af3 | 215 | { |
bobgiesberts | 25:ae111662ee03 | 216 | uint8_t DIG_CONF[1]; |
bobgiesberts | 25:ae111662ee03 | 217 | readSPI(DIG_CONF, 0x04, 1); |
bobgiesberts | 25:ae111662ee03 | 218 | return (float) 8.0f/(16.0f - (float) ((DIG_CONF[0] & 0xf0) >> 4)); |
bobgiesberts | 22:8da965ce5af3 | 219 | } |
bobgiesberts | 20:8e1b1efdbb49 | 220 | |
bobgiesberts | 25:ae111662ee03 | 221 | bool LDC1101::is_New_LHR_data(void) { return(!(get_LHR_status() & 0x01)); } |
bobgiesberts | 25:ae111662ee03 | 222 | bool LDC1101::is_Oscillation_Error(void) { return(get_status() & 0x80); } |
bobgiesberts | 20:8e1b1efdbb49 | 223 | |
bobgiesberts | 22:8da965ce5af3 | 224 | uint8_t LDC1101::get_status(void) |
bobgiesberts | 22:8da965ce5af3 | 225 | { |
bobgiesberts | 25:ae111662ee03 | 226 | uint8_t status[1]; |
bobgiesberts | 25:ae111662ee03 | 227 | readSPI(status, 0x20, 1); |
bobgiesberts | 25:ae111662ee03 | 228 | return status[0]; |
bobgiesberts | 22:8da965ce5af3 | 229 | } |
bobgiesberts | 20:8e1b1efdbb49 | 230 | |
bobgiesberts | 22:8da965ce5af3 | 231 | uint8_t LDC1101::get_LHR_status(void) |
bobgiesberts | 22:8da965ce5af3 | 232 | { |
bobgiesberts | 25:ae111662ee03 | 233 | uint8_t LHR_status[1]; |
bobgiesberts | 25:ae111662ee03 | 234 | readSPI( LHR_status, 0x3B, 1 ); |
bobgiesberts | 25:ae111662ee03 | 235 | |
bobgiesberts | 25:ae111662ee03 | 236 | // ERR_ZC: (LHR_status & 0x10) >> 4 // |
bobgiesberts | 25:ae111662ee03 | 237 | // ERR_OR: (LHR_status & 0x08) >> 3 // |
bobgiesberts | 25:ae111662ee03 | 238 | // ERR_UR: (LHR_status & 0x04) >> 2 // 1 = LHR_DATA < 0 because LHR_OFFSET > LHR_DATA |
bobgiesberts | 25:ae111662ee03 | 239 | // ERR_OF: (LHR_status & 0x02) >> 1 // |
bobgiesberts | 25:ae111662ee03 | 240 | // LHR_DRDY: (LHR_status & 0x01) // 1 = Data ready |
bobgiesberts | 25:ae111662ee03 | 241 | |
bobgiesberts | 25:ae111662ee03 | 242 | return LHR_status[0]; |
bobgiesberts | 22:8da965ce5af3 | 243 | } |
bobgiesberts | 20:8e1b1efdbb49 | 244 | |
bobgiesberts | 25:ae111662ee03 | 245 | |
bobgiesberts | 25:ae111662ee03 | 246 | /* CALCULATE STUFF WITH SENSOR DATA */ |
bobgiesberts | 25:ae111662ee03 | 247 | |
bobgiesberts | 20:8e1b1efdbb49 | 248 | float LDC1101::get_Q(void){ return _RPmin * sqrt(_cap/_inductance*1000000); } |
bobgiesberts | 16:07d0e43c2d12 | 249 | |
bobgiesberts | 19:e205ab9142d8 | 250 | |
bobgiesberts | 25:ae111662ee03 | 251 | float LDC1101::get_fsensor( uint32_t Ldata ) |
bobgiesberts | 16:07d0e43c2d12 | 252 | { |
bobgiesberts | 25:ae111662ee03 | 253 | // LHR mode |
bobgiesberts | 25:ae111662ee03 | 254 | if( Ldata == 0 ) { Ldata = get_LHR_Data(); } |
bobgiesberts | 25:ae111662ee03 | 255 | _fsensor = _fCLKIN * _divider * (Ldata + _LHRoffset)/16777216; // (p.26) |
bobgiesberts | 25:ae111662ee03 | 256 | |
bobgiesberts | 25:ae111662ee03 | 257 | // RP+L mode |
bobgiesberts | 25:ae111662ee03 | 258 | // if( Ldata == 0 ) { Ldata = get_L_Data(); } |
bobgiesberts | 25:ae111662ee03 | 259 | // _fsensor = (_fCLKIN * 6144) / (3 * Ldata); // (p.31) |
bobgiesberts | 25:ae111662ee03 | 260 | |
bobgiesberts | 18:fc9bb81a631f | 261 | return _fsensor; |
bobgiesberts | 19:e205ab9142d8 | 262 | } |
bobgiesberts | 18:fc9bb81a631f | 263 | |
bobgiesberts | 25:ae111662ee03 | 264 | float LDC1101::get_Inductance( uint32_t Ldata ) |
bobgiesberts | 18:fc9bb81a631f | 265 | { |
bobgiesberts | 25:ae111662ee03 | 266 | float fsensor = get_fsensor( Ldata ); |
bobgiesberts | 25:ae111662ee03 | 267 | _inductance = 1./(_cap * 4*PI*PI*fsensor*fsensor); // (p.34) |
bobgiesberts | 19:e205ab9142d8 | 268 | return _inductance; |
bobgiesberts | 19:e205ab9142d8 | 269 | } |
bobgiesberts | 16:07d0e43c2d12 | 270 | |
bobgiesberts | 16:07d0e43c2d12 | 271 | |
bobgiesberts | 25:ae111662ee03 | 272 | /* GETTING DATA FROM SENSOR */ |
bobgiesberts | 25:ae111662ee03 | 273 | |
bobgiesberts | 25:ae111662ee03 | 274 | float LDC1101::get_RP( uint16_t RPdata ) |
bobgiesberts | 25:ae111662ee03 | 275 | { |
bobgiesberts | 25:ae111662ee03 | 276 | if( RPdata == 0 ) |
bobgiesberts | 25:ae111662ee03 | 277 | { |
bobgiesberts | 25:ae111662ee03 | 278 | RPdata = get_RP_Data(); |
bobgiesberts | 25:ae111662ee03 | 279 | } |
bobgiesberts | 25:ae111662ee03 | 280 | |
bobgiesberts | 25:ae111662ee03 | 281 | return (_RPmax * _RPmin) / ( _RPmax * (1.0f - ((float) RPdata / 65535.0f)) + _RPmin * ((float) RPdata / 65535.0f)); |
bobgiesberts | 25:ae111662ee03 | 282 | // return _RPmax * (1.0f - ((float) RPdata / 65535.0f)); |
bobgiesberts | 25:ae111662ee03 | 283 | } |
bobgiesberts | 25:ae111662ee03 | 284 | |
bobgiesberts | 18:fc9bb81a631f | 285 | uint32_t LDC1101::get_LHR_Data(void) |
bobgiesberts | 16:07d0e43c2d12 | 286 | { |
bobgiesberts | 18:fc9bb81a631f | 287 | uint8_t LHR_DATA[3]; |
bobgiesberts | 18:fc9bb81a631f | 288 | readSPI(LHR_DATA, 0x38, 3); // 0x38 + 0x39 + 0x3A |
bobgiesberts | 20:8e1b1efdbb49 | 289 | return (LHR_DATA[2]<<16) | (LHR_DATA[1]<<8) | LHR_DATA[0]; |
bobgiesberts | 16:07d0e43c2d12 | 290 | } |
bobgiesberts | 16:07d0e43c2d12 | 291 | |
bobgiesberts | 25:ae111662ee03 | 292 | uint16_t LDC1101::get_RP_Data(void) |
bobgiesberts | 25:ae111662ee03 | 293 | { |
bobgiesberts | 25:ae111662ee03 | 294 | uint8_t RP_DATA[2]; |
bobgiesberts | 25:ae111662ee03 | 295 | readSPI(RP_DATA, 0x21, 2); // 021 + 0x22 |
bobgiesberts | 25:ae111662ee03 | 296 | return (RP_DATA[1]<<8) | RP_DATA[0]; |
bobgiesberts | 25:ae111662ee03 | 297 | } |
bobgiesberts | 25:ae111662ee03 | 298 | |
bobgiesberts | 25:ae111662ee03 | 299 | uint16_t LDC1101::get_L_Data(void) |
bobgiesberts | 25:ae111662ee03 | 300 | { |
bobgiesberts | 25:ae111662ee03 | 301 | uint8_t L_DATA[2]; |
bobgiesberts | 25:ae111662ee03 | 302 | readSPI(L_DATA, 0x23, 2); // 023 + 0x24 |
bobgiesberts | 25:ae111662ee03 | 303 | return (L_DATA[1]<<8) | L_DATA[0]; |
bobgiesberts | 25:ae111662ee03 | 304 | } |
bobgiesberts | 25:ae111662ee03 | 305 | |
bobgiesberts | 25:ae111662ee03 | 306 | |
bobgiesberts | 16:07d0e43c2d12 | 307 | void LDC1101::readSPI(uint8_t *data, uint8_t address, uint8_t num_bytes) |
bobgiesberts | 16:07d0e43c2d12 | 308 | { |
bobgiesberts | 16:07d0e43c2d12 | 309 | // CSB down |
bobgiesberts | 16:07d0e43c2d12 | 310 | _cs_pin.write(0); |
bobgiesberts | 16:07d0e43c2d12 | 311 | _spiport.write(address | 0x80); //read flag |
bobgiesberts | 16:07d0e43c2d12 | 312 | for(int i=0; i < num_bytes ; i++) |
bobgiesberts | 16:07d0e43c2d12 | 313 | { |
bobgiesberts | 16:07d0e43c2d12 | 314 | data[i] = _spiport.write(0xFF); |
bobgiesberts | 16:07d0e43c2d12 | 315 | } |
bobgiesberts | 16:07d0e43c2d12 | 316 | // CSB up |
bobgiesberts | 16:07d0e43c2d12 | 317 | _cs_pin.write(1); |
bobgiesberts | 16:07d0e43c2d12 | 318 | } |
bobgiesberts | 16:07d0e43c2d12 | 319 | |
bobgiesberts | 16:07d0e43c2d12 | 320 | void LDC1101::writeSPI(uint8_t *data, uint8_t address, uint8_t num_bytes) |
bobgiesberts | 16:07d0e43c2d12 | 321 | { |
bobgiesberts | 16:07d0e43c2d12 | 322 | // CSB down |
bobgiesberts | 16:07d0e43c2d12 | 323 | _cs_pin.write(0); |
bobgiesberts | 16:07d0e43c2d12 | 324 | |
bobgiesberts | 16:07d0e43c2d12 | 325 | _spiport.write(address); |
bobgiesberts | 16:07d0e43c2d12 | 326 | for(int i=0; i < num_bytes ; i++) |
bobgiesberts | 16:07d0e43c2d12 | 327 | { |
bobgiesberts | 16:07d0e43c2d12 | 328 | _spiport.write(data[i]); |
bobgiesberts | 16:07d0e43c2d12 | 329 | } |
bobgiesberts | 16:07d0e43c2d12 | 330 | // CSB up |
bobgiesberts | 16:07d0e43c2d12 | 331 | _cs_pin.write(1); |
bobgiesberts | 16:07d0e43c2d12 | 332 | } |
bobgiesberts | 16:07d0e43c2d12 | 333 | |
bobgiesberts | 16:07d0e43c2d12 | 334 | |
bobgiesberts | 16:07d0e43c2d12 | 335 | // EXTRA test: Get&print values of all variables to verify (to calculate the induction) |
bobgiesberts | 16:07d0e43c2d12 | 336 | // The data will be printed on the screen using RealTerm: baud 9600. |
bobgiesberts | 16:07d0e43c2d12 | 337 | // Begin *********************************************************** |
bobgiesberts | 25:ae111662ee03 | 338 | float LDC1101::get_fCLKIN() {return _fCLKIN;} |
bobgiesberts | 25:ae111662ee03 | 339 | uint8_t LDC1101::get_divider() {return _divider;} |
bobgiesberts | 25:ae111662ee03 | 340 | uint32_t LDC1101::get_LHRoffset() {return _LHRoffset;} |
bobgiesberts | 25:ae111662ee03 | 341 | float LDC1101::get_RPmin() {return _RPmin;} |
bobgiesberts | 25:ae111662ee03 | 342 | float LDC1101::get_RPmax() {return _RPmax;} |
bobgiesberts | 25:ae111662ee03 | 343 | float LDC1101::get_cap() {return _cap;} |
bobgiesberts | 16:07d0e43c2d12 | 344 | // END *********************************************************** |