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.
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
Post a Comment