I am trying to read external ADC AD7798 values using ATmega32-A controller. In the datasheet, Status register Bit 7 (SR7
) indicates the conversion is finished or not:
Ready Bit. Cleared when data is written to the data register. Set after the data register is read or after a period of time before the data register is updated with a new conversion result to indicate to the user not to read the conversion data. It is also set when the part is placed in power-down mode. The end of a conversion is indicated by the DOUT/RDY pin. This pin can be used as an alternative to the status register for monitoring the ADC for conversion data.
Now I want to write code to check if the Status register MSB (Bit7) is 0
or not. So when it is 0
, only then can I issue a read command.
I have written code like this:
unsigned char CheckStatus(void)
{
char adcStatus;
spi(0x40);
adcStatus = spi(0xFF);
while((adcStatus & 0x80)!=0x80);
return adcStatus;
}
But it is not working.
My code explanation:
- Issuing the read command to read ADC status register.
- Reading ADC status register value and stored in
adcStatus
variable. - Checking the MSB bit is not equal to
1
. (I am not sure if thiswhile
loop is correct or not.)
I want to check if the Status register MSB is not equal to 1
so that I can issue the read command (0x58
) to read in the ADC values. If the status register MSB is equal to 1
then I can't read ADC values.
What am I doing wrong?