About multithreaded work

Source: Internet
Author: User
Tags call back

Scheduled interrupt for task switching
As shown in 4 A, the CPU waits in the idle task cycle, periodically interrupts the CPU to call back, according to the task design
Different response frequency, the task satisfies the condition will get CPU resources, the CPU for different tasks "care" after the completion, and then
Time to return to idle tasks, and so on, for each task, it seems that each has a separate CPU, each
Run independently. The best thing about this idea is that the task is easy to crop and the system can be very complex.
Miscellaneous.
After taking full account of the single-chip interrupt feature (where the interrupt is returned to), the actual task switching
As shown in 4 B, timed interrupts can occur at any time of task scheduling, random task execution, as shown in the largest box in the diagram,
No matter when the interrupt occurs, it returns normally, and the impact of a timed interrupt only works in the Task Scheduler module,
In turn, make different tasks at different beats. Task scheduling performs a ready task at a certain priority level.
Summarize the different tasks need CPU care frequency, choose the fastest frequency to set the timer interrupt beat,
Generally choose 200Hz, or 100Hz can be. In addition to each task set a beat control counter C, also
is the number of times each interrupt is performed after the timer executes the task. For example, take a timed interrupt beat of 200Hz, to the task set c=10,
Then the task execution frequency is 200/10=20hz, if is the digital tube scan, presses 40HZ does not blink the law, then the task beat control
The counter c=5 can be. In program design, C represents the tempo control parameters of a task, and we are accustomed to using delay to
Description, different tasks with task0,task1 ... To describe.
Continue writing tomorrow how to implement in code! 2009-6-29
Here we use code to achieve the above multi-task programming ideas.
First, the task switch.
while (1)
{
if (task_delay[0]==0) Task0 (); Task0 Ready,
if (task_delay[1]==0) Task1 (); Task1 Ready,
......
}
Obviously, the task is to execute the condition is the task delay amount task_delay=0, then the task delay volume who control it? Timing
The device! Timer interrupt the task delay amount minus one until zero, the flag task is ready. When no task is ready, the task cuts
Swapping itself is an idle task.
void Timer0 (void) Interrupt 1
{
if (task_delay[0]) task_delay[0]--;
if (task_delay[1]) task_delay[1]--;
......
}
For example, if the interrupt beat for Timer0 is 200hz,task0_delay with an initial value of 10, the Task0 () execution frequency is
200/10=20hz.
With this foundation in mind, we will design a simple multitasking program to further understand this programming idea.
Task requirements: With MCU different IO pin output 1hz,5hz,10hz,20hz square wave signal, this program is very short, will
directly to the.
#include "Reg51.h"
#define TIME_PER_SEC 200//define task clock frequency, 200Hz
#define CLOCK 22118400//To define clocking oscillator, unit Hz
#define MAX_TASK 4//define the number of tasks

extern void Task0 (void); Mission Statement
extern void Task1 (void);
extern void Task2 (void);
extern void Task3 (void);

Sbit f1hz = p1^0; Port definition
Sbit f5hz = p1^1;
Sbit f10hz = p1^2;
Sbit f20hz = p1^3;

unsigned char task_delay[4]; Task delay Variable definition

Timer 0 Initialization
void Timer0_init (void)
{
unsigned char i;
for (i=0;i<max_task;i++) Task_delay= 0; Task Delay Amount Zeroed
Tmod = (Tmod & 0XF0) |        0x01; Timer 0 operates in Mode 1, 16Bit timer mode
Expression
TH0 = 255-clock/time_per_sec/12/256;
TL0 = 255-clock/time_per_sec/12%256;
TR0 = 1;
ET0 = 1; Turn on timers and interrupts
}

System OS Timer Interrupt Service
void Timer0 (void) Interrupt 1
{
unsigned char i;
TH0 = 255-clock/time_per_sec/12/256;
TL0 = 255-clock/time_per_sec/12%256;
for (i=0;i<max_task;i++) if (task_delay) Task_delay--;
The task is ready by minus 1 per beat on the task delay variable to 0.
}

/*main Main Function */
void Main (void)
{
Timer0_init ();
ea=1;//Open Total Interrupt
while (1)
{
if (task_delay[0]==0) {task0 (); task_delay[0] = TIME_PER_SEC/2;}
To produce a 1hz signal, the rollover period is 2Hz, using the same
if (task_delay[1]==0) {Task1 (); task_delay[1] = TIME_PER_SEC/10;}
To produce a 5hz signal, the rollover period is 10Hz, using the same
if (task_delay[2]==0) {task2 (); task_delay[2] = time_per_sec/20;}
if (task_delay[3]==0) {task3 (); task_delay[3] = TIME_PER_SEC/40;}
}
}

void Task0 (void)
{
f1hz =!f1hz;
}

void Task1 (void)
{
f5hz =!f5hz;
}

void Task2 (void)
{
f10hz =!f10hz;
}

void Task3 (void)
{
f20hz =!f20hz;
}

(his program pasted down and I changed it myself)

#include <reg51.h>
#define TIME_PER_SEC 200//define task clock frequency, 200Hz
#define CLOCK 22118400//To define clocking oscillator, unit Hz
#define MAX_TASK 4//define the number of tasks

extern void Task0 (void); Mission Statement
extern void Task1 (void);
extern void Task2 (void);
extern void Task3 (void);

Sbit f1hz = p1^0; Port definition
Sbit f5hz = p1^1;
Sbit f10hz = p1^2;
Sbit f20hz = p1^3;

unsigned char task_delay[4]; Task delay Variable definition

Timer 0 Initialization
void Timer0_init (void)
{
unsigned char i;
for (i=0;i<max_task;i++) task_delay[i]=0; Task Delay Amount Zeroed
Tmod = (Tmod & 0XF0) |        0x01; Timer 0 operates in Mode 1, 16Bit timer mode
TH0 = 220;
TL0 = 30; Timer 0 Timing Period 10US
TR0 = 1;
ET0 = 1; Turn on timers and interrupts
}

System OS Timer Interrupt Service
void Timer0 (void) Interrupt 1
{
unsigned char i;
TH0 = 220;
TL0 = 30;
for (i=0;i<max_task;i++)
{
if (Task_delay[i])
task_delay[i]--;
if (I==max_task)
i=0;
}
The task is ready by minus 1 per beat on the task delay variable to 0.
}

/*main Main Function */
void Main (void)
{
Timer0_init ();
ea=1;//Open Total Interrupt
while (1)
{
if (task_delay[0]==0) {task0 (); task_delay[0] = TIME_PER_SEC/2;}//Execute once and fill new values continuously
To produce a 1hz signal, the rollover period is 2Hz, using the same
if (task_delay[1]==0) {Task1 (); task_delay[1] = TIME_PER_SEC/10;} The new value is constantly populated after execution
To produce a 5hz signal, the rollover period is 10Hz, using the same
if (task_delay[2]==0) {task2 (); task_delay[2] = time_per_sec/20;} The new value is constantly populated after execution
if (task_delay[3]==0) {task3 (); task_delay[3] = TIME_PER_SEC/40;} The new value is constantly populated after execution
}
}

void Task0 (void)
{
f1hz =!f1hz;
}

void Task1 (void)
{
f5hz =!f5hz;
}

void Task2 (void)
{
f10hz =!f10hz;
}

void Task3 (void)
{
f20hz =!f20hz;
}

About multithreaded work

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.