Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I seem to only get 0 values from the board using a Teensy 4.0 #9

Open
BryanVision opened this issue Feb 17, 2024 · 0 comments
Open

Comments

@BryanVision
Copy link

BryanVision commented Feb 17, 2024

I couldn't get the library to run, so I used my own code.

#include <Wire.h>
#include <IntervalTimer.h>

// FDC1004 I2C address
#define FDC1004_I2C_ADDRESS 0x50

// FDC1004 Configuration Register Addresses
#define FDC_CONF_REG 0x0C
#define MEAS1_CONF_REG 0x08
#define MEAS2_CONF_REG 0x09
#define MEAS3_CONF_REG 0x0A
#define MEAS4_CONF_REG 0x0B

// FDC1004 Measurement Register Addresses for MSB and LSB of each channel
#define MEAS1_MSB 0x00
#define MEAS1_LSB 0x01
#define MEAS2_MSB 0x02
#define MEAS2_LSB 0x03
#define MEAS3_MSB 0x04
#define MEAS3_LSB 0x05
#define MEAS4_MSB 0x06
#define MEAS4_LSB 0x07

// IntervalTimer object
IntervalTimer myTimer;

// Function prototypes
void writeFDCRegister(uint16_t reg, uint16_t value);
void readMeasurement(void);
void readMeasurementTask(void);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  delay(1000); // Wait for Serial monitor to open
  Serial.println("Initializing FDC1004 for all 4 channels...");

  // Set the measurement rate to 100S/s and enable repeat measurements
  writeFDCRegister(FDC_CONF_REG, 0x0C00);

  // Configure all 4 measurement channels for single-ended measurements
  writeFDCRegister(MEAS1_CONF_REG, 0x1C00); // Channel 1
  writeFDCRegister(MEAS2_CONF_REG, 0x1C00); // Channel 2
  writeFDCRegister(MEAS3_CONF_REG, 0x1C00); // Channel 3
  writeFDCRegister(MEAS4_CONF_REG, 0x1C00); // Channel 4

  Serial.println("FDC1004 configured for continuous measurement on all channels.");

  // Set up the hardware timer to call readMeasurementTask every 10ms
  myTimer.begin(readMeasurementTask, 10000); // 10000 microseconds = 10ms
}

void loop() {
  // The readMeasurementTask function is called every 10ms by the hardware timer
  // Nothing needed in the loop
}

void readMeasurementTask() {
  // Read measurement from all channels
  readMeasurement();
}

void readMeasurement() {
  for (int i = 0; i < 4; i++) {
    uint16_t msb = 0, lsb = 0;
    uint8_t msbReg = MEAS1_MSB + (i * 2); // Calculate MSB register for current channel
    uint8_t lsbReg = MEAS1_LSB + (i * 2); // Calculate LSB register for current channel

    // Read MSB
    Wire.beginTransmission(FDC1004_I2C_ADDRESS);
    Wire.write(msbReg >> 8);
    Wire.write(msbReg & 0xFF);
    Wire.endTransmission(false);
    Wire.requestFrom(FDC1004_I2C_ADDRESS, 2);
    if (Wire.available() == 2) {
      msb = Wire.read() << 8;
      msb |= Wire.read();
    }

    // Read LSB
    Wire.beginTransmission(FDC1004_I2C_ADDRESS);
    Wire.write(lsbReg >> 8);
    Wire.write(lsbReg & 0xFF);
    Wire.endTransmission(false);
    Wire.requestFrom(FDC1004_I2C_ADDRESS, 2);
    if (Wire.available() == 2) {
      lsb = Wire.read() << 8;
      lsb |= Wire.read();
    }
    
    // Print MSB and LSB for the current channel
    Serial.print("Channel ");
    Serial.print(i + 1);
    Serial.print(": MSB=");
    Serial.print(msb, HEX);
    Serial.print(", LSB=");
    Serial.println(lsb, HEX);
  }

  // Print a separator for readability
  Serial.println("------");
}

void writeFDCRegister(uint16_t reg, uint16_t value) {
  Wire.beginTransmission(FDC1004_I2C_ADDRESS);Channel 1: MSB=0, LSB=0
Channel 2: MSB=0, LSB=0
Channel 3: MSB=0, LSB=0
Channel 4: MSB=0, LSB=0
  Wire.write(highByte(reg)); // MSB of register address
  Wire.write(lowByte(reg)); // LSB of register address
  Wire.write(highByte(value)); // MSB of register value
  Wire.write(lowByte(value)); // LSB of register value
  Wire.endTransmission();
}

Channel 1: MSB=0, LSB=0
Channel 2: MSB=0, LSB=0
Channel 3: MSB=0, LSB=0
Channel 4: MSB=0, LSB=0

