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

📄 task.c

📁 基于bc3.1的uCOSII的5个经典实验例子
💻 C
字号:
/*
*********************************************************************************************************
*                                                uC/OS-II
*                                          The Real-Time Kernel
*
*
*
*                                      EXAMPLE #1  Task Management
*********************************************************************************************************
*/

#include "C:\SOFTWARE\uCOS-II\Task Management\includes.h"

/*
*********************************************************************************************************
*                                               CONSTANTS
*********************************************************************************************************
*/

#define  TASK_STK_SIZE                 512       /* Size of each task's stacks (# of WORDs)            */
#define  N_TASKS                        2        /* Number of identical tasks                          */

/*
*********************************************************************************************************
*                                               VARIABLES
*********************************************************************************************************
*/

OS_STK        TaskStk[N_TASKS][TASK_STK_SIZE];        /* Tasks stacks                                  */
OS_STK        TaskStartStk[TASK_STK_SIZE];            /* TaskStart  task stack                         */
INT8U         TaskData[N_TASKS];                       /* Parameters to pass to each task               */


/*
*********************************************************************************************************
*                                           FUNCTION PROTOTYPES
*********************************************************************************************************
*/

		 void  Task0(void *pdata);              /* Function prototypes of tasks                  */
		 void  Task1(void *pdata);
		 void  TaskStart(void *pdata);                  /* Function prototypes of Startup task           */
static  void  TaskStartCreateTasks(void);                /* Function prototypes of creat task           */
static  void  TaskStartDispInit(void);                /* Function prototypes of Initialization the display          */
static  void  Disp(INT8U);                /* Function prototypes of displaying the information of task         */
/*$PAGE*/
/*
*********************************************************************************************************
*                                                MAIN
*********************************************************************************************************
*/

void  main (void)
{
    PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK);   /* Clear the screen                         */

    OSInit();                                              /* Initialize uC/OS-II                      */
    PC_DOSSaveReturn();                                    /* Save environment to return to DOS        */
    PC_VectSet(uCOS, OSCtxSw);                             /* Install uC/OS-II's context switch vector */

                   

    OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 4);
    OSStart();                                             /* Start multitasking                       */
}


/*
*********************************************************************************************************
*                                              STARTUP TASK
*********************************************************************************************************
*/
void  TaskStart (void *pdata)
{
#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
    OS_CPU_SR  cpu_sr;
#endif
   
    INT16S     key;


    pdata = pdata;                                         /* Prevent compiler warning                 */

    TaskStartDispInit();                                   /* Initialize the display                   */

    OS_ENTER_CRITICAL();
    PC_VectSet(0x08, OSTickISR);                           /* Install uC/OS-II's clock tick ISR        */
    PC_SetTickRate(OS_TICKS_PER_SEC);                      /* Reprogram tick rate                      */
    OS_EXIT_CRITICAL();

    OSStatInit();                                          /* Initialize uC/OS-II's statistics         */

    TaskStartCreateTasks();                                /* Create all the application tasks         */

    for (;;) {
       
        if (PC_GetKey(&key) == TRUE) {                     /* See if key has been pressed              */
            if (key == 0x1B) {                             /* Yes, see if it's the ESCAPE key          */
                PC_DOSReturn();                            /* Return to DOS                            */
            }
        }

        OSCtxSwCtr = 0;                                    /* Clear context switch counter             */
        OSTimeDlyHMSM(0, 0, 1, 0);                         /* Wait one second                          */
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                        INITIALIZE THE DISPLAY
*********************************************************************************************************
*/

static  void  TaskStartDispInit (void)
{
/*                                1111111111222222222233333333334444444444555555555566666666667777777777 */
/*                      01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
	 PC_DispStr( 0,  0, "                         uC/OS-II, The Real-Time Kernel                         ", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK);
	 PC_DispStr( 0,  1, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0,  2, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
	 PC_DispStr( 0,  3, "                         EXAMPLE #1     Task Management                         ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
	 PC_DispStr( 0,  4, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0,  5, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0,  6, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0,  7, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0,  8, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0,  9, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
	 PC_DispStr( 0, 10, "Task0() :                                                                       ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
	 PC_DispStr( 0, 11, "Task1() :                                                                       ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
	 PC_DispStr( 0, 12, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
	 PC_DispStr( 0, 13, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0, 14, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0, 15, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0, 16, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0, 17, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0, 18, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0, 19, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0, 20, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0, 21, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
	 PC_DispStr( 0, 22, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0, 23, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    PC_DispStr( 0, 24, "                            <-PRESS 'ESC' TO QUIT->                             ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY + DISP_BLINK);
/*                                1111111111222222222233333333334444444444555555555566666666667777777777 */
/*                      01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
}
/*$PAGE*/
/*
*********************************************************************************************************
*
*********************************************************************************************************
*                                             CREATE TASKS
*********************************************************************************************************
*/

static  void  TaskStartCreateTasks (void)
{
    INT8U  i;


	 for (i = 0; i < N_TASKS; i++) {                        /* Create tasks           */
		  TaskData[i] =  i;                             /* Each task will display its own information    */
	 }
		OSTaskCreate(Task0, (void *)&TaskData[0], &TaskStk[0][TASK_STK_SIZE - 1], 5);
		OSTaskCreate(Task1, (void *)&TaskData[1], &TaskStk[1][TASK_STK_SIZE - 1], 6);
	 
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                               TASK #0
*
* Description: This task is suspended by itself,then it will be resumed by other tasks.
*********************************************************************************************************
*/
 
void  Task0 (void *pdata)
{ 
	 INT8U i;
	  INT8U err;

		i=*(int *)pdata;

		for (;;) {

		  Disp(i);

			err=OSTaskSuspend(5);       /* suspend itself           */

		  }

}

/*$PAGE*/
/*
*********************************************************************************************************
*                                               TASK #1
*
* Description: This task resume task0
*********************************************************************************************************
*/
void  Task1 (void *pdata)
{ 
	 INT8U i;
     INT8U err;
        
		i=*(int *)pdata;

		for (;;) {
          OSTimeDly(200);
          
		  Disp(i);

		  err=OSTaskResume(5);        /* resume task0           */

		  }

}
/*$PAGE*/
/*
*********************************************************************************************************
*                                       Disp the task information
*
*********************************************************************************************************
*/

static void Disp (INT8U data)
{


	 switch(data){
	 case 0:
  {
	 PC_DispStr( 14, 10, "task0 is   running.                  ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
	 PC_DispStr( 14, 11, "task1 is suspended.                  ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
	OSTimeDly(200);
	}
	case 1:
	{
	 PC_DispStr( 14, 10, "task0 is suspended.                  ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
	 PC_DispStr( 14, 11, "task1 is   running.                  ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
	OSTimeDly(200);
	}
  }

}

⌨️ 快捷键说明

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