Software timer array

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi All,

I only ever post problems, so I thought I'd post my solution to having loads of software timers and ending up with an interrupt routine source code that generally looks confusing to decode.


#define LCD_REFRESH	250
#define ONE_SECOND	12500

/* define a structure for a timer */
struct timerdef {
	unsigned int time;
	unsigned int period;
	} timer[] = {
	{LCD_REFRESH, LCD_REFRESH},
	{ONE_SECOND , ONE_SECOND}
	};

/* define a pointer to a function */
typedef void (*pFunction)(void);

/* define prototypes for timer functions */
void tsr_lcdrefresh( void );
void tsr_onesecond( void );

/* initalise an array of timer functions */
pFunction timer_functions[] = {
	tsr_lcdrefresh,
	tsr_onesecond
	};

/* Timer Service routine for lcdrefresh */
void tsr_lcdrefresh( void )
{
 /* do something here */
}
/* End Timer Service Routine */


/* Timer Service routine for onesecond */
void tsr_onesecond(void)
{
 /* do something here */
}
/* End Timer Service Routine */


void software_timers(void)
{
/* use a local variable for looping */
unsigned char i;

	/* run the software timers */
	for( i=0; i

I an implimentation of this code in a sligthly different form to scan switches too, and then have an array of pointers to functions to service key presses.

This seems to make my life easier when I want to add another software timer.

Does anyone else impliment software timers in this way, or maybe slightly differently/better?

Best Regards,

Brian Sidebotham.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here's a timer implementation I once wrote that
uses linked lists. Beware, the list entries are being
malloc()ed so this makes most sense if your
application already uses malloc(). Otherwise, supply
your own malloc() that just returns a pointer from
a fixed array if you want.

http://www.sax.de/~joerg/avr-timer/

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah interesting, I had considered the length of the timer routines as a possible problem if you're not careful, but being able to prioritise things into higher and lower priorities (two levels) should make things less critical in the timer int.

I have added a few things to the structure therefore, and modified the structure to include the function pointer which makes more sense...


#define NUMBER_OF_TIMERS 2
#define LCD_REFRESH	250
#define ONE_SECOND	12500


/* define a pointer to a function */
typedef void (*pFunction)(void);


/* define prototypes for timer functions */
void tsr_lcdrefresh( void );
void tsr_onesecond( void );


/* define a structure for a timer */
struct timerdef {

	unsigned int time;
	unsigned int period;
	unsigned char priority;
	unsigned char flag;
	pFunction func;
	
	} timer[] = {
	/* time, period, prioirty, flag, func */
	{ LCD_REFRESH, LCD_REFRESH, 0,  0, tsr_lcdrefresh }, 
	{ ONE_SECOND , ONE_SECOND, 1, 0, tsr_onesecond }
	
	};
/* end the timer structure */

void software_timers_hp(void)
{
unsigned char i;

	/* run the software timers */
	for( i=0; i

Thanks, this is a bit better now.

Best Regards,

Brian Sidebotham.