Example program for simulating 51 multitasking using C language on PC

Publisher:风轻迟Latest update time:2020-04-28 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include

#include

 

//Number of task slots. In this example, task swapping is not considered, so how many tasks are actually running?

//Just define as many task slots as you want, no more or less

#define MAX_TASKS 5

 

//Task stack pointer

unsigned char *task_sp[MAX_TASKS];

 

// Maximum stack depth. The minimum is not less than 2, and the conservative value is 12.

//Estimation method: Take 2 as the base, add 2 bytes for each additional layer of function call.

//If an interrupt may occur during the process, the stack depth required by the interrupt must be added.

//Methods to reduce stack depth: 1. Nest subroutines as little as possible 2. Disable interrupts before calling subroutines.

#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

}; //Task stack.

 

//Task loading function. Load the specified function (parameter 1) into the specified (parameter 2) task slot.

//If there is already a task in the slot, the original task will be lost, but the system itself will not fail.

//Enter the low byte and high byte of each task's function address into

//task_stack[task number][0] and task_stack[task number][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("task1n");

i = 0;

}

//task_switch();

}

}

void func2()

{

static unsigned int j;

j = 0;

 

while(1)

{

if(j<654)

{

j++;

}

if(j>=654)

{

printf("task2n");

j = 0;

}

//task_switch();

}

}

 

 

int main()

{

    printf("Hello world!n");

 

    printf("task_stack[0] = %pn",task_stack[0]);

    printf("task_stack+0 = %pn",task_stack+0);

    printf("task_stack+1 = %pn",task_stack+1);

    printf("task_stack+2 = %pn",task_stack+2);

    printf("task_stack+3 = %pn",task_stack+3);

    printf("task_stack+4 = %pn",task_stack+4);

 

    printf("task_stack[0] = %pn",task_stack[0]);

    printf("*(task_stack+0) = %pn",*(task_stack+0));

    printf("*(task_stack+1) = %pn",*(task_stack+1));

    printf("*(task_stack+2) = %pn",*(task_stack+2));

    printf("*(task_stack+3) = %pn",*(task_stack+3));

    printf("*(task_stack+4) = %pn",*(task_stack+4));

 

    printf("task_stack[0] = %pn",task_stack[0]);

    printf("*(task_stack+0)+0 = %pn",*(task_stack+0)+0);

    printf("*(task_stack+1)+1 = %pn",*(task_stack+1)+1);

    printf("*(task_stack+2)+2 = %pn",*(task_stack+2)+2);

    printf("*(task_stack+3)+3 = %pn",*(task_stack+3)+3);

    printf("*(task_stack+4)+4 = %pn",*(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 = %pn",task_stack);

    printf("task_stack[0] + 5 = %pn",(task_stack[0] + 5));

    printf("task_sp = %pn",task_sp);

    

    printf("ntask_sp[0] = %dn",task_sp[0]);

    printf("task_sp[1] = %dn",task_sp[1]);

    printf("task_sp[2] = %dn",task_sp[2]);

    printf("task_sp[3] = %dn",task_sp[3]);

    printf("task_sp[4] = %dn",task_sp[4]);

 

    //task_load(func1, 0);//Load the func1 function into slot 0

//task_load(func2, 1);//Load the func2 function into slot 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 ...

Reference address:Example program for simulating 51 multitasking using C language on PC

Previous article:Multi-task 51 MCU pure C language kernel V1.03
Next article:RTX51 tiny——Multitasking operating system on 51MCU

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号