Quantcast
Channel: MCS Electronics Forum
Viewing all articles
Browse latest Browse all 20577

BASCOM-AVR : Writing code to run multiple tasks at same time : REPLY

$
0
0
Strictly speaking, you cannot run two or more tasks at the the same time with an AVR. But you can certainly switch from task to task quickly enough that you get results from both more-or-less together. The major thing is to realise that you should never 'block' the processor by waiting for something to happen. Always you test whether something is 'ready', and do whatever you need to do to service that. If its not ready, just skip that task and move on to the next. The classic example is waiting for something to come in on a serial port. In the 'blocking' method, you do [code:1:a447866a66] input <var> 'wait for a variable to come in ... process the variable ... 'then do the next task [/code:1:a447866a66] In the non blocking method, you take advantage that Bascom can receive characters in the 'background' using an interrupt. [code:1:a447866a66] while ischarwaiting() = 1 'is anything in the input buffer ? ...process the input wend ... do the next task [/code:1:a447866a66] That way if there is nothing in the buffer, the while-wend is skipped, and the next bit of code is executed. This applies also to many other tasks, not just to interrupts ( although that is also common ). There is a good example in Bascom App note AN177 where several different sensors are read and the results computed while waiting for other sensors to get their readings. While the code is complex, just looking at the bits where the waiting is done ( without actually waiting ) is very instructive.

Viewing all articles
Browse latest Browse all 20577

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>