| Murray R. Van Luyn 06/21/09 23:10 Read: 705 times Msg Score: +1 +3 Informative -2 Overrated |
#166323 - Pseudo timers make programming delays easy. |
Heres one for more recent initiates to microcontroller programming in C. More experienced programmers are welcome to assist me in improving this code, provided they are civil.
Pseudo timers may be useful when non-blocking program delays are required. Multiple pseudo timers may be implemented using just the one microcontroller hardware timer. The number of timing elements available to the programmer is then not restricted to microcontroller timer hardware alone. The Timers_0.0 software module implements four, millisecond precision countdown timers, numbered 0 through 3. Concurrent, non-blocking delays between approximately 1ms to 65 seconds are possible. It is a simple matter to set a timer to expire after a given period, and then to reiteratively test for timer expiry during program execution. A simple illustrative example that flashes an indicator LED at 0.5Hz follows: #include "Timers.h"
sbit LED = P2^6;
void main(void)
{
// Initialise the Timers module.
timers_init();
// Round Robin task management, reiterative execution loop.
while(1)
{
// Flash an LED output continuoulsy at 0.5Hz, 50% duty cycle.
if (timers_is_expired_timer(0))
{
// Invert LED output.
LED = ~LED;
// Set timer 0 to expire in 1000ms.
timers_restart_timer(0, 1000);
}
// Additional Round Robin task managed tasks.
}
}
Happily, it is no more difficult to implement multiple, concurrent delays using the pseudo timer module. Here we use 2 timers to flash 2 indicator LEDs at differing frequencies and duty cycles: #include "Timers.h"
sbit LED_1 = P2^6;
sbit LED_2 = P2^7;
void main(void)
{
// Initialise the Timers module.
timers_init();
// Round Robin task management, reiterative execution loop.
while(1)
{
// Flash an LED output continuoulsy at 0.5Hz, 50% duty cycle.
if (timers_is_expired_timer(0))
{
// Invert LED output.
LED_1 = ~LED_1;
// Set timer 0 to expire in 1000ms.
timers_restart_timer(0, 1000);
}
// Flash another LED continuously at 1.3Hz, 33% duty cycle.
if (timers_is_expired_timer(1))
{
if (LED_2)
{
// Turn LED on.
LED_2 = 0;
// Set timer 1 to expire in 250ms.
timers_restart_timer(1, 250);
}
else
{
// Turn LED off.
LED_2 = 1;
// Set timer 1 to expire in 500ms.
timers_restart_timer(1, 500);
}
}
// Additional Round Robin task managed tasks.
}
}
The Timers_0.0 software module is written 8051/ 8052 variants in Keil C, and is available for free download from my homepage at http://members.iinet.net.au/~vanluynm/ Please link only to the root webpage as the module's URL will change with revisions. I hope you may find this software module useful. Regards, Murray R. Van Luyn. |
| Topic | Author | Date |
| Pseudo timers make programming delays easy. | Murray R. Van Luyn | 06/21/09 23:10 |
| volatile + racing condition | Per Westermark | 06/21/09 23:55 |
| slow processors | Per Westermark | 06/22/09 01:27 |
| You beat me to it... | Jez Smith | 06/22/09 01:50 |
| Timers_0.1 available. | Murray R. Van Luyn | 06/22/09 01:52 |
| SDCC | Jan Waclawek | 06/22/09 02:19 |
| ISR defining with SDCC | Mahesh Joshi | 06/22/09 02:42 |
| oh, I just read it in the manual | Jan Waclawek | 06/22/09 02:50 |
| only conditionally, as #ifdef SDCC | Andy Neil | 06/22/09 03:20 |
| SDCC and ISRs | Andy Peters | 06/23/09 14:17 |
| Prototyping ISRs | Andy Neil | 06/23/09 16:04 |
| you can see it as if.... | Jan Waclawek | 06/23/09 16:39 |
| SDCC Quirk? | Andy Neil | 06/23/09 16:56 |
internals of SDCC | Maarten Brock | 07/08/09 14:31 |
| duh | Andy Peters | 06/24/09 14:49 |
| Too quick | Per Westermark | 06/22/09 02:38 |
| I see something else... | Jan Waclawek | 06/22/09 02:53 |
| That helped. | Murray R. Van Luyn | 06/22/09 22:33 |
| Oops! Timers_0.2 available. | Murray R. Van Luyn | 06/22/09 22:23 |
| you persist | Erik Malund | 06/23/09 07:25 |
| Good idea! | Andy Neil | 06/22/09 01:46 |
| atomicity | Erik Malund | 06/22/09 08:13 |
| No | Jez Smith | 06/23/09 08:40 |
| I gladly, click on a link .... | Erik Malund | 06/23/09 08:53 |
| Direct link | Jon Ledbetter | 06/23/09 10:54 |
| that was clearly possible, I wonder why ... | Erik Malund | 06/23/09 14:11 |
| one more thing, now we are digging deep | Erik Malund | 06/25/09 06:37 |