I confirmed the arduino teensy 4.0 could see the FDC1004 at its 0x50 address by running:


#include <Wire.h>

void setup() {
  Wire.begin();
  
  Serial.begin(9600);
  while (!Serial); // for Leonardo/Micro/Zero
  
  Serial.println("\nI2C Scanner");

  byte error, address;
  int nDevices = 0;

  Serial.println("Scanning...");

  for (address = 1; address < 127; address++ ) {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.print(address, HEX);
      Serial.println("  !");
      
      nDevices++;
    }
    else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
    }    
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  }
  else {
    Serial.println("done\n");
  }
}

void loop() {
  // nothing to do
}

I

2C Scanner
Scanning...
I2C device found at address 0x50 !
done

When running the following code to output from every address:

#include <Wire.h>

// FDC1004 I2C address
#define FDC1004_I2C_ADDRESS 0x50

void setup() {
  Serial.begin(9600);
  Wire.begin();

  // Wait for Serial monitor to open
  while (!Serial) {
    ; // wait for serial port to connect.
  }

  Serial.println("Reading FDC1004 registers...");
}

void loop() {
  // Register addresses to read
  uint8_t registersToRead[] = {
    0x00, // MEAS1_MSB
    0x01, // MEAS1_LSB
    0x02, // MEAS2_MSB
    0x03, // MEAS2_LSB
    0x04, // MEAS3_MSB
    0x05, // MEAS3_LSB
    0x06, // MEAS4_MSB
    0x07, // MEAS4_LSB
    0x08, // CONF_MEAS1
    0x09, // CONF_MEAS2
    0x0A, // CONF_MEAS3
    0x0B, // CONF_MEAS4
    0x0C, // FDC_CONF
    0x0D, // OFFSET_CAL_CIN1
    0x0E, // OFFSET_CAL_CIN2
    0x0F, // OFFSET_CAL_CIN3
    0x10, // OFFSET_CAL_CIN4
    0x11, // GAIN_CAL_CIN1
    0x12, // GAIN_CAL_CIN2
    0x13, // GAIN_CAL_CIN3
    0x14, // GAIN_CAL_CIN4
    0xFE, // Manufacturer ID
    0xFF  // Device ID
  };

  for (uint8_t reg : registersToRead) {
    readAndPrintRegister(reg);
    delay(100); // Short delay between reads
  }

  Serial.println("------");
  delay(1000); // Delay before starting over
}

void readAndPrintRegister(uint8_t reg) {
  Wire.beginTransmission(FDC1004_I2C_ADDRESS);
  Wire.write(reg);
  Wire.endTransmission(false); // End transmission but send a restart to keep the bus active
  Wire.requestFrom(FDC1004_I2C_ADDRESS, 2); // Request 2 bytes for the register value
  
  if (Wire.available() == 2) {
    uint16_t value = Wire.read() << 8; // Read MSB
    value |= Wire.read(); // Read LSB

    Serial.print("Register 0x");
    if (reg < 0x10) {
      Serial.print('0'); // Print leading zero for single digit hex numbers
    }
    Serial.print(reg, HEX);
    Serial.print(": 0x");
    Serial.println(value, HEX);
  } else {
    Serial.print("Failed to read register 0x");
    Serial.println(reg, HEX);
  }
}

We see that I can read values from all of the other addresses.

14:02:03.441 -> ------
14:02:04.454 -> Register 0x00: 0x0
14:02:04.533 -> Register 0x01: 0x0
14:02:04.659 -> Register 0x02: 0x0
14:02:04.751 -> Register 0x03: 0x0
14:02:04.845 -> Register 0x04: 0x0
14:02:04.939 -> Register 0x05: 0x0
14:02:05.017 -> Register 0x06: 0x0
14:02:05.155 -> Register 0x07: 0x0
14:02:05.248 -> Register 0x08: 0x1C00
14:02:05.341 -> Register 0x09: 0x1C00
14:02:05.435 -> Register 0x0A: 0x1C00
14:02:05.558 -> Register 0x0B: 0x1C00
14:02:05.651 -> Register 0x0C: 0x0
14:02:05.745 -> Register 0x0D: 0x0
14:02:05.839 -> Register 0x0E: 0x0
14:02:05.964 -> Register 0x0F: 0x0
14:02:06.043 -> Register 0x10: 0x0
14:02:06.135 -> Register 0x11: 0x4000
14:02:06.261 -> Register 0x12: 0x4000
14:02:06.354 -> Register 0x13: 0x4000
14:02:06.447 -> Register 0x14: 0x4000
14:02:06.538 -> Register 0xFE: 0x5449
14:02:06.663 -> Register 0xFF: 0x1004
14:02:06.757 -> ------

I have attempted to connect a number of things to channels 1-4, with no change from 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant