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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/12/07 04:25
Read: times


 
Msg Score: +1
 +1 Good Answer/Helpful
#144417 - Software timers
Responding to: ???'s previous message
Try searching for something like "Software Timer"

The general principle is that you use a single (hardware) timer to generate a timebase or "tick";
On each "tick", the hardware timer updates each of the currently-running software timers;
When a software timer expires it could call a function and/or set flags to notify the main code loop.

Something like this:
unsigned int timer_1;   // Countdown value for Timer-1
unsigned int timer_2;   // Countdown value for Timer-2
unsigned int timer_3;   // Countdown value for Timer-3
                        // etc...

void timer_isr( void )   // Hardware timer interrupt handler
                         // use appropriate compiler-specific syntax!
{
   if( timer_1 )
   {  
      // The countdown is running
      if( --timer_1 == 0 )
      {
         // The countdown has reached zero; ie, the timer has expired
         // Set flags and/or call functions as appropriate
      }
   }

   if( timer_2 )
   {  
      // The countdown is running
      if( --timer_2 == 0 )
      {
         // The countdown has reached zero; ie, the timer has expired
         // Set flags and/or call functions as appropriate
      }
   }

   if( timer_3 )
   {  
      // The countdown is running
      if( --timer_3 == 0 )
      {
         // The countdown has reached zero; ie, the timer has expired
         // Set flags and/or call functions as appropriate
      }
   }

   // etc, etc,...
}

You start a timer by setting its countdown (global variable) to a suitable non-zero value.

Depending on how many timers you have, it may be more efficient to put them into an array, or even a linked-list, and have a loop to handle them in the ISR...

This is exactly the kind of technique that an RTOS would use for its timers!

List of 44 messages in thread
TopicAuthorDate
ABOUT THE REAL TIME OS            01/01/70 00:00      
   Do you really need it?            01/01/70 00:00      
      Seconds thoughts            01/01/70 00:00      
         but it's a 51 on steroids!... and some philosophy            01/01/70 00:00      
            Definitions            01/01/70 00:00      
               I use the p89c51rd2            01/01/70 00:00      
               It is the architecture            01/01/70 00:00      
                  Mutlitasking on the OS            01/01/70 00:00      
                     I think I said something similar...            01/01/70 00:00      
                     please tell which            01/01/70 00:00      
                        extended stack in SRAM            01/01/70 00:00      
                           Stack expanded to MOVX            01/01/70 00:00      
                  another architecture shortcoming            01/01/70 00:00      
                     Independent of multitasking?            01/01/70 00:00      
                        HUH??            01/01/70 00:00      
                           Clarification            01/01/70 00:00      
                              Identity crisis            01/01/70 00:00      
                                 Oops            01/01/70 00:00      
                                 No crisis here            01/01/70 00:00      
                           My question clarified            01/01/70 00:00      
                              "stack inefficiency"            01/01/70 00:00      
                                 Stack            01/01/70 00:00      
               I disagree somewhat            01/01/70 00:00      
                  Multitasking            01/01/70 00:00      
   I think I need it!            01/01/70 00:00      
      no, you do not            01/01/70 00:00      
         I have made a PLC complier for MCS-51 by VB.            01/01/70 00:00      
            Multi-use timers            01/01/70 00:00      
               Could you help me?            01/01/70 00:00      
                  No            01/01/70 00:00      
                  Software timers            01/01/70 00:00      
                     Linked Lists            01/01/70 00:00      
                     Linked Lists for Software Timers            01/01/70 00:00      
               I do not know how to do?            01/01/70 00:00      
                  Use software timers            01/01/70 00:00      
                     how to use it point to task.            01/01/70 00:00      
                        what is "a task" in your application?            01/01/70 00:00      
                        What task??            01/01/70 00:00      
                  stop running            01/01/70 00:00      
      Unlilely            01/01/70 00:00      
   DS89C450 can handle the RTOS?            01/01/70 00:00      
      PLCs            01/01/70 00:00      
         Russell got it            01/01/70 00:00      
         Thanks russell!            01/01/70 00:00      

Back to Subject List