11 #define FDC1004Q_I2C_ADDR 0x50 16 #define FDC1004Q_MANUFACTURED_ID_VALUE 0x5449 21 #define FDC1004Q_DEVICE_ID_VALUE 0x1004 30 #define FDC_FDC_CONF_RESET_BIT 15 35 #define FIXED_POINT_FRACTIONAL_BITS_OFFSET 11 40 #define FIXED_POINT_FRACTIONAL_BITS_GAIN 14 43 static float fixed_to_float_unsigned(uint16_t input, uint8_t fract_bits);
46 static int16_t float_to_fixed_unsigned(
float input, uint8_t fract_bits);
49 static float fixed_to_float_signed(int16_t input, uint8_t fract_bits);
52 static int16_t float_to_fixed_signed(
float input, uint8_t fract_bits);
79 if (
id == FDC1004Q_MANUFACTURED_ID_VALUE)
83 if (
id == FDC1004Q_DEVICE_ID_VALUE)
99 uint16_t register_value = 0;
103 temp[0] |= 1 << (FDC_FDC_CONF_RESET_BIT-8);
114 uint16_t register_value = temp[0] << 8 | temp[1];
115 flag = (register_value & 0x8000) > 0 ? 1 : 0;
136 temp[0] |= (sampleRate << 2);
150 *sampleRate = (temp[0] & 0x0C) >> 2;
162 if ( (offset < -16) || (offset > 16))
166 uint16_t offset_raw = float_to_fixed_signed(offset, FIXED_POINT_FRACTIONAL_BITS_OFFSET);
167 uint8_t temp[2] = {offset_raw >> 8, offset_raw & 0xFF};
176 float offset_f = fixed_to_float_signed(offset, FIXED_POINT_FRACTIONAL_BITS_OFFSET);
177 if ( (offset_f < 0) || (offset_f > 16))
179 uint8_t temp[2] = {offset >> 8, offset & 0xFF};
192 int16_t reg = temp[0] << 8 | temp[1];
193 float off = (float) (reg);
194 *offset = off/(2048);
196 *offset = fixed_to_float_signed(temp[0] << 8 | temp[1], FIXED_POINT_FRACTIONAL_BITS_OFFSET);
210 *offset = temp[0] << 8 | temp[1];
220 if ( (gain < 0) || (gain > 4))
222 uint16_t gain_u16 = float_to_fixed_unsigned(gain, FIXED_POINT_FRACTIONAL_BITS_GAIN);
223 uint8_t temp[2] = {gain_u16 >> 8, gain_u16 & 0xFF};
232 float gain_f = fixed_to_float_unsigned(gain, FIXED_POINT_FRACTIONAL_BITS_GAIN);
233 if ( (gain_f < 0) || (gain_f > 4))
235 uint8_t temp[2] = {gain >> 8, gain & 0xFF};
248 *gain = fixed_to_float_unsigned(temp[0] << 8 | temp[1], FIXED_POINT_FRACTIONAL_BITS_GAIN);
262 *gain = temp[0] << 8 | temp[1];
281 temp[1] &= ~ ( 1 << (7 - channel));
283 temp[1] |= (1 << (7 - channel));
299 temp[1] &= ~ (1 << (7 - channel));
315 *done = temp[1] & (0x08 >> channel);
341 temp[1] |= channel_flags;
382 uint16_t temp16 = temp[0] << 8 | temp[1];
386 temp16 |= capdac << 5;
392 temp[0] = temp16 >> 8;
393 temp[1] = temp16 & 0xFF;
428 *capacitance = (temp[0] << 24) | (temp[1] << 16);
432 *capacitance |= temp[0] << 8 | temp[1];
447 double temp_cap = (double)(capRaw >> 8);
448 if (temp_cap > ( (1 << 23) -1))
450 temp_cap -= ( 1 << 24);
459 uint16_t temp16 = temp[0] << 8 | temp[1];
460 uint8_t capdac = (temp16 >> 5) & 0x1F;
472 double temp_cap = (double)(capacitance >> 8);
473 if (temp_cap > ( (1 << 23) -1))
475 temp_cap -= ( 1 << 24);
491 uint16_t temp16 = temp[0] << 8 | temp[1];
492 *capdac = (temp16 >> 5) & 0x1F;
507 uint16_t temp16 = temp[0] << 8 | temp[1];
508 *capdac = (temp16 >> 5) & 0x1F;
523 uint16_t temp16 = temp[0] << 8 | temp[1];
524 *input = (temp16 >> 13) & 0x07;
540 uint16_t temp16 = temp[0] << 8 | temp[1];
541 *input = (temp16 >> 10) & 0x07;
555 *done = temp[1] & 0xF;
572 *reg_value = (temp[0] << 8) | temp[1];
586 *reg_value = (temp[0] << 8) | temp[1];
630 float fixed_to_float_unsigned(uint16_t input, uint8_t fract_bits)
632 return ((
float)input / (
float)(1 << fract_bits));
635 int16_t float_to_fixed_unsigned(
float input, uint8_t fract_bits)
637 return (uint16_t)(input * (1 << fract_bits));
640 float fixed_to_float_signed(int16_t input, uint8_t fract_bits)
642 return ((
float)input / (
float)(1 << fract_bits));
645 int16_t float_to_fixed_signed(
float input, uint8_t fract_bits)
647 return (int16_t)(input * (1 << fract_bits));
#define FDC_CH_1
FDC Capacitance channel 1.
uint8_t FDC_ReadRawOffsetCalibration(uint8_t channel, int16_t *offset)
Read channel offset calibration register as Q5.11 format.
uint8_t FDC_ReadCapdacSetting(uint8_t channel, float *capdac)
Read current capdac setting in float format.
#define FDC_DISABLED
Channel disabled - GND.
I2C_ErrorCode I2C_Peripheral_Start(void)
Start the I2C peripheral.
uint8_t FDC_ReadNegativeChannelSetting(uint8_t channel, uint8_t *input)
Read current negative input channel setting.
uint8_t FDC_Reset(void)
Perform a software reset of the sensor.
Hardware specific I2C interface.
#define FDC_COMM_ERR
Communication error on I2C bus.
double FDC_ConvertRawMeasurement(uint32_t capacitance)
Convert raw capacitance measurement in double format.
#define FDC_CH_4
FDC Capacitance channel 4.
#define FDC_400_Hz
400 Samples/second sample rate.
#define FDC1004Q_MANUFACTURER_ID
ID of Texas Instruments Register.
uint8_t FDC_ReadRawCapdacSetting(uint8_t channel, uint8_t *capdac)
Read current capdac setting.
uint8_t FDC_DisableRepeatMeasurement(void)
Disable repated measurement.
Header file for the FDC1004Q driver.
uint8_t FDC_ConfigureMeasurement(uint8_t meas_channel, uint8_t pos_channel, uint8_t neg_channel, uint8_t capdac, int16_t offset, uint16_t gain)
Configure measurement settings.
#define FDC_CAPDAC
CAPDAC input.
uint8_t FDC_ReadManufacturerId(uint16_t *id)
Read the manufacturer ID from the sensor.
I2C_ErrorCode I2C_Peripheral_WriteRegisterMulti(uint8_t device_address, uint8_t register_address, uint8_t register_count, uint8_t *data)
Write multiple bytes over I2C.
uint8_t FDC_ReadDeviceId(uint16_t *id)
Read the device ID from the sensor.
uint8_t FDC_StopMeasurement(uint8_t channel)
Stop measurement for given channel.
uint8_t FDC_IsDeviceConnected(void)
Check if device is connected on the I2C bus.
#define FDC1004Q_CONF_MEAS1
Measurement 1 Configuration Register.
uint8_t FDC_ReadRegister(uint8_t reg_addr, uint8_t *data)
Read a register from the FDC1004Q.
uint8_t FDC_ReadOffsetCalibration(uint8_t channel, float *offset)
Read channel offset calibration register as float value.
#define FDC_CAPDAC_FACTOR
CAPDAC multiplying factor.
#define FDC_CONF_ERR
Channel configuration error.
uint8_t FDC_ReadRawMeasurement(uint8_t channel, uint32_t *capacitance)
Read capacitance measurement in raw format.
uint8_t FDC_ReadMeasurement(uint8_t channel, double *capacitance)
Read capacitance measurement in double format.
uint8_t FDC_EnableRepeatMeasurement(uint8_t channel_flags)
Enable repeated measurement.
#define FDC1004Q_DEVICE_ID
ID of FDC1004Q device Register.
#define FDC_OK
No error occurred.
uint8_t FDC_ReadGainCalibration(uint8_t channel, float *gain)
Read gain calibration register in float format.
uint8_t FDC_WriteRegister(uint8_t reg_addr, uint8_t *data)
Write a register to the FDC1004Q.
uint8_t FDC_IsMeasurementDone(uint8_t channel, uint8_t *done)
Check if measurement for a given channel is complete.
uint8_t FDC_SetRawOffsetCalibration(uint8_t channel, int16_t offset)
Set channel offset calibration register in raw format.
uint8_t FDC_Start(void)
Start the FDC1004Q.
uint8_t FDC_InitMeasurement(uint8_t channel)
Init measurement for given channel.
#define FDC1004Q_OFFSET_CAL_CIN1
CIN1 Offset Calibration Register.
#define FDC_DEV_NOT_FOUND
Device not found on I2C bus.
uint8_t FDC_SetSampleRate(uint8_t sampleRate)
Set up sample rate for continuous measurements.
#define FDC1004Q_MEAS1_MSB
MSB Portion of Measurement 1 Register.
uint8_t FDC_SetOffsetCalibration(uint8_t channel, float offset)
Set channel offset calibration register in float format.
#define FDC1004Q_GAIN_CAL_CIN1
CIN1 Gain Calibration Register.
#define FDC1004Q_FDC_CONF
Capacitance to Digital Configuration Register.
I2C_ErrorCode I2C_Peripheral_Stop(void)
Stop the I2C peripheral.
uint8_t FDC_SetRawGainCalibration(uint8_t channel, uint16_t gain)
Set gain calibration register in Q2.14 format.
uint8_t FDC_ReadSampleRate(uint8_t *sampleRate)
Read current sample rate.
uint8_t FDC_SetGainCalibration(uint8_t channel, float gain)
Set gain calibration register.
uint8_t FDC_HasNewData(uint8_t *done)
Check if new measurement data are available to be read.
void FDC_Stop(void)
Stop communication with the FDC1004Q.
uint8_t FDC_ReadPositiveChannelSetting(uint8_t channel, uint8_t *input)
Read current positive input channel setting.
uint8_t FDC_ConfigureMeasurementInput(uint8_t meas_channel, uint8_t pos_channel, uint8_t neg_channel, uint8_t capdac)
Configure measurement input settings.
uint8_t FDC_ReadRawGainCalibration(uint8_t channel, uint16_t *gain)
Read gain calibration register in fixed point Q2.14 formta.
#define FDC1004Q_MEAS1_LSB
LSB Portion of Measurement 1 Register.
I2C_ErrorCode I2C_Peripheral_ReadRegisterMulti(uint8_t device_address, uint8_t register_address, uint8_t register_count, uint8_t *data)
Read multiple bytes over I2C.