本帖最后由 ann0023 于 2020-6-24 12:53 编辑
前提:已经使能了时间片轮转调度的宏,并且是ucosiii
相关代码: //任务优先级 #define LED1_TASK_PRIO 5 //任务堆栈大小 #define LED1_STK_SIZE 128 //任务控制块 OS_TCB Led1TaskTCB; //任务堆栈 CPU_STK LED1_TASK_STK[LED1_STK_SIZE]; //任务函数 void led1_task(void *p_arg); //任务优先级 #define LED0_TASK_PRIO 5 //任务堆栈大小 #define LED0_STK_SIZE 128 //任务控制块 OS_TCB Led0TaskTCB; //任务堆栈 CPU_STK LED0_TASK_STK[LED1_STK_SIZE]; //任务函数 void led0_task(void *p_arg); OSTaskCreate((OS_TCB * )&Led1TaskTCB, (CPU_CHAR * )"led1 task", (OS_TASK_PTR )led1_task, (void *)0, (OS_PRIO )LED1_TASK_PRIO, (CPU_STK * )&LED1_TASK_STK[0], (CPU_STK_SIZE)LED1_STK_SIZE/10, (CPU_STK_SIZE)LED1_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )10,//使用10个时间片,为10*5=50ms (void *)0, (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, (OS_ERR *)&err); OSTaskCreate((OS_TCB * )&Led0TaskTCB, (CPU_CHAR * )"led0 task", (OS_TASK_PTR )led0_task, (void *)0, (OS_PRIO )LED0_TASK_PRIO, (CPU_STK * )&LED0_TASK_STK[0], (CPU_STK_SIZE)LED0_STK_SIZE/10, (CPU_STK_SIZE)LED0_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )10,//使用10个时间片,为10*5=50ms (void *)0, (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, (OS_ERR *)&err); //led1任务函数 void led1_task(void *p_arg) { OS_ERRerr; p_arg= p_arg; inti; OS_TICKt1,t2; u8task1_num; while(1) { // task1_num++; // LCD_ShowNum(110,130,task1_num,3,16); for(i=0;i<5;i++)printf("Task1:ffff5678\r\n"); LED1=~LED1; OSTimeDlyHMSM(0,0,5,0,OS_OPT_TIME_HMSM_STRICT,&err);//延时5s } } //led0任务函数 void led0_task(void *p_arg) { OS_ERRerr; p_arg= p_arg; inti; while(1) { for(i=0;i<5;i++)printf("Task0:01234\r\n"); LED0=~LED0; OSTimeDlyHMSM(0,0,5,0,OS_OPT_TIME_HMSM_STRICT,&err);//延时5s } } 运行结果: Task1:ffff5678 Task1:ffff5678 Task1:ffff5678 Task1:ffff5678 Task1:ffff5678 Task0:01234 Task0:01234 Task0:01234 Task0:01234 Task0:01234 Task1:ffff5678 Task1:ffff5678 Task1:ffff5678 TaskTask0:01234 //开始乱序 Task0:01234 Task0:01234 Task0:01234 Task0:01234 1:ffff5678 Task1:ffff5678 Task0:01234 //下一轮顺序又正确 Task0:01234 Task0:01234 Task0:01234 Task0:01234 Task1:ffff5678 Task1:ffff5678 Task1:ffff5678 Task1:ffff5678 Task1:ffff5678 Task0:01234 Task0:01234 Task0:01234 Task0:01234 Task1:ffff5678 结果分析:运行结果正确一次乱序一次,不知道原因。已经给了足够的时间片了;哪位大神能告诉原因
|