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.
Running of a Sequence of Tasks at a Defined Rate
Moderator: Benj
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
- Contact:
Re: Running of a Sequence of Tasks at a Defined Rate
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
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
- Attachments
-
- Example.fcf
- (19.06 KiB) Downloaded 241 times
- Jay Dee
- Posts: 398
- Joined: Tue Aug 18, 2009 6:42 pm
- Has thanked: 121 times
- Been thanked: 154 times
- Contact:
Re: Running of a Sequence of Tasks at a Defined Rate
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.
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.
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
- Contact:
Re: Running of a Sequence of Tasks at a Defined Rate
Hi,
Thanks for self-clarification and giving chance to second thought..
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
Thanks for self-clarification and giving chance to second thought..
default is 20Hz and LCD is at 2 Hz that's right. But CAN was not 10Hz.i.e in every leg = 20Hz, every other leg = 10Hz, only one leg = 2 Hz
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 see in your example the IF statement would run 4 times to sample each of the switchs and thus sample them at 5Hz
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
- Attachments
-
- Example_v2.fcf
- (18.08 KiB) Downloaded 244 times
- Jay Dee
- Posts: 398
- Joined: Tue Aug 18, 2009 6:42 pm
- Has thanked: 121 times
- Been thanked: 154 times
- Contact:
Re: Running of a Sequence of Tasks at a Defined Rate
Thanks Enamul,
Thats good stuff. Worked the general idea into a small project that I'm using this weekend, with good results.
Cheers,
J.
Thats good stuff. Worked the general idea into a small project that I'm using this weekend, with good results.
Cheers,
J.
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
- Contact: