how to break a endless loop?

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all. I learned a lot of this forum, but this time I can't find the answer on this problem.

I have a midi in and out connection.  I send a midi sysex command to request info from an external device. To read the midi in sysex messages I use this part of code 

 

unsigned char getMidiSysExBit(void)
{	
    while (! (UCSRA & (1 << RXC))) {}
    return UDR;
}

It works fine as long as the external device is connected. If it's not connected I will stay (obviously) in an endless loop. How can I prevent this?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Indeed, most microcontroller applications will not want to stay in a loop like that waiting for input.  There is usually other stuff to do, like carrying out the previous command or looking at buttons/switches for change of control.

 

So instead, you make a conditional call to see if there is any input.  In PC programming, it often used to be called kbhit() or peek().  If null is never a valid charcter for your application (which will NOT be true for binary data streams) one can do something like

 

 

if (UCSRA & (1 << RXC))
    {
        return UDR;
    }
else
    {
        return 0;
    }

...and then only process the return value if not 0.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The answer is in front of you - test the RXC bit to see if there is any data available rather than sit in the while loop. The ‘usual’ solution would be to make the reception interrupt driven and avoid your issue alltogether. There should be many examples of this on the web.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks both for your fast answer. I will check it out.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

or have a timer ISR that have a timeout counter for your loop.

 

then the loop will break if <char in UDR> or <timeout> 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

or just have a counter in the loop, and exit when the count reaches a certain value

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

2posts in 3 years?

I don't know what to think of that, seems like a long time lurker :)

 

For my project I always tend to think a bit about the global structure before writing code.

I do not use constructs that wait "endlessly" or stuff like delay_ms(10000000) which block all execution.

The overall idea is to never wait for anything, but simply return if a condition is not met, much like as theusch suggests.

I tend to put all code in "threads" even though they are really simple flat structures.

For the more "complicated" "threads" I use state machines.

State machines are really handy if you want to program some sequence of events or if you want to wait for different events at different moments in time.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
 I always tend to think a bit about the global structure before writing code.

Sheesh - you'll be going all clawson on us in a minute and saying we should actually design stuff before jumping into coding ...

 

surprise

 

State machines are really handy

Absolutely!

 

laugh 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Mar 29, 2018 - 11:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Edit - Yep, I misunderstood.  Forget it.  S.

Last Edited: Fri. Mar 30, 2018 - 12:05 AM