本帖最后由 singleywy 于 2011-3-15 09:08 编辑
12# 刘前辈
我给出了这样的程序,使用了信号量查询语句,不知如何:
#include<reg52.h>
#include "Os_api.h"
#include"IOfor52.h"
void OsTimerISR()interrupt 1
{
OsIntEnter();
#if T0_TIMER_EN
ACC=((65536+8-SYS_CLK)%256);
TR0=0;
TL0+=ACC;
ACC=0;
CY=ACC&0x80;
ACC =((65536+8-SYS_CLK)/256)+ACC;
TH0+=ACC;
TR0=1; //时间管理
#endif
#if T2_TIMER_EN
#endif
#if TIME_EXPAND_EN
OsRunTime--;
if(OsRunTime == 0){
OsRunTime=TIME_COEFFICIENT-1;
OsTimeTick();
}
#else
OsTimeTick();
#endif
OsIntExit();
}
void TaskA()
{
while(1)
{
if( OsSemQuery(4) & OsSemQuery(0) & SEM_USEFUL){
OsSemP(4,0);
OsSemP(0,0);
LED1_ON;
OsTaskWait(DO_TMO,200);
LED1_OFF;
OsSemV(0);//释放信号量0
OsSemV(4);//释放信号量4
}
OsTaskWait(DO_TMO,200);
}
}
void TaskB()
{
while(1)
{
if( OsSemQuery(0) & OsSemQuery(1) & SEM_USEFUL){
OsSemP(0,0);
OsSemP(1,0);
LED2_ON;
OsTaskWait(DO_TMO,200);
LED2_OFF;
OsSemV(1);//释放信号量1
OsSemV(0);//释放信号量0 }
OsTaskWait(DO_TMO,200);
}
}
void TaskC()
{
while(1)
{
if( OsSemQuery(1) & OsSemQuery(2) & SEM_USEFUL){
OsSemP(1,0);
OsSemP(2,0);
LED3_ON;
OsTaskWait(DO_TMO,200);
LED3_OFF;
OsSemV(2);//释放信号量2
OsSemV(1);//释放信号量1
}
OsTaskWait(DO_TMO,200);
}
}
void TaskD()
{
while(1)
{
if( OsSemQuery(2) & OsSemQuery(3) & SEM_USEFUL){
OsSemP(2,0);
OsSemP(3,0);
LED4_ON;
OsTaskWait(DO_TMO,200);
LED4_OFF;
OsSemV(3);//释放信号量3
OsSemV(2);//释放信号量2
}
OsTaskWait(DO_TMO,200);
}
}
void TaskE()
{
while(1)
{
if( OsSemQuery(3) & OsSemQuery(4) & SEM_USEFUL){
OsSemP(3,0);
OsSemP(4,0);
LED5_ON;
OsTaskWait(DO_TMO,200);
LED5_OFF;
OsSemV(4);//释放信号量4
OsSemV(3);//释放信号量3
}
OsTaskWait(DO_TMO,200);
}
}
void TaskIdle()
{
static uint8 i;
LED0_ON;
OsSemCreate(0,1);//创建信号量0,初始值为1;
OsSemCreate(1,1);//创建信号量1,初始值为1;
OsSemCreate(2,1);//创建信号量2,初始值为1;
OsSemCreate(3,1);//创建信号量3,初始值为1;
OsSemCreate(4,1);//创建信号量4,初始值为1;
while(1)
{
i++;
LED0=LED0^1;
}
}
void main()
{
OsTaskCreate(0,(uint16)TaskA);
OsTaskCreate(1,(uint16)TaskB);
OsTaskCreate(2,(uint16)TaskC);
OsTaskCreate(3,(uint16)TaskD);
OsTaskCreate(4,(uint16)TaskE);
OsTaskStart(5,(uint16)TaskIdle);
} |