/*下面是一个应用消息队列进行通信的应用程序,运行该程序并观察其运行结果*/
#include "includes.h"
#define TASK_STK_SIZE 512
#define N_MESSAGES 128
OS_STK StartTaskStk[TASK_STK_SIZE];
OS_STK MyTaskStk[TASK_STK_SIZE];
OS_STK YouTaskStk[TASK_STK_SIZE];
char *ss;
char *s100;
char *s;
char *s500;
void *MsgGrp[N_MESSAGES]; //定义消息指针数组
INT8U err;
INT8U y=0;
OS_EVENT *Str_Q; //定义事件控制块
void MyTask(void *data);
void StartTask(void *data);
void YouTask(void *data);
void main (void)
{
OSInit();
PC_DOSSaveReturn(); /* 保存DOS环境 */
PC_VectSet(uCOS, OSCtxSw);/* 安装uC/OS-II中断 */
Str_Q=OSQCreate(&MsgGrp[0],N_MESSAGES); //创建消息队列
OSTaskCreate(StartTask, (void *)0, &StartTaskStk[TASK_STK_SIZE - 1], 0);
OSStart(); /* Start multitasking */
}
void StartTask (void *pdata)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT16S key;
pdata = pdata; /* Prevent compiler warning */
OS_ENTER_CRITICAL();
PC_VectSet(0x08, OSTickISR); /* 安装时钟向量中断 */
PC_SetTickRate(OS_TICKS_PER_SEC);
OS_EXIT_CRITICAL();
OSStatInit(); //Initialize uC/OS-II's statistics
OSTaskCreate(MyTask, (void *)0, &MyTaskStk[TASK_STK_SIZE - 1], 3);
OSTaskCreate(YouTask, (void *)0, &YouTaskStk[TASK_STK_SIZE - 1], 4);
s="How many strings could be geted?";// 1
OSQPostFront(Str_Q,s); //发送消息
for (;;) {
if(OSTimeGet()>100&&OSTimeGet()<500)// 4
{
//MyTask的OSQPend(一次性将消息队列中的消息读取完)判断条件4成立,从队列中先读取消息显示3,再显示2.
//显示完后判断4条件仍成立,再读取3,再读取2.此时4忆不成立读取1.等5成立时再读取5,
//再执行7 后面的句子,最后一直读取6(因为6比1进去的晚,2、3已不成立)
//-----------------------------------------
s100="The value of OSTIME is from 100 to 500 NOW!!";// 2
OSQPostFront(Str_Q,s100); //发送消息
s="The string belongs to which task"; // 3
OSQPostFront(Str_Q,s); //发送消息
//-----------------------------------------
}
if(OSTimeGet()>1000&&OSTimeGet()<1500)//5
{
s500="The value of OSTIME is from 1000 to 1500 NOW!!";//6
OSQPostFront(Str_Q,s500); //发送消息
}
if (PC_GetKey(&key) == TRUE)
{
if (key == 0x1B)
{ PC_DOSReturn(); }
}
OSTimeDlyHMSM(0, 0, 1, 0);
}
}
void MyTask(void *pdata)
{
#if OS_CRITICAL_METHOD == 3
OS_CPU_SR cpu_sr;
#endif
pdata=pdata;
for (;;)
{
ss=OSQPend(Str_Q,0,&err); //7 //请求消息队列
PC_DispStr(10,++y, ss, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
OSTimeDlyHMSM(0, 0, 1, 0);
}
}
void YouTask(void *pdata)
{
#if OS_CRITICAL_METHOD == 3
OS_CPU_SR cpu_sr;
#endif
pdata=pdata;
for (;;)
{
ss=OSQPend(Str_Q,0,&err); //请求消息队列
PC_DispStr(10,++y, ss, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
OSTimeDlyHMSM(0, 0, 1, 0);
}
}
OSQPend一次性从消息队列中把消息取完。
阅读(8352) | 评论(0) | 转发(0) |