Library to communicate with LDC1614
Dependents: Inductive_Sensor_3
Fork of LDC1101 by
LDC1614.cpp@29:41815fd13822, 2016-08-24 (annotated)
- Committer:
- bobgiesberts
- Date:
- Wed Aug 24 07:42:49 2016 +0000
- Revision:
- 29:41815fd13822
- Parent:
- 28:76a2fc42f888
- Child:
- 30:95c53d244f91
Added functions to access status; - set; - get
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bobgiesberts | 16:07d0e43c2d12 | 1 | /** |
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 | 28:76a2fc42f888 | 11 | * @example |
bobgiesberts | 28:76a2fc42f888 | 12 | * Serial pc(USBTX, USBRX); |
bobgiesberts | 28:76a2fc42f888 | 13 | * LDC1614 ldc(PTC6, PTC7, PTC5, 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 | 16:07d0e43c2d12 | 21 | */ |
bobgiesberts | 16:07d0e43c2d12 | 22 | |
bobgiesberts | 28:76a2fc42f888 | 23 | #include "LDC1614.h" |
bobgiesberts | 29:41815fd13822 | 24 | #include "mbed_debug.h" |
bobgiesberts | 28:76a2fc42f888 | 25 | |
bobgiesberts | 29:41815fd13822 | 26 | LDC1614::LDC1614(PinName sda, PinName scl, PinName sd, PinName os, float f_CLKIN, int channels, float capacitor) : _i2c(sda, scl), _shutdown_pin(sd), _oscillator(os) |
bobgiesberts | 16:07d0e43c2d12 | 27 | { |
bobgiesberts | 18:fc9bb81a631f | 28 | // settings |
bobgiesberts | 28:76a2fc42f888 | 29 | _channels = channels; // number of sensors |
bobgiesberts | 28:76a2fc42f888 | 30 | _cap = capacitor; |
bobgiesberts | 28:76a2fc42f888 | 31 | _fCLKIN = f_CLKIN; |
bobgiesberts | 28:76a2fc42f888 | 32 | |
bobgiesberts | 28:76a2fc42f888 | 33 | _Offset = 0; // no offset needed |
bobgiesberts | 28:76a2fc42f888 | 34 | _Rcount = 0xffff; // maximum for greatest precision |
bobgiesberts | 28:76a2fc42f888 | 35 | _SettleCount = 50; // CHx_SETTLECOUNT = t_settle * f_REFx/16 = 50 (p.12) |
bobgiesberts | 28:76a2fc42f888 | 36 | _DriveCurrent = 31; // max??? |
bobgiesberts | 22:8da965ce5af3 | 37 | |
bobgiesberts | 29:41815fd13822 | 38 | // start communication |
bobgiesberts | 28:76a2fc42f888 | 39 | _i2c.frequency(400000); // 400 kHz (p.6) |
bobgiesberts | 16:07d0e43c2d12 | 40 | |
bobgiesberts | 29:41815fd13822 | 41 | // Initilialize the LDC1614 |
bobgiesberts | 16:07d0e43c2d12 | 42 | init(); |
bobgiesberts | 16:07d0e43c2d12 | 43 | } |
bobgiesberts | 16:07d0e43c2d12 | 44 | |
bobgiesberts | 28:76a2fc42f888 | 45 | LDC1614::~LDC1614() |
bobgiesberts | 26:1ef9172cd355 | 46 | { |
bobgiesberts | 28:76a2fc42f888 | 47 | |
bobgiesberts | 26:1ef9172cd355 | 48 | } |
bobgiesberts | 26:1ef9172cd355 | 49 | |
bobgiesberts | 28:76a2fc42f888 | 50 | void LDC1614::init() |
bobgiesberts | 16:07d0e43c2d12 | 51 | { |
bobgiesberts | 20:8e1b1efdbb49 | 52 | /********* SETTINGS ***************** |
bobgiesberts | 25:ae111662ee03 | 53 | ** C_sensor = 120 pF |
bobgiesberts | 25:ae111662ee03 | 54 | ** L_sensor = 5 uH |
bobgiesberts | 25:ae111662ee03 | 55 | ** Rp_min = 1500 Ohm |
bobgiesberts | 20:8e1b1efdbb49 | 56 | ** |
bobgiesberts | 25:ae111662ee03 | 57 | ** RCount = 65535 (max) |
bobgiesberts | 28:76a2fc42f888 | 58 | ** Settlecount = 50 |
bobgiesberts | 20:8e1b1efdbb49 | 59 | ** Samplerate = 15.3 Hz |
bobgiesberts | 20:8e1b1efdbb49 | 60 | ** t_conv = 65.5 ms |
bobgiesberts | 20:8e1b1efdbb49 | 61 | ** |
bobgiesberts | 25:ae111662ee03 | 62 | ** f_sensor_min = 6.4 MHz (d = inf) |
bobgiesberts | 25:ae111662ee03 | 63 | ** f_sensor_max = 10 MHz (d = 0) |
bobgiesberts | 28:76a2fc42f888 | 64 | ** |
bobgiesberts | 28:76a2fc42f888 | 65 | ** CHx_FIN_DIVIDER = 2 (6.4/2 = 3.2 < 16.0/4 = 4) |
bobgiesberts | 28:76a2fc42f888 | 66 | ** CHx_FREF_DIVIDER = 1 (16.0 MHz) |
bobgiesberts | 20:8e1b1efdbb49 | 67 | ************************************/ |
bobgiesberts | 20:8e1b1efdbb49 | 68 | |
bobgiesberts | 29:41815fd13822 | 69 | // Configuring setup, set LDC in configuration modus |
bobgiesberts | 29:41815fd13822 | 70 | sleep(); |
bobgiesberts | 29:41815fd13822 | 71 | |
bobgiesberts | 28:76a2fc42f888 | 72 | for(int i = 0; i < _channels; i++) |
bobgiesberts | 28:76a2fc42f888 | 73 | { |
bobgiesberts | 28:76a2fc42f888 | 74 | // set Reference Count to highest resolution |
bobgiesberts | 28:76a2fc42f888 | 75 | setReferenceCount( i, _Rcount ); |
bobgiesberts | 18:fc9bb81a631f | 76 | |
bobgiesberts | 28:76a2fc42f888 | 77 | // set the settling time |
bobgiesberts | 28:76a2fc42f888 | 78 | // t_settle = (settlecount * 16) /f_REF |
bobgiesberts | 28:76a2fc42f888 | 79 | setSettlecount( i, _SettleCount ); |
bobgiesberts | 28:76a2fc42f888 | 80 | |
bobgiesberts | 28:76a2fc42f888 | 81 | // set Divider to 1 (for large range / ENOB / resolution) |
bobgiesberts | 28:76a2fc42f888 | 82 | setDivider( i, 1, 2 ); // IN = 2 | REF = 1 |
bobgiesberts | 28:76a2fc42f888 | 83 | |
bobgiesberts | 28:76a2fc42f888 | 84 | // set the drive current during sampling |
bobgiesberts | 28:76a2fc42f888 | 85 | setDriveCurrent( i, _DriveCurrent ); // (p. 15 | Figure 14) |
bobgiesberts | 28:76a2fc42f888 | 86 | |
bobgiesberts | 28:76a2fc42f888 | 87 | // shift the signal down a bit |
bobgiesberts | 28:76a2fc42f888 | 88 | setOffset( i, _Offset ); |
bobgiesberts | 28:76a2fc42f888 | 89 | } |
bobgiesberts | 16:07d0e43c2d12 | 90 | |
bobgiesberts | 28:76a2fc42f888 | 91 | // error_config (all is standard) |
bobgiesberts | 20:8e1b1efdbb49 | 92 | |
bobgiesberts | 28:76a2fc42f888 | 93 | // mux_config |
bobgiesberts | 28:76a2fc42f888 | 94 | set( MUX_CONFIG, AUTOSCAN_EN, _channels > 1 ); |
bobgiesberts | 29:41815fd13822 | 95 | set( MUX_CONFIG, RR_SEQUENCE, ( ( _channels - 2 > 0 ) ? _channels - 2 : 0 ) ); |
bobgiesberts | 28:76a2fc42f888 | 96 | set( MUX_CONFIG, DEGLITCH, DEGLITCH_10M ); |
bobgiesberts | 18:fc9bb81a631f | 97 | |
bobgiesberts | 28:76a2fc42f888 | 98 | // override Rp and use own Drive Current to reduce power consumption |
bobgiesberts | 28:76a2fc42f888 | 99 | set( CONFIG, RP_OVERRIDE_EN, 1 ); |
bobgiesberts | 28:76a2fc42f888 | 100 | set( CONFIG, SENSOR_ACTIVATE_SEL, 1 ); |
bobgiesberts | 28:76a2fc42f888 | 101 | set( CONFIG, AUTO_AMP_DIS, 1 ); |
bobgiesberts | 28:76a2fc42f888 | 102 | set( CONFIG, REF_CLK_SRC, 1 ); // external f_CLKIN |
bobgiesberts | 28:76a2fc42f888 | 103 | set( CONFIG, INTB_DIS, 1 ); |
bobgiesberts | 28:76a2fc42f888 | 104 | set( CONFIG, HIGH_CURRENT_DRV, 0 ); |
bobgiesberts | 28:76a2fc42f888 | 105 | |
bobgiesberts | 28:76a2fc42f888 | 106 | // Done configuring settings, set LDC1614 in measuring modus |
bobgiesberts | 28:76a2fc42f888 | 107 | wakeup(); |
bobgiesberts | 16:07d0e43c2d12 | 108 | } |
bobgiesberts | 16:07d0e43c2d12 | 109 | |
bobgiesberts | 28:76a2fc42f888 | 110 | |
bobgiesberts | 28:76a2fc42f888 | 111 | void LDC1614::func_mode(LDC_MODE mode) |
bobgiesberts | 28:76a2fc42f888 | 112 | { |
bobgiesberts | 28:76a2fc42f888 | 113 | switch (mode) |
bobgiesberts | 28:76a2fc42f888 | 114 | { |
bobgiesberts | 28:76a2fc42f888 | 115 | case LDC_MODE_ACTIVE: |
bobgiesberts | 28:76a2fc42f888 | 116 | case LDC_MODE_SLEEP: |
bobgiesberts | 29:41815fd13822 | 117 | |
bobgiesberts | 29:41815fd13822 | 118 | // turn on oscillator |
bobgiesberts | 29:41815fd13822 | 119 | _oscillator.write( 1 ); |
bobgiesberts | 29:41815fd13822 | 120 | wait_ms(3); // datasheet oscillator says 3 ms max |
bobgiesberts | 29:41815fd13822 | 121 | |
bobgiesberts | 29:41815fd13822 | 122 | // turn on LDC |
bobgiesberts | 28:76a2fc42f888 | 123 | _shutdown_pin.write( 0 ); |
bobgiesberts | 29:41815fd13822 | 124 | set( CONFIG, SLEEP_MODE_EN, mode ); |
bobgiesberts | 29:41815fd13822 | 125 | wait_us(400); // Wait 16384 f_INT clock cycles (0.377 ms) (p.16) |
bobgiesberts | 28:76a2fc42f888 | 126 | break; |
bobgiesberts | 28:76a2fc42f888 | 127 | |
bobgiesberts | 28:76a2fc42f888 | 128 | case LDC_MODE_SHUTDOWN: |
bobgiesberts | 28:76a2fc42f888 | 129 | _shutdown_pin.write( 1 ); |
bobgiesberts | 29:41815fd13822 | 130 | _oscillator.write( 0 ); |
bobgiesberts | 28:76a2fc42f888 | 131 | break; |
bobgiesberts | 28:76a2fc42f888 | 132 | } |
bobgiesberts | 19:e205ab9142d8 | 133 | } |
bobgiesberts | 29:41815fd13822 | 134 | void LDC1614::sleep( void ) { func_mode( LDC_MODE_SLEEP ); } |
bobgiesberts | 29:41815fd13822 | 135 | void LDC1614::wakeup( void ) { func_mode( LDC_MODE_ACTIVE ); } |
bobgiesberts | 29:41815fd13822 | 136 | void LDC1614::shutdown( void ) { func_mode( LDC_MODE_SHUTDOWN ); } |
bobgiesberts | 19:e205ab9142d8 | 137 | |
bobgiesberts | 28:76a2fc42f888 | 138 | void LDC1614::setReferenceCount( uint8_t channel, uint16_t rcount ) |
bobgiesberts | 28:76a2fc42f888 | 139 | { |
bobgiesberts | 28:76a2fc42f888 | 140 | writeI2Cregister( RCOUNT_CH0 + channel, rcount ); |
bobgiesberts | 25:ae111662ee03 | 141 | } |
bobgiesberts | 25:ae111662ee03 | 142 | |
bobgiesberts | 28:76a2fc42f888 | 143 | void LDC1614::setOffset( uint8_t channel, uint32_t offset ) |
bobgiesberts | 19:e205ab9142d8 | 144 | { |
bobgiesberts | 28:76a2fc42f888 | 145 | _Offset = offset; |
bobgiesberts | 28:76a2fc42f888 | 146 | writeI2Cregister( OFFSET_CH0 + channel, uint16_t (offset >> 16) ); |
bobgiesberts | 19:e205ab9142d8 | 147 | } |
bobgiesberts | 19:e205ab9142d8 | 148 | |
bobgiesberts | 28:76a2fc42f888 | 149 | void LDC1614::setSettlecount( uint8_t channel, uint16_t settlecount ) |
bobgiesberts | 17:a5cf2b4bec13 | 150 | { |
bobgiesberts | 28:76a2fc42f888 | 151 | // _t_settle = (settlecount * 16) / (_f_CLKIN / dividerREF[channel]) |
bobgiesberts | 28:76a2fc42f888 | 152 | writeI2Cregister( SETTLECOUNT_CH0 + channel, settlecount ); |
bobgiesberts | 28:76a2fc42f888 | 153 | } |
bobgiesberts | 28:76a2fc42f888 | 154 | |
bobgiesberts | 28:76a2fc42f888 | 155 | void LDC1614::setDivider( uint8_t channel, uint8_t divIN, uint8_t divREF ) |
bobgiesberts | 28:76a2fc42f888 | 156 | { |
bobgiesberts | 28:76a2fc42f888 | 157 | // make sure the values fit |
bobgiesberts | 29:41815fd13822 | 158 | _dividerIN = (( divIN < 15) ? (( divIN > 1) ? divIN : 1) : 15 ); // 4 bit |
bobgiesberts | 29:41815fd13822 | 159 | _dividerIN = ((divREF < 255) ? ((divREF > 1) ? divREF : 1) : 255 ); // 8 bit |
bobgiesberts | 28:76a2fc42f888 | 160 | writeI2Cregister( CLOCK_DIVIDERS_CH0 + channel, uint16_t ((_dividerIN << 12) + _dividerREF) ); |
bobgiesberts | 20:8e1b1efdbb49 | 161 | } |
bobgiesberts | 20:8e1b1efdbb49 | 162 | |
bobgiesberts | 28:76a2fc42f888 | 163 | void LDC1614::setDriveCurrent( uint8_t channel, uint8_t idrive ) |
bobgiesberts | 28:76a2fc42f888 | 164 | { |
bobgiesberts | 29:41815fd13822 | 165 | _DriveCurrent = ((idrive < 31) ? idrive : 31 ); // 5-bit (b1 1111) |
bobgiesberts | 29:41815fd13822 | 166 | |
bobgiesberts | 28:76a2fc42f888 | 167 | // todo: read initial idrive [10:6] |
bobgiesberts | 28:76a2fc42f888 | 168 | |
bobgiesberts | 28:76a2fc42f888 | 169 | writeI2Cregister(DRIVE_CURRENT_CH0 + channel, uint16_t (_DriveCurrent<<10) ); |
bobgiesberts | 25:ae111662ee03 | 170 | } |
bobgiesberts | 25:ae111662ee03 | 171 | |
bobgiesberts | 28:76a2fc42f888 | 172 | void LDC1614::set( ADDR addr, SETTING setting, uint8_t value ) |
bobgiesberts | 20:8e1b1efdbb49 | 173 | { |
bobgiesberts | 28:76a2fc42f888 | 174 | uint8_t mask = 1; |
bobgiesberts | 28:76a2fc42f888 | 175 | if ( addr == MUX_CONFIG ) |
bobgiesberts | 28:76a2fc42f888 | 176 | { |
bobgiesberts | 28:76a2fc42f888 | 177 | switch (setting){ |
bobgiesberts | 28:76a2fc42f888 | 178 | case AUTOSCAN_EN: mask = 1; break; // 1 |
bobgiesberts | 28:76a2fc42f888 | 179 | case RR_SEQUENCE: mask = 3; break; // 11 |
bobgiesberts | 28:76a2fc42f888 | 180 | case DEGLITCH: mask = 7; break; // 111 |
bobgiesberts | 28:76a2fc42f888 | 181 | } |
bobgiesberts | 28:76a2fc42f888 | 182 | } |
bobgiesberts | 28:76a2fc42f888 | 183 | regchange( addr, setting, value, mask ); |
bobgiesberts | 20:8e1b1efdbb49 | 184 | } |
bobgiesberts | 20:8e1b1efdbb49 | 185 | |
bobgiesberts | 29:41815fd13822 | 186 | uint8_t LDC1614::get( ADDR addr, SETTING setting, uint8_t mask ) |
bobgiesberts | 29:41815fd13822 | 187 | { |
bobgiesberts | 29:41815fd13822 | 188 | if ( addr == MUX_CONFIG ) |
bobgiesberts | 29:41815fd13822 | 189 | { |
bobgiesberts | 29:41815fd13822 | 190 | switch (setting){ |
bobgiesberts | 29:41815fd13822 | 191 | case AUTOSCAN_EN: mask = 1; break; // 1 |
bobgiesberts | 29:41815fd13822 | 192 | case RR_SEQUENCE: mask = 3; break; // 11 |
bobgiesberts | 29:41815fd13822 | 193 | case DEGLITCH: mask = 7; break; // 111 |
bobgiesberts | 29:41815fd13822 | 194 | } |
bobgiesberts | 29:41815fd13822 | 195 | } |
bobgiesberts | 29:41815fd13822 | 196 | |
bobgiesberts | 29:41815fd13822 | 197 | uint16_t data[1]; |
bobgiesberts | 29:41815fd13822 | 198 | readI2C( data, addr ); |
bobgiesberts | 29:41815fd13822 | 199 | return ( data[0]>>setting ) & mask; |
bobgiesberts | 29:41815fd13822 | 200 | } |
bobgiesberts | 29:41815fd13822 | 201 | |
bobgiesberts | 28:76a2fc42f888 | 202 | |
bobgiesberts | 28:76a2fc42f888 | 203 | |
bobgiesberts | 28:76a2fc42f888 | 204 | |
bobgiesberts | 28:76a2fc42f888 | 205 | |
bobgiesberts | 28:76a2fc42f888 | 206 | |
bobgiesberts | 28:76a2fc42f888 | 207 | /* GETTING DATA FROM SENSOR */ |
bobgiesberts | 28:76a2fc42f888 | 208 | |
bobgiesberts | 28:76a2fc42f888 | 209 | uint16_t LDC1614::get_status( void ) |
bobgiesberts | 20:8e1b1efdbb49 | 210 | { |
bobgiesberts | 28:76a2fc42f888 | 211 | uint16_t status[1]; |
bobgiesberts | 28:76a2fc42f888 | 212 | readI2C( status, STATUS ); |
bobgiesberts | 28:76a2fc42f888 | 213 | return status[0]; |
bobgiesberts | 20:8e1b1efdbb49 | 214 | } |
bobgiesberts | 29:41815fd13822 | 215 | bool LDC1614::is_ready( uint8_t status ) |
bobgiesberts | 29:41815fd13822 | 216 | { |
bobgiesberts | 29:41815fd13822 | 217 | if( status == 17 ) { status = get_status(); } |
bobgiesberts | 29:41815fd13822 | 218 | return ( status>>DRDY ) & 1; |
bobgiesberts | 29:41815fd13822 | 219 | } |
bobgiesberts | 29:41815fd13822 | 220 | bool LDC1614::is_error( uint8_t status ) |
bobgiesberts | 29:41815fd13822 | 221 | { |
bobgiesberts | 29:41815fd13822 | 222 | if( status == 17 ) { status = get_status(); } |
bobgiesberts | 29:41815fd13822 | 223 | return ((( status>>ERR_ZC) & 6) == 0); |
bobgiesberts | 29:41815fd13822 | 224 | } |
bobgiesberts | 17:a5cf2b4bec13 | 225 | |
bobgiesberts | 17:a5cf2b4bec13 | 226 | |
bobgiesberts | 28:76a2fc42f888 | 227 | uint16_t LDC1614::get_Rcount( uint8_t channel ) |
bobgiesberts | 16:07d0e43c2d12 | 228 | { |
bobgiesberts | 28:76a2fc42f888 | 229 | uint16_t rcount[1]; |
bobgiesberts | 28:76a2fc42f888 | 230 | readI2C( rcount, RCOUNT_CH0 + channel ); |
bobgiesberts | 28:76a2fc42f888 | 231 | return rcount[0]; |
bobgiesberts | 20:8e1b1efdbb49 | 232 | } |
bobgiesberts | 20:8e1b1efdbb49 | 233 | |
bobgiesberts | 28:76a2fc42f888 | 234 | uint32_t LDC1614::get_Data( uint8_t channel ) |
bobgiesberts | 22:8da965ce5af3 | 235 | { |
bobgiesberts | 28:76a2fc42f888 | 236 | uint16_t data[2]; |
bobgiesberts | 28:76a2fc42f888 | 237 | readI2C(data, DATA_MSB_CH0 + channel, 2); |
bobgiesberts | 29:41815fd13822 | 238 | |
bobgiesberts | 29:41815fd13822 | 239 | if( ((data[0]>>CHx_ERR_UR) & 1) == 1 ) { debug( "Under-range Error" ); } |
bobgiesberts | 29:41815fd13822 | 240 | if( ((data[0]>>CHx_ERR_OR) & 1) == 1 ) { debug( "Over-range Error" ); } |
bobgiesberts | 29:41815fd13822 | 241 | if( ((data[0]>>CHx_ERR_WD) & 1) == 1 ) { debug( "Watchdog Timeout Error" ); } |
bobgiesberts | 29:41815fd13822 | 242 | if( ((data[0]>>CHx_ERR_AE) & 1) == 1 ) { debug( "Amplitude Error" ); } |
bobgiesberts | 29:41815fd13822 | 243 | |
bobgiesberts | 28:76a2fc42f888 | 244 | return ( (data[0] & 0x0fff)<<16) | data[1]; // MSB + LSB |
bobgiesberts | 22:8da965ce5af3 | 245 | } |
bobgiesberts | 20:8e1b1efdbb49 | 246 | |
bobgiesberts | 28:76a2fc42f888 | 247 | |
bobgiesberts | 28:76a2fc42f888 | 248 | |
bobgiesberts | 29:41815fd13822 | 249 | /* REGISTER FUNCTIONS (READ / WRITE) */ |
bobgiesberts | 28:76a2fc42f888 | 250 | |
bobgiesberts | 20:8e1b1efdbb49 | 251 | |
bobgiesberts | 28:76a2fc42f888 | 252 | void LDC1614::readI2C( uint16_t *data, uint8_t address, uint8_t length ) |
bobgiesberts | 22:8da965ce5af3 | 253 | { |
bobgiesberts | 28:76a2fc42f888 | 254 | // I2C reads per 8-bits, char is 8-bit, combine 8-bit in 16-bit sets |
bobgiesberts | 28:76a2fc42f888 | 255 | char temp[length*2]; |
bobgiesberts | 28:76a2fc42f888 | 256 | _i2c.read( address, temp, length*2 ); |
bobgiesberts | 28:76a2fc42f888 | 257 | |
bobgiesberts | 28:76a2fc42f888 | 258 | for ( int i = 0; i < length; i++ ) |
bobgiesberts | 28:76a2fc42f888 | 259 | data[i] = (uint16_t) (temp[2*i+1]<<8) | temp[2*i]; |
bobgiesberts | 22:8da965ce5af3 | 260 | } |
bobgiesberts | 20:8e1b1efdbb49 | 261 | |
bobgiesberts | 28:76a2fc42f888 | 262 | void LDC1614::writeI2C( uint16_t *data, uint8_t address, uint8_t length ) |
bobgiesberts | 22:8da965ce5af3 | 263 | { |
bobgiesberts | 28:76a2fc42f888 | 264 | // I2C reads per 8-bits, char is 8-bit, split 16-bit data up in sets of 8-bit |
bobgiesberts | 28:76a2fc42f888 | 265 | char temp[length*2]; |
bobgiesberts | 28:76a2fc42f888 | 266 | for ( int i = 0; i < length; i++ ) |
bobgiesberts | 28:76a2fc42f888 | 267 | { |
bobgiesberts | 28:76a2fc42f888 | 268 | temp[2*i] = (data[i] & 0x00ff) >> 0; // 0, 2, 4 ... |
bobgiesberts | 28:76a2fc42f888 | 269 | temp[2*i+1] = (data[i] & 0xff00) >> 8; // 1, 3, 5 ... |
bobgiesberts | 28:76a2fc42f888 | 270 | } |
bobgiesberts | 28:76a2fc42f888 | 271 | _i2c.write( address, temp, length*2 ); |
bobgiesberts | 22:8da965ce5af3 | 272 | } |
bobgiesberts | 20:8e1b1efdbb49 | 273 | |
bobgiesberts | 28:76a2fc42f888 | 274 | void LDC1614::writeI2Cregister(uint8_t reg, uint16_t value) |
bobgiesberts | 28:76a2fc42f888 | 275 | { |
bobgiesberts | 28:76a2fc42f888 | 276 | writeI2C( &value, reg ); |
bobgiesberts | 28:76a2fc42f888 | 277 | } |
bobgiesberts | 28:76a2fc42f888 | 278 | |
bobgiesberts | 28:76a2fc42f888 | 279 | void LDC1614::regchange( uint8_t addr, uint8_t setting, uint8_t value, uint8_t mask ) |
bobgiesberts | 28:76a2fc42f888 | 280 | { |
bobgiesberts | 28:76a2fc42f888 | 281 | uint16_t config[1]; |
bobgiesberts | 28:76a2fc42f888 | 282 | readI2C( config, addr ); |
bobgiesberts | 28:76a2fc42f888 | 283 | writeI2Cregister( addr, uint16_t ( (config[0] & !(mask<<setting)) | (value<<setting)) ); // replace bits with number SETTING |
bobgiesberts | 28:76a2fc42f888 | 284 | } |
bobgiesberts | 28:76a2fc42f888 | 285 | |
bobgiesberts | 28:76a2fc42f888 | 286 | |
bobgiesberts | 25:ae111662ee03 | 287 | |
bobgiesberts | 25:ae111662ee03 | 288 | /* CALCULATE STUFF WITH SENSOR DATA */ |
bobgiesberts | 25:ae111662ee03 | 289 | |
bobgiesberts | 28:76a2fc42f888 | 290 | float LDC1614::get_fsensor( uint32_t LData ) |
bobgiesberts | 28:76a2fc42f888 | 291 | { |
bobgiesberts | 28:76a2fc42f888 | 292 | _fsensor = _dividerIN * (_fCLKIN/_dividerREF) * ((LData / 268435456) + (_Offset / 65536)); // (p.14) |
bobgiesberts | 18:fc9bb81a631f | 293 | return _fsensor; |
bobgiesberts | 19:e205ab9142d8 | 294 | } |
bobgiesberts | 18:fc9bb81a631f | 295 | |
bobgiesberts | 28:76a2fc42f888 | 296 | float LDC1614::get_Inductance( uint32_t Ldata ) |
bobgiesberts | 18:fc9bb81a631f | 297 | { |
bobgiesberts | 25:ae111662ee03 | 298 | float fsensor = get_fsensor( Ldata ); |
bobgiesberts | 28:76a2fc42f888 | 299 | _inductance = 1.0 / (_cap * 4 * PI*PI * fsensor*fsensor ); // ??? |
bobgiesberts | 19:e205ab9142d8 | 300 | return _inductance; |
bobgiesberts | 19:e205ab9142d8 | 301 | } |
bobgiesberts | 16:07d0e43c2d12 | 302 | |
bobgiesberts | 16:07d0e43c2d12 | 303 | |
bobgiesberts | 16:07d0e43c2d12 | 304 | |
bobgiesberts | 16:07d0e43c2d12 | 305 | // EXTRA test: Get&print values of all variables to verify (to calculate the induction) |
bobgiesberts | 16:07d0e43c2d12 | 306 | // The data will be printed on the screen using RealTerm: baud 9600. |
bobgiesberts | 16:07d0e43c2d12 | 307 | // Begin *********************************************************** |
bobgiesberts | 28:76a2fc42f888 | 308 | float LDC1614::get_fCLKIN() {return _fCLKIN;} |
bobgiesberts | 28:76a2fc42f888 | 309 | uint8_t LDC1614::get_dividerIN() {return _dividerIN;} |
bobgiesberts | 28:76a2fc42f888 | 310 | uint8_t LDC1614::get_dividerREF() {return _dividerREF;} |
bobgiesberts | 28:76a2fc42f888 | 311 | uint32_t LDC1614::get_Offset() {return _Offset;} |
bobgiesberts | 28:76a2fc42f888 | 312 | float LDC1614::get_cap() {return _cap;} |
bobgiesberts | 16:07d0e43c2d12 | 313 | // END *********************************************************** |