51 Series Small OS Essence simple implementation 6 C language version to be improved

Source: Internet
Author: User

#include "stc12c5a. H


#define Timer_reload () {tl0=0x00; Th0=0xc4;} Enable to T/CInitial 10ms


#define MAX_TASKS 8//Maximum number of task slots.
unsigned char idata task_stack[max_tasks][2];//task stack. The PC pointer is 16 bits and requires 2 bytes.
unsigned char idata task_time[max_tasks]; Timing Time
unsigned char task_id=0; Current active Task number


void rtos_wait (unsigned char time)
{


Save Current task time
Task_time[task_id]=time;


Save the current breakpoint and put sp=sp-2, task switch to the next task;
Task_stack[task_id][1] =* ((unsigned char *) (SP));
sp--;
Task_stack[task_id][0] =* ((unsigned char *) (SP));
sp--;



Task number
if (++task_id = = max_tasks)
task_id = 0;
}
unsigned char a=0;
unsigned char b=0;
void Task_test ()
{
while (1)
{
a++;
Rtos_wait (100);After running, record the next address and return
a++;


}
}
void Task_test2 ()
{
while (1)
{
b++;
Rtos_wait (10);After running, record the next address and return
b++;
}
}
void Timer0init () @18.432mhz
{
AUXR &= 0x7F; Timer Clock 12T mode
Tmod &= 0xF0; Set Timer mode
Tmod |= 0x01; Set Timer mode
TL0 = 0x00; Set the timing initial value
TH0 =0xc4; Set timing Initial value 10ms
TF0 = 0; Clear the TF0 flag
TR0 = 1; Timer 0 Start Timer


Et0=1;
Ea=1;
}
void Main ()
{
Timer0init ();
Task_test ();
Task_test2 ();
while (1);
}
void Rwcl () Task time to switch back to real time
{
From the timing time minus 1, look for which task to, task time to real-time switch back to
unsigned char i;
for (i=0;i<max_tasks;i++)
{
if (Task_time[i])
{
task_time[i]--;
if (task_time[i]==0)
sp=task_stack[i]+1;
}
}
}
void Tm0_isr () Interrupt 1 using 1
{
Timed Time overloading
Timer_reload ();


RWCL (); Task time to switch back to real time
}

51 Series Small OS Essence simple implementation 6 C language version to be improved

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.