I've been trying to get the ADS8341 to communicate with the XIAO ESP32S3 but can't get information from it. I'm using code I've found that's said to work but am only getting a constant 0 output. I'm new to using SPI so any help would be appreciated, here's the code for reference
#include <SPI.h>
//based off GPIO??
// #define CS 1
// #define MOSI 9
// #define MISO 8
// #define SPICLOCK 7=
#define SELPIN 1 // Chip select pin (any digital pin)
void setup() {
Serial.begin(9600);
//Set pin modes
pinMode(SELPIN, OUTPUT);
digitalWrite(SELPPIN, HIGH); //disable device at start
//initialize SPI library
SPI.begin();
//Configure SPI settings: Speed(data sheet), MSBFIRST, SPI_MODE0
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE0));
}
int read_adc(int channel) {
int adcvalue = 0;
byte commandbits;
//make command byte based on desired channel
switch (channel) {
case 0:
commandbits = 0x97; //Channel 0
break;
case 1:
commandbits = 0x00; //need to fill later for Channel 1
break;
default:
commandbits = 0x97;
break;
}
digitalWrite(SELPIN, LOW); //select ADC
SPI.transfer(commandbits); //send the command byte and recive the first responce simutaneously
byte response1 = SPI.transfer(0x00); //send dummy byte
byte responce2 = SPI.transfer(0x00); //dummy byte
digitalWrite(SELPIN, HIGH); //deselect ADC
adcvalue = (response1 << 8) | response2;
return adcvalue;
}
void loop() {
int value0 = read_adc(0);
// int value1=read_adc(1); //channel 1
Serial.print("Channel 0 value");
Serial.println(value0);
// Serial.print("Channel 1 value");
// Serial.println(value1);
Serial.println(" ");
delay(500);
}
I recall when first using the XIAO that the pin numbers were unusual. I don;t recall what I ended up with but I used a simple light a led sketch to determine what a physical poin was called. As I recall, they are out by one.
I did that to confirm the pinouts, it is was correct in that regard as well as using a serial.print of the MOSI, MISO, and SCK without defining the pins in a separate sketch
It looks like you need to handle the BUSY signal before fetching the data.
From datasheet
Since one clock cycle of the serial clock is consumed with
BUSY going high (while the MSB decision is being made),
16 additional clocks must be given to clock out all 16 bits
of data; thus, one conversion takes a minimum of 25 clock
cycles to fully read the data. Since most microprocessors
communicate in 8-bit transfers, this means that an additional
transfer must be made to capture the LSB.
In code we add a delay to skip the busy bit.
If that does not work the program must explicitly wait for the pulse on the BUSY pin.
#include <SPI.h>
//based off GPIO??
// #define CS 1
// #define MOSI 9
// #define MISO 8
// #define SPICLOCK 7
#define SELECT_PIN 1 // Chip select pin (any digital pin)
#define BUSY_PIN 2 // todo
uint16_t read_adc(int channel)
{
uint8_t commandbits = 0x97; // default, channel 0
// make command byte based on desired channel
switch (channel)
{
case 0:
commandbits = 0x97; // Start bit, Channel 0 + no shutdown between conversions.
break;
case 1:
commandbits = 0xD7;
break;
case 2:
commandbits = 0xA7;
break;
case 3:
commandbits = 0xE7;
break;
default:
commandbits = 0x97; // channel 0
break;
}
// Configure SPI settings: Speed(data sheet), MSBFIRST, SPI_MODE0
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE0));
digitalWrite(SELECT_PIN, LOW); // select ADS8341
SPI.transfer(commandbits); // send the command byte and receive the first response simultaneously
// need delay due to BUSY bit
delayMicroseconds(100); // to be tuned
uint8_t highBits = SPI.transfer(0x00); // send dummy byte
uint8_t lowBits = SPI.transfer(0x00); // send dummy byte
digitalWrite(SELECT_PIN, HIGH); // deselect ADC
SPI.endTransaction();
return highBits * 256 + lowBits;
}
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.println();
// Set pin modes
pinMode(SELECT_PIN, OUTPUT);
// unselect device at start
digitalWrite(SELECT_PIN, HIGH);
// initialize SPI library
SPI.begin();
}
void loop()
{
for (int channel = 0; channel < 4; channel++)
{
uint16_t value = read_adc(channel);
Serial.print("Channel ");
Serial.print(channel);
Serial.print(": ");
Serial.println(value);
Serial.println();
delay(500);
}
delay(1000);
}
Vref is 3.3V the COM is grounded, ive checked all grounds are connected. the decoupling caps are also in place. ive checked the DCLK, DIN and DOUT on the with an oscilloscope and found that the DCLK and DIN is getting to the board but im not getting the expected DOUT signal im getting the DCLK and DIN sum'd onto eachother at a much lower voltage. i dont have an image of it, i do have the DCLK and DIN though
Looks like you checked everyhing that could be checked.
Do you have a voltage connected to any of the ADS8341 inputs for testing, like an adjustable pot?
I guess it is possible that the IC could be bad.
yes, i have a pot connected to channel 0 with a range from 3.3-0V, ive also started to think that it could be the IC, ive ordered a new IC to test if thats the case.