Page 1 of 1

Running of a Sequence of Tasks at a Defined Rate

Posted: Wed Aug 15, 2012 3:20 pm
by Jay Dee
Hi Guys,
Looking at another style of program structure,
I'd like to create a program that works through a sequence of events but does not stall/delay whilst waiting for external events and executes these tasks at a reasonably consistent rate,

A very simple example project might have the following tasks,

Sampling 4 external inputs at roughly 20Hz each.
Transmit a CAN message at 10Hz
Update of LCD values at 2 Hz
Update all of the other calculations as fast as possible.

I guess this is basically looking for a tidy and flexible way to use a timer based interrupt, just not sure how to implement this.
I understand setting up an interrupt to roll over at a specific frequency and use this to index a counter or something but how do I trigger these events from the counter? Use a sequence of IF statements ?
I also can see that you need some headroom in processor capacity to enable each task to be completed, before then next task has to run.

Any good stripped down examples? :)
Thanks,
J.

Re: Running of a Sequence of Tasks at a Defined Rate

Posted: Wed Aug 15, 2012 7:42 pm
by Enamul
Hi
Here is an example using TMR1 to have different frequencies to read switches, transmit CANmsg and update LCD..It's not a complete code but a skeleton which you can use to write your code..
Please feel free to ask me if your are not clear.
Enamul

Re: Running of a Sequence of Tasks at a Defined Rate

Posted: Wed Aug 15, 2012 9:00 pm
by Jay Dee
Hi Enamul, Aaah thanks for that. Cleared that query up.

Breaking it down

A Timer based interrupt is set-up for 20Hz
The Interrupt increments a Counter and rolls over at value 10.
Thus the counter is incrementing at 20 Hz and rolls over at 2 Hz
A flag called Scan is also set 'true' each time the interrupt runs, Scan is thus reset at 20Hz.

So for the first bit;
Use a Switch statement to run specified macros in response to each of the counter increments.
I can place a given macro call in several of the switch statements 'legs' to increase the frequency which they are run.
i.e in every leg = 20Hz, every other leg = 10Hz, only one leg = 2 Hz

For the second bit
An IF statement triggered by the Scan flag ensures these tasks are processed at 20Hz.
I see in your example the IF statement would run 4 times to sample each of the switchs and thus sample them at 5Hz but I can clearly see the idea and can play with the concept now you have explained it.
Much thanks, easy when you know how! :)
J.

Re: Running of a Sequence of Tasks at a Defined Rate

Posted: Wed Aug 15, 2012 9:40 pm
by Enamul
Hi,
Thanks for self-clarification and giving chance to second thought..
i.e in every leg = 20Hz, every other leg = 10Hz, only one leg = 2 Hz
default is 20Hz and LCD is at 2 Hz that's right. But CAN was not 10Hz.
I see in your example the IF statement would run 4 times to sample each of the switchs and thus sample them at 5Hz
I noticed that after posting and thought you can change easily..I am not sure whether you want to sense one sw at a time or all four at a time..If all four then 20 Hz but if as posted then 5Hz is sampling freq.

I have posted v2 with CAN sampling correction..now int freq is 80Hz so sw freq is 20..LCD 2 Hz and CAN 10 Hz
Enamul

Re: Running of a Sequence of Tasks at a Defined Rate

Posted: Fri Aug 17, 2012 11:34 am
by Jay Dee
Thanks Enamul,
Thats good stuff. Worked the general idea into a small project that I'm using this weekend, with good results.
Cheers,
J.

Re: Running of a Sequence of Tasks at a Defined Rate

Posted: Fri Aug 17, 2012 12:11 pm
by Enamul
Nice to know that it worked.. :)
Enamul