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

📄 task.c

📁 mcs51,2051,x86系列MCU
💻 C
字号:
/*
 *  ApBUILDER CODE FILE - Intel Corporation
 *
 *
 *  Purpose:      Task control code.
 *                TASK_CONTROL_BLOCK
 *
 *                The TCB functionality is a simple task scheduling system utilizing
 *                a single timer to provide multiple timed events at various intervals.
 *
 *                The system is flexible, but has the needed efficiency for embedded
 *                applications.
 *
 *                In main(), you will find the main task loop, where the task controller
 *                function iterates through the available task control lists.
 *                Each list contains TCB entries.  A TCB entry is composed of 2 items.
 *                First, is the Timer variable.  This is a 1 byte counter that is in 1
 *                of 3 states:
 *
 *                -1    Not Active
 *                0     Active
 *                >0    In the process of counting down to 0, prior to activation.
 *
 *                Second, is a function associated with the timer variable.  The function
 *                is executed each time the task controller iterates through the entry,
 *                and the assocoated timer is set to 0.  Each TCB list contains as many
 *                TCB entries as you wish.  Each TCB list has it's timer variables
 *                decremented periodicly.  The rate at which the block timers are
 *                decremented should be evident by the name of the list..
 *                ie..  Tenth_sec..  has all of its timers decremented every 100ms.
 *                By utilizing the TCB lists, you can schedule functions to execute on
 *                various cycles.  These functions can reset their respective timer
 *                variables to repeat the cycle, or can set the timer variables to -1
 *                to deactivate, effectivly causing a 1-shot type operation.
 *
 *                It is the responsibility of the hardware timer to decrement the most
 *                frequently updated task-list timer variables. (Hundredth_sec) at the
 *                required rate.  A routine in this TCB list is responsible for
 *                decrementing all the Tenth_sec timers at the appropriate rate.
 *                A routine in the Tenth_sec TCB list is responsible for decrementing
 *                all the Whole_sec timers.  In this way, you can acheive 3 lists, each
 *                with differing cycles, but all driven by a single timer resource.
 *
 *                By turning on the TASK_CONTROL_BLOCK_ENABLED switch in global.h,
 *                you enable the feature.
 *
 *
 *                The Software is provided "AS IS."
 *
 *                LIMITATION OF LIABILITY:    NEITHER INTEL NOR ITS VENDORS OR AGENTS
 *                SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
 *                INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
 *                CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
 *                OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 *                While we have made every attempt to completely test this code, we request that
 *                you personally test it completely prior to actual inclusion in your own projects.
 *
 *  Compiler:     Developed using Compass251 from Production Languages corporation.
 *
 *  Ext Packages: None
 *
 *  Author:       Brad B.
 *
 *  Revisions:
 *
 *
 */


#include "global.h"
#include "main.h"



#ifdef TASK_CONTROL_BLOCK_ENABLED
/* Function prototypes */
static void interrupt timer1_handler(void);     /* Called every 100ms */
static void Hundredth_sec_timer(void);
static void Tenth_sec_timer(void);
static void Stop_Beep(void);                    /* example use function */
static void Flash_led(void);                    /* example use function */
static void Debounce_handler(void);             /* example use function */
static void Task_controller(TCB *);
/* Task timer variables */
static char Tmt_Hundredth_sec = 0;
static char Tmt_Tenth_sec = 10;
static char Tmt_Beep = TASK_DISABLED;    /* example function, 1/2 second single self-terminating beep */
static char Tmt_flash = 2;               /* Toggle a port-bit every 2 seconds (this is an example usage) */
static char Debounce_timer = 2, Debounce_state = 0, Button_state = NOT_PRESSED;

/* Functions, whose execution timers are decremented each hundredth-second (by the hardware timer) */
static const TCB Hundredth_sec[] =
{
   &(Tmt_Hundredth_sec), Hundredth_sec_timer,
   &(Debounce_timer), Debounce_handler,
   END_LIST,0
};
/* Functions, whose execution timers are decremented each tenth-second (by the Hundredth_sec_timer) */
static const TCB Tenth_sec[] =
{
   &(Tmt_Tenth_sec), Tenth_sec_timer,
   &(Tmt_Beep), Stop_Beep,
   END_LIST,0
};
/* Functions, whose execution timers are decremented each whole second (by the Tenth_sec_timer) */
static const TCB Whole_sec[] =
{
   &(Tmt_flash), Flash_led,            /* alternativly, could put this in Tenth_sec TCB, but set Tmt_flash to 20 */
   END_LIST,0
};
#endif      /* end TASK_CONTROL_BLOCK_ENABLED */


/* Below are example routines and necessary control functions for using the TCB functionality */
#ifdef TASK_CONTROL_BLOCK_ENABLED

/* -----------------7/30/96 9:55AM-------------------
   Function: timer1_handler()

   If using TCB functionality, we use Timer1 as the base timer-tick that cycles at 10ms interval, providing
   the 1/100 sec timer decrementing.  Remember, the 10ms timing is not set up by ApBUILDER, you must fill in
   appropriate values in the sfrs[] array at top of file

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

static void interrupt timer1_handler(void)       /* Called every 10ms */
{
   TCB *ptr;

   /* Decrement all timers in Hundredth_sec TCB list */
   for (ptr = &(Hundredth_sec[0]); ptr ->task_flag != END_LIST; ptr ++)
   {
      if (*(ptr -> task_flag) > 0)
      {
         *(ptr -> task_flag) -= 1;
      }
   }
}

