Library to communicate with LDC1614
Dependents: Inductive_Sensor_3
Fork of LDC1101 by
Diff: LDC1614.cpp
- Revision:
- 35:00c9c01f0c0f
- Parent:
- 34:b03d7bb9010c
diff -r b03d7bb9010c -r 00c9c01f0c0f LDC1614.cpp --- a/LDC1614.cpp Wed Sep 21 14:25:20 2016 +0000 +++ b/LDC1614.cpp Wed Oct 05 08:15:26 2016 +0000 @@ -33,7 +33,7 @@ _Offset = 0; // highest 16-bit of 32-bit number (so e.g. 100E6 / 65536 = 1525) _Rcount = 0xffff; // maximum for greatest precision (0xffff) _SettleCount = 50; // CHx_SETTLECOUNT = t_settle * f_REFx/16 = 50 (p.12) - _DriveCurrent = 21; // max = 31, automatically settles at 20 + _DriveCurrent = 23; // max = 31, automatically settles at 22 _dividerREF = 2; // 2 (f_REF = f_CLKIN/2 = 40/2 = 20 < 35) (p.10) _dividerIN = 2; // 2 (f_IN = f_SENSOR/2 = 6.5/2 = 3.4 < 5 = 20/4 = f_REF/4 @@ -169,6 +169,52 @@ // debug("[DriveCurrent channel %d: 0x%02x]\r\n", channel, get_DriveCurrent( channel ) ); } +uint8_t LDC1614::autoDriveCurrent( uint8_t channel ) +{ + // set automatic calculation mode + sleep(); + set( CONFIG, AUTO_AMP_DIS, 0); + wakeup(); + + // find the right register based on the channel + uint8_t tempDriveCurrent = 0; + ADDR addr; + switch ( channel ){ + case 1: addr = DRIVE_CURRENT_CH1; break; + case 2: addr = DRIVE_CURRENT_CH2; break; + case 3: addr = DRIVE_CURRENT_CH3; break; + default: addr = DRIVE_CURRENT_CH0; break; + } + + // read data until DriveCurrent settles + for(int i = 0; i < 10; i++) + { + while( !is_ready( channel ) ) { } + get_Data( channel ); + tempDriveCurrent = get( addr, CHx_INIT_IDRIVE, 31 ); + + debug( "[poging %d] new: %d, old: %d\r\n", i, tempDriveCurrent, _DriveCurrent ); + /* + if ( _DriveCurrent == tempDriveCurrent ) + { + break; + }else{ + _DriveCurrent = tempDriveCurrent; + } + */ + } + + _DriveCurrent = tempDriveCurrent; + + // set back to manual configuration + sleep(); + setDriveCurrent( channel, _DriveCurrent ); + set( CONFIG, AUTO_AMP_DIS, 1); + wakeup(); + + return _DriveCurrent; +} + void LDC1614::set( ADDR addr, SETTING setting, uint8_t value ) { uint8_t mask = 1;