Library to communicate with LDC1614

Dependencies:   SHTx

Dependents:   Inductive_Sensor_3

Fork of LDC1101 by Bob Giesberts

Committer:
bobgiesberts
Date:
Sat Sep 10 12:46:36 2016 +0000
Revision:
32:9712c9bdaf44
Parent:
31:ab4354a71996
Child:
33:2f4c791f37b2
Read / Write functions established, good communication now! Working version to get data from the LDC1614

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bobgiesberts 30:95c53d244f91 1 /** LDC1614 library
bobgiesberts 28:76a2fc42f888 2 * @file LDC1614.cpp
bobgiesberts 16:07d0e43c2d12 3 * @brief this C++ file contains all required
bobgiesberts 16:07d0e43c2d12 4 * functions to interface with Texas
bobgiesberts 28:76a2fc42f888 5 * Instruments' LDC1614.
bobgiesberts 28:76a2fc42f888 6 *
bobgiesberts 28:76a2fc42f888 7 * @author Bob Giesberts
bobgiesberts 28:76a2fc42f888 8 *
bobgiesberts 28:76a2fc42f888 9 * @date 2016-08-09
bobgiesberts 16:07d0e43c2d12 10 *
bobgiesberts 30:95c53d244f91 11 * @code
bobgiesberts 28:76a2fc42f888 12 * Serial pc(USBTX, USBRX);
bobgiesberts 31:ab4354a71996 13 * LDC1614 ldc(PTC5, PTC4, PTC6, 16E6, 2, 120E-12);
bobgiesberts 28:76a2fc42f888 14 * int main(){
bobgiesberts 28:76a2fc42f888 15 * while(1) {
bobgiesberts 28:76a2fc42f888 16 * while( !ldc.is_ready() ) {}
bobgiesberts 28:76a2fc42f888 17 *
bobgiesberts 28:76a2fc42f888 18 * pc.printf("sensor 1: %d | sensor 2: %d\r\n", ldc.get_Data(0), ldc.get_Data(1) );
bobgiesberts 28:76a2fc42f888 19 * }
bobgiesberts 28:76a2fc42f888 20 * }
bobgiesberts 30:95c53d244f91 21 * @endcode
bobgiesberts 16:07d0e43c2d12 22 */
bobgiesberts 16:07d0e43c2d12 23
bobgiesberts 28:76a2fc42f888 24 #include "LDC1614.h"
bobgiesberts 29:41815fd13822 25 #include "mbed_debug.h"
bobgiesberts 28:76a2fc42f888 26
bobgiesberts 32:9712c9bdaf44 27 #include "i2c.hpp"
bobgiesberts 32:9712c9bdaf44 28
bobgiesberts 30:95c53d244f91 29
bobgiesberts 31:ab4354a71996 30 LDC1614::LDC1614(PinName sda, PinName scl, PinName sd, float f_CLKIN, int channels, float capacitor) : _i2c(sda, scl), _shutdown_pin(sd)
bobgiesberts 16:07d0e43c2d12 31 {
bobgiesberts 18:fc9bb81a631f 32 // settings
bobgiesberts 28:76a2fc42f888 33 _channels = channels; // number of sensors
bobgiesberts 28:76a2fc42f888 34 _cap = capacitor;
bobgiesberts 28:76a2fc42f888 35 _fCLKIN = f_CLKIN;
bobgiesberts 28:76a2fc42f888 36
bobgiesberts 32:9712c9bdaf44 37 _Offset = 0; // highest 16-bit of 32-bit number (so e.g. 100E6 / 65536 = 1525)
bobgiesberts 28:76a2fc42f888 38 _Rcount = 0xffff; // maximum for greatest precision
bobgiesberts 32:9712c9bdaf44 39 _SettleCount = 500; // CHx_SETTLECOUNT = t_settle * f_REFx/16 = 50 (p.12)
bobgiesberts 32:9712c9bdaf44 40 _DriveCurrent = 20; // max = 31, automatically settles at 20
bobgiesberts 22:8da965ce5af3 41
bobgiesberts 29:41815fd13822 42 // start communication
bobgiesberts 32:9712c9bdaf44 43 _i2c.setFrequency( 400000 ); // 400 kHz (p.6)
bobgiesberts 16:07d0e43c2d12 44
bobgiesberts 29:41815fd13822 45 // Initilialize the LDC1614
bobgiesberts 16:07d0e43c2d12 46 init();
bobgiesberts 16:07d0e43c2d12 47 }
bobgiesberts 16:07d0e43c2d12 48
bobgiesberts 28:76a2fc42f888 49 LDC1614::~LDC1614()
bobgiesberts 26:1ef9172cd355 50 {
bobgiesberts 28:76a2fc42f888 51
bobgiesberts 26:1ef9172cd355 52 }
bobgiesberts 26:1ef9172cd355 53
bobgiesberts 28:76a2fc42f888 54 void LDC1614::init()
bobgiesberts 16:07d0e43c2d12 55 {
bobgiesberts 20:8e1b1efdbb49 56 /********* SETTINGS *****************
bobgiesberts 25:ae111662ee03 57 ** C_sensor = 120 pF
bobgiesberts 25:ae111662ee03 58 ** L_sensor = 5 uH
bobgiesberts 25:ae111662ee03 59 ** Rp_min = 1500 Ohm
bobgiesberts 20:8e1b1efdbb49 60 **
bobgiesberts 25:ae111662ee03 61 ** RCount = 65535 (max)
bobgiesberts 28:76a2fc42f888 62 ** Settlecount = 50
bobgiesberts 20:8e1b1efdbb49 63 ** Samplerate = 15.3 Hz
bobgiesberts 20:8e1b1efdbb49 64 ** t_conv = 65.5 ms
bobgiesberts 20:8e1b1efdbb49 65 **
bobgiesberts 25:ae111662ee03 66 ** f_sensor_min = 6.4 MHz (d = inf)
bobgiesberts 25:ae111662ee03 67 ** f_sensor_max = 10 MHz (d = 0)
bobgiesberts 28:76a2fc42f888 68 **
bobgiesberts 28:76a2fc42f888 69 ** CHx_FIN_DIVIDER = 2 (6.4/2 = 3.2 < 16.0/4 = 4)
bobgiesberts 28:76a2fc42f888 70 ** CHx_FREF_DIVIDER = 1 (16.0 MHz)
bobgiesberts 20:8e1b1efdbb49 71 ************************************/
bobgiesberts 20:8e1b1efdbb49 72
bobgiesberts 29:41815fd13822 73 // Configuring setup, set LDC in configuration modus
bobgiesberts 29:41815fd13822 74 sleep();
bobgiesberts 29:41815fd13822 75
bobgiesberts 28:76a2fc42f888 76 for(int i = 0; i < _channels; i++)
bobgiesberts 28:76a2fc42f888 77 {
bobgiesberts 28:76a2fc42f888 78 // set Reference Count to highest resolution
bobgiesberts 28:76a2fc42f888 79 setReferenceCount( i, _Rcount );
bobgiesberts 18:fc9bb81a631f 80
bobgiesberts 28:76a2fc42f888 81 // set the settling time
bobgiesberts 28:76a2fc42f888 82 // t_settle = (settlecount * 16) /f_REF
bobgiesberts 28:76a2fc42f888 83 setSettlecount( i, _SettleCount );
bobgiesberts 28:76a2fc42f888 84
bobgiesberts 28:76a2fc42f888 85 // set Divider to 1 (for large range / ENOB / resolution)
bobgiesberts 28:76a2fc42f888 86 setDivider( i, 1, 2 ); // IN = 2 | REF = 1
bobgiesberts 28:76a2fc42f888 87
bobgiesberts 28:76a2fc42f888 88 // set the drive current during sampling
bobgiesberts 28:76a2fc42f888 89 setDriveCurrent( i, _DriveCurrent ); // (p. 15 | Figure 14)
bobgiesberts 28:76a2fc42f888 90
bobgiesberts 28:76a2fc42f888 91 // shift the signal down a bit
bobgiesberts 28:76a2fc42f888 92 setOffset( i, _Offset );
bobgiesberts 28:76a2fc42f888 93 }
bobgiesberts 16:07d0e43c2d12 94
bobgiesberts 32:9712c9bdaf44 95 // error_config
bobgiesberts 32:9712c9bdaf44 96 set( ERROR_CONFIG, UR_ERR2OUT, 1 );
bobgiesberts 32:9712c9bdaf44 97 set( ERROR_CONFIG, OR_ERR2OUT, 1 );
bobgiesberts 32:9712c9bdaf44 98 set( ERROR_CONFIG, WD_ERR2OUT, 1 );
bobgiesberts 32:9712c9bdaf44 99 // set( ERROR_CONFIG, AH_ERR2OUT, 1 );
bobgiesberts 32:9712c9bdaf44 100 // set( ERROR_CONFIG, AL_ERR2OUT, 1 );
bobgiesberts 20:8e1b1efdbb49 101
bobgiesberts 28:76a2fc42f888 102 // mux_config
bobgiesberts 32:9712c9bdaf44 103 set( MUX_CONFIG, AUTOSCAN_EN, _channels > 1 );
bobgiesberts 32:9712c9bdaf44 104 set( MUX_CONFIG, RR_SEQUENCE, ( ( _channels - 2 > 0 ) ? _channels - 2 : 0 ) );
bobgiesberts 32:9712c9bdaf44 105 set( MUX_CONFIG, DEGLITCH, DEGLITCH_10M );
bobgiesberts 18:fc9bb81a631f 106
bobgiesberts 28:76a2fc42f888 107 // override Rp and use own Drive Current to reduce power consumption
bobgiesberts 32:9712c9bdaf44 108 set( CONFIG, ACTIVE_CHAN, 0 ); // CH0. Will be overruled when _channels > 1
bobgiesberts 28:76a2fc42f888 109 set( CONFIG, RP_OVERRIDE_EN, 1 );
bobgiesberts 28:76a2fc42f888 110 set( CONFIG, SENSOR_ACTIVATE_SEL, 1 );
bobgiesberts 28:76a2fc42f888 111 set( CONFIG, AUTO_AMP_DIS, 1 );
bobgiesberts 28:76a2fc42f888 112 set( CONFIG, REF_CLK_SRC, 1 ); // external f_CLKIN
bobgiesberts 28:76a2fc42f888 113 set( CONFIG, INTB_DIS, 1 );
bobgiesberts 28:76a2fc42f888 114 set( CONFIG, HIGH_CURRENT_DRV, 0 );
bobgiesberts 28:76a2fc42f888 115
bobgiesberts 28:76a2fc42f888 116 // Done configuring settings, set LDC1614 in measuring modus
bobgiesberts 28:76a2fc42f888 117 wakeup();
bobgiesberts 16:07d0e43c2d12 118 }
bobgiesberts 16:07d0e43c2d12 119
bobgiesberts 28:76a2fc42f888 120
bobgiesberts 28:76a2fc42f888 121 void LDC1614::func_mode(LDC_MODE mode)
bobgiesberts 28:76a2fc42f888 122 {
bobgiesberts 28:76a2fc42f888 123 switch (mode)
bobgiesberts 28:76a2fc42f888 124 {
bobgiesberts 28:76a2fc42f888 125 case LDC_MODE_ACTIVE:
bobgiesberts 28:76a2fc42f888 126 case LDC_MODE_SLEEP:
bobgiesberts 31:ab4354a71996 127
bobgiesberts 29:41815fd13822 128 // turn on LDC
bobgiesberts 28:76a2fc42f888 129 _shutdown_pin.write( 0 );
bobgiesberts 32:9712c9bdaf44 130 wait_us(10);
bobgiesberts 29:41815fd13822 131 set( CONFIG, SLEEP_MODE_EN, mode );
bobgiesberts 32:9712c9bdaf44 132 wait_us(377); // Wait 16384 f_INT clock cycles (0.377 ms) (p.16)
bobgiesberts 32:9712c9bdaf44 133 wait_ms(1);
bobgiesberts 28:76a2fc42f888 134 break;
bobgiesberts 28:76a2fc42f888 135
bobgiesberts 28:76a2fc42f888 136 case LDC_MODE_SHUTDOWN:
bobgiesberts 28:76a2fc42f888 137 _shutdown_pin.write( 1 );
bobgiesberts 28:76a2fc42f888 138 break;
bobgiesberts 28:76a2fc42f888 139 }
bobgiesberts 19:e205ab9142d8 140 }
bobgiesberts 29:41815fd13822 141 void LDC1614::sleep( void ) { func_mode( LDC_MODE_SLEEP ); }
bobgiesberts 29:41815fd13822 142 void LDC1614::wakeup( void ) { func_mode( LDC_MODE_ACTIVE ); }
bobgiesberts 29:41815fd13822 143 void LDC1614::shutdown( void ) { func_mode( LDC_MODE_SHUTDOWN ); }
bobgiesberts 19:e205ab9142d8 144
bobgiesberts 28:76a2fc42f888 145 void LDC1614::setReferenceCount( uint8_t channel, uint16_t rcount )
bobgiesberts 28:76a2fc42f888 146 {
bobgiesberts 28:76a2fc42f888 147 writeI2Cregister( RCOUNT_CH0 + channel, rcount );
bobgiesberts 25:ae111662ee03 148 }
bobgiesberts 25:ae111662ee03 149
bobgiesberts 32:9712c9bdaf44 150 void LDC1614::setOffset( uint8_t channel, uint16_t offset )
bobgiesberts 19:e205ab9142d8 151 {
bobgiesberts 28:76a2fc42f888 152 _Offset = offset;
bobgiesberts 32:9712c9bdaf44 153 writeI2Cregister( OFFSET_CH0 + channel, offset );
bobgiesberts 19:e205ab9142d8 154 }
bobgiesberts 19:e205ab9142d8 155
bobgiesberts 28:76a2fc42f888 156 void LDC1614::setSettlecount( uint8_t channel, uint16_t settlecount )
bobgiesberts 17:a5cf2b4bec13 157 {
bobgiesberts 28:76a2fc42f888 158 // _t_settle = (settlecount * 16) / (_f_CLKIN / dividerREF[channel])
bobgiesberts 28:76a2fc42f888 159 writeI2Cregister( SETTLECOUNT_CH0 + channel, settlecount );
bobgiesberts 28:76a2fc42f888 160 }
bobgiesberts 28:76a2fc42f888 161
bobgiesberts 28:76a2fc42f888 162 void LDC1614::setDivider( uint8_t channel, uint8_t divIN, uint8_t divREF )
bobgiesberts 28:76a2fc42f888 163 {
bobgiesberts 28:76a2fc42f888 164 // make sure the values fit
bobgiesberts 29:41815fd13822 165 _dividerIN = (( divIN < 15) ? (( divIN > 1) ? divIN : 1) : 15 ); // 4 bit
bobgiesberts 29:41815fd13822 166 _dividerIN = ((divREF < 255) ? ((divREF > 1) ? divREF : 1) : 255 ); // 8 bit
bobgiesberts 28:76a2fc42f888 167 writeI2Cregister( CLOCK_DIVIDERS_CH0 + channel, uint16_t ((_dividerIN << 12) + _dividerREF) );
bobgiesberts 20:8e1b1efdbb49 168 }
bobgiesberts 20:8e1b1efdbb49 169
bobgiesberts 28:76a2fc42f888 170 void LDC1614::setDriveCurrent( uint8_t channel, uint8_t idrive )
bobgiesberts 28:76a2fc42f888 171 {
bobgiesberts 29:41815fd13822 172 _DriveCurrent = ((idrive < 31) ? idrive : 31 ); // 5-bit (b1 1111)
bobgiesberts 29:41815fd13822 173
bobgiesberts 28:76a2fc42f888 174 // todo: read initial idrive [10:6]
bobgiesberts 28:76a2fc42f888 175
bobgiesberts 28:76a2fc42f888 176 writeI2Cregister(DRIVE_CURRENT_CH0 + channel, uint16_t (_DriveCurrent<<10) );
bobgiesberts 25:ae111662ee03 177 }
bobgiesberts 25:ae111662ee03 178
bobgiesberts 28:76a2fc42f888 179 void LDC1614::set( ADDR addr, SETTING setting, uint8_t value )
bobgiesberts 20:8e1b1efdbb49 180 {
bobgiesberts 28:76a2fc42f888 181 uint8_t mask = 1;
bobgiesberts 28:76a2fc42f888 182 if ( addr == MUX_CONFIG )
bobgiesberts 28:76a2fc42f888 183 {
bobgiesberts 28:76a2fc42f888 184 switch (setting){
bobgiesberts 28:76a2fc42f888 185 case AUTOSCAN_EN: mask = 1; break; // 1
bobgiesberts 28:76a2fc42f888 186 case RR_SEQUENCE: mask = 3; break; // 11
bobgiesberts 28:76a2fc42f888 187 case DEGLITCH: mask = 7; break; // 111
bobgiesberts 28:76a2fc42f888 188 }
bobgiesberts 28:76a2fc42f888 189 }
bobgiesberts 28:76a2fc42f888 190 regchange( addr, setting, value, mask );
bobgiesberts 20:8e1b1efdbb49 191 }
bobgiesberts 20:8e1b1efdbb49 192
bobgiesberts 29:41815fd13822 193 uint8_t LDC1614::get( ADDR addr, SETTING setting, uint8_t mask )
bobgiesberts 29:41815fd13822 194 {
bobgiesberts 29:41815fd13822 195 if ( addr == MUX_CONFIG )
bobgiesberts 29:41815fd13822 196 {
bobgiesberts 29:41815fd13822 197 switch (setting){
bobgiesberts 29:41815fd13822 198 case AUTOSCAN_EN: mask = 1; break; // 1
bobgiesberts 29:41815fd13822 199 case RR_SEQUENCE: mask = 3; break; // 11
bobgiesberts 29:41815fd13822 200 case DEGLITCH: mask = 7; break; // 111
bobgiesberts 29:41815fd13822 201 }
bobgiesberts 29:41815fd13822 202 }
bobgiesberts 29:41815fd13822 203
bobgiesberts 29:41815fd13822 204 uint16_t data[1];
bobgiesberts 29:41815fd13822 205 readI2C( data, addr );
bobgiesberts 29:41815fd13822 206 return ( data[0]>>setting ) & mask;
bobgiesberts 29:41815fd13822 207 }
bobgiesberts 29:41815fd13822 208
bobgiesberts 32:9712c9bdaf44 209 uint16_t LDC1614::get_config()
bobgiesberts 32:9712c9bdaf44 210 {
bobgiesberts 32:9712c9bdaf44 211 uint16_t data[1];
bobgiesberts 32:9712c9bdaf44 212 readI2C( data, CONFIG );
bobgiesberts 32:9712c9bdaf44 213 return data[0];
bobgiesberts 32:9712c9bdaf44 214 }
bobgiesberts 28:76a2fc42f888 215
bobgiesberts 32:9712c9bdaf44 216 uint16_t LDC1614::get_error_config()
bobgiesberts 32:9712c9bdaf44 217 {
bobgiesberts 32:9712c9bdaf44 218 uint16_t data[1];
bobgiesberts 32:9712c9bdaf44 219 readI2C( data, ERROR_CONFIG );
bobgiesberts 32:9712c9bdaf44 220 return data[0];
bobgiesberts 32:9712c9bdaf44 221 }
bobgiesberts 28:76a2fc42f888 222
bobgiesberts 28:76a2fc42f888 223
bobgiesberts 28:76a2fc42f888 224 /* GETTING DATA FROM SENSOR */
bobgiesberts 28:76a2fc42f888 225
bobgiesberts 28:76a2fc42f888 226 uint16_t LDC1614::get_status( void )
bobgiesberts 20:8e1b1efdbb49 227 {
bobgiesberts 28:76a2fc42f888 228 uint16_t status[1];
bobgiesberts 28:76a2fc42f888 229 readI2C( status, STATUS );
bobgiesberts 28:76a2fc42f888 230 return status[0];
bobgiesberts 20:8e1b1efdbb49 231 }
bobgiesberts 32:9712c9bdaf44 232 bool LDC1614::is_ready( uint8_t channel )
bobgiesberts 29:41815fd13822 233 {
bobgiesberts 32:9712c9bdaf44 234 uint8_t status = get_status();
bobgiesberts 32:9712c9bdaf44 235 if( channel < 4 )
bobgiesberts 32:9712c9bdaf44 236 {
bobgiesberts 32:9712c9bdaf44 237 return ( status>>(3-channel)) & 1; // this specific channel is ready
bobgiesberts 32:9712c9bdaf44 238 }else{
bobgiesberts 32:9712c9bdaf44 239 return ( status>>DRDY ) & 1; // all channels are ready
bobgiesberts 32:9712c9bdaf44 240 }
bobgiesberts 29:41815fd13822 241 }
bobgiesberts 29:41815fd13822 242 bool LDC1614::is_error( uint8_t status )
bobgiesberts 29:41815fd13822 243 {
bobgiesberts 29:41815fd13822 244 if( status == 17 ) { status = get_status(); }
bobgiesberts 32:9712c9bdaf44 245 return ((( status>>ERR_ZC ) & 7) != 0);
bobgiesberts 29:41815fd13822 246 }
bobgiesberts 32:9712c9bdaf44 247 uint8_t LDC1614::what_error( uint8_t channel )
bobgiesberts 32:9712c9bdaf44 248 {
bobgiesberts 32:9712c9bdaf44 249 uint8_t status = get_status();
bobgiesberts 32:9712c9bdaf44 250 if ( ( ( status>>ERR_CHAN ) & 2 ) == channel )
bobgiesberts 32:9712c9bdaf44 251 {
bobgiesberts 32:9712c9bdaf44 252 if ((( status>>ERR_AHE ) & 1) != 0) return 1; // Amplitude High Error
bobgiesberts 32:9712c9bdaf44 253 if ((( status>>ERR_ALE ) & 1) != 0) return 2; // Amplitide Low Error
bobgiesberts 32:9712c9bdaf44 254 if ((( status>>ERR_ZC ) & 1) != 0) return 3; // Zero Count Error
bobgiesberts 32:9712c9bdaf44 255 }
bobgiesberts 32:9712c9bdaf44 256 return 0; // no error?
bobgiesberts 32:9712c9bdaf44 257 }
bobgiesberts 17:a5cf2b4bec13 258
bobgiesberts 28:76a2fc42f888 259 uint16_t LDC1614::get_Rcount( uint8_t channel )
bobgiesberts 16:07d0e43c2d12 260 {
bobgiesberts 28:76a2fc42f888 261 uint16_t rcount[1];
bobgiesberts 28:76a2fc42f888 262 readI2C( rcount, RCOUNT_CH0 + channel );
bobgiesberts 28:76a2fc42f888 263 return rcount[0];
bobgiesberts 20:8e1b1efdbb49 264 }
bobgiesberts 20:8e1b1efdbb49 265
bobgiesberts 28:76a2fc42f888 266 uint32_t LDC1614::get_Data( uint8_t channel )
bobgiesberts 22:8da965ce5af3 267 {
bobgiesberts 28:76a2fc42f888 268 uint16_t data[2];
bobgiesberts 30:95c53d244f91 269 readI2C( data, DATA_MSB_CH0 + channel, 2 );
bobgiesberts 29:41815fd13822 270
bobgiesberts 32:9712c9bdaf44 271 if( ((data[0]>>CHx_ERR_UR) & 1) == 1 ) { debug( "Sensor %d: Under-range Error\r\n", channel ); }
bobgiesberts 32:9712c9bdaf44 272 if( ((data[0]>>CHx_ERR_OR) & 1) == 1 ) { debug( "Sensor %d: Over-range Error\r\n", channel ); }
bobgiesberts 32:9712c9bdaf44 273 if( ((data[0]>>CHx_ERR_WD) & 1) == 1 ) { debug( "Sensor %d: Watchdog Timeout Error\r\n", channel ); }
bobgiesberts 32:9712c9bdaf44 274 if( ((data[0]>>CHx_ERR_AE) & 1) == 1 ) { debug( "Sensor %d: Amplitude Error\r\n", channel ); }
bobgiesberts 29:41815fd13822 275
bobgiesberts 30:95c53d244f91 276 return ( (data[0] & 0x0fff)<<16 ) | data[1]; // MSB + LSB
bobgiesberts 22:8da965ce5af3 277 }
bobgiesberts 20:8e1b1efdbb49 278
bobgiesberts 32:9712c9bdaf44 279 uint16_t LDC1614::get_ID( void )
bobgiesberts 32:9712c9bdaf44 280 {
bobgiesberts 32:9712c9bdaf44 281 uint16_t ID[1];
bobgiesberts 32:9712c9bdaf44 282 readI2C( ID, DEVICE_ID, 1 );
bobgiesberts 32:9712c9bdaf44 283 return ID[0];
bobgiesberts 32:9712c9bdaf44 284
bobgiesberts 32:9712c9bdaf44 285 }
bobgiesberts 32:9712c9bdaf44 286
bobgiesberts 32:9712c9bdaf44 287 uint16_t LDC1614::get_manufacturer_ID( void )
bobgiesberts 32:9712c9bdaf44 288 {
bobgiesberts 32:9712c9bdaf44 289 // uint16_t ID[1];
bobgiesberts 32:9712c9bdaf44 290 // readI2C( ID, MANUFACTURER_ID, 1 );
bobgiesberts 32:9712c9bdaf44 291 // return ID[0];
bobgiesberts 32:9712c9bdaf44 292
bobgiesberts 32:9712c9bdaf44 293 _i2c.start();
bobgiesberts 32:9712c9bdaf44 294
bobgiesberts 32:9712c9bdaf44 295 // Write address + 0 (write)
bobgiesberts 32:9712c9bdaf44 296 if( _i2c.write( 0x2A << 1 ) == true ){ // NACK = true
bobgiesberts 32:9712c9bdaf44 297 _i2c.stop();
bobgiesberts 32:9712c9bdaf44 298 return 1;
bobgiesberts 32:9712c9bdaf44 299 }
bobgiesberts 32:9712c9bdaf44 300
bobgiesberts 32:9712c9bdaf44 301 // Write register address
bobgiesberts 32:9712c9bdaf44 302 if ( _i2c.write( 0x7E ) == true ) { // NACK = true
bobgiesberts 32:9712c9bdaf44 303 _i2c.stop();
bobgiesberts 32:9712c9bdaf44 304 return 2;
bobgiesberts 32:9712c9bdaf44 305 }
bobgiesberts 32:9712c9bdaf44 306
bobgiesberts 32:9712c9bdaf44 307 _i2c.start();
bobgiesberts 32:9712c9bdaf44 308
bobgiesberts 32:9712c9bdaf44 309 // Write address + 1 (read)
bobgiesberts 32:9712c9bdaf44 310 if ( _i2c.write( (0x2A << 1) | 0x01 ) == true ) { // NACK = true
bobgiesberts 32:9712c9bdaf44 311 _i2c.stop();
bobgiesberts 32:9712c9bdaf44 312 return 3;
bobgiesberts 32:9712c9bdaf44 313 }
bobgiesberts 32:9712c9bdaf44 314
bobgiesberts 32:9712c9bdaf44 315 uint16_t data;
bobgiesberts 32:9712c9bdaf44 316 data = _i2c.read(1) << 8; // ACK
bobgiesberts 32:9712c9bdaf44 317 data |= _i2c.read(0); // NACK
bobgiesberts 32:9712c9bdaf44 318 _i2c.stop();
bobgiesberts 32:9712c9bdaf44 319 return data;
bobgiesberts 32:9712c9bdaf44 320
bobgiesberts 32:9712c9bdaf44 321 //return _i2c.read(1) << 8; // MSB
bobgiesberts 32:9712c9bdaf44 322 //return data[i] |= _i2c.read(0); // LSB
bobgiesberts 32:9712c9bdaf44 323 //_i2c.stop();
bobgiesberts 28:76a2fc42f888 324
bobgiesberts 28:76a2fc42f888 325
bobgiesberts 32:9712c9bdaf44 326 }
bobgiesberts 30:95c53d244f91 327
bobgiesberts 30:95c53d244f91 328
bobgiesberts 29:41815fd13822 329 /* REGISTER FUNCTIONS (READ / WRITE) */
bobgiesberts 28:76a2fc42f888 330
bobgiesberts 28:76a2fc42f888 331 void LDC1614::readI2C( uint16_t *data, uint8_t address, uint8_t length )
bobgiesberts 22:8da965ce5af3 332 {
bobgiesberts 32:9712c9bdaf44 333 for( int i = 0; i < length; i++ )
bobgiesberts 32:9712c9bdaf44 334 {
bobgiesberts 32:9712c9bdaf44 335 _i2c.start();
bobgiesberts 32:9712c9bdaf44 336 _i2c.write( ( 0x2A << 1 ) | 0 ); // 7 bit 0x2A + 0 (write) = 0x55
bobgiesberts 32:9712c9bdaf44 337 _i2c.write( address );
bobgiesberts 32:9712c9bdaf44 338
bobgiesberts 32:9712c9bdaf44 339 _i2c.start();
bobgiesberts 32:9712c9bdaf44 340 _i2c.write( ( 0x2A << 1 ) | 1 ); // 7 bit 0x2A + 1 (read) = 0x55
bobgiesberts 32:9712c9bdaf44 341
bobgiesberts 32:9712c9bdaf44 342 data[i] = _i2c.read(1) << 8; // MSB
bobgiesberts 32:9712c9bdaf44 343 data[i] |= _i2c.read(0); // LSB
bobgiesberts 32:9712c9bdaf44 344 _i2c.stop();
bobgiesberts 32:9712c9bdaf44 345 }
bobgiesberts 22:8da965ce5af3 346 }
bobgiesberts 20:8e1b1efdbb49 347
bobgiesberts 28:76a2fc42f888 348 void LDC1614::writeI2C( uint16_t *data, uint8_t address, uint8_t length )
bobgiesberts 22:8da965ce5af3 349 {
bobgiesberts 32:9712c9bdaf44 350
bobgiesberts 28:76a2fc42f888 351 for ( int i = 0; i < length; i++ )
bobgiesberts 28:76a2fc42f888 352 {
bobgiesberts 32:9712c9bdaf44 353 _i2c.start();
bobgiesberts 32:9712c9bdaf44 354 _i2c.write( ( 0x2A << 1 ) | 0 ); // 7 bit 0x2A + 0 (write) = 0x54
bobgiesberts 32:9712c9bdaf44 355 _i2c.write( address + i );
bobgiesberts 32:9712c9bdaf44 356
bobgiesberts 32:9712c9bdaf44 357 _i2c.write( ( data[i] & 0xff00 ) >> 8 ); // MSB
bobgiesberts 32:9712c9bdaf44 358 _i2c.write( ( data[i] & 0x00ff ) >> 0 ); // LSB
bobgiesberts 32:9712c9bdaf44 359 _i2c.stop();
bobgiesberts 28:76a2fc42f888 360 }
bobgiesberts 32:9712c9bdaf44 361
bobgiesberts 32:9712c9bdaf44 362
bobgiesberts 22:8da965ce5af3 363 }
bobgiesberts 20:8e1b1efdbb49 364
bobgiesberts 28:76a2fc42f888 365 void LDC1614::writeI2Cregister(uint8_t reg, uint16_t value)
bobgiesberts 28:76a2fc42f888 366 {
bobgiesberts 28:76a2fc42f888 367 writeI2C( &value, reg );
bobgiesberts 28:76a2fc42f888 368 }
bobgiesberts 28:76a2fc42f888 369
bobgiesberts 28:76a2fc42f888 370 void LDC1614::regchange( uint8_t addr, uint8_t setting, uint8_t value, uint8_t mask )
bobgiesberts 28:76a2fc42f888 371 {
bobgiesberts 28:76a2fc42f888 372 uint16_t config[1];
bobgiesberts 28:76a2fc42f888 373 readI2C( config, addr );
bobgiesberts 32:9712c9bdaf44 374 writeI2Cregister( addr, uint16_t ( (config[0] & ~(mask<<setting)) | (value<<setting)) ); // replace bits with number SETTING
bobgiesberts 28:76a2fc42f888 375 }
bobgiesberts 28:76a2fc42f888 376
bobgiesberts 28:76a2fc42f888 377
bobgiesberts 25:ae111662ee03 378
bobgiesberts 25:ae111662ee03 379 /* CALCULATE STUFF WITH SENSOR DATA */
bobgiesberts 25:ae111662ee03 380
bobgiesberts 28:76a2fc42f888 381 float LDC1614::get_fsensor( uint32_t LData )
bobgiesberts 28:76a2fc42f888 382 {
bobgiesberts 32:9712c9bdaf44 383 _fsensor = _dividerIN * (_fCLKIN/_dividerREF) * ((LData / 268435456) + _Offset); // (p.14)
bobgiesberts 18:fc9bb81a631f 384 return _fsensor;
bobgiesberts 19:e205ab9142d8 385 }
bobgiesberts 18:fc9bb81a631f 386
bobgiesberts 28:76a2fc42f888 387 float LDC1614::get_Inductance( uint32_t Ldata )
bobgiesberts 18:fc9bb81a631f 388 {
bobgiesberts 25:ae111662ee03 389 float fsensor = get_fsensor( Ldata );
bobgiesberts 28:76a2fc42f888 390 _inductance = 1.0 / (_cap * 4 * PI*PI * fsensor*fsensor ); // ???
bobgiesberts 19:e205ab9142d8 391 return _inductance;
bobgiesberts 19:e205ab9142d8 392 }
bobgiesberts 16:07d0e43c2d12 393
bobgiesberts 16:07d0e43c2d12 394
bobgiesberts 16:07d0e43c2d12 395
bobgiesberts 16:07d0e43c2d12 396 // EXTRA test: Get&print values of all variables to verify (to calculate the induction)
bobgiesberts 16:07d0e43c2d12 397 // The data will be printed on the screen using RealTerm: baud 9600.
bobgiesberts 16:07d0e43c2d12 398 // Begin ***********************************************************
bobgiesberts 28:76a2fc42f888 399 float LDC1614::get_fCLKIN() {return _fCLKIN;}
bobgiesberts 28:76a2fc42f888 400 uint8_t LDC1614::get_dividerIN() {return _dividerIN;}
bobgiesberts 28:76a2fc42f888 401 uint8_t LDC1614::get_dividerREF() {return _dividerREF;}
bobgiesberts 32:9712c9bdaf44 402 uint16_t LDC1614::get_Offset() {return _Offset;}
bobgiesberts 28:76a2fc42f888 403 float LDC1614::get_cap() {return _cap;}
bobgiesberts 32:9712c9bdaf44 404 // END ***********************************************************