/* -----------------7/30/96 9:44AM-------------------
   Function: Task_controller()

   This function is used to iterate through the TCB via it's passed pointer.
   While iterating through the passed TCB, if it finds the task is enabled
   via it's task-flag being set to TASK_ENABLED, it will call the task.

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

static void Task_controller(TCB *tcb_list)
{
   while(tcb_list -> task_flag != END_LIST)        /* while not at end of TCB list */
   {
      if(*tcb_list -> task_flag == TASK_ENABLED)   /* if the timer says the task is ready to execute */
      {
         tcb_list -> task_action();                /* execute the timed task */
      }
      tcb_list++;                                  /* move on to next timed element in the list */
   }
}

/* -----------------7/30/96 9:46AM-------------------
   Function: Hundredth_sec_timer()

   This function is responsible for decrementing all the timer variables
   in the Tenth_sec TCB list.  This allows a single timer to cascade
   through various different TCB lists.

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

static void Hundredth_sec_timer(void)
{
   TCB *ptr;
   Tmt_Hundredth_sec = 10;                             /* Reset timer-variable to re-execute this routine in 1/10 second */

   /* Decrement all timers in Tenth_sec TCB list every 1/10 second */
   for (ptr = &(Tenth_sec[0]); ptr ->task_flag != END_LIST; ptr ++)
   {
      if (*(ptr -> task_flag) > 0)
      {
         *(ptr -> task_flag) -= 1;
      }
   }
}

/* -----------------7/30/96 9:46AM-------------------
   Function: Tenth_sec_timer()

   This function is responsible for decrementing all the timer variables
   in the Whole_sec TCB list.  This allows a single timer to cascade
   through various different TCB lists.

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

static void Tenth_sec_timer(void)
{
   TCB *ptr;
   Tmt_Tenth_sec = 10;                             /* Reset timer-variable to re-execute this routine in 1 second */

   /* Decrement all whole_sec timers every 1/10 second */
   for (ptr = &(Whole_sec[0]); ptr ->task_flag != END_LIST; ptr ++)
   {
      if (*(ptr -> task_flag) > 0)
      {
         *(ptr -> task_flag) -= 1;
      }
   }
}

/* -----------------7/30/96 9:48AM-------------------
   Function: Flash_led()

   Example function, toggles simulated LED on P0.3,
   then re-sets the timer for another 2-second interval.
 * --------------------------------------------------*/

static void Flash_led(void)
{
   Tmt_flash = 2;                /* Count-down and Reset timer.. Reset for 2 second execution again */
   P0 ^= 0x08;                   /* Toggle some bit each 2 seconds */
}

/* -----------------7/30/96 9:49AM-------------------
   Function: beep_it()

   Example function, turns on beep, and sets a timer to expire
   and turn off the beep in 1/2 second.
   Assumes some beeper is activated by setting bit0 of P0 high.
 * --------------------------------------------------*/

static void beep_it(void)
{
   P0 |= 0x01;
   Tmt_Beep = 5;                 /* 5 * 1/10 second = 1/2 second beep */
}

/* -----------------7/30/96 9:50AM-------------------
   Function: Stop_Beep()

   This routine called by Task_controller when Tmt_Beep times
   out (becomes 0).  It simulates turning off a beeper
   by clearing P0.0, and then disables the task timer..
   one-shot usage, non-auto-repeating
 * --------------------------------------------------*/

static void Stop_Beep(void)
{
   Tmt_Beep = TASK_DISABLED;     /* Count-down and terminate timer.. Activated elsewhere */
   P0 &= 0xfe;                   /* Turn off the beeper */
}


/* -----------------7/30/96 9:51AM-------------------
   Function: get_button_push()

   Example function.. Retrieves the state of a button on P0.5
 * --------------------------------------------------*/

void get_button_push(void)
{
   return(Button_state);                           /* PRESSED, or NOT_PRESSED */
}

/* -----------------7/30/96 9:51AM-------------------
   Function: Debounce_handler()

   Simulated active-high button on P0.5.
   Uses state-machine to check button state, utilizing debounce-time
   to ensure we don't respond to a glitch/noise, or get
   multiple responses due to key-bounce.
   This routine Called every 20ms, via 10ms TCB timer, and a timer
   variable (Debounce_timer) set to 2.
 * --------------------------------------------------*/

static void Debounce_handler(void)
{
   static char prev_state = NOT_PRESSED;  /* state variable must have static duration */
   char input_bit;

   Debounce_timer = 2;                    /* reset timer for 20ms delay */

   switch(Debounce_state)
   {
      case 0:                             /* while in state 0, we are looking for an initial transition on P0.5 */
         input_bit =  _BIT(P0,5);
         if (input_bit != prev_state)     /* if signal has transitioned from previous stable state */
         {
            Debounce_state++;             /* move on to next state */
            prev_state = input_bit;
         }
      break;

      case 1:
      case 2:
         Debounce_state++;                /* wait a while to let contacts settle */
      break;

      case 3:
         input_bit = _BIT(P0,5);
         if (input_bit == prev_state)     /* ok, now check to see if it's still in same condition as in state 0 */
         {
            Button_state = prev_state;    /* if it is, then set button state to match it */
         }
         else
         {
            prev_state = input_bit;       /* if it isn't, then we may have received a glitch.. reset prev_state */
         }
         Debounce_state = 0;
      break;
   }
}

#endif      /* TASK_CONTROL_BLOCK_ENABLED */




⌨️ 快捷键说明

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