Hey guys, I would like to ask for help in a project.
I use the AD7715 AD converter to read voltage values, but I get inconsistent results. I looked through a lot of forum posts with this ADC, and after many tries, I got this code, that samewhat works. There are 2 problems:
- The calibration of the ADC doesn't end as expected. When I check the setup register value, it's 0 or 255 most of the time. When it gives the correct value (40), it continues the code, but then I get the 2nd problem.
- The read values are 128 / 0 (the 2 bytes of the read data) at 0V analog input, and 255 / 255 at 2,5V and above. The values between these two voltages scale up good, it gives linear scale, which is good, but the range below 128 / 0 is not usable this way.
I use an Arduino Uno with a 9V adapter, this provides the power to the circuit through the 5V pin. The AD7715-5 ADC has a 2,4576 MHz crystal as internal clock, and an AD780 reference IC provides the reference voltage. The SPI-specific wires connect as the Arduino reference provides (MOSI / Din: pin 11, MISO / Dout: pin 12, SCLK: pin 13).
The circuit is exactly the same as the datasheet provided (including the capacitors). The drawing is attached.
Here is the code currently:
#include<SPI.h>
int DRDY = 8;
int read1 = 0;
int read2 = 0;
int busy = 1;
int result = 0;
void setup() {
pinMode(DRDY, INPUT);
SPI.begin();
SPI.beginTransaction(SPISettings(60, MSBFIRST, SPI_MODE3));
Serial.begin(115200);
while(read1 != 40) {
calibrate();
}
}
void loop() {
while(busy == 1) { // wairing for an avaliable result
busy = digitalRead(DRDY);
Serial.println("busy");
}
SPI.transfer(0x38);
read1 = SPI.transfer(0);
read2 = SPI.transfer(0);
conversion(read1, read2);
delay(1000);
}
void calibrate() {
busy = 1;
while(busy == 1) {
busy = digitalRead(DRDY);
Serial.println("busy");
}
delay(50);
SPI.transfer(16); // 0 0 0 1 0 0 0 0 -> request to write to setup register
SPI.transfer(104); // 0 1 1 0 1 0 0 0 -> start self-calibration
delay(1000);
busy = 1;
while(busy == 1) { // waiting for calibration to finish
buys = digitalRead(DRDY);
Serial.println("busy");
}
SPI.transfer(24); // 0 0 0 1 1 0 0 0 -> request to read from setup
read1 = SPI.transfer(0);
Serial.print("Setup register value: ");
Serial.println(read1);
delay(1000);
}
void conversion(int read1, int read2) { // convert to decimal
result = (read1 * 256) + read2;
Serial.println(result);
}
Can this be an syncronization error, or maybe the calibration is not running correctly? Can somebody help me?