#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 ...
Previous article:Multi-task 51 MCU pure C language kernel V1.03
Next article:RTX51 tiny——Multitasking operating system on 51MCU
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [National Technology N32 MCU Development Package] --N32G031 Series
- If signal modulation was like this back then, would you still not be able to learn it?
- Components Science Popularization: Pressure Sensor
- Question: How to accurately measure the duty cycle of a 100MHZ square wave? Thank you!
- Antenna Basics - Huawei
- What wattage is this resistor?
- Displaying the ESP32's REPL on the ST7735 LCD
- One camera has 7 crystals --- Hikvision network camera disassembly
- I saw a function and felt that the function it described might not be possible, so I would like to ask you for help.
- [HC32F460 Development Board Review] NO.4 ThreadX Porting