51系列小型操作系统精华 简单实现4

51系列小型操作系统精髓 简单实现4
#include <REG52.H>


#define MAX_TASKS 2 //任务槽个数.必须和实际任务数一至
#define MAX_TASK_DEP 12 //最大栈深.最低不得少于2 个,保守值为12.
unsigned char idata task_stack[MAX_TASKS][MAX_TASK_DEP];//任务堆栈.
unsigned char idata task_sp[MAX_TASKS];
unsigned char task_id; //当前活动任务号


//任务切换函数(任务调度器)
void task_switch(){
task_sp[task_id] = SP;
if(++task_id == MAX_TASKS)
task_id = 0;
SP = task_sp[task_id];
}
//任务装入函数.将指定的函数(参数1)装入指定(参数2)的任务槽中.如果该槽中原来就有任
//务,则原任务丢失,但系统本身不会发生错误.
void task_load(unsigned int fn, unsigned char tid){
task_sp[tid] = task_stack[tid] + 1;
task_stack[tid][0] = (unsigned int)fn & 0xff; //低字节
task_stack[tid][1] = (unsigned int)fn >> 8;     //高字节
}
//从指定的任务开始运行任务调度.调用该宏后,将永不返回.
#define os_start(tid) {task_id = tid,SP = task_sp[tid];return;}
/*============================以下为测试代码==========================*/
void task1(){
static unsigned char i;
while(1){
i++;
task_switch();//编译后在这里打上断点
}
}
void task2(){
static unsigned char j;
while(1){
j+=2;
task_switch();//编译后在这里打上断点
}
}
void main(){
//这里装载了两个任务,因此在定义MAX_TASKS 时也必须定义为2
task_load(task1, 0);//将task1 函数装入0 号槽
task_load(task2, 1);//将task2 函数装入1 号槽
os_start(0);

}

以上为网摘

以下为本人改进:任务函数切换

#include <REG52.H>

#define MAX_TASKS 2 //任务槽个数.必须和实际任务数一至
#define MAX_TASK_DEP 12 //最大栈深.最低不得少于2 个,保守值为12.
unsigned char idata task_stack[MAX_TASKS][MAX_TASK_DEP];//任务堆栈.
//unsigned char idata task_sp[MAX_TASKS];
unsigned char task_id; //当前活动任务号

//任务切换函数(任务调度器)
void task_switch(){
//	task_sp[task_id] = SP;
	if(++task_id == MAX_TASKS)
		task_id = 0;
	SP = task_stack[task_id] + 1;
}
//任务装入函数.将指定的函数(参数1)装入指定(参数2)的任务槽中.如果该槽中原来就有任
//务,则原任务丢失,但系统本身不会发生错误.
void task_load(unsigned int fn, unsigned char tid){
//	task_sp[tid] = task_stack[tid] + 1;
	task_stack[tid][0] = (unsigned int)fn & 0xff;	 //低字节
	task_stack[tid][1] = (unsigned int)fn >> 8;	     //高字节
}
//从指定的任务开始运行任务调度.调用该宏后,将永不返回.
#define os_start(tid) {task_id = tid,SP = task_stack[tid] + 1;return;}
/*============================以下为测试代码==========================*/
void task1(){
	static unsigned char i;
	while(1){
		i++;
		task_switch();//编译后在这里打上断点
	}
}
void task2(){
	static unsigned char j;
	while(1){
		j+=2;
		task_switch();//编译后在这里打上断点
	}
}
void main(){
	//这里装载了两个任务,因此在定义MAX_TASKS 时也必须定义为2
	task_load(task1, 0);//将task1 函数装入0 号槽
	task_load(task2, 1);//将task2 函数装入1 号槽
	os_start(0);
}

以本人再改进:任务断点切换

//任务切换函数(任务调度器)
void task_switch(){
	//保存当前断点;
	task_stack[task_id][0] =*((unsigned char *)(SP-1));
	task_stack[task_id][1] =*((unsigned char *)(SP-2));
	//设置任务优先级

	//切换到下一任务  ,如果没有下一任务,则回到0任务,切换到哪里?
	if(task_id==0)
		SP=SP-2;  //切换出去,接着执行
	else
	{
			SP = task_stack[task_id] + 1;    //


	}
				
}



全部程序

#include <REG52.H>

#define MAX_TASKS 2 //任务槽个数.必须和实际任务数一至
#define MAX_TASK_DEP 2 //最大栈深.最低不得少于2 个,保守值为12.
unsigned char idata task_stack[MAX_TASKS][MAX_TASK_DEP]={0};//任务堆栈.
//unsigned char idata task_sp[MAX_TASKS];
unsigned char task_id=0; //当前活动任务号

//任务切换函数(任务调度器)
void task_switch(){
	//保存当前断点;
	task_stack[task_id][0] =*((unsigned char *)(SP-1));
	task_stack[task_id][1] =*((unsigned char *)(SP-2));
	//设置任务优先级


	//切换到下一任务  ,如果没有下一任务,则回到0任务,切换到哪里?

	
	if(task_id==0)
	{
		task_id=1;
		if(task_stack[1][0]==0 && task_stack[1][1]==0 )
		{
			SP=SP-2;  //切换出去,接着执行
		}
		else
		{		
			SP = task_stack[task_id] + 1;
		}

		
	}
	else
	{
		   	task_id=0;
			SP = task_stack[task_id] + 1;    //
	}
	//



}
/*
//任务装入函数.将指定的函数(参数1)装入指定(参数2)的任务槽中.如果该槽中原来就有任
//务,则原任务丢失,但系统本身不会发生错误.
void task_load(unsigned int fn, unsigned char tid){
//	task_sp[tid] = task_stack[tid] + 1;
	task_stack[tid][0] = (unsigned int)fn & 0xff;	 //低字节
	task_stack[tid][1] = (unsigned int)fn >> 8;	     //高字节
}
//从指定的任务开始运行任务调度.调用该宏后,将永不返回.
#define os_start(tid) {task_id = tid,SP = task_stack[tid] + 1;return;}
*/
/*============================以下为测试代码==========================*/
void task1(){
	static unsigned char i;
	while(1){
		i++;
		task_switch();//编译后在这里打上断点
	}
}
void task2(){
	static unsigned char j;
	while(1){
		j+=2;
		task_switch();//编译后在这里打上断点
	}
}
void main(){
	task1();
	task2();
	/*
	//这里装载了两个任务,因此在定义MAX_TASKS 时也必须定义为2
	task_load(task1, 0);//将task1 函数装入0 号槽
	task_load(task2, 1);//将task2 函数装入1 号槽
	os_start(0);*/
}