⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 intimers.c

📁 时间触发式51单片机嵌入式多任务系统
💻 C
字号:
/*------------------------------------------------------------------*-
 * FILENAME: intimers.c
 *
 * Copyright  2000-2004 By InterNiche Technologies Inc. All rights reserved
 *
 * Handles NicheTask task & application interval timers.
 *
 * MODULE: NTF
 *
 * ROUTINES: check_interval_timers(),in_timerkill(),
 * ROUTINES: in_timerset(), in_timerget(), 
 *
 * PORTABLE: yes

  使用方法:
  1.系统上电调用in_timerinit(),初始化计时器;
  2.定义_OS_INICHE_TIMERS(intimers.h)为1;
  3.需要计时器时,调用in_timerset()取得一个计时器;
  4.调用in_timerflash()开始计时;
  5.调用in_timerstatus()判断计时器是否在使用,用in_timerget()检测是否超时;
  6.调用in_timerstop()停止计时;
  7.调用in_timerkill()清掉计时器;

-*------------------------------------------------------------------*/

#include "os_cfg.h"
#include "os_cpu.h"
#include "intimers.h"


#if _OS_INICHE_TIMERS > 0


tyIntimer intimers[NUM_INTIMERS];

/*------------------------------------------------------------------*-
   FUNCTION: check_interval_timers()

   Check to see if any interval timers are ready to fire.

   RETURNS: NA
 -*------------------------------------------------------------------*/
static tByte tAutoData numtimers = 0;         // number of active timers

void check_interval_timers(void)
{
   tByte tAutoData i;
   tByte tAutoData found;                       // number of valid timers found

   if(numtimers) for(found = 0, i = 0; i < NUM_INTIMERS; i++)
   {
      if(intimers[i].Use)                       // is this timer active?
      {
         if(intimers[i].Start)
         {
            if(intimers[i].Count <= mFosc_Tick)    // timer ready fire?
            {
               intimers[i].Ok = TRUE;              // call user routine
            }
            else
            {
               intimers[i].Count -= mFosc_Tick;    // set next tmo
            }
         }
         // If we've examined all the active timers, return
         if(++found >= numtimers) break;
      }
   }
}

/*------------------------------------------------------------------*-
  FUNCTION: in_timerset()

   Create an interval timer

   PARAM1: number of milliseconds between callback calls

   RETURNS: timer ID if OK, else if table is full.
 -*------------------------------------------------------------------*/
tByte in_timerset(tWord msecs)
{
   tByte tAutoData  i;

   for(i = 0; i < NUM_INTIMERS; i++)
   {
      if(intimers[i].Use == FALSE)
      {
         intimers[i].Use = TRUE;
         intimers[i].Start = FALSE;
         intimers[i].Ok = FALSE;
         intimers[i].Count = msecs;
         numtimers++;
         return(i + 1);
      }
   }
   return(0);
}

#if _INTIMER_GETOK > 0
/*------------------------------------------------------------------*-
  FUNCTION: in_timerget()

   Get an interval timer

   PARAM1: number of timer

   RETURNS: timer OK flag is OK
 -*------------------------------------------------------------------*/
tByte in_timerget(tByte timer)
{
   return(intimers[timer - 1].Ok);
}
#endif

#if _INTINER_STATUS > 0
/*------------------------------------------------------------------*-
  FUNCTION: in_timerstatus()

   Get an interval timer

   PARAM1: number of timer

   RETURNS: timer OK flag is OK
 -*------------------------------------------------------------------*/
tByte in_timerstatus(tByte timer)
{
   return(intimers[timer - 1].Start);
}
#endif

/*------------------------------------------------------------------*-
  FUNCTION: in_timerflash()

   flash an interval timer

   PARAM1: number of timer

 -*------------------------------------------------------------------*/
void in_timerflash(tByte timer, tWord msecs)
{
   timer--;
   intimers[timer].Count = msecs;
   intimers[timer].Start = TRUE;
   intimers[timer].Ok = FALSE;
}

#if _INTIMER_STOP > 0
/*------------------------------------------------------------------*-
  FUNCTION: in_timerstop()

   flash an interval timer

   PARAM1: number of timer

 -*------------------------------------------------------------------*/
void in_timerstop(tByte timer)
{
   timer--;
   intimers[timer].Start = FALSE;
   intimers[timer].Ok = FALSE;
}
#endif

#if _INTIMER_KILL > 0
/*------------------------------------------------------------------*-
  FUNCTION: in_timerkill()

  Delete a timer created previously by a call to in_timerset()

  PARAM1: the timer to delete.

  RETURNS: 0 if OK, ENP error if timer not in list.
 -*------------------------------------------------------------------*/
tByte in_timerkill(tByte timer)
{
   if(timer > 0 && timer <= NUM_INTIMERS)
   {
      timer--;
      intimers[timer].Use = FALSE;
      intimers[timer].Start = FALSE;
      intimers[timer].Ok = FALSE;
      numtimers--;
      return(0);      /* OK return */
   }
   return(ERROR_PARAM);
}
#endif

#if _INTINER_INIT > 0
/*------------------------------------------------------------------*-
  FUNCTION: in_timerinit()

  init all timer 

 -*------------------------------------------------------------------*/
void in_timerinit(void)
{
   tByte tAutoData i;

   for(i = 0; i < NUM_INTIMERS; i++)
   {
      intimers[i].Use = FALSE;
      intimers[i].Start = FALSE;
      intimers[i].Ok = FALSE;
      numtimers = 0;
   }
}
#endif

#endif   /* INICHE_TIMERS */

/*------------------------------------------------------------------*-
  ---- END OF FILE -------------------------------------------------
-*------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -