I am having problems getting a correct value from the Data Output Register (DOR). I am using an ADS1212 in slave mode with an Arduino as the master and I've mimicked the circuit configuration in Figure 31 on page 32 of the data sheet (2MHz clock).
I am able to communicate with the chip, which I verified by writing to and reading from the command register. However, I am a bit stumped on how to process the data read the DOR. I am receiving values that are inconsistant even when I bring the analog input to GND. For example, I may receive a value of 0 on one read but then -263,000 and then - 34,000. I have tried to tie AinP (Pin 1) to GND and AinN (Pin 2) to GND, AinP to 5V, 3.3V, 2.5V...but all with the same result.
Here are the specific questions I have:
- Is my reading of the DOR and conversion to 2's complement correct?
- How does the effective resolution effect the output? For example, if my effective resolution with be 20 bits, do I drop the first 4 bits of the number, or should I expect to receive 0 on those bits?
- Is each bit of resolution tied back to REFin or AVdd? So if I have 20 bits of resolution, 1,048,576 places, will each bit be 5V/1,048,576?
Maybe I am missing something really basic or fried the chip at some point, not sure, but I'd appreciate any help. My code is below. FYI, I am using the SPI library from Arduino.
///// Code /////
#include <SPI.h>
//SPI
const byte PIN_SCK = 13; //clock
const byte PIN_MISO = 12; //master in slave out
const byte PIN_MOSI = 11; //master out slave in
const byte PIN_SS = 10; //slave select
const byte DRDY = 9; //data ready
byte Insr;
byte DorOne;
byte DorTwo;
byte DorThree;
byte CmrOne;
byte CmrTwo;
byte CmrThree;
byte CmrFour;
byte OcrOne;
byte OcrTwo;
byte OcrThree;
byte FcrOne;
byte FcrTwo;
byte FcrThree;
long analogOutput;
long analogOutputTwos;
void setup(){
Serial.begin(9600);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV128);
pinMode(SS,OUTPUT);
pinMode(DRDY,INPUT);
digitalWrite(PIN_SS,HIGH);
delay(1000);
while(digitalRead(DRDY)==HIGH){}; //wait until the chip is ready
digitalWrite(PIN_SS,LOW);
SPI.transfer(B01100100); //instruction to tell ADS to go to CMR byte 3
SPI.transfer(B01010010); //write to the CMR byte 3
SPI.transfer(B00100000); //write to the CMR byte 2 go through self calibration
SPI.transfer(B00000000); //write to the CMR byte 1
SPI.transfer(B00001100); //write to the CMR byte 0
delay(20);
digitalWrite(PIN_SS,HIGH);
delay(250);
}
void loop(){
if(digitalRead(DRDY)==LOW){
digitalWrite(PIN_SS,LOW); //get the chip ready to receive commands
SPI.transfer(B11000000); //instruction register to read 3 bytes from Dor
DorThree = SPI.transfer(0x00);
DorTwo = SPI.transfer(0x00);
DorOne = SPI.transfer(0x00);
digitalWrite(SS,HIGH);
analogOutput = DorThree;
analogOutput = analogOutput<<8; //bit shift to make room for byte 2
analogOutput |= DorTwo;
analogOutput = analogOutput<<8; //bit shift to make room for byte 1
analogOutput |= DorOne;
analogOutputTwos = ~analogOutput; //convert to twos complement
analogOutputTwos = analogOutputTwos + 1;
Serial.print("Decimal");
Serial.print('\t');
Serial.println(analogOutputTwos);
Serial.print("Binary");
Serial.print('\t');
Serial.println(analogOutputTwos,BIN);
Serial.print("Dor3");
Serial.print('\t');
Serial.println(DorThree,BIN);
Serial.print("Dor2");
Serial.print('\t');
Serial.println(DorTwo,BIN);
Serial.print("Dor1");
Serial.print('\t');
Serial.println(DorOne,BIN);
Serial.println();
}
delay(250);
}