代码来自于书本光盘 《嵌入式操作系统UCOSII原理及应用》
实例1信号量
#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 * s1=”MyTask”;
char * s2=”YouTask”;
INT8U err;
INT8U y=0; //字符显示位置
OS_EVENT *Fun_Semp; //定义信号量
void StartTask(void *data);
void MyTask(void *data);
void YouTask(void *data);
void Fun(INT8U x,INT8U y);
void Quitkey(void);
/************************主函数*********************************************/
void main (void)
{
OSInit(); //初始化uCOS_II
PC_DOSSaveReturn(); //保存Dos环境
PC_VectSet(uCOS, OSCtxSw); //安装uCOS_II中断
Fun_Semp=OSSemCreate(1); //创建信号量
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);
OSTaskSuspend(OS_PRIO_SELF);
}
//*****************************MyTask********************************************
void MyTask (void *pdata)
{
pdata = pdata;
for (;;)
{
// OSSemPend(Fun_Semp,0,&err); //请求信号量
PC_DispStr(0,++y,s1,DISP_BGND_BLACK+DISP_FGND_WHITE);
Fun(9,y);
// OSSemPost(Fun_Semp); //发送信号量
Quitkey();
OSTimeDlyHMSM(0,0,1,0);
}
}
//*****************************YouTask********************************************
void YouTask (void *pdata)
{
pdata = pdata;
for (;;)
{
// OSSemPend(Fun_Semp,0,&err);
PC_DispStr(0,++y,s2,DISP_BGND_BLACK+DISP_FGND_WHITE);
Fun(10,y);
// OSSemPost(Fun_Semp);
Quitkey();
OSTimeDlyHMSM(0,0,2,0);
}
}
//*****************************Fun()********************************************
void Fun(INT8U x,INT8U y)
{
PC_DispStr(x,y,”call Fun()”,DISP_BGND_BLACK+DISP_FGND_YELLOW);
}
//*****************************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 * s1=”MyTask is running”;
char * s2=”YouTask is running”;
char * s3=”HerTask is running”;
char * ss=”MyTask pend a sem”;
INT8U err;
INT8U x=0,y=0;//字符显示位置
INT32U Times=0;
OS_EVENT *Semp;
void StartTask(void *data);
void MyTask(void *data);
void YouTask(void *data);
void HerTask(void *data);
void Quitkey(void);
/************************主函数*********************************************/
void main (void)
{
OSInit(); //初始化uCOS_II
PC_DOSSaveReturn(); //保存Dos环境
PC_VectSet(uCOS, OSCtxSw); //安装uCOS_II中断
Semp=OSSemCreate(1);
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);
OSTaskCreate(YouTask,0,&YouTaskStk[TASK_STK_SIZE – 1],7);
OSTaskCreate(HerTask,0,&HerTaskStk[TASK_STK_SIZE – 1],8);
OSTaskSuspend(OS_PRIO_SELF);
}
//*****************************MyTask********************************************
void MyTask (void *pdata)
{
pdata = pdata;
for (;;)
{
OSTimeDlyHMSM(0,0,1,200);
{
PC_DispStr(10,++y,ss,DISP_BGND_BLACK+DISP_FGND_YELLOW);
OSSemPend(Semp,0,&err);
PC_DispStr(10,++y,s1,DISP_BGND_BLACK+DISP_FGND_YELLOW);
OSSemPost(Semp);
Quitkey();
}
OSTimeDlyHMSM(0,0,0,200);
}
}
//*****************************YouTask********************************************
void YouTask (void *pdata)
{
pdata = pdata;
for (;;)
{
PC_DispStr(7,++y,s2,DISP_BGND_BLACK+DISP_FGND_WHITE);
Quitkey();
OSTimeDlyHMSM(0,0,0,300);
}
}
//*****************************HerTask********************************************
void HerTask (void *pdata)
{
pdata = pdata;
for (;;)
{
OSSemPend(Semp,0,&err);
PC_DispStr(10,++y,s3,DISP_BGND_BLACK+DISP_FGND_WHITE);
for(Times;Times<30000000;Times++)
{OS_Sched();}
OSSemPost(Semp);
Quitkey();
OSTimeDlyHMSM(0,0,1,0);
}
}
//*****************************Quitkey()********************************************
void Quitkey(void)
{
if(PC_GetKey(&key)==TRUE)
{
if(key==0x1B)
{PC_DOSReturn();}
}
}
实例三信号量集
只有后两个任务执行后,第一个任务才执行
#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 *s1=”MyTask is running”;
char *s2=”YouTask is running”;
char *s3=”HerTask is running”;
INT8U err;
INT8U x=0,y=0; //字符显示位置
OS_FLAG_GRP *WFlag; /*事件标志组指针*/
void StartTask(void *data);
void MyTask(void *data);
void YouTask(void *data);
void HerTask(void *data);
void Quitkey(void);
/************************主函数*********************************************/
void main (void)
{
OSInit(); //初始化uCOS_II
PC_DOSSaveReturn(); //保存Dos环境
PC_VectSet(uCOS, OSCtxSw); //安装uCOS_II中断
WFlag=OSFlagCreate(0,&err); /*创建事件标志组*/
if(err==OS_NO_ERR) /*检测是否创建成功*/
PC_DispStr(0,23,”No error”,DISP_BGND_BLACK+DISP_FGND_WHITE);
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);
OSTaskCreate(YouTask,0,&YouTaskStk[TASK_STK_SIZE – 1],7);
OSTaskCreate(HerTask,0,&HerTaskStk[TASK_STK_SIZE – 1],8);
OSTaskSuspend(OS_PRIO_SELF);
}
//*****************************MyTask********************************************
void MyTask (void *pdata)
{
pdata = pdata;
for (;;)
{
OSFlagPend(WFlag,(OS_FLAGS)3,OS_FLAG_WAIT_SET_ALL,0,&err);
PC_DispStr(10,++y,s1,DISP_BGND_BLACK+DISP_FGND_WHITE);
Quitkey();
OSTimeDlyHMSM(0,0,2,0);
}
}
//*****************************YouTask********************************************
void YouTask (void *pdata)
{
pdata = pdata;
for (;;)
{
PC_DispStr(10,++y,s2,DISP_BGND_BLACK+DISP_FGND_WHITE);
OSTimeDlyHMSM(0,0,8,0);
OSFlagPost(WFlag,(OS_FLAGS)2,OS_FLAG_SET,&err);
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);
OSFlagPost(WFlag,(OS_FLAGS)1,OS_FLAG_SET,&err);
Quitkey();
OSTimeDlyHMSM(0,0,1,0);
}
}
//*****************************Quitkey()********************************************
void Quitkey(void)
{
if(PC_GetKey(&key)==TRUE)
{
if(key==0x1B)
{PC_DOSReturn();}
}
}