1

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 similarily 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;
        }
    }
}
mch
  • 9,424
  • 2
  • 28
  • 42
Sam
  • 17
  • 3

2 Answers2

3
char read()
{
   while(1)
   {
       delay_ms(10000); // Stops the program
       break; // Then leaves

       if( (uart_RxChar() >= 65 && uart_RxChar() <= 90) || (uart_RxChar() >= 97 && uart_RxChar() >= 122))
       {

           uart_TxChar('k');
           Glow_GreenLED();
       }
       else 
       {
           Glow_RedLED();
           break;
       }
    }
 }

What you are trying to do won't succeed. You have to know that delays don't work that way. When your program will reach to the delay, it will stop everything - inputs too - during the delay. Meaning that whatever you do, it will wait then break.
The only way to achieve what you want to do using delays is to use threads, and have one thread waiting and mesuring the time, and an other thread waiting for an input. A way to do this without threads is using a timer and use the value of the timer as a condition to break. May be this link could help. Nevertheless, it is perfectly possible to have as much break conditions as you want :

if (CONDITION_1) 
    break;
else if (CONDITION_2) 
{ 
      printf("something");
      break;
}
else if (CONDITION_3) 
{
      someFunction();
      break;
}
else 
      break;
Badda
  • 1,329
  • 2
  • 15
  • 40
2

The first break; you get to, will break out of the loop, meaning,

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

is the same as

delay_ms(10000);

Your second break; as well as your whole if-else is "cut out" since the loop will break before

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • if I use the delay_ms(1000) outside while(1) loop, will it work? – Sam Jun 01 '17 at 08:40
  • What will work? the _delay_ms(1000)_ will be executed either way. If you want to delay AND check if any character other than alphabets is received, I suggest you'll look into threads – CIsForCookies Jun 01 '17 at 08:44
  • I think you didn't understand my question, I just want to give the function a specific time frame to execute and if didn't execute in that slot, it should return control to the next statement in main() > – Sam Jun 01 '17 at 09:07
  • using _delay_ms(X)_ will not give your _read()_ X ms to work, if that's what you meant. It will delay X ms and then continue from there - in your case, it will break. If you want _read()_ to execute in that time frame, you should consider threads, where one starts _read()_ and one is timing it – CIsForCookies Jun 01 '17 at 09:13