51 Series Small OS Essence simple implementation 3

Source: Internet
Author: User

Problems encountered today:

1. Tasks do not have to be preloaded into a task. Load a task.

When there is no task. How does the timer work? When there is only one task, what do you do with it? How do multiple tasks rotate or shift by priority?

2. With regard to the timing of each task, is there a better way to reduce the trouble?

3. No protection deposit, so switch, is it correct? When a task function has multiple layers nested. Is there a problem?

#include "stc12c5a. H "#define Timer_reload () {tl0=0x00; Th0=0xc4;} Enable T/C initial 10ms#define Max_tasks 8//number of task slots. Must and actual task number one to # define MAX_TASK_DEP//maximum stack depth. Minimum of 2, conservative value 12.unsigned char idata TAS k_stack[max_tasks][2];//task stack. unsigned char idata task_time[max_tasks];unsigned char task_id=0; Currently active task number bit akg=0,bkg=0;/*//task switch function (Task Scheduler) void Task_switch () {task_sp[task_id] = sp;if (++task_id = max_tasks) task_id = 0; SP = task_sp[task_id];} The *//*//task loads the function. Loads the specified function (1) into the task slot of the specified (2). Assuming that the slot is already in service, the original task is lost, but the system itself does not error. void task_load (unsigned int fn, unsigned Char tid) {Task_sp[tid] = Task_stack[tid] + 1;task_stack[tid][0] = (unsigned int) FN & 0xff;//low byte task_stack[tid][1] = (     unsigned int) FN >> 8; High byte} */void task_switch_wait (unsigned char time) {//Task mount, save current location and data. Task_load ();//Task Time task_time[task_id]=time;//Save the current breakpoint and put Sp=sp-2;task_stack[task_id][0] =* (sp--); task_stack[task_ Id][1] =* (sp--); if (++task_id = = max_tasks) task_id = 0;//task switches to the next task//task_switch ();} void Task_a () {while (1) {Akg=0;task_switch_waiT (1); Akg=1;}} void Task_b () {while (1) {bkg=0;task_switch_wait (1); bkg=1;}} void Timer0init ()//@18.432mhz{auxr &= 0x7f;//Timer clock 12T mode tmod &= 0xf0;//set timer mode tmod |= 0x01;//Set timer mode TL0 = 0x00;/ /Set Timing initial value TH0 =0xc4;//set Timing initial value 10mstf0 = 0;//Clear TF0 Flag TR0 = 1;//Timer 0 start timing et0=1; Ea=1;} void Main () {timer0init (); Task_a (); while (1) {Task_b ();}} void Tm0_isr () Interrupt 1 using 1{//task time is reached. Task time to real-time switch back//real-time switch back to sp=task_stack[task_id]+1;//timing time overload timer_reload ();}


51 Series Small OS Essence simple implementation 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.