PDA

View Full Version : Scheduling tasks



HenrikOlsson
- 8th September 2006, 13:51
Hi,
I have a program using a 100Hz interrupt as it's timebase. There's a variable (ISRLoop) counting the number of interrupts from 1 to 100 and then it starts over.

Now, I have a couple of different tasks that I need to run (in the main loop - not in the ISR) at various intervalls, like 1Hz, 5Hz, 20Hz, 50Hz and 100Hz for example. The 1Hz and 100Hz are easy but how do I trig the 20Hz and 50Hz ones? Without using something like:


'****Run 5Hz task******
If (ISRLoop = 20) OR (ISRLoop = 40) OR (ISRLoop = 60) '...and so on...

The above may work for the 2 and 5 Hz tasks but won't be very pretty for the 50Hz one.

I'm sure there's an elegant solution to this...anyone?

Thank you in advance!
/Henrik Olsson.

niels
- 8th September 2006, 14:17
Have a look at the modulus operator (//). It should simplify what you're trying to do.

mister_e
- 8th September 2006, 15:01
without the whole idea it's hard to say but
Select case ISRLoop
CASE 20,40,60,80

could be interesting.

HenrikOlsson
- 8th September 2006, 17:02
Niels,
Ahh..thank you! Just what I needed!

So basicly:


Main:
If ISRLoop // 10 = 0 then
Gosub 10HzTask
EndIf

If ISRLoop // 50 = 0 then
Gosub 2HzTask
EndIf

If ISRLoop // 2 = 0 then
Gosub 50HzTask
EndIf

If (ISRLoop + 1) // 2 = 0 then
Gosub Other50HzTask
Endif
Goto Main

Or something like that...
If I got that right it will work. I'll have to make sure that the tasks that may be run at any given time doesn't take longer than a total of 10mS because then ISRLoop will have been incremented before the 'scheduler' can check if the task should run. Need to do some carefull timing and planning here! And perhaps 'spread' the tasks out like with the 'Other50HzTask' above.

Thanks!
/Henrik Olsson.

Charles Linquis
- 8th September 2006, 23:45
One thing to be careful of - any technique that relies on exact counts gets into trouble if a task takes longer than the interrupt interval. If you are unsure of your execution speed, you should check for a count not just equal to, but instead check for equal to or above the specified number of ISRLoop counts.