单片机中的ms级软定时器

因单片机中常有一些控制与延时关联,这些操作并不需要特别高的精度,为了少敲点字,故做了这个单片机软定时器,不足之处,欢迎指正。

timer.h

#ifndef __SOFT_TIMER_H__
#define __SOFT_TIMER_H__

#define EVENT_TYPE_ONESHOT 0
#define EVENT_TYPE_PERIODIC 1

#define TMR_POOL_SIZE 20
#define HANDLE int

typedef void (*TimerProc)(void*);

void TimerInit(void);
//HANDLE : -1 means fail,
HANDLE SetTimer(unsigned long uElapse,TimerProc pFunc,void *para,unsigned int Tmr_type);
void KillTimer(HANDLE hTmr);
void TimerServer(void); // call in main loop
void TimerSignal(void); // call it in timer isr
unsigned long TmrGetTime(void);

#endif

 

timer.c

  1 #include "timer.h"
  2 
  3 typedef struct _tagTimer{
  4     unsigned int elapse;
  5     unsigned int interval;
  6     TimerProc pFunc;
  7     void *para;
  8     unsigned char state;
  9     unsigned char event_type;
 10     unsigned char timeout;
 11 }Timer_Typedef;
 12  
 13 #define TMR_STATE_FREE 0
 14 #define TMR_STATE_RUNNING 1
 15 
 16 static Timer_Typedef _timerArray[TMR_POOL_SIZE]={0};
 17 static unsigned _tmr_tick = 0;
 18 
 19 void TimerInit(void)
 20 {
 21     
 22 }
 23 
 24 HANDLE SetTimer(unsigned long uElapse,TimerProc pFunc,void *para,unsigned int Tmr_type)
 25 {
 26     int i, unused_slot = -1;
 27     for(i=0;i<TMR_POOL_SIZE;++i)
 28     {
 29        if(_timerArray[i].state == TMR_STATE_FREE)
 30        {
 31             unused_slot = i;
 32             break;
 33        }
 34     }
 35     if(unused_slot !=  -1)
 36     {
 37         _timerArray[unused_slot].pFunc = pFunc;
 38         _timerArray[unused_slot].para = para;
 39         _timerArray[unused_slot].interval = uElapse;
 40         _timerArray[unused_slot].elapse = uElapse + _tmr_tick;
 41         _timerArray[unused_slot].event_type = Tmr_type;
 42         _timerArray[unused_slot].state = TMR_STATE_RUNNING;
 43     }
 44     return unused_slot;
 45 }
 46 
 47 void KillTimer(HANDLE hTmr)
 48 {
 49     if((hTmr >= 0)&&(hTmr < TMR_POOL_SIZE))
 50     {
 51        _timerArray[hTmr].state = TMR_STATE_FREE;
 52        _timerArray[hTmr].timeout = 0;
 53     }
 54 }
 55 
 56 void TimerServer(void)
 57 {
 58     int i = 0;
 59     Timer_Typedef* pTmr = _timerArray;
 60 
 61     for(i = 0;i<TMR_POOL_SIZE;++i)
 62     {
 63         if((pTmr->timeout)&&(pTmr->pFunc))
 64         {
 65             (*(pTmr->pFunc))(pTmr->para);
 66             pTmr->timeout = 0;
 67         }
 68         pTmr++;
 69     }
 70 }
 71 
 72 void TimerSignal(void)
 73 {
 74     int i = 0;
 75     Timer_Typedef* pTmr = _timerArray;
 76 
 77     ++_tmr_tick;
 78     for(i = 0;i<TMR_POOL_SIZE;++i)
 79     {
 80         if(pTmr->state == TMR_STATE_RUNNING)
 81         {
 82             if(pTmr->elapse == _tmr_tick)
 83             {
 84                 if(pTmr->event_type == EVENT_TYPE_PERIODIC)
 85                 { 
 86                     pTmr->elapse += pTmr->interval;
 87                 }
 88                 else
 89                 {
 90                     pTmr->state = TMR_STATE_FREE;       // kill timer
 91                 }
 92                 pTmr->timeout = 1;
 93             }
 94         }
 95         pTmr++;
 96     }
 97 }
 98 
 99 unsigned long TmrGetTime(void)
100 {
101     return _tmr_tick;
102 }

 

posted @ 2014-08-04 12:41  西瓜大叔  阅读(735)  评论(0编辑  收藏  举报