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