多任务 51单片机纯C语言内核V1.03

时间:2021-08-05 20:04:39

#include<reg52.h>

#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long

/*
Task_creat(1),Task_creat(2),Task_creat(3)
[task1][task2][task3][idel]...
...
Task_destroy(2)
[task1][idel][task3][idel]...
...
Task_destroy(1)
[idel][idel][task3][idel]...
...
Task_creat(4)
[task4][idel][task3][idel]...
//----------------------------------------------
TaskOrder[i]<->TaskCounter[i]<->TaskCountValue[i]
*/

#define TASKNUM 10 //任务空间
#define TASKREALNUM 9
#define STACKLENGTH 10
//---------------------------------------
uchar TaskCharge = TASKNUM;
uchar TaskNowRuning = 0;
//函数调用原型

uchar TaskOrder [TASKNUM] = {0};  //任务表
uchar TaskCounter [TASKNUM] = {0};
uchar TaskCountValue [TASKNUM] = {0};
uchar TaskPriority [TASKNUM] = {0};

uchar TaskStack [STACKLENGTH] = {0}; //任务栈
uchar TaskStackHead;
uchar TaskStackEnd;

struct flag
{
 uchar TaskFinish :1;
};
struct flag SysBit;
//-----------------调试用---------------------
uint Timerlengh;
uint Timerlenghhigh;
uint Timerlenghlow;
/*
ulong SysCounter;
ulong SysCountShow;
uint SysOnems;
*/

//---------------------------------------    
void Core_ini (void);
void Idel (void);
void Task_creat (uchar task_id,uchar task_count,uchar task_pri);
void Task_destory (uchar task_id);
//---------------------------------------
void Idle (void)
{}

void Core_ini (void)
{
 TaskLib[0] = &Idle;
 TaskLib[1] = &f1;
 TaskLib[2] = &f2;
 TaskLib[3] = &f3;
 TaskLib[4] = &f4;
 TaskLib[5] = &f5;
 TaskLib[6] = &f6;
 TaskLib[7] = &f7;
 TaskLib[8] = &f8;
 TaskLib[9] = &f9;

 TaskOrder [0] = 0;    //生成系统任务task0
 
 TaskStackHead = 0;
 TaskStackEnd = 0;
 
 SysBit.TaskFinish = 0;
//-----------------------------------------------
 Timerlengh = 0;
 Timerlenghhigh = 0;
 Timerlenghlow = 0xffff;
/*
 SysCounter = 0;
 SysCountShow = 0;
 SysOnems = 0;
*/ 
}
//-------------------------------------------------
void Task_creat (uchar task_id,uchar task_count,uchar task_pri)
{
 uchar i;
 uchar taskseat;
 i = 1;
 taskseat = 0;
            
 while (taskseat == 0)
 {  
  if (TaskOrder[i] == 0)
  {
   TaskOrder[i] = task_id;   //任务生成
   TaskPriority [i] = task_pri;
   TaskCounter[i] = 0;
   TaskCountValue[i] += task_count;
   taskseat = 1;
  }
  else
  {
   if (i < TASKREALNUM)
   {
    i++;
   }
   else
   {
    taskseat = 1;  //无空位
   }
  }
 }
}

 

void Task_destory (uchar task_id)
{
 uchar i;
 uchar taskaway;
 i = 1;
 taskaway = 0;

 while (taskaway == 0)
 {  
  if (TaskOrder[i] == task_id)
  {
   TaskOrder[i] = 0;   //任务删除
   TaskPriority [i] = 0;
   TaskCounter[i] = 0;
   TaskCountValue[i] = 0;
   taskaway = 1;
  }
  else
  {
   if (i < TASKREALNUM)
   {
    i++;
   }
   else
   {
    taskaway = 1;  //无空位
   }
  }
 } 
}

 

void WatchDog (void)
{}

 

#define timeH 0xfc
#define timeL 0x17 //1ms减去入栈误差

 

void timer0_ini (void)
{
 TMOD = 0x01;
   TH0 = timeH;
   TL0 = timeL;
   TR0 = 1;
   ET0 = 1;
   EA = 1;
}

 

void timer0 (void) interrupt 1
{
 uchar i;
 TR0 = 0;
 TL0 = timeL;
 TH0 = timeH;
 TR0 = 1;
//---------------------watchdog更新--------------------
//---------------------计数维护---------------------------  
....
//-------------------优先级排序(倒排序)----------------- 
....
//------------------------任务出栈------------------------   
....
//-------------------------调试用--------------------------
 Timerlengh = (TH0 * 256 + TL0) - 0xfc17;
 if (Timerlenghhigh < Timerlengh)
 {
  Timerlenghhigh = Timerlengh;
 }
 if (Timerlenghlow > Timerlengh)
 {
  Timerlenghlow = Timerlengh;
 }
}

 

void main (void)

 SP = 0x70;
 Core_ini ();
//----------------------------
 Task_creat (1,10,1);
 Task_creat (2,10,2);
 Task_creat (3,10,3);
 Task_creat (4,10,4);
 Task_creat (5,10,5);
 Task_creat (6,10,6);
 Task_creat (7,10,7);
 Task_creat (8,10,8);
 Task_creat (9,10,9);
// Task_destory (4);
 timer0_ini ();
 WatchDog ();
//------------------------------------
 for ( ; ; )
 {
//函数调用原型
  SysBit.TaskFinish = 1;
  while (SysBit.TaskFinish)
  {
//   SysCounter++;
  }
//------------------------------------ 
 }
}

 

 

 

 

特点:

1.时间片轮转调度

2.多任务

3.优先级并发控制

4.内核消耗小

5.任务任意载入删除

6.实时性强

 

 

如有任何问题请加我qq:549917930