EX1L.C
上传用户:jyxpgd88
上传日期:2013-04-13
资源大小:90k
文件大小:8k
源码类别:

uCOS

开发平台:

C/C++

  1. /*
  2. *********************************************************************************************************
  3. *                                                uC/OS-II
  4. *                                          The Real-Time Kernel
  5. *
  6. *                        (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
  7. *                                           All Rights Reserved
  8. *
  9. *                                                 V2.00
  10. *
  11. *                                               EXAMPLE #1
  12. *********************************************************************************************************
  13. */
  14. #include "ucos_ii.h"
  15. #include "pc.h"
  16. #include <stdio.h>
  17. #define CLOCK_SPEED 29491200
  18. /*
  19. *********************************************************************************************************
  20. *                                               CONSTANTS
  21. *********************************************************************************************************
  22. */
  23. #define  TASKSTART_STK_SIZE            128      
  24. #define  TASK_STK_SIZE                  64       /* Size of each task's stacks (# of WORDs)            */
  25. #define  N_TASKS                        10       /* Number of identical tasks                          */
  26. /*
  27. *********************************************************************************************************
  28. *                                               VARIABLES
  29. *********************************************************************************************************
  30. */
  31. OS_STK           TaskStk[N_TASKS][TASK_STK_SIZE];     /* Tasks stacks                                  */
  32. OS_STK           TaskStartStk[TASKSTART_STK_SIZE];
  33. char             TaskData[N_TASKS];                   /* Parameters to pass to each task               */
  34. OS_EVENT        *RandomSem;
  35. /*
  36. *********************************************************************************************************
  37. *                                           FUNCTION PROTOTYPES
  38. *********************************************************************************************************
  39. */
  40. void   Task(void *) KCREENTRANT;                  /* Function prototypes of tasks                  */
  41. void   TaskStart(void *) KCREENTRANT;             /* Function prototypes of Startup task           */
  42. /*$PAGE*/
  43. /*
  44. *********************************************************************************************************
  45. *                                                MAIN
  46. *********************************************************************************************************
  47. */
  48. void main (void)
  49. {
  50. DPX=0;
  51.     PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK);      /* Clear the screen                         */
  52.     OSInit();                                              /* Initialize uC/OS-II                      */
  53.     PC_DOSSaveReturn();                                    /* Save environment to return to DOS        */   
  54.     PC_VectSet(uCOS, OSCtxSw);                             /* Install uC/OS-II's context switch vector */
  55.     RandomSem = OSSemCreate(1);                            /* Random number semaphore                  */
  56.     OSTaskCreate(TaskStart, (void *)0, (void *)&TaskStartStk[TASKSTART_STK_SIZE - 1], 0);
  57.     OSStart();                                             /* Start multitasking                       */
  58. }
  59. /*$PAGE*/
  60. /*
  61. *********************************************************************************************************
  62. *                                              STARTUP TASK
  63. *********************************************************************************************************
  64. */
  65. void TaskStart (void *d) KCREENTRANT
  66. {
  67.     UBYTE  i;
  68.     static char   s[100];
  69.     WORD   key;
  70.     d = d;                                           /* Prevent compiler warning                 */
  71.     PC_DispStr(26,  0, "uC/OS-II, The Real-Time Kernel", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK);
  72.     PC_DispStr(33,  1, "Jean J. Labrosse", DISP_FGND_WHITE);
  73.     PC_DispStr(36,  3, "EXAMPLE #1", DISP_FGND_WHITE);
  74. //    OS_ENTER_CRITICAL();
  75. //    PC_VectSet(0x08, OSTickISR);                           /* Install uC/OS-II's clock tick ISR        */
  76. //    PC_SetTickRate(OS_TICKS_PER_SEC);                      /* Reprogram tick rate                      */
  77. //    OS_EXIT_CRITICAL();
  78. /* Modification for KC51, set up timer 0 and start it
  79. */
  80. TMOD=(TMOD & 0XF0) | 0X01; /* Timer 0 working in MODE 1 */
  81. TL0=(0xFFFF-(((CLOCK_SPEED/4)/12)/OS_TICKS_PER_SEC))%0x100; /* Freq=OS_TICKS_PER_SEC*/
  82. TH0=(0xFFFF-(((CLOCK_SPEED/4)/12)/OS_TICKS_PER_SEC))/0x100;
  83. TR0=1;
  84. ET0=1;
  85. EA =1;
  86. /* End kc51 modification
  87. */
  88.     PC_DispStr(0, 22, "Determining  CPU's capacity ...", DISP_FGND_WHITE);
  89.     OSStatInit();                                          /* Initialize uC/OS-II's statistics         */
  90.     PC_DispClrLine(22, DISP_FGND_WHITE + DISP_BGND_BLACK);
  91.     for (i = 0; i < N_TASKS; i++) {                        /* Create N_TASKS identical tasks           */
  92.         TaskData[i] = '0' + i;                             /* Each task will display its own letter    */
  93.         OSTaskCreate(Task, (void *)&TaskData[i], (void *)&TaskStk[i][TASK_STK_SIZE - 1], i + 1);
  94.     }
  95.     PC_DispStr( 0, 22, "#Tasks          : xxxxx  CPU Usage: xxx %", DISP_FGND_WHITE);
  96.     PC_DispStr( 0, 23, "#Task switch/sec: xxxxx", DISP_FGND_WHITE);
  97.     PC_DispStr(28, 24, "<-PRESS 'ESC' TO QUIT->", DISP_FGND_WHITE + DISP_BLINK);
  98.     for (;;) {
  99.         sprintf(s, "%5bd", OSTaskCtr);                     /* Display #tasks running                    */
  100.         PC_DispStr(18, 22, s, DISP_FGND_BLUE + DISP_BGND_CYAN);
  101.         sprintf(s, "%3bd", OSCPUUsage);                    /* Display CPU usage in %                    */
  102.         PC_DispStr(36, 22, s, DISP_FGND_BLUE + DISP_BGND_CYAN);
  103.         sprintf(s, "%5ld", OSCtxSwCtr);                    /* Display #context switches per second      */
  104.         PC_DispStr(18, 23, s, DISP_FGND_BLUE + DISP_BGND_CYAN);
  105.         OSCtxSwCtr = 0;
  106.         
  107.         sprintf(s, "V%3.2f", (float)OSVersion() * 0.01);   /* Display version number as Vx.yy          */
  108.         PC_DispStr(75, 24, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);
  109.         PC_GetDateTime(s);                                 /* Get and display date and time            */
  110.         PC_DispStr(0, 24, s, DISP_FGND_BLUE + DISP_BGND_CYAN);
  111.         if (PC_GetKey(&key) == TRUE) {                     /* See if key has been pressed              */     
  112.             if (key == 0x1B) {                             /* Yes, see if it's the ESCAPE key          */
  113.                 PC_DOSReturn();                            /* Return to DOS                            */
  114.             }
  115.         }
  116.         OSTimeDlyHMSM(0, 0, 1, 0);                         /* Wait one second                          */
  117.     }
  118. }
  119. /*$PAGE*/
  120. /*
  121. *********************************************************************************************************
  122. *                                                  TASKS
  123. *********************************************************************************************************
  124. */
  125. void Task (void * pvdata) KCREENTRANT
  126. {
  127.     UBYTE x;
  128.     UBYTE y;
  129.     UBYTE err;
  130.     for (;;) {
  131.         OSSemPend(RandomSem, 0, &err);           /* Acquire semaphore to perform random numbers        */
  132.         x = random(80);                          /* Find X position where task number will appear      */
  133.         y = random(16);                          /* Find Y position where task number will appear      */
  134.         OSSemPost(RandomSem);                    /* Release semaphore                                  */
  135.                                                  /* Display the task number on the screen              */
  136.         PC_DispChar(x, y + 5, *(char *)pvdata, DISP_FGND_LIGHT_GRAY);    
  137.         OSTimeDly(1);                            /* Delay 1 clock tick                                 */
  138.     }
  139. }