Hi,
I have designed PCB and attached to an Arduino using C. I could not find a complete source code to help me. I am reading 0 after setup AFE4300 using this code. Please check and advice.
Thank you
void AFE4300_writeRegister(uint8_t regAddress, uint16_t data)
{
uint8_t firstByte = (uint8_t)(data >> 8);
uint8_t secondByte = (uint8_t)data & 0xFF;
regAddress=regAddress & 0x1F; //Last 5 bits specify address
SPI.transfer(regAddress);
delayMicroseconds(10);
SPI.transfer(firstByte);
delayMicroseconds(10);
SPI.transfer(secondByte);
delayMicroseconds(10);
}
int16_t AFE4300_readRegister(uint8_t regAddress)
{
int16_t spiReceive = 0;
uint8_t spiDataBuf[2] = {0,0};
SPI.transfer(0x20 | regAddress);
SPI.transfer(regAddress);
//2 = two bytes to read
delayMicroseconds(50);
spiDataBuf[0]=SPI.transfer(0x0);
delayMicroseconds(50);
spiDataBuf[1]=SPI.transfer(0x0);
//Combine the two received bytes into a signed int
spiReceive = (spiDataBuf[0] << 8);
spiReceive |= spiDataBuf[1];
//Note that every time read, it must be rewritten except ADC dataout register
if(regAddress != ADC_DATA_RESULT) {
AFE4300_writeRegister(regAddress, spiReceive);
}
return spiReceive;
}
void AFE4300V2::init() //const
{
writeRegister(ADC_CONTROL_REGISTER, 0x5140);//d 0x4950);
delayMicroseconds(800);
writeRegister(MISC1_REGISTER, 0x0000);
delayMicroseconds(800);
writeRegister(MISC2_REGISTER, 0xFFFF);
delayMicroseconds(800);
writeRegister(DEVICE_CONTROL_1, 0x0004); //?? 0x96 ~ 0110 0000
delayMicroseconds(800);
writeRegister(ISW_MATRIX, 0x0000);
delayMicroseconds(800);
writeRegister(VSW_MATRIX, 0x0000);
delayMicroseconds(800);
writeRegister(IQ_MODE_ENABLE, 0x0000);
delayMicroseconds(800);
writeRegister(WEIGHT_SCALE_CONTROL, 0x0000);
delayMicroseconds(800);
writeRegister(BCM_DAC_FREQ, 0x0040);// 0x00FA);
delayMicroseconds(800);
writeRegister(DEVICE_CONTROL_2, 0x0000);
delayMicroseconds(800);
writeRegister(ADC_CONTROL_REGISTER_2, 0x0011);
delayMicroseconds(800);
writeRegister(MISC3_REGISTER, 0x00C0);
delayMicroseconds(800);
}
void AFE4300V2::initWeightScale() const
{
writeRegister(ADC_CONTROL_REGISTER, 0x4120);// 0x4150); // ?? Differential measurement mode, 32 SPS
delayMicroseconds(100);
writeRegister(DEVICE_CONTROL_1, 0x0005);// 0x4001); //Power up weigh scale signal chain
delayMicroseconds(100);
writeRegister(ADC_CONTROL_REGISTER_2, 0x0000); //ADC selects output of weigh scale
delayMicroseconds(100);
writeRegister(WEIGHT_SCALE_CONTROL, 0x003F); //Gain = 1 DAC Offset = -1
delayMicroseconds(100);
writeRegister(BCM_DAC_FREQ, 0x0040); //?? Frequency = default
delayMicroseconds(100);
//writeRegister(DEVICE_CONTROL_2, 0x1000); //Power up weigh scale signal chain
// delayMicroseconds(100);
writeRegister(IQ_MODE_ENABLE, 0x0000); //Disable IQ mode
delayMicroseconds(100);
writeRegister(ISW_MATRIX, 0x0000); //Channels IOUTP1 and IOUTN0
delayMicroseconds(100);
writeRegister(VSW_MATRIX, 0x0000); //Channels VSENSEP1 and VSENSEN0
delayMicroseconds(100);
}
/**
* @brief Initializes the BCM Module
*/
void AFE4300V2::initBCM()
{
writeRegister(ADC_CONTROL_REGISTER,0x4120); //Differential measurement mode, 32 SPS
delayMicroseconds(100);
writeRegister(DEVICE_CONTROL_1,0x0006); //Power up BCM signal chain
delayMicroseconds(100);
writeRegister(ISW_MATRIX,0x0804); //Channels IOUTP1 and IOUTN0
delayMicroseconds(100);
writeRegister(VSW_MATRIX,0x0804); //Channels VSENSEP1 and VSENSEN0
delayMicroseconds(100);
writeRegister(ADC_CONTROL_REGISTER_2,0x0063); //ADC selects output of BCM-I output
delayMicroseconds(100);
writeRegister(WEIGHT_SCALE_CONTROL,0x0000); //Gain = 1 DAC Offset = 0
delayMicroseconds(100);
writeRegister(BCM_DAC_FREQ,0x0032);
delayMicroseconds(100);
writeRegister(MISC3_REGISTER,0x0030);
delayMicroseconds(100);
}
void setup()
{
Bio.resetAFE4300();//Pull RESET pin low for 50 Usec and pull high
Bio.init();
Bio.initWeightScale();
////START BCM
Bio.initBCM();
PWMsetup();//1 Mhz CLOCK
}
void loop()
{
reg_val=readRegister(ADC_DATA_RESULT);
Serial.println(reg_val, HEX);
delay(100);
}