Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/22/09 04:10
Read: 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.

List of 29 messages in thread
TopicAuthorDate
Pseudo timers make programming delays easy.            01/01/70 00:00      
   volatile + racing condition            01/01/70 00:00      
      slow processors            01/01/70 00:00      
         You beat me to it...            01/01/70 00:00      
      Timers_0.1 available.            01/01/70 00:00      
         SDCC            01/01/70 00:00      
            ISR defining with SDCC            01/01/70 00:00      
               oh, I just read it in the manual            01/01/70 00:00      
                  only conditionally, as #ifdef SDCC            01/01/70 00:00      
               SDCC and ISRs            01/01/70 00:00      
                  Prototyping ISRs            01/01/70 00:00      
                     you can see it as if....            01/01/70 00:00      
                        SDCC Quirk?            01/01/70 00:00      
                           internals of SDCC            01/01/70 00:00      
                     duh            01/01/70 00:00      
         Too quick            01/01/70 00:00      
            I see something else...            01/01/70 00:00      
               That helped.            01/01/70 00:00      
            Oops! Timers_0.2 available.            01/01/70 00:00      
               you persist            01/01/70 00:00      
   Good idea!            01/01/70 00:00      
   atomicity            01/01/70 00:00      
      No            01/01/70 00:00      
         I gladly, click on a link ....            01/01/70 00:00      
            Direct link            01/01/70 00:00      
               that was clearly possible, I wonder why ...            01/01/70 00:00      
               one more thing, now we are digging deep            01/01/70 00:00      

Back to Subject List