# Problem with while loop

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)
{
spi(0x40);
}


But it is not working.

My code explanation:

2. Reading ADC status register value and stored in adcStatus variable.
3. Checking the MSB bit is not equal to 1. (I am not sure if this while 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?

You need to re-read the ADC status register inside the loop; otherwise, you're just re-testing the bit you read the first time. Also, you need to reverse the sense of the test — you want to repeat the test if the bit is "1" and drop out of the loop once it switches to "0". It may also be necessary to toggle chip select to the ADC on each read, as shown in the comments below.

unsigned char CheckStatus(void)
{

• The expression (adcStatus & 0x80) can only have two different values: 0x00 or 0x80, depending on the state of bit 7 in adcStatus. – Dave Tweed Oct 16 '12 at 12:02