This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Break condition in while loop

Following is my program. I am trying to read characters at different baud rate and if no character is received within 10 seconds I want to break out of the loop and similarly if any character other than alphabets is received, I again want to break out of the loop? How can I do that. Please guide me.

char read()
{ while(1) { delay_ms(10000); break;

if((uart_RxChar() >= 65 && uart_RxChar() <= 90) || (uart_RxChar() >= 97 && uart_RxChar() >= 122)) { uart_TxChar('k'); Glow_GreenLED(); } else { Glow_RedLED(); break; } }
}

Parents
  • Think about the loop more logically so things occur in parallel and aren't serialized. ie Loop 10000 times waiting 1ms, checking the availability as it occurs, not after 10 seconds has expired. Better yet use some time stamp that is independent of the loop itself, and check for characters as they arrive, at high baud rates this may exceed one per millisecond, and the goal is to not miss something by ignoring things for an arbitrary period.

    Each time you call the function it likely pulls another character, not the same one, read it once into a variable, then range check the variable.

Reply
  • Think about the loop more logically so things occur in parallel and aren't serialized. ie Loop 10000 times waiting 1ms, checking the availability as it occurs, not after 10 seconds has expired. Better yet use some time stamp that is independent of the loop itself, and check for characters as they arrive, at high baud rates this may exceed one per millisecond, and the goal is to not miss something by ignoring things for an arbitrary period.

    Each time you call the function it likely pulls another character, not the same one, read it once into a variable, then range check the variable.

Children