
Originally Posted by
HankMcSpank
....but I need a whole heap of different pauses (& some quite longish...I've always had an epic fail when I've tried to use timers for long pauses!) ....now I realise I could just reset the timer preload value accordingly each time I need a different pause, but ....aaargh ....brainache. I guess that's what it comes down to... a solution that avoids brainache ...
Here is pseudocode that blinks 2 LEDs at different speeds (1Hz and 2Hz) and does not use any pause (blocking delay) type commands. You can easily expand it to any number of LEDs (or tasks) with no performance degradation. It uses a single hardware timer to provide a system tick (also known as a heartbeat). It can be used as a template to create simple multitasking applications.
Code:
sub timer1 interrupt
inc(cyctick1)
inc(cyctick2)
if cyctick1 > 500 ' 500mS delay, 1st. counter
cyctick1 = 0 ' reset counter
cycflag1 = 1 ' set flag
endif
if cyctick2 > 250 ' 250mS delay, 2nd. counter
cyctick2 = 0 ' reset counter
cycflag2 = 1 ' set flag
endif
end sub
sub usrfunction1
toggle LED0
end sub
sub usrfunction2
toggle LED1
end sub
main:
cyctick1=0:cyctick2=0 ' Initialize variables
ccyflag1=0:cycflag2=0 ' "
timer1 = 1000 ' set up timer1 to interrupt every 1mS
while true
if cycflag1=1 then ' check if 500mS has expired
cycflag1 = 0 ' reset flag 1
do userfunction1
endif
if cycflag2=1 then ' check if 250mS has expired
cycflag2 = 0 ' reset flag 2
do userfunction2
endif
wend
Bookmarks