AFE4300 WITH DUE

HI,
i already posted my problem in Arduino DUE section , but no response . Kindly help me .

i am trying to connect the AFE4300 ADC to DUE .

it supposed to make RDY pin toggle at every 8uS once its registers set properly .

but the chip not responding at all .

i am sure my hardware connections are right . i attached my code and the signal from logic analyzer .

The chip ADC clock 1MHz is generated from Logic analyzer .

Connection diagram attached , code formatted . thanks.

here is my code :

#include <SPI.h>

// pins
#define PIN_RESET  9
#define PIN_DRDY  2
#define PIN_LED 13
#define SlaveSelectPIn 4

void resetAFE4300();

void writeRegister(unsigned char address, unsigned int data); //Writes to a register on the AFE4300

int readRegister(unsigned char address); //Reads from a register on the AFE4300

void initAFE4300(); //intialize AFE

void initWeighScale(); // Initializes the Weigh Scale Module

void initBCM(); // Initializes the BCM Module

int readData(); //  Reads the ADC data register

/***************************

AFE4300 register address definitions

****************************/
#define ADC_DATA_RESULT         0x00
#define ADC_CONTROL_REGISTER    0x01
#define MISC1_REGISTER          0x02
#define MISC2_REGISTER          0x03
#define DEVICE_CONTROL_1        0x09
#define ISW_MATRIX              0x0A
#define VSW_MATRIX              0x0B
#define IQ_MODE_ENABLE          0x0C
#define WEIGHT_SCALE_CONTROL    0x0D
#define BCM_DAC_FREQ            0x0E
#define DEVICE_CONTROL_2        0x0F
#define ADC_CONTROL_REGISTER_2  0x10
#define MISC3_REGISTER          0x1A

void setup()
{


   pinMode(PIN_LED,   OUTPUT);
   pinMode(PIN_RESET, OUTPUT);
   pinMode(PIN_DRDY, INPUT);
   pinMode (PIN_CS, OUTPUT);


   Serial.begin(115200);
   Serial.flush();
      
   pinMode (13,OUTPUT);
   SPI.begin(SlaveSelectPIn); // SPI CS for slave 
   SPI.setClockDivider(SlaveSelectPIn,42); // 2MHz SPI clock generation 
   
   resetAFE4300();
   initAFE4300();
   initWeighScale();



               
}

void loop() 
   {


      readData();

   }

/**
* @brief  Resets the AFE4300 device
*/

void resetAFE4300()
{
       digitalWrite(PIN_RESET ,LOW);

       delay(100);

       digitalWrite(PIN_RESET ,HIGH);
}


/**
* @brief  Writes to a register on the AFE4300
**/

void writeRegister(unsigned char address, unsigned int data)

{
    unsigned char firstByte = (unsigned char)(data >> 8);
    unsigned char secondByte = (unsigned char)data;

   SPI.transfer(SlaveSelectPIn,address,SPI_CONTINUE);
 
   //Send 2 bytes to be written
   SPI.transfer(SlaveSelectPIn,firstByte,SPI_CONTINUE);
   SPI.transfer(SlaveSelectPIn,secondByte,SPI_LAST);

}       

/**
* @brief  Reads from a register on the AFE4300
*/
int readRegister(unsigned char address)
{
         SPI.begin(SlaveSelectPIn);
         int spiReceive = 0;
         unsigned char spiReceiveFirst = 0;
         unsigned char spiReceiveSecond = 0;
        
         address = address & 0x1F; //Last 5 bits specify address
         address = address | 0x20; //First 3 bits need to be 001 for read opcode
         
         SPI.transfer(SlaveSelectPIn,address,SPI_CONTINUE);
         spiReceiveFirst  = SPI.transfer(SlaveSelectPIn,0x00,SPI_CONTINUE);
         spiReceiveSecond = SPI.transfer(SlaveSelectPIn,0x00,SPI_LAST);
         
          //Combine the two received bytes into a signed int
         spiReceive = (spiReceiveFirst << 8);
         spiReceive |= spiReceiveSecond;
         return spiReceive;

}

/**
* @brief  Initializes the AFE4300 device
*/

