Soft timers in object-oriented C

January 15, 2013

Soft timer operation
Two operations are required by the soft timer object. The primary operation is usually done by the system timer interrupt service routine. This service routine will update a globally accessible variable once per every hard timer interrupt. In general, this variable will be updated as a simple C increment operation. An example of such an update is shown in Listing 1.

volatile unsigned long my_system_tick;
void interrupt my_timer_service (void)
{
    ++my_system_tick;
}

Listing 1. Update of a globally accessible hard timer variable

Yes, that is all there is to it . . . other than the timer service initialization and hardware setup. It's also important to note that the variable my_system_tick is an unsigned long. This is important because of the type of arithmetic that the soft timer object will do.

The other operation is what to do with the global tick variable. This variable will get "captured" by the operation of the soft timer. The capture and compare will be part of the soft timer variable instance operation as discussed below.

Soft timer interface
One of the very first things to do with any object-C-based project is to define the interface. The interface is nothing more than a C header file. Defining and writing the header file for the soft timer is shown in Listing 2.

#ifndef __GPTIMER_H
    #define   __GPTIMER_H
/****************************************************************************/
/*    FILE: gpTimer.h                                                       */
/*                                                                          */
/*    A general purpose C/C++ timer object                                  */
/*                                                                          */
/*    BY:   Ken Wada                                                        */
/*          12-January-2013                                                 */
/*                                                                          */
/****************************************************************************/
    #define   GP_MSEC_PER_TIC     1     /* Each TIC == 1 millisecond        */
    #define   TIC_PER_SEC         (1000/GP_MSEC_PER_TIC)
    typedef unsigned char (*TMR_IDLEFN)(void);
/****************************************************************************/
/*    A structure that defines a generic timer type.                        */
/****************************************************************************/
    typedef struct general_purpose_timer_object  {
      unsigned long lastCount;          /* Last saved count for this timer  */
      unsigned long timeOut;            /* Number of tics to timeout        */
      unsigned char timedOut;           /* time-out status                  */
    } GPTIMER;

    #ifdef  __cplusplus
      extern  "C" {
    #endif
      void  GPTIMER_init            (GPTIMER *_this, const unsigned long tics);
      void  GPTIMER_reTrigger       (GPTIMER *_this);
      int   GPTIMER_isTimedOut      (GPTIMER *_this);
//      void  GPTIMER_waitForTick   (GPTIMER *_this);
//      long  GPTIMER_timeLeft      (GPTIMER *_this);
//      void  GPTIMER_waitForTick_fn(GPTIMER *_this, TMR_IDLEFN idle_fn);

      /*****
        The following are the system call-backs needed for this library
      ***/
      unsigned long sysRetrieveClock    (void);

    #ifdef  __cplusplus
      }
    #endif
#endif

It is important to note that the general format of an object "C" interface is a function which references a pointer to the object as the 1st element in the call list. In this example, we define the object instance using the user defined symbol _this. We do this because we may want to use this simple object in C++ applications too.
< Previous
Page 2 of 5
Next >

Loading comments...

Parts Search Datasheets.com

KNOWLEDGE CENTER