Hi everyone, there's an issue I'm looking forward to a solution. Specifically, I connect my Wemos D1 R1 to 'eval-cn0565-ardz' to measure 4-wire Impedance using AD5940 (SPI). But when I use my oscilloscope, there is no waveform at CE0.
I don't know why. Here is my full version of the code. The value in the code comment is the value I did read from that register
#include <SPI.h>
#define CS_PIN 15 // Chân CS tùy chỉnh
#define SPICMD_SETADDR 0x20 /**< set the register address that is going to operate. */
#define SPICMD_READREG 0x6d /**< command to read register */
#define SPICMD_WRITEREG 0x2d /**< command to write register */
#define SPICMD_READFIFO 0x5f /**< command to read FIFO */
#define REG_CHIPID 0x0404 /* Chip Identification */
#define REG_AFECON_CLKCON0 0x00000408 /* AFECON Clock Divider Configuration */
#define REG_AFECON_CLKSEL 0x00000414 /* AFECON Clock Select */
#define REG_AFECON_CLKEN1 0x00000410 /* AFECON Clock Gate Enable */
#define REG_ALLON_OSCKEY 0x00000A0C /* ALLON Key Protection for OSCCON */
#define REG_ALLON_OSCCON 0x00000A10 /* ALLON Oscillator Control */
#define REG_AFE_WGFREQ 0x00002030 /* AFE Waveform Generator - Sinusoid Frequency */
#define REG_AFE_WGAMPLITUDE 0x0000203C /* AFE Waveform Generator - Sinusoid Amplitude */
#define REG_AFE_WGCON 0x00002014 /* AFE Waveform Generator Configuration */
#define REG_AFE_BUFSENCON 0x00002180 /* AFE HP and LP Buffer Control */
#define REG_AFE_LPREFBUFCON 0x00002050 /* AFE LPREF_BUF_CON */
#define REG_AFE_HSDACCON 0x00002010 /* AFE High Speed DAC Configuration */
#define REG_AFE_SWMUX 0x0000235C /* AFE Switch Mux for ECG */
#define REG_AFE_DSWFULLCON 0x00002150 /* AFE Switch Matrix Full Configuration (D) */
#define REG_AFE_NSWFULLCON 0x00002154 /* AFE Switch Matrix Full Configuration (N) */
#define REG_AFE_PSWFULLCON 0x00002158 /* AFE Switch Matrix Full Configuration (P) */
#define REG_AFE_TSWFULLCON 0x0000215C /* AFE Switch Matrix Full Configuration (T) */
#define REG_AFE_SWCON 0x0000200C /* AFE Switch Matrix Configuration */
#define REG_AFE_AFECON 0x00002000 /* AFE Configuration */
#define REG_AFE_DSWSTA 0x000021B0 /* AFE Switch Matrix Status (D) */
#define REG_AFE_PSWSTA 0x000021B4 /* AFE Switch Matrix Status (P) */
#define REG_AFE_NSWSTA 0x000021B8 /* AFE Switch Matrix Status (N) */
#define REG_AFE_TSWSTA 0x000021BC /* AFE Switch Matrix Status (T) */
/*--------------------------------------------- Read and Write -------------------------------------------*/
void ReadWriteNBytes(unsigned char *pSendBuffer, unsigned char *pRecvBuff, unsigned long length)
{
SPI.beginTransaction(SPISettings(160000, MSBFIRST, SPI_MODE0));
for (int i = 0; i < length; i++)
{
pRecvBuff[i] = SPI.transfer(pSendBuffer[i]);
delay(1);
}
SPI.endTransaction();
}
unsigned char ReadWrite8B(unsigned char data)
{
uint8_t tx[1], rx[1];
tx[0] = data;
ReadWriteNBytes(tx, rx, 1);
return rx[0];
}
uint16_t ReadWrite16B(uint16_t data)
{
uint8_t SendBuffer[2];
uint8_t RecvBuffer[2];
SendBuffer[0] = data >> 8;
SendBuffer[1] = data & 0xFF;
ReadWriteNBytes(SendBuffer, RecvBuffer, 2);
return (((uint16_t)RecvBuffer[0]) << 8) | RecvBuffer[1];
}
uint32_t ReadWrite32B(uint32_t data)
{
uint8_t SendBuffer[4];
uint8_t RecvBuffer[4];
SendBuffer[0] = (data >> 24) & 0xFF;
SendBuffer[1] = (data >> 16) & 0xFF;
SendBuffer[2] = (data >> 8) & 0xFF;
SendBuffer[3] = (data) & 0xFF;
ReadWriteNBytes(SendBuffer, RecvBuffer, 4);
return (((uint32_t)RecvBuffer[0]) << 24) | (((uint32_t)RecvBuffer[1]) << 16) | (((uint32_t)RecvBuffer[2]) << 8) | RecvBuffer[3];
}
void WriteReg(uint16_t RegAddr, uint32_t RegData)
{
digitalWrite(CS_PIN, LOW);
ReadWrite8B(SPICMD_SETADDR);
ReadWrite16B(RegAddr);
digitalWrite(CS_PIN, HIGH);
digitalWrite(CS_PIN, LOW);
ReadWrite8B(SPICMD_WRITEREG);
if (((RegAddr >= 0x1000) && (RegAddr <= 0x3014)))
{
ReadWrite32B(RegData);
}
else
{
ReadWrite16B(RegData);
}
digitalWrite(CS_PIN, HIGH);
}
uint32_t ReadReg(uint16_t RegAddr)
{
uint32_t Data = 0;
digitalWrite(CS_PIN, LOW);
ReadWrite8B(SPICMD_SETADDR);
ReadWrite16B(RegAddr);
digitalWrite(CS_PIN, HIGH);
digitalWrite(CS_PIN, LOW);
ReadWrite8B(SPICMD_READREG);
ReadWrite8B(0);
if ((RegAddr >= 0x1000) && (RegAddr <= 0x3014))
{
Data = ReadWrite32B(0);
}
else
{
Data = ReadWrite16B(0);
}
digitalWrite(CS_PIN, HIGH);
return Data;
}
//-------------------------------------------------------------------------------------------------------------------------------------------
void setup() {
SPI.begin();
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
Serial.println("");
Serial.print("ID =");
Serial.println(ReadReg(REG_CHIPID), HEX); // Chip ID // (0x5502)
//--------------------------------------------- Initialization -------------------------------------------------
WriteReg(0x0908, 0x2c9); // (0x2C9)
WriteReg(0x0c08, 0x206C); // (0x206C)
WriteReg(0x21F0, 0x0010); // (0x10)
WriteReg(0x0410, 0x2C9); // (0x1C9)
WriteReg(0x0A28, 0x0009); // (0x9)
WriteReg(0x238c, 0x0104); // (0x0104)
WriteReg(0x0a04, 0x4859); // (0x4859)
WriteReg(0x0a04, 0xF27B); // (0xF27B)
WriteReg(0x0a00, 0x8009); // (0x8009)
WriteReg(0x22F0, 0x0000); // (0x21000)
// ------------------------------------------------- Clock -----------------------------------------------------------
// ReadReg(REG_AFECON_CLKCON0); // [Done] - System and ADC clock frequency 16MHz // (0x441)
// ReadReg(REG_AFECON_CLKSEL); // [Done] - System and ADC clock source // (0x0)
// ReadReg(REG_AFECON_CLKEN1); // [Done] - Turn on ACLK clock [bit 5 = 0] // (0x1C9)
// ReadReg(REG_ALLON_OSCCON); // - Enable clock [bit 0 = 1] [bit 1 = 1] // (0x303)
// --------------------------------------- Waveform Generator ---------------------------------------------------------
WriteReg(REG_AFE_WGFREQ, 0x333333); // [Done] - Waveform freq : f = f_aclk * data /(2^30) where f_aclk = 16MHz, data = 0x333333 => f = 50kHz (0x333333)
WriteReg(REG_AFE_WGAMPLITUDE, 0x4ff); // [Done] - Waveform Amp // (0x4ff)
WriteReg(REG_AFE_WGCON, 0x4); // [Done] - // Sine Waveform and Bypass DAC // (0x4)
//------------------------------------ HIGH AND LOW POWER DAC & EXCITATION BUFFER ----------------------------------------------------
// ReadReg(REG_AFE_BUFSENCON); // [Done] - High power and low buffer control // (0x37)
// ReadReg(REG_AFE_LPREFBUFCON); // [Done] - Enable low-power reference and 2.5V buffer // (0x0)
// ReadReg(REG_AFE_HSDACCON); // [Done] - Excitation Buffer Gain = 2, Attenuation = 1 and Rate = 15 // (0x1E)
//-------------------------------------- Switch Matrices -------------------------------------------------
WriteReg(REG_AFE_DSWFULLCON, 0x10); // Close D5 // (0x10)
WriteReg(REG_AFE_PSWFULLCON, 0x10); // Close P5 // (0x10)
WriteReg(REG_AFE_NSWFULLCON, 0x2); // Close N2 // (0x2)
WriteReg(REG_AFE_TSWFULLCON, 0x102); // Close T2 and T9 // (0x102)
WriteReg(REG_AFE_SWCON, 0x32255); // Update switch configuration (T9 = 1 bit 17, SWSource = 1, close D5, N2, P5 and T2) // (0x32255)
// ReadReg(REG_AFE_DSWSTA); // (0x10);
// ReadReg(REG_AFE_PSWSTA); // (0x10);
// ReadReg(REG_AFE_NSWSTA); // (0x2);
// ReadReg(REG_AFE_TSWSTA); // (0x102);
// ----------------------------------------- General Configuration --------------------------------------------
WriteReg(REG_AFE_AFECON, 0x39CFC0); // Enable DAC (bit 20, 21) = 1, Waveform Generator = 1, Excitation = 1, ADC = 1, HS reference = 0
}
void loop() {
// Nothing here for now
}
Someone helps me handle it. Thank you ^^.
