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

📄 test.c

📁 uCOS2嵌入式操作系统源码
💻 C
字号:
#include "includes.h"
#define  TASK_STK_SIZE                 512       /* Size of each task's stacks (# of WORDs)            */
#define  N_TASKS                        10       /* Number of identical tasks                          */
OS_STK        TaskStk[N_TASKS][TASK_STK_SIZE];        /* Tasks stacks                                  */
OS_STK        TaskStartStk[TASK_STK_SIZE];
INT8U         TaskData[N_TASKS];                      /* Parameters to pass to each task               */
        void  Task(void *data);                       /* Function prototypes of tasks                  */
        void  TaskStart(void *data);                  /* Function prototypes of Startup task           */
static  void  TaskStartCreateTasks(void);
static  void  TaskStartDispInit(void);
static  void  TaskStartDisp(void);

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 */

    OSTaskCreateExt(TaskStart,
                    (void *)0,
                    &TaskStartStk[TASK_STK_SIZE - 1],
                    0,                                     /* Task priority = 0                        */
                    0,
                    &TaskStartStk[0],
                    TASK_STK_SIZE,
                    (void *)0,
                    OS_TASK_OPT_SAVE_FP);
    OSStart();                                             /* Start multitasking                       */
}

void  TaskStart (void *pdata)
{
#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
  OS_CPU_SR  cpu_sr;
#endif    
  INT8U      i;
  char       s[100];
  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 (;;) 
  {
    TaskStartDisp();

    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;
    OSTimeDlyHMSM(0, 0, 1, 0);                         /* Wait one second                          */
  }
}

static  void  TaskStartDispInit (void)
{
  PC_DispStr( 0,  0, "                         uC/OS-II, The Real-Time Kernel                         ", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK);
  PC_DispStr( 0,  1, "                                Jean J. Labrosse                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  PC_DispStr( 0,  2, "                             by Xu Dong   2005.6.25                             ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  PC_DispStr( 0,  3, "                                    EXAMPLE #4                                  ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  PC_DispStr( 0,  4, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  PC_DispStr( 0,  5, "TaskPrio      Angle   cos(Angle)   sin(Angle)                                   ", 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, "                                                                                ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  PC_DispStr( 0, 11, "                                                                                ", 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, "#Tasks          :        CPU Usage:     %                                       ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  PC_DispStr( 0, 23, "#Task switch/sec:                                                               ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
  PC_DispStr( 0, 24, "                          <- Press 'ESC' To Quit ->               uC/OS-II      ", DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY + DISP_BLINK);
}

static void TaskStartDisp(void)
{
  char  s[80];

  sprintf(s, "%5d", OSTaskCtr);                                  /* Display #tasks running               */
  PC_DispStr(18, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);

#if OS_TASK_STAT_EN > 0
  sprintf(s, "%3d", OSCPUUsage);                                 /* Display CPU usage in %               */
  PC_DispStr(36, 22, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);
#endif

  sprintf(s, "%5d", OSCtxSwCtr);                                 /* Display #context switches per second */
  PC_DispStr(18, 23, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);

  sprintf(s, "V%4.2f", (float)OSVersion() * 0.01);               /* Display uC/OS-II's version number    */
  PC_DispStr(75, 24, s, DISP_FGND_RED + DISP_BGND_LIGHT_GRAY + DISP_BLINK);

  switch (_8087) 
  {                                               /* Display whether FPU present          */
    case 0:
         PC_DispStr(71, 22, " NO  FPU ", DISP_FGND_YELLOW + DISP_BGND_BLUE);
         break;

    case 1:
         PC_DispStr(71, 22, " 8087 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE);
         break;

    case 2:
         PC_DispStr(71, 22, "80287 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE);
         break;

    case 3:
         PC_DispStr(71, 22, "80387 FPU", DISP_FGND_YELLOW + DISP_BGND_BLUE);
         break;
  }
}



static void TaskStartCreateTasks(void)
{
  INT8U  i;
  INT8U  prio;
  
  for(i = 0;i < N_TASKS;i++)
  {
    prio = i + 1;    //设置任务优先级
    TaskData[i] = prio;  //每个任务的优先级都放在一个数组中
    OSTaskCreateExt(Task,
                    (void *)&TaskData[i],
                    &TaskStk[i][TASK_STK_SIZE -1],
                    prio,
                    0,
                    &TaskStk[i][0],
                    TASK_STK_SIZE,
                    (void *)0,
                    OS_TASK_OPT_SAVE_FP);
  }
}

void Task(void *pdata)
{
  FP32  x;
  FP32  y;
  FP32  angle;
  FP32  radians;
  char  s[81];
  INT8U ypos;
  
  ypos = *(INT8U *)pdata + 7;
  angle = (FP32)(*(INT8U *)pdata) * (FP32)36.0;
  for(;;)
  {
    radians = (FP32)2.0 * (FP32)3.141592 * angle / (FP32)360.0;
    x = cos(radians);
    y = sin(radians);
    sprintf(s,"  %2d   %8.3f  %8.3f  %8.3f",*(INT8U *)pdata,angle,x,y);
    PC_DispStr(0,ypos,s,DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
    if(angle >= (FP32)360.0)
    {
      angle = (FP32)0.0;
    }
      else
      {
        angle += (FP32)0.01;
      }
    OSTimeDly(1);  // 每个任务延时一个时钟节拍(5ms),运行200次/秒。
  }
}
       

⌨️ 快捷键说明

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