void initAFE4300()
{

           writeRegister(ADC_CONTROL_REGISTER,0x5140);

           writeRegister(MISC1_REGISTER,0x0000);

           writeRegister(MISC2_REGISTER,0xFFFF);

           writeRegister(DEVICE_CONTROL_1,0x0004); //Power down both signal chains

           writeRegister(ISW_MATRIX,0x0000);

           writeRegister(VSW_MATRIX,0x0000);

           writeRegister(IQ_MODE_ENABLE,0x0000);

           writeRegister(WEIGHT_SCALE_CONTROL,0x0000);

           writeRegister(BCM_DAC_FREQ,0x0040);

           writeRegister(DEVICE_CONTROL_2,0x0000);

           writeRegister(ADC_CONTROL_REGISTER_2,0x0011);

           writeRegister(MISC3_REGISTER,0x00C0);

}
       
/**
* @brief  Initializes the Weigh Scale Module
*/

void initWeighScale()

{
   writeRegister(ADC_CONTROL_REGISTER,0x4120); //Differential measurement mode, 32 SPS

   writeRegister(DEVICE_CONTROL_1,0x0005); //Power up weigh scale signal chain

   writeRegister(ADC_CONTROL_REGISTER_2,0x0000); //ADC selects output of weigh scale

   writeRegister(WEIGHT_SCALE_CONTROL,0x003F); //Gain = 1 DAC Offset = -1

   writeRegister(BCM_DAC_FREQ,0x0040); //Frequency = default

   writeRegister(IQ_MODE_ENABLE,0x0000); //Disable IQ mode

   writeRegister(ISW_MATRIX,0x0000); //Channels IOUTP1 and IOUTN0

   writeRegister(VSW_MATRIX,0x0000); //Channels VSENSEP1 and VSENSEN0

}

/**
* @brief  Initializes the BCM Module
*/
void initBCM()
{
 
 writeRegister(ADC_CONTROL_REGISTER,0x4120);   //Differential measurement mode, 32 SPS

 writeRegister(DEVICE_CONTROL_1,0x0006);          //Power up BCM signal chain

 writeRegister(ISW_MATRIX,0x0804);                   //Channels IOUTP1 and IOUTN0

 writeRegister(VSW_MATRIX,0x0804);                  //Channels VSENSEP1 and VSENSEN0

 writeRegister(ADC_CONTROL_REGISTER_2,0x0063);   //ADC selects output of BCM-I output

 writeRegister(WEIGHT_SCALE_CONTROL,0x0000);     //Gain = 1 DAC Offset = 0
}

/**
* @brief  Reads the ADC data register
*/
int readData()
{

 return readRegister(ADC_DATA_RESULT);

}

Thanks in advance ,

-prag - INDIA

Duplicate thread in Due section deleted.

Please remember to use code tags when posting code.

Hi,

Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

an you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom.... :)

i modified with Code tags and connection diagram attached . thanks .

You might want to look into using the extended SPI library for the Due. http://www.arduino.cc/en/Reference/DueExtendedSPI

You're setting the speed to 2MHz, but the IC can only handle 1MHz. http://www.ti.com/lit/ds/symlink/afe4300.pdf

You may need to set the proper data mode. http://www.arduino.cc/en/Reference/SPISetDataMode

Mr . dlloyd , thanks a lot , i got the RDY working , i didn’t set the SPI mode , once i set to MODE1 it is started working .

Actually the TI eval board they used 2MHz so i used it , the SPI clock not a issue . Thank you so much . i am standing upside down for a week , thanks .

final working code below .

#include <SPI.h>

// pins
#define PIN_RESET  9
#define PIN_DRDY   2
#define PIN_LED     13

#define SlaveSelectPIn 4

void resetAFE4300();
void writeRegister(unsigned char address, unsigned int data); //Writes to a register on the AFE4300
int readRegister(unsigned char address); //Reads from a register on the AFE4300
void initAFE4300(); //intialize AFE
void initWeighScale(); // Initializes the Weigh Scale Module
void initBCM(); // Initializes the BCM Module

