I am newbie of esp32 wroom. I am using 16bit external ADC (ADS8860) with esp32. I don't know how to write SPI communication in Arduino IDE for esp32.
Currently I use this code to read the adc value from ADS8860. The problem is that when I test ADC reading with pure sine wave with DC offset, I cannot get the pure sine wave.
#include <SPI.h>
// Define GPIO pins for SPI bus
//ESP32----------------> ADS8860
#define HSPI_MISO 19//12 // Dout
#define HSPI_MOSI 13
#define HSPI_SCLK 18//14 // CLK
#define HSPI_SS 5//15 // CONVST (CS
static const int spiClk = 500000; // 1 MHz
int long MyData [3];
int long ReadData = 0;
float adc_value;
unsigned long time_now,t1,t2,dt;
unsigned long time_now2;
int i=0;
int k=0;
float sampling_period_us;
float sampling_frequency=5000.0;
//uninitalised pointers to SPI objects
SPIClass * hspi = NULL;
void setup() {
sampling_period_us=round(1000000*(1.0/sampling_frequency));
hspi = new SPIClass(HSPI);
hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS); //SCLK, MISO, MOSI, SS
pinMode(HSPI_SS, OUTPUT); //HSPI SS
digitalWrite(HSPI_SS,HIGH); // CS pin HIGH
Serial.begin(115200); // initialize UART
}
void loop()
{
time_now = micros();
hspiCommand();
while(micros() - time_now < sampling_period_us){}
}
void hspiCommand()
{
hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(HSPI_SS, LOW);
delayMicroseconds(1);
ReadData = hspi->transfer(0);
MyData[0] = ReadData;
ReadData = hspi->transfer(0);
MyData[1] = ReadData;
digitalWrite(HSPI_SS, HIGH);
hspi->endTransaction();
adc_value=MyData[1] + (MyData[0]*256);adc_value = adc_value*(3.3/65535.0);
Serial.println(adc_value);
}
In datasheet of the ADS8860, there are 16 bit in Dout when the CS goes low. But in Arduino IDE, esp32 can read only 8 bit at a time.
Therefore, I would like to know how to read 16 bit Dout value in Arduino IDE.
I also attached the Arduino IDE serial plotter waveform and I cannot get the sine wave (input signal to external ADC).
ADC reading of ADS8860 in Arduino IDE for ESP32 via SPI protocol
Who is online
Users browsing this forum: Baidu [Spider] and 88 guests