51上写操作系统1(三任务调度)

typedef unsigned char uc8;
typedef unsigned int ui16;

//让灯轮流亮灭
#include <reg52.h>


sbit led0=P0^0;
sbit led1=P0^1;
sbit led2=P0^2;


uc8 yxhao; //当前任务号
uc8 rwdz0[10];
uc8 rwdz1[10];
uc8 rwdz2[10];
uc8 rwsp[3];


void rw_0();
void rw_1();
void rw_2(); 
  
void rw_td(uc8 rwhao)
{
rwsp[yxhao]=SP;
yxhao=rwhao;
SP=rwsp[yxhao];
}
void rw_ys(ui16 ys)
{
ui16 i;
ui16 j;
for(i=0;i<ys;i++)
{for(j=0;j<10000;j++){;}}
}
void main()
{
rwdz0[1]=(ui16)rw_0;
rwdz0[2]=(ui16)rw_0>>8;
rwdz1[1]=(ui16)rw_1;
rwdz1[2]=(ui16)rw_1>>8;
rwdz2[1]=(ui16)rw_2;
rwdz2[2]=(ui16)rw_2>>8;
rwsp[0]=rwdz0;
rwsp[0]+=2;
rwsp[1]=rwdz1;
rwsp[1]+=2;
rwsp[2]=rwdz2;
rwsp[2]+=2;


yxhao=0;
SP=rwsp[0];
}
void rw_0()
{
while(1)
{
led0=~led0;
rw_ys(10);
rw_td(1);
}
}
void rw_1()
{
while(1)
{
led1=~led1;
rw_ys(10);
rw_td(2);
}
}
void rw_2()
{
while(1)
{
led2=~led2;
rw_ys(10);
rw_td(0);
}
}

你可能感兴趣的:(操作系统)