Library to communicate with LDC1614

Dependencies:   SHTx

Dependents:   Inductive_Sensor_3

Fork of LDC1101 by Bob Giesberts

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?

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 16:07d0e43c2d12 7 * @author Victor Sluiter
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 18:fc9bb81a631f 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 16:07d0e43c2d12 19 _spiport.format(8,3);
bobgiesberts 16:07d0e43c2d12 20 _spiport.frequency(1E6);
bobgiesberts 18:fc9bb81a631f 21 setFrequency(f_CLKIN);
bobgiesberts 16:07d0e43c2d12 22
bobgiesberts 16:07d0e43c2d12 23 _cs_pin.write(1);
bobgiesberts 16:07d0e43c2d12 24 wait_us(100);
bobgiesberts 16:07d0e43c2d12 25
bobgiesberts 16:07d0e43c2d12 26 init();
bobgiesberts 16:07d0e43c2d12 27 }
bobgiesberts 16:07d0e43c2d12 28
bobgiesberts 16:07d0e43c2d12 29 void LDC1101::init()
bobgiesberts 16:07d0e43c2d12 30 {
bobgiesberts 18:fc9bb81a631f 31 // Set LDC1101 in configuration modus
bobgiesberts 19:e205ab9142d8 32 func_mode(LDC_MODE_STANDBY); // STANDBY = 0x01 naar 0x0B
bobgiesberts 16:07d0e43c2d12 33 wait(0.1);
bobgiesberts 18:fc9bb81a631f 34
bobgiesberts 19:e205ab9142d8 35 // - initialise LHR mode
bobgiesberts 19:e205ab9142d8 36 setLHRmode();
bobgiesberts 16:07d0e43c2d12 37
bobgiesberts 19:e205ab9142d8 38 // - set ResponseTime to 6144
bobgiesberts 19:e205ab9142d8 39 // (This setting MUST be applied, leaving it to default does not work)
bobgiesberts 18:fc9bb81a631f 40 setResponseTime(LDC_RESPONSE_6144); // 6144 = 0x07 naar 0x04
bobgiesberts 19:e205ab9142d8 41
bobgiesberts 19:e205ab9142d8 42 // - set Reference Count to 8192 (13 ENOB - 2^13)
bobgiesberts 19:e205ab9142d8 43 setReferenceCount(0x8192); //0xffff
bobgiesberts 18:fc9bb81a631f 44
bobgiesberts 19:e205ab9142d8 45 // - disable RP_MAX
bobgiesberts 19:e205ab9142d8 46 // - set RP_MIN to 3 kOhm
bobgiesberts 19:e205ab9142d8 47 setRPsettings(1, RPMIN_12);
bobgiesberts 16:07d0e43c2d12 48
bobgiesberts 19:e205ab9142d8 49 // - set Divider to 2
bobgiesberts 18:fc9bb81a631f 50 setDivider(DIVIDER_2);
bobgiesberts 18:fc9bb81a631f 51
bobgiesberts 18:fc9bb81a631f 52 // Done configuring settings, set LDC1101 in measuring modus
bobgiesberts 19:e205ab9142d8 53 func_mode(LDC_MODE_ACTIVE); // ACTIVE = 0x00 naar 0x0B
bobgiesberts 16:07d0e43c2d12 54 }
bobgiesberts 16:07d0e43c2d12 55
bobgiesberts 16:07d0e43c2d12 56 void LDC1101::setResponseTime(LDC_RESPONSE responsetime)
bobgiesberts 16:07d0e43c2d12 57 {
bobgiesberts 18:fc9bb81a631f 58 uint16_t resps[] = {0, 0, 192, 384, 768, 1536, 3072, 6144};
bobgiesberts 18:fc9bb81a631f 59 _responsetime = resps[responsetime];
bobgiesberts 16:07d0e43c2d12 60 writeSPIregister(0x04, responsetime);
bobgiesberts 16:07d0e43c2d12 61 }
bobgiesberts 16:07d0e43c2d12 62
bobgiesberts 19:e205ab9142d8 63 void LDC1101::setReferenceCount(uint16_t rcount)
bobgiesberts 19:e205ab9142d8 64 {
bobgiesberts 19:e205ab9142d8 65 _Rcount = rcount;
bobgiesberts 19:e205ab9142d8 66
bobgiesberts 19:e205ab9142d8 67 uint8_t LHR_RCOUNT_LSB = (rcount & 0x00ff);
bobgiesberts 19:e205ab9142d8 68 uint8_t LHR_RCOUNT_MSB = ((rcount & 0xff00) >> 8);
bobgiesberts 19:e205ab9142d8 69
bobgiesberts 19:e205ab9142d8 70 writeSPIregister(0x30, LHR_RCOUNT_LSB); //LSB
bobgiesberts 19:e205ab9142d8 71 writeSPIregister(0x31, LHR_RCOUNT_MSB); //MSB
bobgiesberts 19:e205ab9142d8 72 }
bobgiesberts 19:e205ab9142d8 73
bobgiesberts 19:e205ab9142d8 74 void LDC1101::setRPsettings(bool RP_MAX_DIS, RPMIN rpmin)
bobgiesberts 19:e205ab9142d8 75 {
bobgiesberts 19:e205ab9142d8 76 float rpmins[] = {96, 48, 24, 12, 6, 3, 1.5, 0.75};
bobgiesberts 19:e205ab9142d8 77 _RPmin = rpmins[rpmin];
bobgiesberts 19:e205ab9142d8 78 writeSPIregister(0x01, ((RP_MAX_DIS & 0x80) << 7 | rpmin));
bobgiesberts 19:e205ab9142d8 79 }
bobgiesberts 19:e205ab9142d8 80
bobgiesberts 17:a5cf2b4bec13 81 void LDC1101::setDivider(DIVIDER div)
bobgiesberts 17:a5cf2b4bec13 82 {
bobgiesberts 19:e205ab9142d8 83 uint8_t divs[] = {1, 2, 4, 8};
bobgiesberts 19:e205ab9142d8 84 _divider = divs[div];
bobgiesberts 17:a5cf2b4bec13 85 writeSPIregister(0x34, div);
bobgiesberts 17:a5cf2b4bec13 86 }
bobgiesberts 17:a5cf2b4bec13 87
bobgiesberts 17:a5cf2b4bec13 88
bobgiesberts 19:e205ab9142d8 89 float LDC1101::get_Q(void)
bobgiesberts 16:07d0e43c2d12 90 {
bobgiesberts 19:e205ab9142d8 91 return _RPmin * sqrt(_cap/_inductance*1000000);
bobgiesberts 19:e205ab9142d8 92 }
bobgiesberts 16:07d0e43c2d12 93
bobgiesberts 19:e205ab9142d8 94
bobgiesberts 19:e205ab9142d8 95 float LDC1101::get_fsensor(void)
bobgiesberts 16:07d0e43c2d12 96 {
bobgiesberts 18:fc9bb81a631f 97 _L_data = get_LHR_Data();
bobgiesberts 18:fc9bb81a631f 98 _fsensor = _fCLKIN * _divider * _L_data/16777216; // (p.26)
bobgiesberts 18:fc9bb81a631f 99 return _fsensor;
bobgiesberts 19:e205ab9142d8 100 }
bobgiesberts 18:fc9bb81a631f 101
bobgiesberts 18:fc9bb81a631f 102
bobgiesberts 19:e205ab9142d8 103 float LDC1101::get_Inductance(void)
bobgiesberts 18:fc9bb81a631f 104 {
bobgiesberts 18:fc9bb81a631f 105 _fsensor = get_fsensor();
bobgiesberts 16:07d0e43c2d12 106
bobgiesberts 18:fc9bb81a631f 107 // 1
bobgiesberts 18:fc9bb81a631f 108 // L = --------------------- --> p. 34
bobgiesberts 18:fc9bb81a631f 109 // C * (2*PI*f_sensor)^2
bobgiesberts 19:e205ab9142d8 110 _inductance = 1./(_cap * 4*PI*PI*_fsensor*_fsensor);
bobgiesberts 19:e205ab9142d8 111 return _inductance;
bobgiesberts 19:e205ab9142d8 112 }
bobgiesberts 16:07d0e43c2d12 113
bobgiesberts 16:07d0e43c2d12 114
bobgiesberts 18:fc9bb81a631f 115 uint32_t LDC1101::get_LHR_Data(void)
bobgiesberts 16:07d0e43c2d12 116 {
bobgiesberts 18:fc9bb81a631f 117 // LHR_DATA (p.26 & p.27)
bobgiesberts 18:fc9bb81a631f 118 uint8_t LHR_DATA[3];
bobgiesberts 18:fc9bb81a631f 119 readSPI(LHR_DATA, 0x38, 3); // 0x38 + 0x39 + 0x3A
bobgiesberts 18:fc9bb81a631f 120 uint32_t combinedbytes = (LHR_DATA[2]<<16) | (LHR_DATA[1]<<8) | LHR_DATA[0];
bobgiesberts 16:07d0e43c2d12 121 return combinedbytes;
bobgiesberts 16:07d0e43c2d12 122 }
bobgiesberts 16:07d0e43c2d12 123
bobgiesberts 16:07d0e43c2d12 124 void LDC1101::readSPI(uint8_t *data, uint8_t address, uint8_t num_bytes)
bobgiesberts 16:07d0e43c2d12 125 {
bobgiesberts 16:07d0e43c2d12 126 // CSB down
bobgiesberts 16:07d0e43c2d12 127 _cs_pin.write(0);
bobgiesberts 16:07d0e43c2d12 128
bobgiesberts 16:07d0e43c2d12 129 // makes sure the address starts with 1... Why?
bobgiesberts 16:07d0e43c2d12 130 _spiport.write(address | 0x80); //read flag
bobgiesberts 16:07d0e43c2d12 131 for(int i=0; i < num_bytes ; i++)
bobgiesberts 16:07d0e43c2d12 132 {
bobgiesberts 16:07d0e43c2d12 133 data[i] = _spiport.write(0xFF);
bobgiesberts 16:07d0e43c2d12 134 }
bobgiesberts 16:07d0e43c2d12 135 // CSB up
bobgiesberts 16:07d0e43c2d12 136 _cs_pin.write(1);
bobgiesberts 16:07d0e43c2d12 137 }
bobgiesberts 16:07d0e43c2d12 138
bobgiesberts 16:07d0e43c2d12 139 void LDC1101::writeSPI(uint8_t *data, uint8_t address, uint8_t num_bytes)
bobgiesberts 16:07d0e43c2d12 140 {
bobgiesberts 16:07d0e43c2d12 141 // CSB down
bobgiesberts 16:07d0e43c2d12 142 _cs_pin.write(0);
bobgiesberts 16:07d0e43c2d12 143
bobgiesberts 16:07d0e43c2d12 144 _spiport.write(address);
bobgiesberts 16:07d0e43c2d12 145 for(int i=0; i < num_bytes ; i++)
bobgiesberts 16:07d0e43c2d12 146 {
bobgiesberts 16:07d0e43c2d12 147 _spiport.write(data[i]);
bobgiesberts 16:07d0e43c2d12 148 }
bobgiesberts 16:07d0e43c2d12 149 // CSB up
bobgiesberts 16:07d0e43c2d12 150 _cs_pin.write(1);
bobgiesberts 16:07d0e43c2d12 151 }
bobgiesberts 16:07d0e43c2d12 152
bobgiesberts 16:07d0e43c2d12 153
bobgiesberts 16:07d0e43c2d12 154 // EXTRA test: Get&print values of all variables to verify (to calculate the induction)
bobgiesberts 16:07d0e43c2d12 155 // The data will be printed on the screen using RealTerm: baud 9600.
bobgiesberts 16:07d0e43c2d12 156 // Begin ***********************************************************
bobgiesberts 19:e205ab9142d8 157 float LDC1101::get_fCLKIN() {return _fCLKIN;};
bobgiesberts 19:e205ab9142d8 158 uint16_t LDC1101::get_responsetime() {return _responsetime;};
bobgiesberts 19:e205ab9142d8 159 uint16_t LDC1101::get_Rcount() {return _Rcount;};
bobgiesberts 19:e205ab9142d8 160 uint8_t LDC1101::get_divider() {return _divider;};
bobgiesberts 19:e205ab9142d8 161 float LDC1101::get_RPmin() {return _RPmin;};
bobgiesberts 19:e205ab9142d8 162 float LDC1101::get_cap() {return _cap;};
bobgiesberts 16:07d0e43c2d12 163 // END ***********************************************************