Hi,
I'm trying to read a 32-bit word from an external ADC chip that interfaces with my arduino through SPI. I'm having trouble finding example projects.
I don't get anything out.
Here's my code:
#include <SPI.h>
int SS = 10;
int SCLK = 13;
int MOSI = 11;
int MISO = 12;
uint32_t testVal = 0;
void setup() {
pinMode(SS, OUTPUT);
pinMode(SCLK, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
Serial.begin(9600);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV16);
testVal = ReadAdc();
Serial.println(testVal);
}
void loop() {
// put your main code here, to run repeatedly:
}
uint32_t ReadAdc()
{
uint32_t value = 0;
digitalWrite(SS, LOW);
value = SPI.transfer(0x00);
value = (value << 8) | SPI.transfer(0x00);
value = (value << 8) | SPI.transfer(0x00);
value = (value << 8) | SPI.transfer(0x00);
digitalWrite(SS, HIGH);
return (value);
}
blh64
August 16, 2021, 4:38pm
2
Without knowing what ADC chip you are trying to talk to, it is difficult to say. Link to datasheet?
Typically, you have to WRITE to the chip and then READ back your data. I don't see any WRITEs happening in your code.
For anyone having difficulty with SPI and arduino, I got it working with this:
#include <SPI.h>
int chipSelect = 10;
const uint16_t CFR_REG_CONFIG = 0x8800;
/*
int SCLK = 13;
int MOSI = 11;
int MISO = 12;
*/
uint32_t testVal = 0;
void setup() {
pinMode(chipSelect, OUTPUT);
/*
pinMode(SCLK, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
*/
Serial.begin(9600);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV16);
ReadAdc32();
writeADC(CFR_REG_CONFIG);
}
void loop() {
testVal = ReadAdc16();
Serial.println(testVal);
delay(500);
}
uint16_t writeADC(uint16_t configVal)
{
digitalWrite(chipSelect, LOW);
SPI.transfer(configVal >> 8);
SPI.transfer(configVal);
SPI.transfer(0x00);
SPI.transfer(0x00);
digitalWrite(chipSelect, HIGH);
}
uint32_t ReadAdc32()
{
uint32_t value = 0;
digitalWrite(chipSelect, LOW);
value = SPI.transfer(0x00);
value = (value << 8) | SPI.transfer(0x00);
value = (value << 8) | SPI.transfer(0x00);
value = (value << 8) | SPI.transfer(0x00);
value = value >> 4;
digitalWrite(chipSelect, HIGH);
return (value);
}
uint32_t ReadAdc16()
{
uint32_t value = 0;
digitalWrite(chipSelect, LOW);
value = SPI.transfer(0x00);
value = (value << 8) | SPI.transfer(0x00);
value = value >> 2;
digitalWrite(chipSelect, HIGH);
return (value);
}
system
Closed
December 15, 2021, 9:14am
5
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.