int readData(); //  Reads the ADC data register

/***************************
 AFE4300 register address definitions
****************************/
#define ADC_DATA_RESULT         0x00
#define ADC_CONTROL_REGISTER    0x01
#define MISC1_REGISTER          0x02
#define MISC2_REGISTER          0x03
#define DEVICE_CONTROL_1        0x09
#define ISW_MATRIX              0x0A
#define VSW_MATRIX              0x0B
#define IQ_MODE_ENABLE          0x0C
#define WEIGHT_SCALE_CONTROL    0x0D
#define BCM_DAC_FREQ            0x0E
#define DEVICE_CONTROL_2        0x0F
#define ADC_CONTROL_REGISTER_2  0x10
#define MISC3_REGISTER          0x1A

void setup()
{
 
 
        pinMode(PIN_LED,   OUTPUT);
 pinMode(PIN_RESET, OUTPUT);
 pinMode(PIN_DRDY, INPUT);
   


    Serial.begin(115200);
    Serial.flush();
       
    pinMode (13,OUTPUT);
    SPI.begin(SlaveSelectPIn); // SPI CS for slave 
    SPI.setClockDivider(SlaveSelectPIn,42); // 2MHz clock generation 
    SPI.setDataMode(SlaveSelectPIn, SPI_MODE1); // as Mr dlloyd  suggested . MODE 0 not working . 
    resetAFE4300();
    initAFE4300();
    initWeighScale();


 
                
}
void loop() 
    {
       readData();
    }
/**
* @brief  Resets the AFE4300 device
*/
void resetAFE4300()
{
        digitalWrite(PIN_RESET ,LOW);
        delay(100);
        digitalWrite(PIN_RESET ,HIGH);
}

/**
* @brief  Writes to a register on the AFE4300
**/
void writeRegister(unsigned char address, unsigned int data)
{
  unsigned char firstByte = (unsigned char)(data >> 8);
  unsigned char secondByte = (unsigned char)data;

  SPI.transfer(SlaveSelectPIn,address,SPI_CONTINUE);
  //Send 2 bytes to be written
  SPI.transfer(SlaveSelectPIn,firstByte,SPI_CONTINUE);
  SPI.transfer(SlaveSelectPIn,secondByte,SPI_LAST);
}       

/**
* @brief  Reads from a register on the AFE4300
*/
int readRegister(unsigned char address)
{
SPI.begin(SlaveSelectPIn);
 int spiReceive = 0;
          unsigned char spiReceiveFirst = 0;
          unsigned char spiReceiveSecond = 0;
         
          address = address & 0x1F; //Last 5 bits specify address
          address = address | 0x20; //First 3 bits need to be 001 for read opcode
          SPI.transfer(SlaveSelectPIn,address,SPI_CONTINUE);
          spiReceiveFirst  = SPI.transfer(SlaveSelectPIn,0x00,SPI_CONTINUE);
          spiReceiveSecond = SPI.transfer(SlaveSelectPIn,0x00,SPI_LAST);
           //Combine the two received bytes into a signed int
          spiReceive = (spiReceiveFirst << 8);
          spiReceive |= spiReceiveSecond;
          return spiReceive;
}

/**
* @brief  Initializes the AFE4300 device
*/
void initAFE4300()
{
            writeRegister(ADC_CONTROL_REGISTER,0x5140);
            writeRegister(MISC1_REGISTER,0x0000);
            writeRegister(MISC2_REGISTER,0xFFFF);
            writeRegister(DEVICE_CONTROL_1,0x0004); //Power down both signal chains
            writeRegister(ISW_MATRIX,0x0000);
            writeRegister(VSW_MATRIX,0x0000);
            writeRegister(IQ_MODE_ENABLE,0x0000);
            writeRegister(WEIGHT_SCALE_CONTROL,0x0000);
            writeRegister(BCM_DAC_FREQ,0x0040);
            writeRegister(DEVICE_CONTROL_2,0x0000);
            writeRegister(ADC_CONTROL_REGISTER_2,0x0011);
            writeRegister(MISC3_REGISTER,0x00C0);
}
        
