problem with reading register of 24 bit adc using spi

Hi,

I am using Arduino due and interface AD7193 adc.Here is the link to the datasheet.(http://www.analog.com/static/imported-files/data_sheets/AD7193.pdf.The chip select pin of adc is connected to pin 4 of arduino. My code is given below.When I upload this code,I am getting output all ones.Why is it so?I am writing to the mode register and configuration register in three steps,since they are 24 bits.Is this approch right?

#include "SPI.h"

int ss=4;

void setup() {
  
pinMode(ss, OUTPUT);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  Serial.begin(9600);

}

void loop() {
  
digitalWrite(ss,LOW);
  SPI.transfer(0x08);//command for communication reg to write to mode register
  SPI.transfer(0x0C);//command for mode register to set clock source (msb byte)                                 
  SPI.transfer(0x00);//command for mode reg to write setup register (middle byte)                    
  SPI.transfer(0x60);//command for mode register to set clock source (lsb byte)
  
     
  SPI.transfer(0x10);//command for communication reg to write to configuration register 
  SPI.transfer(0x00);//command for configuration reg to write to set gain,select channel,differential mode(msb byte)   
  SPI.transfer(0x01);//command for configuration reg to write to set gain,select channel,differential mode(middle byte)  
  SPI.transfer(0x1C);//command for configuration reg to write to set gain,select channel,differential mode(lsb byte)
     

  while(1)
  {
      char DataNotReady = 0x80;
      while(DataNotReady) // wait for end of conversion 
      {
          SPI.transfer(0x40);//command for comm register to read status register
          DataNotReady =SPI.transfer(0x00); // Read status register 
          DataNotReady &= 0x80;
      }
      SPI.transfer(0x58);//command for the comm register to read data register
      union{
    unsigned int dataregvalue;
    struct{
       byte low;
       byte mid;
       byte high;
       byte zero;
    };
  } converter;
  converter.zero = 0;
  converter.high=SPI.transfer(0x00);
  converter.mid=SPI.transfer(0x00);
  converter.low=SPI.transfer(0x00);
  Serial.println(converter.dataregvalue); //print the data register value,ie the adc output
  delay(1000);
  }
}

thanks

Your code looks confused.

Why are you trying to declare a union structure in the middle of your function ? It might work, but it is confusing.

And I don't see anywhere, that you are actually trying to read any data from your device.

This is the code I use to read an external MCP3208 12-bit adc device. I only read two bytes of data instead of your three bytes but the principle is the same.

To get N bytes of data from a spi device, you have to send it N+1 bytes of data. It sends nothing in response to the first byte. And then, when you send the j+1 st byte ( for j=1…N ), it sends the j’th byte of its response back to you.

uint16_t MCP3208::getVal( uint8_t chan )
{
    if ( chan < 0 || chan > 7 || CSpin < 1 ) return 0x0000 ;  // invalid channel number
    SPI.begin();
    digitalWrite( CSpin, LOW ) ;  // enable this chip
 
    byte s1 = ( chan > 3 )? ( 0x07 ) : ( 0x06 ) ;
    byte s2 = ( 0x03 & chan ) << 6 ;
    byte s3 = 0x00 ;

    SPI.transfer( s1 );
    byte c2 = SPI.transfer( s2 );
    byte c3 = SPI.transfer( s3 );

    digitalWrite( CSpin, HIGH );
    SPI.end() ;  // not sure if we need to end the SPI every time.

    uint16_t ret = (c2<<8)|c3 ;
    ret &= 0x0FFF ;
    return ret ;
}

It seems to me, you should be doing all that configuration stuff once, not every time you call loop( )

An arduino "int" is two bytes, so your union is crap. That may be your problem right there. You 24 bit answer needs to go into a long.