EX1L.C
上传用户:jyxpgd88
上传日期:2013-04-13
资源大小:90k
文件大小:8k
- /*
- *********************************************************************************************************
- * uC/OS-II
- * The Real-Time Kernel
- *
- * (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
- * All Rights Reserved
- *
- * V2.00
- *
- * EXAMPLE #1
- *********************************************************************************************************
- */
- #include "ucos_ii.h"
- #include "pc.h"
- #include <stdio.h>
- #define CLOCK_SPEED 29491200
- /*
- *********************************************************************************************************
- * CONSTANTS
- *********************************************************************************************************
- */
- #define TASKSTART_STK_SIZE 128
- #define TASK_STK_SIZE 64 /* Size of each task's stacks (# of WORDs) */
- #define N_TASKS 10 /* Number of identical tasks */
- /*
- *********************************************************************************************************
- * VARIABLES
- *********************************************************************************************************
- */
- OS_STK TaskStk[N_TASKS][TASK_STK_SIZE]; /* Tasks stacks */
- OS_STK TaskStartStk[TASKSTART_STK_SIZE];
- char TaskData[N_TASKS]; /* Parameters to pass to each task */
- OS_EVENT *RandomSem;
- /*
- *********************************************************************************************************
- * FUNCTION PROTOTYPES
- *********************************************************************************************************
- */
- void Task(void *) KCREENTRANT; /* Function prototypes of tasks */
- void TaskStart(void *) KCREENTRANT; /* Function prototypes of Startup task */
- /*$PAGE*/
- /*
- *********************************************************************************************************
- * MAIN
- *********************************************************************************************************
- */
- void main (void)
- {
- DPX=0;
- 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 */
- RandomSem = OSSemCreate(1); /* Random number semaphore */
- OSTaskCreate(TaskStart, (void *)0, (void *)&TaskStartStk[TASKSTART_STK_SIZE - 1], 0);
- OSStart(); /* Start multitasking */
- }
- /*$PAGE*/
- /*
- *********************************************************************************************************
- * STARTUP TASK
- *********************************************************************************************************
- */
- void TaskStart (void *d) KCREENTRANT
- {
- UBYTE i;
- static char s[100];
- WORD key;
- d = d; /* Prevent compiler warning */
- PC_DispStr(26, 0, "uC/OS-II, The Real-Time Kernel", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK);
- PC_DispStr(33, 1, "Jean J. Labrosse", DISP_FGND_WHITE);
- PC_DispStr(36, 3, "EXAMPLE #1", DISP_FGND_WHITE);
- // 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();
- /* Modification for KC51, set up timer 0 and start it
- */
-
- TMOD=(TMOD & 0XF0) | 0X01; /* Timer 0 working in MODE 1 */
- TL0=(0xFFFF-(((CLOCK_SPEED/4)/12)/OS_TICKS_PER_SEC))%0x100; /* Freq=OS_TICKS_PER_SEC*/
- TH0=(0xFFFF-(((CLOCK_SPEED/4)/12)/OS_TICKS_PER_SEC))/0x100;
- TR0=1;
- ET0=1;
- EA =1;
- /* End kc51 modification
- */
- PC_DispStr(0, 22, "Determining CPU's capacity ...", DISP_FGND_WHITE);
- OSStatInit(); /* Initialize uC/OS-II's statistics */
- PC_DispClrLine(22, DISP_FGND_WHITE + DISP_BGND_BLACK);
- for (i = 0; i < N_TASKS; i++) { /* Create N_TASKS identical tasks */
- TaskData[i] = '0' + i; /* Each task will display its own letter */
- OSTaskCreate(Task, (void *)&TaskData[i], (void *)&TaskStk[i][TASK_STK_SIZE - 1], i + 1);
- }
- PC_DispStr( 0, 22, "#Tasks : xxxxx CPU Usage: xxx %", DISP_FGND_WHITE);
- PC_DispStr( 0, 23, "#Task switch/sec: xxxxx", DISP_FGND_WHITE);
- PC_DispStr(28, 24, "<-PRESS 'ESC' TO QUIT->", DISP_FGND_WHITE + DISP_BLINK);
- for (;;) {
- sprintf(s, "%5bd", OSTaskCtr); /* Display #tasks running */
- PC_DispStr(18, 22, s, DISP_FGND_BLUE + DISP_BGND_CYAN);
- sprintf(s, "%3bd", OSCPUUsage); /* Display CPU usage in % */
- PC_DispStr(36, 22, s, DISP_FGND_BLUE + DISP_BGND_CYAN);
- sprintf(s, "%5ld", OSCtxSwCtr); /* Display #context switches per second */
- PC_DispStr(18, 23, s, DISP_FGND_BLUE + DISP_BGND_CYAN);
- OSCtxSwCtr = 0;
-
- sprintf(s, "V%3.2f", (float)OSVersion() * 0.01); /* Display version number as Vx.yy */
- PC_DispStr(75, 24, s, DISP_FGND_YELLOW + DISP_BGND_BLUE);
- PC_GetDateTime(s); /* Get and display date and time */
- PC_DispStr(0, 24, s, DISP_FGND_BLUE + DISP_BGND_CYAN);
- 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 */
- }
- }
- OSTimeDlyHMSM(0, 0, 1, 0); /* Wait one second */
- }
- }
- /*$PAGE*/
- /*
- *********************************************************************************************************
- * TASKS
- *********************************************************************************************************
- */
- void Task (void * pvdata) KCREENTRANT
- {
- UBYTE x;
- UBYTE y;
- UBYTE err;
- for (;;) {
- OSSemPend(RandomSem, 0, &err); /* Acquire semaphore to perform random numbers */
- x = random(80); /* Find X position where task number will appear */
- y = random(16); /* Find Y position where task number will appear */
- OSSemPost(RandomSem); /* Release semaphore */
- /* Display the task number on the screen */
- PC_DispChar(x, y + 5, *(char *)pvdata, DISP_FGND_LIGHT_GRAY);
- OSTimeDly(1); /* Delay 1 clock tick */
- }
- }