/**
* @brief  Initializes the Weigh Scale Module
*/
void initWeighScale()
{
  writeRegister(ADC_CONTROL_REGISTER,0x4120); //Differential measurement mode, 32 SPS
  writeRegister(DEVICE_CONTROL_1,0x0005); //Power up weigh scale signal chain
  writeRegister(ADC_CONTROL_REGISTER_2,0x0000); //ADC selects output of weigh scale
  writeRegister(WEIGHT_SCALE_CONTROL,0x003F); //Gain = 1 DAC Offset = -1
  writeRegister(BCM_DAC_FREQ,0x0040); //Frequency = default
  writeRegister(IQ_MODE_ENABLE,0x0000); //Disable IQ mode
  writeRegister(ISW_MATRIX,0x0000); //Channels IOUTP1 and IOUTN0
  writeRegister(VSW_MATRIX,0x0000); //Channels VSENSEP1 and VSENSEN0
}

/**
* @brief  Initializes the BCM Module
*/
void initBCM()
{
  writeRegister(ADC_CONTROL_REGISTER,0x4120); //Differential measurement mode, 32 SPS
  writeRegister(DEVICE_CONTROL_1,0x0006); //Power up BCM signal chain
  writeRegister(ISW_MATRIX,0x0804); //Channels IOUTP1 and IOUTN0
  writeRegister(VSW_MATRIX,0x0804); //Channels VSENSEP1 and VSENSEN0
  writeRegister(ADC_CONTROL_REGISTER_2,0x0063); //ADC selects output of BCM-I output
  writeRegister(WEIGHT_SCALE_CONTROL,0x0000); //Gain = 1 DAC Offset = 0
}

/**
* @brief  Reads the ADC data register
*/
int readData()
{
  return readRegister(ADC_DATA_RESULT);
}

Moving back to Due section. Sorry.

Hello, can you help me, I want to activate the AFE4300 BCM, I have no way to do it, please help.

Hi wmarquinar ,

The AFE not throwing correct values for Body composition ( FAT mass ) ., we are also working on that i will keep you posted .

-prag

I’m testing the AFE4300 BCM, I use the AFE4300EVM-PDK (Development Guide), I use an RC circuit with two resistors 820 and 180 ohms and a capacitor 4 nF, I try to frequencies of 1, 4.16, 50, 100 and 250 KHz, with the following code, it works perfectly (I tried it with an oscilloscope), but I can’t read the result of ADC_DATA_RESULT, I can’t get the code to do so, you could help me with that ?.
thanks

the code is this

ARDUINO DUE:

#include <SPI.h>

// pins
#define PIN_RESET 9
#define PIN_DRDY 2
#define PIN_LED 13

#define SlaveSelectPIn 4

void resetAFE4300();
void writeRegister(unsigned char address, unsigned int data); //Escribir en un registro del AFE4300
int readRegister(unsigned char address); //Leer desde el registro del AFE4300
void initAFE4300(); //Inicializar el AFE4300
//void initWeighScale(); // Initializes the Weigh Scale Module
//void initBCM(); // Initializes the BCM Module

int readData(); // Leer del ADC_DATA_RESULT

/***************************
AFE4300 Definimos la direccion de los registros
****************************/
#define ADC_DATA_RESULT 0x00
#define ADC_CONTROL_REGISTER 0x01
#define MISC1_REGISTER 0x02
#define MISC2_REGISTER 0x03
#define DEVICE_CONTROL_1 0x09
#define ISW_MATRIX 0x0A
#define VSW_MATRIX 0x0B
#define IQ_MODE_ENABLE 0x0C
#define WEIGHT_SCALE_CONTROL 0x0D
#define BCM_DAC_FREQ 0x0E
#define DEVICE_CONTROL_2 0x0F
#define ADC_CONTROL_REGISTER_2 0x10
#define MISC3_REGISTER 0x1A

