Library to communicate with LDC1614

Dependencies:   SHTx

Dependents:   Inductive_Sensor_3

Fork of LDC1101 by Bob Giesberts

Committer:
bobgiesberts
Date:
Mon Jan 18 15:40:36 2016 +0000
Revision:
23:8faeabb395db
Parent:
22:8da965ce5af3
Child:
24:6bf98c909dfb
correction of small error: no need to put CS low before shutdown

Who changed what in which revision?

UserRevisionLine numberNew 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 22:8da965ce5af3 19
bobgiesberts 16:07d0e43c2d12 20 _spiport.format(8,3);
bobgiesberts 16:07d0e43c2d12 21 _spiport.frequency(1E6);
bobgiesberts 18:fc9bb81a631f 22 setFrequency(f_CLKIN);
bobgiesberts 16:07d0e43c2d12 23
bobgiesberts 16:07d0e43c2d12 24 _cs_pin.write(1);
bobgiesberts 16:07d0e43c2d12 25 wait_us(100);
bobgiesberts 16:07d0e43c2d12 26
bobgiesberts 16:07d0e43c2d12 27 init();
bobgiesberts 16:07d0e43c2d12 28 }
bobgiesberts 16:07d0e43c2d12 29
bobgiesberts 21:7c9e04e7d34f 30 void LDC1101::func_mode(LDC_MODE mode) { writeSPI((uint8_t *)(&mode), 0x0B); wait_ms(0.8); }
bobgiesberts 22:8da965ce5af3 31
bobgiesberts 22:8da965ce5af3 32 void LDC1101::sleep(void)
bobgiesberts 22:8da965ce5af3 33 {
bobgiesberts 22:8da965ce5af3 34 /* stop toggling the CLKIN pin input and drive the CLKIN pin Low */
bobgiesberts 22:8da965ce5af3 35 func_mode( LDC_MODE_SHUTDOWN );
bobgiesberts 22:8da965ce5af3 36 suicide( this );
bobgiesberts 22:8da965ce5af3 37 }
bobgiesberts 22:8da965ce5af3 38 void LDC1101::wakeup(void) {
bobgiesberts 22:8da965ce5af3 39 /* start toggling the clock input on the CLKIN pin */
bobgiesberts 22:8da965ce5af3 40 init();
bobgiesberts 22:8da965ce5af3 41 wait(0.5);
bobgiesberts 22:8da965ce5af3 42 }
bobgiesberts 20:8e1b1efdbb49 43
bobgiesberts 20:8e1b1efdbb49 44
bobgiesberts 16:07d0e43c2d12 45 void LDC1101::init()
bobgiesberts 16:07d0e43c2d12 46 {
bobgiesberts 20:8e1b1efdbb49 47 /********* SETTINGS *****************
bobgiesberts 22:8da965ce5af3 48 ** C_sensor = 120 pF
bobgiesberts 22:8da965ce5af3 49 ** L_sensor = 5 uH
bobgiesberts 22:8da965ce5af3 50 ** Rp_min = 1500 Ohm
bobgiesberts 20:8e1b1efdbb49 51 **
bobgiesberts 20:8e1b1efdbb49 52 ** RCount = 65535
bobgiesberts 20:8e1b1efdbb49 53 ** Samplerate = 15.3 Hz
bobgiesberts 20:8e1b1efdbb49 54 ** t_conv = 65.5 ms
bobgiesberts 20:8e1b1efdbb49 55 **
bobgiesberts 20:8e1b1efdbb49 56 ** f_sensor_min = 6.4 MHz (d = inf)
bobgiesberts 20:8e1b1efdbb49 57 ** f_sensor_max = 10 MHz (d = 0)
bobgiesberts 20:8e1b1efdbb49 58 ** divider = (4*f_sensor_max)/f_CLKIN = 4*10/16 = 2,5 --> 2
bobgiesberts 20:8e1b1efdbb49 59 ************************************/
bobgiesberts 20:8e1b1efdbb49 60
bobgiesberts 20:8e1b1efdbb49 61
bobgiesberts 18:fc9bb81a631f 62 // Set LDC1101 in configuration modus
bobgiesberts 20:8e1b1efdbb49 63 func_mode( LDC_MODE_STANDBY ); // STANDBY = 0x01 naar 0x0B
bobgiesberts 18:fc9bb81a631f 64
bobgiesberts 20:8e1b1efdbb49 65 // - initialise LHR mode & enable SHUTDOWN mode
bobgiesberts 19:e205ab9142d8 66 setLHRmode();
bobgiesberts 16:07d0e43c2d12 67
bobgiesberts 19:e205ab9142d8 68 // - set ResponseTime to 6144
bobgiesberts 20:8e1b1efdbb49 69 setResponseTime( LDC_RESPONSE_6144 );
bobgiesberts 19:e205ab9142d8 70
bobgiesberts 20:8e1b1efdbb49 71 // - set Reference Count to highest resolution
bobgiesberts 20:8e1b1efdbb49 72 setReferenceCount( 0xffff );
bobgiesberts 20:8e1b1efdbb49 73
bobgiesberts 20:8e1b1efdbb49 74 // - set calibrated value for f_sensor_min (d = inf, no target)
bobgiesberts 20:8e1b1efdbb49 75 setf_sensorMin( 6.4 ); // 6.4 MHz
bobgiesberts 18:fc9bb81a631f 76
bobgiesberts 19:e205ab9142d8 77 // - disable RP_MAX
bobgiesberts 22:8da965ce5af3 78 // - set RP_MIN to 1,5 kOhm (RPMIN_1)
bobgiesberts 22:8da965ce5af3 79 setRPsettings( 1, RPMIN_1 );
bobgiesberts 16:07d0e43c2d12 80
bobgiesberts 19:e205ab9142d8 81 // - set Divider to 2
bobgiesberts 20:8e1b1efdbb49 82 setDivider( DIVIDER_2 );
bobgiesberts 18:fc9bb81a631f 83
bobgiesberts 18:fc9bb81a631f 84 // Done configuring settings, set LDC1101 in measuring modus
bobgiesberts 20:8e1b1efdbb49 85 func_mode( LDC_MODE_ACTIVE );
bobgiesberts 16:07d0e43c2d12 86 }
bobgiesberts 16:07d0e43c2d12 87
bobgiesberts 20:8e1b1efdbb49 88 void LDC1101::setLHRmode( void ){
bobgiesberts 20:8e1b1efdbb49 89 writeSPIregister( 0x05, 0x03 ); // ALT_CONFIG: 0000 0011 --> LHR modus + Shutdown enabled
bobgiesberts 20:8e1b1efdbb49 90 writeSPIregister( 0x0C, 0x01 ); // D_CONFIG: Enables LHR modus, disables RP
bobgiesberts 19:e205ab9142d8 91 }
bobgiesberts 19:e205ab9142d8 92
bobgiesberts 19:e205ab9142d8 93 void LDC1101::setRPsettings(bool RP_MAX_DIS, RPMIN rpmin)
bobgiesberts 19:e205ab9142d8 94 {
bobgiesberts 19:e205ab9142d8 95 float rpmins[] = {96, 48, 24, 12, 6, 3, 1.5, 0.75};
bobgiesberts 19:e205ab9142d8 96 _RPmin = rpmins[rpmin];
bobgiesberts 19:e205ab9142d8 97 writeSPIregister(0x01, ((RP_MAX_DIS & 0x80) << 7 | rpmin));
bobgiesberts 19:e205ab9142d8 98 }
bobgiesberts 19:e205ab9142d8 99
bobgiesberts 17:a5cf2b4bec13 100 void LDC1101::setDivider(DIVIDER div)
bobgiesberts 17:a5cf2b4bec13 101 {
bobgiesberts 19:e205ab9142d8 102 uint8_t divs[] = {1, 2, 4, 8};
bobgiesberts 19:e205ab9142d8 103 _divider = divs[div];
bobgiesberts 17:a5cf2b4bec13 104 writeSPIregister(0x34, div);
bobgiesberts 20:8e1b1efdbb49 105 }
bobgiesberts 20:8e1b1efdbb49 106
bobgiesberts 20:8e1b1efdbb49 107 void LDC1101::setResponseTime(LDC_RESPONSE responsetime)
bobgiesberts 20:8e1b1efdbb49 108 {
bobgiesberts 20:8e1b1efdbb49 109 uint16_t resps[] = {0, 0, 192, 384, 768, 1536, 3072, 6144};
bobgiesberts 20:8e1b1efdbb49 110 _responsetime = resps[responsetime];
bobgiesberts 20:8e1b1efdbb49 111 uint8_t buffer[1];
bobgiesberts 20:8e1b1efdbb49 112 readSPI(buffer, 0x04, 1);
bobgiesberts 20:8e1b1efdbb49 113 writeSPIregister(0x04, (buffer[0] & 0xF8) + responsetime);
bobgiesberts 20:8e1b1efdbb49 114 }
bobgiesberts 20:8e1b1efdbb49 115
bobgiesberts 20:8e1b1efdbb49 116 void LDC1101::setReferenceCount(uint16_t rcount)
bobgiesberts 20:8e1b1efdbb49 117 {
bobgiesberts 20:8e1b1efdbb49 118 _Rcount = rcount;
bobgiesberts 20:8e1b1efdbb49 119 uint8_t LHR_RCOUNT_LSB = (rcount & 0x00ff);
bobgiesberts 20:8e1b1efdbb49 120 uint8_t LHR_RCOUNT_MSB = ((rcount & 0xff00) >> 8);
bobgiesberts 20:8e1b1efdbb49 121 writeSPIregister(0x30, LHR_RCOUNT_LSB); //LSB
bobgiesberts 20:8e1b1efdbb49 122 writeSPIregister(0x31, LHR_RCOUNT_MSB); //MSB
bobgiesberts 20:8e1b1efdbb49 123 }
bobgiesberts 20:8e1b1efdbb49 124
bobgiesberts 20:8e1b1efdbb49 125 void LDC1101::setSampleRate(float samplerate){ setReferenceCount( ((_fCLKIN/samplerate)-55)/16 ); }
bobgiesberts 17:a5cf2b4bec13 126
bobgiesberts 17:a5cf2b4bec13 127
bobgiesberts 20:8e1b1efdbb49 128 void LDC1101::setf_sensorMin(float f_sensor_min)
bobgiesberts 16:07d0e43c2d12 129 {
bobgiesberts 20:8e1b1efdbb49 130 uint8_t buffer[1];
bobgiesberts 20:8e1b1efdbb49 131 readSPI(buffer, 0x04, 1);
bobgiesberts 20:8e1b1efdbb49 132 uint8_t MIN_FREQ = 16 - (8 / f_sensor_min);
bobgiesberts 20:8e1b1efdbb49 133 writeSPIregister(0x04, ((buffer[0] & 0x0F) + (MIN_FREQ << 4)));
bobgiesberts 20:8e1b1efdbb49 134 }
bobgiesberts 20:8e1b1efdbb49 135
bobgiesberts 22:8da965ce5af3 136 bool LDC1101::is_New_LHR_data(void)
bobgiesberts 22:8da965ce5af3 137 {
bobgiesberts 22:8da965ce5af3 138 return(~(get_LHR_status() & 0x01));
bobgiesberts 22:8da965ce5af3 139 }
bobgiesberts 20:8e1b1efdbb49 140
bobgiesberts 22:8da965ce5af3 141 bool LDC1101::is_Oscillation_Error(void)
bobgiesberts 22:8da965ce5af3 142 {
bobgiesberts 22:8da965ce5af3 143 return(get_status() & 0x80);
bobgiesberts 22:8da965ce5af3 144 }
bobgiesberts 20:8e1b1efdbb49 145
bobgiesberts 22:8da965ce5af3 146 uint8_t LDC1101::get_status(void)
bobgiesberts 22:8da965ce5af3 147 {
bobgiesberts 22:8da965ce5af3 148
bobgiesberts 22:8da965ce5af3 149 uint8_t buffer[1];
bobgiesberts 22:8da965ce5af3 150 readSPI(buffer, 0x20, 1);
bobgiesberts 22:8da965ce5af3 151
bobgiesberts 22:8da965ce5af3 152 return buffer[0];
bobgiesberts 22:8da965ce5af3 153 }
bobgiesberts 20:8e1b1efdbb49 154
bobgiesberts 22:8da965ce5af3 155 uint8_t LDC1101::get_LHR_status(void)
bobgiesberts 22:8da965ce5af3 156 {
bobgiesberts 22:8da965ce5af3 157 uint8_t buffer[1];
bobgiesberts 22:8da965ce5af3 158 readSPI(buffer, 0x3B, 1);
bobgiesberts 22:8da965ce5af3 159 return buffer[0];
bobgiesberts 22:8da965ce5af3 160 }
bobgiesberts 20:8e1b1efdbb49 161
bobgiesberts 20:8e1b1efdbb49 162 float LDC1101::get_Q(void){ return _RPmin * sqrt(_cap/_inductance*1000000); }
bobgiesberts 16:07d0e43c2d12 163
bobgiesberts 19:e205ab9142d8 164
bobgiesberts 19:e205ab9142d8 165 float LDC1101::get_fsensor(void)
bobgiesberts 16:07d0e43c2d12 166 {
bobgiesberts 18:fc9bb81a631f 167 _L_data = get_LHR_Data();
bobgiesberts 18:fc9bb81a631f 168 _fsensor = _fCLKIN * _divider * _L_data/16777216; // (p.26)
bobgiesberts 18:fc9bb81a631f 169 return _fsensor;
bobgiesberts 19:e205ab9142d8 170 }
bobgiesberts 18:fc9bb81a631f 171
bobgiesberts 18:fc9bb81a631f 172
bobgiesberts 19:e205ab9142d8 173 float LDC1101::get_Inductance(void)
bobgiesberts 18:fc9bb81a631f 174 {
bobgiesberts 18:fc9bb81a631f 175 _fsensor = get_fsensor();
bobgiesberts 18:fc9bb81a631f 176 // 1
bobgiesberts 18:fc9bb81a631f 177 // L = --------------------- --> p. 34
bobgiesberts 18:fc9bb81a631f 178 // C * (2*PI*f_sensor)^2
bobgiesberts 20:8e1b1efdbb49 179 _inductance = 1./(_cap * 4*PI*PI*_fsensor*_fsensor); // (p.34)
bobgiesberts 19:e205ab9142d8 180 return _inductance;
bobgiesberts 19:e205ab9142d8 181 }
bobgiesberts 16:07d0e43c2d12 182
bobgiesberts 16:07d0e43c2d12 183
bobgiesberts 18:fc9bb81a631f 184 uint32_t LDC1101::get_LHR_Data(void)
bobgiesberts 16:07d0e43c2d12 185 {
bobgiesberts 18:fc9bb81a631f 186 uint8_t LHR_DATA[3];
bobgiesberts 18:fc9bb81a631f 187 readSPI(LHR_DATA, 0x38, 3); // 0x38 + 0x39 + 0x3A
bobgiesberts 20:8e1b1efdbb49 188 return (LHR_DATA[2]<<16) | (LHR_DATA[1]<<8) | LHR_DATA[0];
bobgiesberts 16:07d0e43c2d12 189 }
bobgiesberts 16:07d0e43c2d12 190
bobgiesberts 16:07d0e43c2d12 191 void LDC1101::readSPI(uint8_t *data, uint8_t address, uint8_t num_bytes)
bobgiesberts 16:07d0e43c2d12 192 {
bobgiesberts 16:07d0e43c2d12 193 // CSB down
bobgiesberts 16:07d0e43c2d12 194 _cs_pin.write(0);
bobgiesberts 16:07d0e43c2d12 195 _spiport.write(address | 0x80); //read flag
bobgiesberts 16:07d0e43c2d12 196 for(int i=0; i < num_bytes ; i++)
bobgiesberts 16:07d0e43c2d12 197 {
bobgiesberts 16:07d0e43c2d12 198 data[i] = _spiport.write(0xFF);
bobgiesberts 16:07d0e43c2d12 199 }
bobgiesberts 16:07d0e43c2d12 200 // CSB up
bobgiesberts 16:07d0e43c2d12 201 _cs_pin.write(1);
bobgiesberts 16:07d0e43c2d12 202 }
bobgiesberts 16:07d0e43c2d12 203
bobgiesberts 16:07d0e43c2d12 204 void LDC1101::writeSPI(uint8_t *data, uint8_t address, uint8_t num_bytes)
bobgiesberts 16:07d0e43c2d12 205 {
bobgiesberts 16:07d0e43c2d12 206 // CSB down
bobgiesberts 16:07d0e43c2d12 207 _cs_pin.write(0);
bobgiesberts 16:07d0e43c2d12 208
bobgiesberts 16:07d0e43c2d12 209 _spiport.write(address);
bobgiesberts 16:07d0e43c2d12 210 for(int i=0; i < num_bytes ; i++)
bobgiesberts 16:07d0e43c2d12 211 {
bobgiesberts 16:07d0e43c2d12 212 _spiport.write(data[i]);
bobgiesberts 16:07d0e43c2d12 213 }
bobgiesberts 16:07d0e43c2d12 214 // CSB up
bobgiesberts 16:07d0e43c2d12 215 _cs_pin.write(1);
bobgiesberts 16:07d0e43c2d12 216 }
bobgiesberts 16:07d0e43c2d12 217
bobgiesberts 16:07d0e43c2d12 218
bobgiesberts 16:07d0e43c2d12 219 // EXTRA test: Get&print values of all variables to verify (to calculate the induction)
bobgiesberts 16:07d0e43c2d12 220 // The data will be printed on the screen using RealTerm: baud 9600.
bobgiesberts 16:07d0e43c2d12 221 // Begin ***********************************************************
bobgiesberts 19:e205ab9142d8 222 float LDC1101::get_fCLKIN() {return _fCLKIN;};
bobgiesberts 19:e205ab9142d8 223 uint8_t LDC1101::get_divider() {return _divider;};
bobgiesberts 19:e205ab9142d8 224 float LDC1101::get_RPmin() {return _RPmin;};
bobgiesberts 19:e205ab9142d8 225 float LDC1101::get_cap() {return _cap;};
bobgiesberts 16:07d0e43c2d12 226 // END ***********************************************************