Loop is executing infinite times

avrcmicrocontrollerprogramming

I am working with an ATmega32-A microcontroller. Using USART communication, I am displaying data on monitor and am using switch case to select different commands. In one of the commands I have written the following code:

 case(DATA):

  init_io();
  statusreg = AD7798_8(0x40, 0xFF); 
  printf("status register %02x\r\n", statusreg); 

  if (statusreg != 0x80) 
      printf("unexpected power on status %02x\r\n", statusreg); 

  while(1){
            while ((statusreg & 0x80) != 0);                 // wait till ADC is ready 
            adc = AD7798_16(0x58, 0xFFFF);                   // read register 
            printf("ADC value is %04x\r\n", adc); 
          } 
  Command=0;
  break;

But the above code is printing the ADC value infinite times. I have changed the while loop to the following

   while(1){
            while ((statusreg & 0x80) != 0);                 // wait till ADC is ready 
            adc = AD7798_16(0x58, 0xFFFF);                   // read register 
            printf("ADC value is %04x\r\n", adc); 
          } 

After that it is printing only once, but does not accept any more commands. I have to re-build if I want to enter any other command. Any help appreciated.

Best Answer

A while(1){} loop never exits, (unless you have a break inside) so it will always stick there. Why do you need the while(1) anyway? It looks like you are only reading once from the ADC.
If you do want to loop more than once, you need to add a condition to equal 0 when whatever it is you want to do inside has finished.