void setup()
{

pinMode(PIN_LED, OUTPUT);
pinMode(PIN_RESET, OUTPUT);
pinMode(PIN_DRDY, INPUT);

Serial.begin(9600);
Serial.flush();

pinMode (13,OUTPUT);
SPI.begin(SlaveSelectPIn); // SPI CS para seleccionar el esclavo
SPI.setClockDivider(SlaveSelectPIn,21); // 2MHz clock generation
SPI.setDataMode(SlaveSelectPIn, SPI_MODE1); // as Mr dlloyd suggested . MODE 0 not working .
resetAFE4300();
initAFE4300();
//initWeighScale();

}
void loop()
{
readData();
}
/**

  • @brief Resets the AFE4300 device
    */
    void resetAFE4300()
    {
    digitalWrite(PIN_RESET ,LOW);
    delay(100);
    digitalWrite(PIN_RESET ,HIGH);
    }

/**

  • @brief Writes to a register on the AFE4300
    **/
    void writeRegister(unsigned char address, unsigned int data)
    {
    unsigned char firstByte = (unsigned char)(data >> 8); //LA CARA ES UN OCHO)
    unsigned char secondByte = (unsigned char)data;

SPI.transfer(SlaveSelectPIn,address,SPI_CONTINUE);
//Send 2 bytes to be written
SPI.transfer(SlaveSelectPIn,firstByte,SPI_CONTINUE);
SPI.transfer(SlaveSelectPIn,secondByte,SPI_LAST);
}

/**

  • @brief Reads from a register on the AFE4300
    */
    int readRegister(unsigned char address)
    {
    SPI.begin(SlaveSelectPIn);
    int spiReceive = 0;
    unsigned char spiReceiveFirst = 0;
    unsigned char spiReceiveSecond = 0;

address = address & 0x1F; //Ultimos 5 bits especifican la direccion
address = address | 0x20; //Los primeros 3 bits tienen que ser 001 por código de operación de lectura
SPI.transfer(SlaveSelectPIn,address,SPI_CONTINUE);
spiReceiveFirst = SPI.transfer(SlaveSelectPIn,0x00,SPI_CONTINUE);
spiReceiveSecond = SPI.transfer(SlaveSelectPIn,0x00,SPI_LAST);
//Combine the two received bytes into a signed int
spiReceive = (spiReceiveFirst << 8); //LA CARA ES UN OCHO)
spiReceive |= spiReceiveSecond;
return spiReceive;
}

/**

  • @brief Initializes the AFE4300 device
    */
    void initAFE4300()
    {
    writeRegister(ADC_CONTROL_REGISTER,0x4140);
    writeRegister(MISC1_REGISTER,0x0000);
    writeRegister(MISC2_REGISTER,0xFFFF);
    writeRegister(DEVICE_CONTROL_1,0x6006);
    writeRegister(ISW_MATRIX,0x0804);
    writeRegister(VSW_MATRIX,0x0804);
    writeRegister(IQ_MODE_ENABLE,0x0000);
    writeRegister(WEIGHT_SCALE_CONTROL,0x0000);
    writeRegister(BCM_DAC_FREQ,0x0010);
    writeRegister(DEVICE_CONTROL_2,0x0000);
    writeRegister(ADC_CONTROL_REGISTER_2,0x0063);
    writeRegister(MISC3_REGISTER,0x0030);
    }

/**

  • @brief Reads the ADC data register
    */
    int readData()
    {
    return readRegister(ADC_DATA_RESULT);
    }

ARDUINO UNO:

#include <SPI.h>
//#include “pins_arduino.h”

const int dataReadyPin = 8;
const int dataResetPin = 9;
const int slaveSelectPin = 10;

//void resetAFE4300()
//{
// digitalWrite(dataResetPin ,LOW);
// delay(100);
// digitalWrite(dataResetPin ,HIGH);
//}

void setup() {
SPI.begin();
SPI.setBitOrder(MSBFIRST); // Segun DATASHET
Serial.begin(9600);
pinMode (slaveSelectPin, OUTPUT);
pinMode (dataReadyPin, INPUT);
//pinMode (dataResetPin, OUTPUT);
digitalWrite (MOSI, LOW);
digitalWrite (slaveSelectPin, HIGH);
SPI.setDataMode (SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV16);
}

