• Resolved

FDC1004 returns fixed value

HI all,

I am new to FDC1004, i am using FDC1004 with arduino pro-mini with I2C in single measurement mode. But FDC always returns fixed value FFFF FFFF so it not responding to change in capacitance at CIN pin. Here is my code, can you Please suggest where i am going wrong.

///////////////////////////////////////Main.ino\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#include <Wire.h>
#include <FDC1004.h>

int capdac = 12;

FDC1004 fdc;

void setup() {
Serial.begin(9600);
delay(5000);
Serial.println("Starting Configuration....");
}

void loop() {
uint8_t measurement = 0;
uint8_t channel = 0;
char result[100];
Serial.print("DDDD");
delay(1000);
fdc.configureMeasurementSingle(measurement, channel, capdac);
fdc.triggerSingleMeasurement(measurement, FDC1004_100HZ);
//wait for completion
delay(15);
uint16_t value[2];
if (! fdc.readMeasurement(measurement, value)) {

// calculate capacitance;
// The absolute capacitance is a function of the capdac and the measurement
// We only use the msb because the FDC1004 only has 16bits effective resolution;
// the last 8 bits are more or less random noise.
int16_t msb = (int16_t) value[0];
int32_t capacitance = ((int32_t)457) * ((int32_t)msb); //in attofarads
capacitance /= 1000; //in femtofarads
capacitance += ((int32_t)3028) * ((int32_t)capdac);

sprintf(result, "Raw: %04X %04X @ %02X\n ->", msb, value[1], capdac);
Serial.print(result);
Serial.print(capacitance);
Serial.print(" fF\n");

//adjust capdac
int16_t upper_bound = 0x4000;
int16_t lower_bound = -1 * upper_bound;
if (msb > upper_bound) {
if (capdac < FDC1004_CAPDAC_MAX) capdac++;
} else if (msb < lower_bound) {
if (capdac > 0) capdac--;
}
}
delay(200);
}

////////////////////////////////FDC1004.cpp\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#include <FDC1004.h>

#define FDC1004_UPPER_BOUND ((int16_t) 0x4000)
#define FDC1004_LOWER_BOUND (-1 * FDC1004_UPPER_BOUND)

uint8_t MEAS_CONFIG[] = {0x08, 0x09, 0x0A, 0x0B};
uint8_t MEAS_MSB[] = {0x00, 0x02, 0x04, 0x06};
uint8_t MEAS_LSB[] = {0x01, 0x03, 0x05, 0x07};
uint8_t SAMPLE_DELAY[] = {11,11,6,3};

FDC1004::FDC1004(uint16_t rate){
this->_addr = 0b1010000; //not configurable, to my knowledge
this->_rate = rate;
}

void FDC1004::write16(uint8_t reg, uint16_t data) {
Wire.beginTransmission(_addr);
Wire.write(reg); //send address
Wire.write( (uint8_t) (data >> 8));
Wire.write( (uint8_t) data);
Wire.endTransmission();
}

uint16_t FDC1004::read16(uint8_t reg) {
Wire.beginTransmission(_addr);
Wire.write(reg);
Wire.endTransmission();
uint16_t value;
Wire.beginTransmission(_addr);
Wire.requestFrom(_addr, (uint8_t)2);
value = Wire.read();
value <<= 8;
value |= Wire.read();
Wire.endTransmission();
return value;
}

//configure a measurement (call only when changing the setup of a measurement)
uint8_t FDC1004::configureMeasurementSingle(uint8_t measurement, uint8_t channel, uint8_t capdac) {
//Verify data
if (!FDC1004_IS_MEAS(measurement) || !FDC1004_IS_CHANNEL(channel) || capdac > FDC1004_CAPDAC_MAX) {
Serial.println("bad configuration");
return 1;
}

//build 16 bit configuration
uint16_t configuration_data;
configuration_data = ((uint16_t)channel) << 13; //CHA
configuration_data |= ((uint16_t)0x04) << 10; //CHB disable / CAPDAC enable
configuration_data |= ((uint16_t)capdac) << 5; //CAPDAC value
write16(MEAS_CONFIG[measurement], configuration_data);
return 0;
}

