Implementation of software timer and time-sharing multiplexing

Source: Internet
Author: User

In SCM programming, the most important thing is to master the time beat of the microcontroller, to maximize the use of CPU resources, the following provides a base timer based on software timing, task execution using time-sharing method, the execution time of each task is specified, the execution times to the next task after switching.

U16 Timer_count = 0,timer_count_overflow = 0;
U16 This_time = 0,last_time = 0,time_overflow = 0; #define TIMER_PERIOD 60000 #define OUTOFRUNNINGTIME (time) (timer_period*timer_count_overflow+this_time-last_t              IME >= time) #define TIME_MS (x) (1*x) #define TIME_S (x) (1000*x) #define Total_task

3//Define the total number of tasks enum {TASK1,TASK2,TASK3};
	void interrupt PIT0 (void)/interrupt timer, 1ms enter once {timer_count + +;
		if (Timer_count >= timer_period) {timer_count = 0;
	Timer_count_overflow + +;
	}} static U8 Task1 (void) {last_time = Timer_count;
	Timer_count_overflow = 0;
	for (;;)
		{this_time = Timer_count;
		Do_some_thing ();
	if (Outofrunningtime (time_s (2))) return TASK2;
	}} static U8 Task2 (void) {last_time = Timer_count;
	Timer_count_overflow = 0;
	for (;;)
		{this_time = Timer_count;
		Do_some_thing ();
	if (Outofrunningtime (time_s (2))) return TASK3;
	}} static U8 Task3 (void) {last_time = Timer_count; Timer_count_overfLow = 0;
	for (;;)
		{this_time = Timer_count;
		Do_some_thing ();
	if (Outofrunningtime (time_s (2))) return TASK1;
	}} U8 Task_process (U8 Task) {U8 Next_task;
		Switch (Task) {Case task1:next_task = Task1 ();
		Break
		Case task2:next_task = Task2 ();
		Break
		Case task3:next_task = Task3 ();
		Break
	Default:break;
} return next_task;
} U8 Current_task = TASK1;
	void Main (void) {current_task = System_init ();
	for (;;)
	{current_task = task_process (Current_task); }
}

The code defines a time base of 1ms interrupt timer, with Timer_count as the free count value, the upper limit of the count is Timer_period, after reaching the upper limit will timer_count clear, and statistics of the number of overflow timer_count_overflow, The number of overflows is free to count until the overflow is cleared and into the next cycle. The main function executes at the time of entering the task_process function, while passing into the current need to execute the task number, the first time to execute the task number is TASK1, starting from Task1 () execution, the following task1 () analysis.

The task function is executed by logging the value of the current Timer_count, timer_count_overflow the overflow number, and then executing the do_some_thing () in the For Loop, while recording and detecting the current timer_count value. Based on the current Timer_count value and the Timer_count comparison when the task is entered, if the time arrives at the time we need the task to execute, jump out of the for loop while returning the next task number to execute, and in the main function, proceed to the next task based on the returned task number.

The implementation of the code is the basic software architecture, and there is no specific program to execute the function code, when the transplant as long as the use of a single-chip microcomputer timer, and constantly produce a 1ms timer interrupt.

Related Article

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.