全部博文(404)
分类: LINUX
2009-04-27 09:56:13
3)使用消息队列 #define TASK_STK_SIZE 512 #define TASK_START_ID 0 #define TASK_CLK_ID 1 #define TASK_1_ID 2 #define TASK_2_ID 3 #define TASK_3_ID 4 #define TASK_4_ID 5 #define TASK_5_ID 6 #define TASK_START_PRIO 10 #define TASK_CLK_PRIO 11 #define TASK_1_PRIO 12 #define TASK_2_PRIO 13 #define TASK_3_PRIO 14 #define TASK_4_PRIO 15 #define TASK_5_PRIO 16 #define MSG_QUEUE_SIZE 20 /* 消息队列大小*/ typedef struct { char TaskName[30]; INT16U TaskCtr; INT16U TaskExecTime; INT32U TaskTotExecTime; } TASK_USER_DATA; OS_STK TaskStartStk[TASK_STK_SIZE]; OS_STK TaskClkStk[TASK_STK_SIZE]; OS_STK Task1Stk[TASK_STK_SIZE]; OS_STK Task2Stk[TASK_STK_SIZE]; OS_STK Task3Stk[TASK_STK_SIZE]; OS_STK Task4Stk[TASK_STK_SIZE]; OS_STK Task5Stk[TASK_STK_SIZE]; TASK_USER_DATA TaskUserData[7]; OS_EVENT *MsgQueue; /*消息队列指针*/ void *MsgQueueTbl[20]; /*消息存储*/ void main (void) { PC_DispClrScr(DISP_BGND_BLACK); OSInit(); PC_DOSSaveReturn(); PC_VectSet(uCOS, OSCtxSw); PC_ElapsedInit(); strcpy(TaskUserData[TASK_START_ID].TaskName, "StartTask"); OSTaskCreateExt(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], TASK_START_PRIO, TASK_START_ID, &TaskStartStk[0], TASK_STK_SIZE, &TaskUserData[TASK_START_ID], 0); OSStart(); } void TaskStart (void *pdata) { #if OS_CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr; #endif INT16S key; pdata = pdata; TaskStartDispInit(); OS_ENTER_CRITICAL(); PC_VectSet(0x08, OSTickISR); PC_SetTickRate(OS_TICKS_PER_SEC); OS_EXIT_CRITICAL(); OSStatInit(); MsgQueue = OSQCreate(&MsgQueueTbl[0], MSG_QUEUE_SIZE); /*创建消息队列,大小为*/ TaskStartCreateTasks(); for (;;) { TaskStartDisp(); if (PC_GetKey(&key)) { if (key == 0x1B) { PC_DOSReturn(); } } OSCtxSwCtr = 0; OSTimeDly(OS_TICKS_PER_SEC); } } void Task1 (void *pdata) { char *msg; INT8U err; pdata = pdata; for (;;) { msg = (char *)OSQPend(MsgQueue, 0, &err);//从消息队列中取消息 PC_DispStr(70, 13, msg, DISP_FGND_YELLOW + DISP_BGND_BLUE); OSTimeDlyHMSM(0, 0, 0, 100); } } void Task2 (void *pdata) { char msg[20]; pdata = pdata; strcpy(&msg[0], "Task 2"); for (;;) { OSQPost(MsgQueue, (void *)&msg[0]);//发送消息到队列中 OSTimeDlyHMSM(0, 0, 0, 500); } } void Task3 (void *pdata) { char msg[20]; pdata = pdata; strcpy(&msg[0], "Task 3"); for (;;) { OSQPost(MsgQueue, (void *)&msg[0]);//发送消息到队列中 OSTimeDlyHMSM(0, 0, 0, 500); } } void Task4 (void *pdata) { char msg[20]; pdata = pdata; strcpy(&msg[0], "Task 4"); for (;;) { OSQPost(MsgQueue, (void *)&msg[0]);//发送消息到队列中 OSTimeDlyHMSM(0, 0, 0, 500); } } void OSTaskStatHook (void) { char s[80]; INT8U i; INT32U total; INT8U pct; total = 0L; /* Totalize TOT. EXEC. TIME for each task for (i = 0; i < 7; i++) { total += TaskUserData[i].TaskTotExecTime; DispTaskStat(i); /* Display task data } if (total > 0) { for (i = 0; i < 7; i++) { /* Derive percentage of each task */ pct = 100 * TaskUserData[i].TaskTotExecTime / total; sprintf(s, "%3d %%", pct); PC_DispStr(62, i + 11, s, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY); } } if (total > 1000000000L) { /* Reset total time counters at 1 billion for (i = 0; i < 7; i++) { TaskUserData[i].TaskTotExecTime = 0L; } } } void OSTaskSwHook (void) { INT16U time; TASK_USER_DATA *puser; time = PC_ElapsedStop(); /* This task is done PC_ElapsedStart(); /* Start for next task puser = OSTCBCur->OSTCBExtPtr; /* Point to used data if (puser != (TASK_USER_DATA *)0) { puser->TaskCtr++; /* Increment task counter puser->TaskExecTime = time; /* Update the task's execution time puser->TaskTotExecTime += time; /* Update the task's total execution time } } |