uint8_t FDC1004::triggerSingleMeasurement(uint8_t measurement, uint8_t rate) {
//verify data
if (!FDC1004_IS_MEAS(measurement) || !FDC1004_IS_RATE(rate)) {
Serial.println("bad trigger request");
return 1;
}
uint16_t trigger_data;
trigger_data = ((uint16_t)rate) << 10; // sample rate
trigger_data |= 0 << 8; //repeat disabled
trigger_data |= (1 << (7-measurement)); // 0 > bit 7, 1 > bit 6, etc
write16(FDC_REGISTER, trigger_data);
}

/**
* Check if measurement is done, and read the measurement into value if so.
* Return 0 if successful, 1 if bad request, 2 if measurement did not complete.
* value should be at least 4 bytes long (24 bit measurement)
*/
uint8_t FDC1004::readMeasurement(uint8_t measurement, uint16_t * value) {
if (!FDC1004_IS_MEAS(measurement)) {
Serial.println("bad read request");
return 1;
}

//check if measurement is complete
uint16_t fdc_register = read16(FDC_REGISTER);
if (! (fdc_register & ( 1 << (3-measurement)))) {
Serial.println("measurement not completed");
return 2;
}

//read the value
uint16_t msb = read16(MEAS_MSB[measurement]);
uint16_t lsb = read16(MEAS_LSB[measurement]);
value[0] = msb;
value[1] = lsb;
return 0;
}

/**
* Convenience method to take a measurement, uses the measurement register equal to the channel number
* If you want to do something more complicated, you'll need to use the functions this functions calls
*/
uint8_t FDC1004::measureChannel(uint8_t channel, uint8_t capdac, uint16_t * value) {
uint8_t measurement = channel; //4 measurement configs, 4 channels, seems fair
if (configureMeasurementSingle(measurement, channel, capdac)) return 1;
if (triggerSingleMeasurement(measurement, this->_rate)) return 1;
delay(SAMPLE_DELAY[this->_rate]);
return readMeasurement(measurement, value);
}

/**
* High level function to get the capacitance from a channel.
* Attempts to manage capdac automagically
* Uses measureChannel, so you don't control the channel setup.
*/
int32_t FDC1004::getCapacitance(uint8_t channel) {
fdc1004_measurement_t value;
uint8_t result = getRawCapacitance(channel, &value);
if (result) return 0x80000000;

int32_t capacitance = ((int32_t)ATTOFARADS_UPPER_WORD) * ((int32_t)value.value); //attofarads
capacitance /= 1000; //femtofarads
capacitance += ((int32_t)FEMTOFARADS_CAPDAC) * ((int32_t)value.capdac);
return capacitance;
}

