Skip to main content

AFE4300 with Arduino Due

In this post, I make an experiment to use Arduino Due to control AFE4300 for measuring in BCM mode.

The code based on a guy in this link from Arduino Forum
https://forum.arduino.cc/index.php?topic=319141.0

However, this not work now because the SPI library now have changed to new style and really different if you want control Clock of SPI with the command SPI.setClockDevider().

Actually, if you use this function to control SPI, the clock of SCLK keep around 4Mhz and it gives incorrect data when you communicate with AFE4300.

For solving it, you need to rewrite two functions writeRegister() and readRegister() as below:

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.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE1));

digitalWrite(SS,LOW);
SPI.transfer(address);
//Send 2 bytes to be written
SPI.transfer(firstByte);
SPI.transfer(secondByte);
digitalWrite(SS,HIGH);

SPI.endTransaction();
}   

int readRegister(unsigned char address)
{
 
   SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE1));
   
   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.begin();
     digitalWrite(SS,LOW);
     SPI.transfer(address);
     spiReceiveFirst  = SPI.transfer(0x00);
     spiReceiveSecond = SPI.transfer(0x00);
     digitalWrite (SS, HIGH);
     //SPI.end();
    SPI.endTransaction();
   
       //Combine the two received bytes into a signed int
    spiReceive = (spiReceiveFirst << 8);  //LA CARA ES UN OCHO)
    spiReceive |= spiReceiveSecond;
    return spiReceive;
}

Here, we need to use  SPI.beginTransaction() with setting 1Mhz clock for SCLK and SPI.SPI.endTransaction(); to assure when communication no interruption is permitted.

Furthermore, AFE4300 requires a 1Mhz CLK from outside to make inject current from 1khz - 150khz. From the library pwm_lib.h we can make a 1Mhz Clk which not affected the operation of the Due board because it is based on the DMA technique.

Lastly, to read the value of the ADC_DATA_RESULT register. I use an interrupt on the DRDY pin which connects to PIN 2 of Due board.
When Data conversation on AFE4300 finishes DRDY will make a pulse in 8us and we can read the value of this register after this signal state and send data through serial(115200) to plot on PC.

The full  program can access through  this link
https://github.com/LongNguyen1984/AFE4300-

Notice:
AFE4300 when reading any registers except the data output register (read-only register), the stored register value will be clear, so we need to rewrite the content of the register.  When you use the readRegister() function above to read ADC_DATA_RESULT, it is fine. However, for read any other  registers, must be careful to rewrite the value you have just read. So for safety readRegister we need a function to read and rewrite the register:

int readRewriteRegister(unsigned char address, int valAFE)
{
    valAFE = readRegister(registerAddress);
    writeRegister(registerAddress,valAFE);
    return valAFE;
}

The image below shows the SCLK and MISO wires on Oscilloscope when using AFE4300EVM with MMB3 board to read a value from ADC_DATA_RESULT:

As you can see, after reading the register, it rewrites the have-just-read value. 
 

Comments

Popular posts from this blog

Experiment to CC2640R2 and Sensor Booster Pack In this example, I follow a tutorial from TI to read sensor OTP3001. http://dev.ti.com/tirex/content/simplelink_academy_cc2640r2sdk_1_13_02_07/modules/ble_02_thermostat/ble_02_thermostat.html Because the sensor TMP0007 has out of date now, so I little modify to use with OTP3001. Step1. Copy file i2ctmp from simpllink sdk  cc2640R2 version Reference The Booster pack datasheet  http://www.ti.com/lit/ug/slau666b/slau666b.pdf TMP0007 and OTP3001 share the I2C bus including SCL, SDA.  Address's  TMP is 0x04, Data Register: 0x00 Address's OTP is 0x47, DataRegister: 0x00  So in the code we modify: #define OTP3001_REG         0x0000  /* Die Temp Result Register for TMP006 */ #define OTP3001_ADDR        0x47; According to OTP3001 datasheet, in default, a configuration register (address 0x01) in sleep mode, so if we read data ou...
 Bộ lọc Kalman - giành cho người mới bắt đầu Ý tưởng:  Để thực hiện việc loại bỏ nhiễu trong việc đo lường tín hiệu, ta sử dụng một hệ thống lọc nhiễu hay gọi vắn tắt là bộ lọc (Filter). Các cách lọc nhiễu trong hệ thống căn bản gồm các bộ lọc thông thấp (Lowpass Filters), bộ lọc thông dải (Bandpass Filters), bộ lọc chặn (Notch Filters) và lọc thông cao (Highpass Filters). Dựa trên quan sát sự khác biệt giữa tín hiệu và nhiễu để thực hiện việc tách tín hiệu khỏi nhiễu. Fig1. Các kiểu bộ lọc cơ bản Trong trường hợp cơ bản, để đo chất lượng tín hiệu ta dùng chỉ số SNR (Signal-to-Noise ratio) để đánh giá mức độ mạnh giữa tín hiệu so với nhiễu. Nếu giữa tín hiệu và nhiễu có tính độc lập ( Tương quan là 0) thì việc tách tín hiệu là khả thi. Chẳng hạn các tín hiệu ta đo lường thường có một số tần số nhất định, ví dụ các tín hiệu sinh học thường khu trú trong vùng tần số thấp < 25 Hz. Các tín hiệu nhiễu nền thường là nhiễu trắng có phân bố rộng, nhiễu điện lưới có tần số xác định ...

Arduino Code for test Heart Rate 7 Click

Heart Rate 7 Click is the newest module from MikroE which uses VEM8080 photodetector has a wide range spectrum from 300nm - 1000nm. To control and acquisition data, AFE4404 from TI inc. is adopted. This chip permits control 3 LED channels, and sample heart rate default 100 SPS.  A 22-bit ADC permit collecting very small changed voltage from a PD sensor. In this example we config Arduino Mega  2650 as below: Pin 4 for RST PIN 5 CLK PIN 6 INT PIN 20 SDA PIN 21 SCL Config registers follow the default of AFE4404 datasheet Page 27 with some minor changes. 1. Config Internal Clock through  AFE_CONTROL2 register addr.: 0x23 value: 0x104218  // setup all dynamic power to save energy 2. Control LED2 current through AFE_LEDCNTRL register addr: 0x22 value: 0x000100 3. Read data using PROG_TG_EN signal through AFE_TIA_GAIN register addr: 0x21 value: 0x000125 Time to start and end of PROG_TG setup through two registers: AFE_PROG_TG_STC register (...