Go Down

Topic: Programming ADC1231 (Read 547 times) previous topic - next topic

brett_bender

           To anyone familiar with these chips, what is some simple code I can use to read the data from the chip, and print it to the serial monitor on an Arduino Uno.

I have this so far.
int data = 9;
int sclk = 10;
int pdwn = 11;
int clkin = 12;
int SPEED = 13;
void setup() {
  pinMode(SPEED, LOW);
  pinMode(clkin, LOW);
  pinMode(data, HIGH);
  Serial.begin(9600);
}                         
void loop() {
  if (digitalRead(data == LOW)){
    const int packetLength = 25;
    int SG[packetLength];
    for(int x = 0; x <= 25; x++){
      digitalWrite(sclk, HIGH);
       SG
  • = analogRead(data);
          Serial.println(SG
  • );
          digitalWrite(sclk, LOW);
        }
      }
    }
    I'm only storing it in an array now, because I might be storing the whole array on an SD card at the end of every loop. I am mostly concerned with getting a correct reading.

    Here's a link to the data sheet.
    http://www.ti.com/lit/ds/symlink/ads1231.pdf
    Thanks.

groundfungus

#1
Oct 28, 2013, 09:37 pm Last Edit: Oct 29, 2013, 02:02 am by groundfungus Reason: 1
Quote
pinMode(SPEED, LOW);
 pinMode(clkin, LOW);
 pinMode(data, HIGH);


pinMode is either INPUT, OUTPUT or INPUT_PULLUP.  You need  separate statements to set them low if they are outputs.
Quote
for(int x = 0; x <= 25; x++){
     digitalWrite(sclk, HIGH);
      SG    = analogRead(data);
     Serial.println(SG );

     digitalWrite(sclk, LOW);

analogRead on a digital input is not right.  Use digitalRead.  Your data is not going to an array.  SG will hold only the value of the last read bit.  The data is coming in serially.  Investigate shiftIn().  With 3 calls to shifIn() you can input all 3 bytes from the AD.

brett_bender

Digital read only reads whether the pin is high or low. The data pin goes low when it is ready for data to be read, and shifting sclk to high pushes out one of the 24 bits. How and where do I store and read this bit? I can't use digital read on the data pin, as it will only read 1 or 0. 

groundfungus

#3
Oct 29, 2013, 02:07 am Last Edit: Oct 29, 2013, 02:49 am by groundfungus Reason: 1
If you use shiftIn(data,sclk,MSB_FIRST) The clocking and bit shifting is done for you.
You would do like:
once the data line goes LOW,

Code: [Select]
byte highBYTE = shiftIn(data,sclk,MSB_FIRST);
byte middleBYTE = shiftIn(data,sclk,MSB_FIRST);
byte lowBYTE = shiftIn(data,sclk,MSB_FIRST);


I found this discussion from the forum that may be of help.  It is for a similar chip.
http://forum.arduino.cc/index.php/topic,128823.0.html

brett_bender

Thanks, that post was very helpful.

My new code looks like this,

Code: [Select]
int data = 9;
int sclk = 10;
int pdwn = 11;
int clkin = 12;
int SPEED = 13;
void setup() {
  // put your setup code here, to run once:
  pinMode(SPEED, OUTPUT);
  pinMode(clkin, OUTPUT);
  pinMode(data, INPUT);
  pinMode(sclk, OUTPUT);
  pinMode(pdwn, OUTPUT);
  digitalWrite(SPEED, LOW);
  digitalWrite(clkin, LOW);
  digitalWrite(data, HIGH);
  Serial.begin(9600);

}                         

void loop() {
    int32_t value = 0;
    digitalWrite(sclk, HIGH); //enter standby
    delay(1000);
    digitalWrite(sclk, LOW); //wake up
    //wait for data ready
    if (digitalRead(data) == LOW);
    value = shiftIn(data, sclk, MSBFIRST);
    value <<= 8;
    value |= shiftIn(data, sclk, MSBFIRST);
    value <<= 8;
    value |= shiftIn(data, sclk, MSBFIRST);
   
    digitalWrite(sclk, HIGH); // polling pulse
    digitalWrite(sclk, LOW);
    value = ((signed long) (value << 8)) >> 8;
   
    Serial.println(value,DEC);
   
}


All thats printed to the serial monitor is -1 every iteration. It prints -1 even if No pins are connected to the arduino. Would my breadboard circuit, or code be wrong? Thanks again.

brett_bender

Also when I use

Code: [Select]
while (digitalRead(data) == LOW);
    byte highBYTE = shiftIn(data, sclk, MSBFIRST);
    byte midBYTE  = shiftIn(data, sclk, MSBFIRST);
    byte lowBYTE  = shiftIn(data, sclk, MSBFIRST);
   
    digitalWrite(sclk, HIGH); // polling pulse
    digitalWrite(sclk, LOW);
   
   
    Serial.println(highBYTE);
    Serial.println(midBYTE);
    Serial.println(lowBYTE);


I get a constant value of 255 which would be 1.

groundfungus

Code: [Select]
digitalWrite(SPEED, LOW);
  digitalWrite(clkin, LOW);
  digitalWrite(data, HIGH);  // did you mean pdwn vs data.  This turns on the internal pullup on pin 9 as it is


For troubleshooting tie pins 11, 12, 13 (pdwn, clkin, SPEED) to +5 for pdwn, gnd to clkin and gnd to SPEED on the breadboard to remove them from the equation.

brett_bender

Tying the pins helped. The code is shifting data and printing it, but now with a balanced bridge the first byte is always 128, the second 0, and the third 0 as well. With the fourth resistor of the bridge swapped for another of different resistance, the first byte is always around some constant number, and the second and third bytes seem to be random.

groundfungus

#8
Oct 29, 2013, 08:49 pm Last Edit: Oct 29, 2013, 08:53 pm by groundfungus Reason: 1
Try the code from your reply #4.  That should yield a decimal number.  What do you mean first byte , second bye...?  In terms of high, middle low would be less ambiguous.  

brett_bender

Im pretty sure I've got it working. When balanced it reads 8388608, which is half of 2^24. This makes sense meaning that zero is in the middle of the chips range. When I change a resistor the value always changes to something else, that is rather constant. I haven't figured out quite the right conversion to get it to volts yet, but I'm close. Thanks for all your help.

groundfungus

See page 12 on the data sheet for output range.

brett_bender

Hmm so when balanced, the reading is the negative full scale input? Well, that's not right.

michinyon

My unhelpful advice would be to throw away that chip and get one that works by standard I2C  or SPI.    There are so many different things that could be causing your problem,  it is hard to know where to start.

Go Up