pc上用C语言模拟51多任务的案例程序
#include <stdio.h>
#include <stdlib.h>
//任务槽个数.在本例中并未考虑任务换入换出,所以实际运行的任务有多少个,
//就定义多少个任务槽,不可多定义或少定义
#define MAX_TASKS 5
//任务的栈指针
unsigned char *task_sp[MAX_TASKS];
//最大栈深.最低不得少于2个,保守值为12.
//预估方法:以2为基数,每增加一层函数调用,加2字节.
//如果其间可能发生中断,则还要再加上中断需要的栈深.
//减小栈深的方法:1.尽量少嵌套子程序 2.调子程序前关中断.
#define MAX_TASK_DEP 12
unsigned char task_stack[MAX_TASKS][MAX_TASK_DEP] =
{
0,1,2,3,4,5,6,7,8,9,'a',9,8,7,6,5,4,3,2,1,0,11,12,13,113,31,4
};//任务堆栈.
//任务装入函数.将指定的函数(参数1)装入指定(参数2)的任务槽中.
//如果该槽中原来就有任务,则原任务丢失,但系统本身不会发生错误.
//将各任务的函数地址的低字节和高字节分别入在
//task_stack[任务号][0]和task_stack[任务号][1]中
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;
}
void func1()
{
static unsigned char i;
i = 0;
while(1)
{
if(i<250)
{
i++;
}
if(i>=250)
{
printf("task1\n");
i = 0;
}
//task_switch();
}
}
void func2()
{
static unsigned int j;
j = 0;
while(1)
{
if(j<654)
{
j++;
}
if(j>=654)
{
printf("task2\n");
j = 0;
}
//task_switch();
}
}
int main()
{
printf("Hello world!\n");
printf("task_stack[0] = %p\n",task_stack[0]);
printf("task_stack+0 = %p\n",task_stack+0);
printf("task_stack+1 = %p\n",task_stack+1);
printf("task_stack+2 = %p\n",task_stack+2);
printf("task_stack+3 = %p\n",task_stack+3);
printf("task_stack+4 = %p\n",task_stack+4);
printf("task_stack[0] = %p\n",task_stack[0]);
printf("*(task_stack+0) = %p\n",*(task_stack+0));
printf("*(task_stack+1) = %p\n",*(task_stack+1));
printf("*(task_stack+2) = %p\n",*(task_stack+2));
printf("*(task_stack+3) = %p\n",*(task_stack+3));
printf("*(task_stack+4) = %p\n",*(task_stack+4));
printf("task_stack[0] = %p\n",task_stack[0]);
printf("*(task_stack+0)+0 = %p\n",*(task_stack+0)+0);
printf("*(task_stack+1)+1 = %p\n",*(task_stack+1)+1);
printf("*(task_stack+2)+2 = %p\n",*(task_stack+2)+2);
printf("*(task_stack+3)+3 = %p\n",*(task_stack+3)+3);
printf("*(task_stack+4)+4 = %p\n",*(task_stack+4)+4);
task_sp[0] = (task_stack[0]+1);
task_sp[1] = (task_stack[0]+2);
task_sp[2] = (task_stack[0]+3);
task_sp[3] = (task_stack[0]+4);
task_sp[4] = (task_stack[0]+5);
//task_sp[0] = *(task_stack+0)+9;
printf("task_stack = %p\n",task_stack);
printf("task_stack[0] + 5 = %p\n",(task_stack[0] + 5));
printf("task_sp = %p\n",task_sp);
printf("\ntask_sp[0] = %d\n",task_sp[0]);
printf("task_sp[1] = %d\n",task_sp[1]);
printf("task_sp[2] = %d\n",task_sp[2]);
printf("task_sp[3] = %d\n",task_sp[3]);
printf("task_sp[4] = %d\n",task_sp[4]);
//task_load(func1, 0);//将func1函数装入0号槽
//task_load(func2, 1);//将func2函数装入1号槽
return 0;
}
Hello world!
task_stack[0] = 00403000
task_stack+0 = 00403000
task_stack+1 = 0040300C
task_stack+2 = 00403018
task_stack+3 = 00403024
task_stack+4 = 00403030
task_stack[0] = 00403000
*(task_stack+0) = 00403000
*(task_stack+1) = 0040300C
*(task_stack+2) = 00403018
*(task_stack+3) = 00403024
*(task_stack+4) = 00403030
task_stack[0] = 00403000
*(task_stack+0)+0 = 00403000
*(task_stack+1)+1 = 0040300D
*(task_stack+2)+2 = 0040301A
*(task_stack+3)+3 = 00403027
*(task_stack+4)+4 = 00403034
task_stack = 00403000
task_stack[0] + 5 = 00403005
task_sp = 004050B0
task_sp[0] = 4206593
task_sp[1] = 4206594
task_sp[2] = 4206595
task_sp[3] = 4206596
task_sp[4] = 4206597
Terminated with return code 0
Press any key to continue ...