/**
* High level function to get the raw capacitance from a channel
* Attempts to manage capdac automagically
* uses measureChannel.
*/
uint8_t FDC1004::getRawCapacitance(uint8_t channel, fdc1004_measurement_t * value) {
if (!FDC1004_IS_CHANNEL(channel)) return 1;
value->value = 0x7FFF;
uint16_t raw_value[2];
value->capdac = this->_last_capdac[channel]; //load last capdac as starting point

//sample until we get a good result
while(value->value > 0x7E00 || value->value < 0x8100) {
if (measureChannel(channel, value->capdac, raw_value)) {
Serial.println("error");
return 1;
}
value->value = (int16_t)raw_value[0];

//adjust capdac if necessary
if (value->value > FDC1004_UPPER_BOUND && value->capdac < FDC1004_CAPDAC_MAX) {
value->capdac++;
} else if (value->value < FDC1004_LOWER_BOUND && value->capdac > 0) {
value->capdac--;
} else {
//out of range, but capdac is already maxed (or minned). Return.
this->_last_capdac[channel] = value->capdac;
return 0;
}

}
this->_last_capdac[channel] = value->capdac;
return 0;

}

  • In reply to Abhishek Nagesh:

    HI,

    First check connections of SCL,SDA. if connections are correct then try this code.

    Its ino file code.

    //////////////////////////////////////////////||||||||||||||||\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    #include <Wire.h>
    int channel =0;
    int capdac = 0;

    void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
    delay(2000);
    Wire.begin();
    delay(1000);
    Configure_meas();
    Trigger_Meas();

    }

    void loop() {

    if(Check_read())
    {
    read_val();
    //delay(1000);
    }
    }

    void Configure_meas()
    {
    //configure measurment
    uint16_t configuration_data=0;
    configuration_data |= ((uint16_t)channel) << 13; //CHA
    configuration_data |= ((uint16_t)0x04) << 10; //CHB disable / CAPDAC enable
    configuration_data |= ((uint16_t)capdac) << 5; //CAPDAC value

    Wire.beginTransmission(80);
    Wire.write(0x08); //send address
    Wire.write( (uint8_t) (configuration_data >> 8));
    Wire.write( (uint8_t) configuration_data);
    Wire.endTransmission();
    //Serial.println("Config Done");
    }

    void Trigger_Meas()
    {
    //trigger measurment
    int measurement = 0;
    uint16_t trigger_data=0;
    uint16_t rate = 0x01;
    trigger_data = ((uint16_t)rate) << 10; // sample rate
    trigger_data |= 1 << 8; //repeat disabled
    trigger_data |= (1 << (7-measurement));

    Wire.beginTransmission(80);
    Wire.write(0x0C); //send address
    Wire.write( (uint8_t) (trigger_data >> 8));
    Wire.write( (uint8_t) trigger_data);
    Wire.endTransmission();
    //Serial.println("Trigger Done");
    }

    int Check_read()
    {
    // check conversion is done
    Wire.beginTransmission(80);
    Wire.write(0x0C);
    Wire.endTransmission();
    //delay(40);
    uint16_t value=0;
    while(value == 1416)
    {
    Wire.beginTransmission(80);
    Wire.requestFrom(80, (uint8_t)2);
    value = Wire.read();
    value <<= 8;
    value |= Wire.read();
    Wire.endTransmission();
    if(value != 0 && value != 65535)
    {
    Serial.println(value);
    }
    }
    return 1;
    }

    void read_val()
    {
    // read value
    Wire.beginTransmission(80);
    Wire.write(0x00);
    Wire.endTransmission();
    //delay(40);
    uint16_t value_msb;
    Wire.beginTransmission(80);
    Wire.requestFrom(80, (uint8_t)2);
    value_msb = Wire.read();
    value_msb <<= 8;
    value_msb |= Wire.read();
    Wire.endTransmission();
    //Serial.println(value_msb);

    Wire.beginTransmission(80);
    Wire.write(0x01);
    Wire.endTransmission();
    //delay(40);
    uint16_t value_lsb;
    Wire.beginTransmission(80);
    Wire.requestFrom(80, (uint8_t)2);
    value_lsb = Wire.read();
    value_lsb <<= 8;
    value_lsb |= Wire.read();
    Wire.endTransmission();
    //Serial.println(value_lsb);

    //Custome Conversion
    /* int32_t capacitance = (int32_t) value_msb << 16;
    capacitance |= (int32_t)value_lsb;
    capacitance = capacitance >> 8;
    capacitance = capacitance/524288;
    */

    //Lib Conversion
    int32_t capacitance = ((int32_t)457) * ((int32_t)value_msb); //in attofarads
    double cap_double = capacitance/1000; //femto farads
    double cap_double_fm = cap_double/1000; //in picofarads

    //Serial.print("Capacitance=");
    Serial.println(cap_double);
    //Serial.print(" fm ");
    //Serial.print(cap_double_fm);
    //Serial.println(" pf");
    }

    int Check_read_complete()
    {
    Wire.beginTransmission(80);
    Wire.write(0x0C);
    Wire.endTransmission();
    uint16_t value=65535;
    while((value & 8) != 0)
    {

    Wire.beginTransmission(80);
    Wire.requestFrom(80, (uint8_t)2);
    value = Wire.read();
    value <<= 8;
    value |= Wire.read();
    Wire.endTransmission();
    if(value != 65535)
    {
    Serial.println(value);
    }
    }
    return 1;
    }
  • In reply to Akshay Ghadage53:

    Hi Akshay,

    Thank you ver much, your program really works.Tons of Thank you
    One last question. How do I Set negative offfset? ( about -10pF)
    I checked online They say Edit Capdac, I edited in same program , But i cannot.:(
  • In reply to Akshay Ghadage53:

    Hi Akshay,

    How to get the raw data from that program? And How do I know the value of Capdac?