17.编写一个有2个任务的应用程序,每一个任务每次运行显示一个字符。当调度器进行5次调度之后,这些显示的字符会在显示器上构成一个字符串“ Hello,μC/OS−II!”
————————————————
版权声明:本文为CSDN博主「梦远花落白衣衫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40400656/article/details/105042722
在这里插入代码片#include “includes.h”
#define TASK_STK_SIZE 512
OS_STK MyTaskStk[TASK_STK_SIZE];
OS_STK YouTaskStk[TASK_STK_SIZE];
INT8U x = 0, y = 0;
INT16S key;
extern INT8U myTaskTime = 0;
extern INT8U YouTaskTime = 0;
void MyTask(void *pdata);
void YouTask(void *pdata);
void main()
{
OSInit();
OS_ENTER_CRITICAL();
PC_DOSSaveReturn();
PC_VectSet(uCOS, OSCtxSw);
OS_EXIT_CRITICAL();
OSTaskCreate(
MyTask,
(void *)0,
&MyTaskStk[TASK_STK_SIZE - 1],
0
);
OSStart();
}
void MyTask(void *pdata)
{
#if OS_CRITICAL_METHOD == 3
OS_CPU_SR cpu_sr
#endif
pdata = pdata;
OS_ENTER_CRITICAL();
PC_VectSet(0x08, OSTickISR);
PC_SetTickRate(OS_TICKS_PER_SEC);
OS_ENTER_CRITICAL();
OSStatInit();
OSTaskCreate(
YouTask,
(void *)0,
&YouTaskStk[TASK_STK_SIZE - 1],
1
);
for(;😉
{
if(myTaskTime == 0)
{
OSSchedLock();
PC_DispChar(
20,
10,
‘H’,
DISP_BGND_BLACK + DISP_FGND_WHITE
);
OSTimeDlyHMSM(0, 0, 1, 0);
}
else if(myTaskTime == 1)
{
PC_DispChar(
21,
10,
‘e’,
DISP_FGND_WHITE + DISP_BGND_BLACK
);
OSTimeDlyHMSM(0, 0, 1, 0);
}
else if(myTaskTime == 2)
{
PC_DispChar(
22,
10,
‘l’,
DISP_BGND_BLACK + DISP_FGND_WHITE
);
OSTimeDlyHMSM(0, 0, 1, 0);
}
else if(myTaskTime == 3)
{
PC_DispChar(
23,
10,
‘l’,
DISP_BGND_BLACK + DISP_FGND_WHITE
);
OSTimeDlyHMSM(0, 0, 1, 0);
}
else if(myTaskTime == 4)
{
PC_DispChar(
24,
10,
‘o’,
DISP_BGND_BLACK + DISP_FGND_WHITE
);
OSTimeDlyHMSM(0, 0, 1, 0);
}
else if(myTaskTime == 5)
{
PC_DispChar(
25,
10,
‘,’,
DISP_BGND_BLACK + DISP_FGND_WHITE
);
OSTimeDlyHMSM(0, 0, 1, 0);
}
else if(myTaskTime == 6)
{
PC_DispChar(
26,
10,
‘u’,
DISP_BGND_BLACK + DISP_FGND_WHITE
);
OSTimeDlyHMSM(0, 0, 1, 0);
}
else if(myTaskTime == 7)
{
PC_DispChar(
27,
10,
‘C’,
DISP_BGND_BLACK + DISP_FGND_WHITE
);
OSTimeDlyHMSM(0, 0, 1, 0);
}
else if(myTaskTime == 8)
{
PC_DispChar(
28,
10,
‘/’,
DISP_BGND_BLACK + DISP_FGND_WHITE
);
OSTimeDlyHMSM(0, 0, 1, 0);
}
else if(myTaskTime == 9)
{
PC_DispChar(
29,
10,
‘O’,
DISP_BGND_BLACK + DISP_FGND_WHITE
);
OSTimeDlyHMSM(0, 0, 1, 0);
}
else if(myTaskTime == 10)
{
PC_DispChar(
31,
10,
'-',
DISP_BGND_BLACK + DISP_FGND_WHITE
);
OSSchedUnlock();
}
else if(myTaskTime == 11)
{
PC_DispChar(
33,
10,
'I',
DISP_BGND_BLACK + DISP_FGND_WHITE
);
}
if(PC_GetKey(&key) == TRUE)
{
if(key == 0x1B)
{
PC_DOSReturn();
}
}
myTaskTime++;
OSTimeDlyHMSM(0, 0, 2, 0);
}
}
void YouTask(void *pdata)
{
#if OS_CRITICAL_METHOD == 3
OS_CPU_SR cpu_sr
#endif
pdata = pdata;
for(;😉
{
if(YouTaskTime == 0)
{
PC_DispChar(
30,
10,
‘S’,
DISP_BGND_BLACK + DISP_FGND_WHITE
);
}
else if(YouTaskTime == 1)
{
PC_DispChar(
32,
10,
‘I’,
DISP_FGND_WHITE + DISP_BGND_BLACK
);
}
else if(YouTaskTime == 2)
{
PC_DispChar(
34,
10,
‘!’,
DISP_BGND_BLACK + DISP_FGND_WHITE
);
}
YouTaskTime++;
OSTimeDlyHMSM(0, 0, 2, 0);
}
}
程序运行结果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/d55cccee9736447d8380322f7139c3f8.png#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/a568c1a3e26b443d80f05eb06352f767.png#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/04bf21d144c84e02899eae5dd754c456.png#pic_center)
百思不得其解,我觉得应该是先打印后面33位的I,再打印前面32位的I啊,但是执行结果并不是,刚好相反。youtask打印32位置应该要在mytask打印33位置之后啊,因为youtask打印30位置后也有2秒的延迟,这是为啥?
我的理解是:打印29位置后,再打印31位置,31位置那里解锁了,开始调度执行youTASK打印30位置,youTASK延时2秒。那接下来就是mytask2秒延时时间到,再打印33位置啊,然后mytask再延时2秒,操作系统接下来去调度打印32位置啊。为啥32位置抢先打印在33位置前面了?求高手解答。。。![在这里插入图片描述](https://img-blog.csdnimg.cn/0d913496b0e34cf9b154a6e4@[TOC](这里写自定义目录标题)f0568b9c.png)
这个 函数是在X,Y位置打印一个字符
![在这里插入图片描述](https://img-blog.csdnimg.cn/244484bebe494fc2aa34d7aba4dd82ef.png)这个函数只是 一个退出意思,不用管它