void loop() {
digitalWrite (slaveSelectPin, LOW);
//ADC_CONTROL_REGISTER1 0x00
SPI.transfer (0x00); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0x00); //escribimos DATA el MSB [15…8]
SPI.transfer (0x00); //escribimos DATA el LSB [7…0]
//ADC_CONTROL_REGISTER1 0x01
SPI.transfer (0x01); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0x41); //escribimos DATA el MSB [15…8]
SPI.transfer (0x40); //escribimos DATA el LSB [7…0]
//delay(10);
//MISC_REGISTER1 0x02
SPI.transfer (0x02); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0x00); //escribimos DATA el MSB [15…8]
SPI.transfer (0x00); //escribimos DATA el LSB [7…0]
//delay(10);
//MISC_REGISTER2 0x03
SPI.transfer (0x03); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0xFF); //escribimos DATA el MSB [15…8]
SPI.transfer (0xFF); //escribimos DATA el LSB [7…0]
//delay(10);
//DEVICE_CONTROL1 0x09
SPI.transfer (0x09); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0x60); //escribimos DATA el MSB [15…8]
SPI.transfer (0x06); //escribimos DATA el LSB [7…0]
//delay(10);
//ISW_MUX 0x0A
SPI.transfer (0x0A); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0x08); //escribimos DATA el MSB [15…8]
SPI.transfer (0x04); //escribimos DATA el LSB [7…0]
//delay(10);
//VSENSE_MUX 0x0B
SPI.transfer (0x0B); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0x08); //escribimos DATA el MSB [15…8]
SPI.transfer (0x04); //escribimos DATA el LSB [7…0]
//delay(10);
//IQ_MODE_ENABLE 0x0C
SPI.transfer (0x0C); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0x00); //escribimos DATA el MSB [15…8]
SPI.transfer (0x00); //escribimos DATA el LSB [7…0]
//delay(10);
//WEIGHT_SCALE_CONTROL 0x0D
SPI.transfer (0x0D); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0x00); //escribimos DATA el MSB [15…8]
SPI.transfer (0x00); //escribimos DATA el LSB [7…0]
//delay(10);
//ADC_CONTROL_REGISTER2 0x10
SPI.transfer (0x10); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0x00); //escribimos DATA el MSB [15…8]
SPI.transfer (0x63); //escribimos DATA el LSB [7…0]
//delay(10);
//MISC_REGISTER3 0x1A
SPI.transfer (0x1A); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0x00); //escribimos DATA el MSB [15…8]
SPI.transfer (0x30); //escribimos DATA el LSB [7…0]
//delay(10);
//BCM_DAC_FREQ 0x0E
SPI.transfer (0x0E); //ponemos el bit 21 a ‘0’ para escribir datos
SPI.transfer (0x00); //escribimos DATA el MSB [15…8]
SPI.transfer (0x01); //escribimos DATA el LSB [7…0]
//delay(10);

//LEER DATOS 0X20
// SPI.transfer (0x20); //ponemos el bit 21 a ‘1’ para leer datos
// pinMode(MISO, INPUT);
// SPI.setDataMode (SPI_MODE0);
// byte MSB = SPI.transfer (0x20 >> 8); // LA CARA ES UN OCHO) enviamos 00 para leer dato MSB
// byte LSB = SPI.transfer (0x20); //enviamos 00 para leer dato LSB
// Serial.print(MSB, LSB);

digitalWrite (slaveSelectPin, HIGH);
digitalWrite (MOSI, HIGH);

SPI.end ();
}

I do not understand

address = address & 0x1F; // Last 5 bits specify the address address = address | 0x20; // The first 3 bits have to be 001 per code reading operation

if only the bit 21 is to be put to one that is would be 0010 0000 I do not understand the 1F.

I got it, it works fine, thanks

Hi wmarquinar , Sorry for the late reply , in evaluation PDK board., the BCM values in FWR mode with calibration shows wrong values , i am comparing the FAT % with OMRON body FAT analyzer the values are differed a lot . 42% viscal FAT it shows when OMROM shows 23% , are you comparing with any standerd machine with PDK board ?

are you feeding age / gender information correct to the calculation ?

-ariesprg