代码来自于书本光盘 《嵌入式操作系统UCOSII原理及应用》
实例1,申请一个内存块并写入数据然后显示出来
#include "includes.h"
#define TASK_STK_SIZE 512 /*任务堆栈长度*/
OS_STK StartTaskStk[TASK_STK_SIZE]; /*定义任务堆栈区*/
OS_STK MyTaskStk[TASK_STK_SIZE];
OS_MEM *IntBuffer;
INT8U IntPart[50][64];
INT8U *IntBlkPtr;
INT16S key; /*用于退出uCOS_II的键*/
char *s1="MyTask is running";
INT8U err;
INT8U x=0,y=0;/*字符显示位置*/
void StartTask(void *data);
void MyTask(void *data);
void Quitkey(void);
/************************main function*********************************************/
void main (void)
{
OSInit(); /*初始化uCOS_II*/
PC_DOSSaveReturn(); /*保存Dos环境*/
PC_VectSet(uCOS, OSCtxSw); /*安装uCOS_II中断*/
IntBuffer=OSMemCreate(IntPart,50,64,&err);
OSTaskCreate(StartTask,0,&StartTaskStk[TASK_STK_SIZE - 1],5);
OSStart();
}
//*****************************StartTask********************************************
void StartTask(void *pdata)
{
pdata=pdata;
OS_ENTER_CRITICAL();
PC_VectSet(0x08, OSTickISR);
PC_SetTickRate(OS_TICKS_PER_SEC);
OS_EXIT_CRITICAL();
// OSStatInit();
OSTaskCreate(MyTask,0,&MyTaskStk[TASK_STK_SIZE - 1],6);
OSTaskSuspend(OS_PRIO_SELF);
}
//*****************************MyTask********************************************
void MyTask (void *pdata)
{
pdata = pdata;
for (;;)
{
IntBlkPtr=OSMemGet(IntBuffer,&err);
*IntBlkPtr=1;
PC_DispStr(*IntBlkPtr*10,++y,s1,DISP_BGND_BLACK+DISP_FGND_WHITE);
* ++IntBlkPtr=2;
PC_DispStr(*IntBlkPtr*10,++y,s1,DISP_BGND_BLACK+DISP_FGND_WHITE);
IntBlkPtr--;
OSMemPut(IntBuffer,IntBlkPtr);
Quitkey();
OSTimeDlyHMSM(0,0,1,0);
}
}
//*****************************Quitkey()********************************************
void Quitkey(void)
{
if(PC_GetKey(&key)==TRUE)
{
if(key==0x1B)
{PC_DOSReturn();}
}
}
实例2
#include "includes.h"
#define TASK_STK_SIZE 512 //任务堆栈长度
OS_STK StartTaskStk[TASK_STK_SIZE]; //定义任务堆栈区
OS_STK MyTaskStk[TASK_STK_SIZE];
OS_STK YouTaskStk[TASK_STK_SIZE];
OS_STK HerTaskStk[TASK_STK_SIZE];
INT16S key; //用于退出uCOS_II的键
char *s;
char *s1="MyTask";
char *s2="YouTask";
char *s3="HerTask";
INT8U err;
INT8U x=0,y=0; //字符显示位置
INT8U Times=0;
OS_MEM *IntBuffer;
INT8U IntPart[8][6];
INT8U *IntBlkPtr;
OS_MEM_DATA MemInfo;
void StartTask(void *pdata);
void MyTask(void *pdata);
void YouTask(void *pdata);
void HerTask(void *pdata);
void Quitkey(void);
/************************主函数*********************************************/
void main (void)
{
OSInit(); //初始化uCOS_II
PC_DOSSaveReturn(); //保存Dos环境
PC_VectSet(uCOS, OSCtxSw); //安装uCOS_II中断
IntBuffer=OSMemCreate(IntPart,8,6,&err);
OSTaskCreate(StartTask,0,&StartTaskStk[TASK_STK_SIZE - 1],0);
OSStart();
}
//*****************************StartTask********************************************
void StartTask(void *pdata)
{
pdata=pdata;
OS_ENTER_CRITICAL();
PC_VectSet(0x08, OSTickISR);
PC_SetTickRate(OS_TICKS_PER_SEC);
OS_EXIT_CRITICAL();
// OSStatInit();
OSTaskCreate(MyTask,0,&MyTaskStk[TASK_STK_SIZE - 1],1);
OSTaskCreate(YouTask,0,&YouTaskStk[TASK_STK_SIZE - 1],2);
OSTaskCreate(HerTask,0,&HerTaskStk[TASK_STK_SIZE - 1],3);
OSTaskSuspend(OS_PRIO_SELF);
}
//*****************************MyTask********************************************
void MyTask (void *pdata)
{
pdata = pdata;
for (;;)
{
PC_DispStr(10,++y,s1,DISP_BGND_BLACK+DISP_FGND_WHITE);
IntBlkPtr=OSMemGet(IntBuffer,&err);
OSMemQuery(IntBuffer,&MemInfo);
sprintf(s,"%0x",MemInfo.OSFreeList);
PC_DispStr(30,y,s,DISP_BGND_BLACK+DISP_FGND_WHITE);
sprintf(s,"%d",MemInfo.OSNUsed);
PC_DispStr(40,y,s,DISP_BGND_BLACK+DISP_FGND_WHITE);
if(Times>4)
{
OSMemPut(IntBuffer,IntBlkPtr);
}
Times++;
Quitkey();
OSTimeDlyHMSM(0,0,1,0);
}
}
//*****************************YouTask********************************************
void YouTask (void *pdata)
{
pdata = pdata;
for (;;)
{
PC_DispStr(10,++y,s2,DISP_BGND_BLACK+DISP_FGND_WHITE);
IntBlkPtr=OSMemGet(IntBuffer,&err);
OSMemQuery(IntBuffer,&MemInfo);
sprintf(s,"%0x",MemInfo.OSFreeList);
PC_DispStr(30,y,s,DISP_BGND_BLACK+DISP_FGND_WHITE);
sprintf(s,"%d",MemInfo.OSNUsed);
PC_DispStr(40,y,s,DISP_BGND_BLACK+DISP_FGND_WHITE);
OSMemPut(IntBuffer,IntBlkPtr);
Quitkey();
OSTimeDlyHMSM(0,0,2,0);
}
}
//*****************************HerTask********************************************
void HerTask (void *pdata)
{
pdata = pdata;
for (;;)
{
PC_DispStr(10,++y,s3,DISP_BGND_BLACK+DISP_FGND_WHITE);
IntBlkPtr=OSMemGet(IntBuffer,&err);
OSMemQuery(IntBuffer,&MemInfo);
sprintf(s,"%0x",MemInfo.OSFreeList);
PC_DispStr(30,y,s,DISP_BGND_BLACK+DISP_FGND_WHITE);
sprintf(s,"%d",MemInfo.OSNUsed);
PC_DispStr(40,y,s,DISP_BGND_BLACK+DISP_FGND_WHITE);
OSMemPut(IntBuffer,IntBlkPtr);
Quitkey();
OSTimeDlyHMSM(0,0,1,0);
}
}
//*****************************Quitkey()********************************************
void Quitkey(void)
{
if(PC_GetKey(&key)==TRUE)
{
if(key==0x1B)
{PC_DOSReturn();}
}
}