print value from 24 bit register of adc

Hi,
I am new to arduino.
I want to print 24 bit value from an adc register .Will the following do?
//read adc register
byte High=SPI.transfer(0x00);
byte Middle=SPI.transfer(0x00);
byte low=SPI.transfer(0x00);
adcvalue=(High<<16) | (Middle<< 8 ) | (low);
Serial.print(adcvalue);

In the above,is it ensured that the most signigicant 8 bits will go to "High" variable.middle 8 bits to "Middle" variable and least significant 8 bits to "low" variable?

How is adcvalue declared ?

you can do qa first check and print the individual bytes to check if anything comes in...

I'd cast the byte values to "unsigned long" before shifting.

Also make sure that is the older that the SPI ADC transfers it's value. A datasheet will answer that.

You could try something like this:

  union{
    unsigned long adcvalue;
    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.print(converter.adcvalue);

I'm just a fan of unions is all :smiley:

And in the tradition of true K&R C language usage. :wink:

Such a shame they're not that portable.

Hi,

I am not getting the output.Now,when I try to read the id,the output is printed as 3,instead of 2.
My code to read AD7193 id is given below.

#include <SPI.h>
void setup()
{
  Serial.begin(9600);
  SPI.begin(4);
  SPI.setClockDivider(4,21);
  pinMode(4,OUTPUT);


}

void loop()
{
 digitalWrite(4,LOW);
 SPI.transfer(4,0x60,SPI_CONTINUE);//command for communication  register to read id register
 byte result = SPI.transfer(4,0x00,SPI_CONTINUE);//read id reg value
 Serial.println(result&0x0F);//id is 2
 delay(1000);

}

uaim:
Hi,

I am not getting the output.Now,when I try to read the id,the output is printed as 3,instead of 2.
My code to read AD7193 id is given below.

#include <SPI.h>

void setup()
{
  Serial.begin(9600);
  SPI.begin(4);
  SPI.setClockDivider(4,21);
  pinMode(4,OUTPUT);

}

void loop()
{
digitalWrite(4,LOW);
SPI.transfer(4,0x60,SPI_CONTINUE);//command for communication  register to read id register
byte result = SPI.transfer(4,0x00,SPI_CONTINUE);//read id reg value
Serial.println(result&0x0F);//id is 2
delay(1000);

}

Here is a different solution, use it just like a const uint32

struct{
  operator uint32_t() const {
    return (uint32_t( SPI.transfer(0x00) ) << 16) + (SPI.transfer(0x00) << 8) + SPI.transfer(0x00);
  }
} ADC_24Bit;

//Then to print or read the live value:
Serial.print( ADC_24Bit, HEX );