2504 views|8 replies

6818

Posts

11

Resources
The OP
 

[National Technology N32G430] 4. Make a simple and easy-to-use multi-task scheduling system [Copy link]

 
 

【National Technology N32G430】3. Timer - Domestic chip exchange - Electronic Engineering World - Forum (eeworld.com.cn)

[Purpose] When I first learned microcontrollers, delays were all implemented using while blocks, and there was no task scheduling system. Later, because I needed to coordinate several peripherals, I learned state machines, and then I came into contact with operating systems such as freeRTos and RT-hread. This time, when I was learning 51 programming from Diwenping, I learned a very simple and practical multi-task scheduling system. Today, I will transfer it to N32F430 to deepen my impression of task creation, scheduling, and deletion.

1. Following the creation of the timer in the previous article, create a new task.c file as follows:

#include "task.h"
#include <string.h>
#include "main.h"

typedef struct{
  u32 countdown;
  u32 interval;
  TASK_FUNC func_cb;
  u8 sta; 
}TASK;

#define TASK_STA_IDLE  0x00 //空闲
#define TASK_STA_WAIT  0x01 //等待

#define TASK_TABLE_LEN 8
static TASK task_table[TASK_TABLE_LEN];
static double cur_tick,last_tick,elapsed_tick;
static u16 task_id;

void task_init()
{
  cur_tick = 0;
  last_tick = 0;
  memset((void*)task_table,0,sizeof(task_table));
}

void task_tick(void)
{
  cur_tick +=1;
}

//返回任务id,从1开始的,0代表失败
u16 task_create(u32 countdown, u32 interval, TASK_FUNC func_cb)
{
  u16 i;
  for(i=0; i<TASK_TABLE_LEN; i++)
  {
    if(task_table[i].sta == TASK_STA_IDLE)
    {
      task_table[i].countdown = countdown;
      task_table[i].interval = interval;
      task_table[i].func_cb = func_cb;
      task_table[i].sta = TASK_STA_WAIT;
      return i+1;
    }
  }
  return 0; //创建失败
}

void task_delete(u16 id)
{
  if(id>=1 && id<= TASK_TABLE_LEN)
  {
    id--;
    task_table[id].countdown = 0;
    task_table[id].interval = 0;
    task_table[id].func_cb = 0;
    task_table[id].sta = TASK_STA_IDLE;
  }
  
}

void task_handler(void)
{
  elapsed_tick = cur_tick - last_tick;//获得时间差
  last_tick = cur_tick;
  for(task_id = 0; task_id < TASK_TABLE_LEN; task_id ++)
  {
    if(task_table[task_id].countdown == 0)
      continue;
    if(task_table[task_id].countdown>elapsed_tick)
    {
      task_table[task_id].countdown -= elapsed_tick;
    }
    else{
      task_table[task_id].countdown = 0;
    }
    if((task_table[task_id].countdown == 0) && (task_table[task_id].sta == TASK_STA_WAIT))
    {
      if(task_table[task_id].func_cb)
        task_table[task_id].func_cb();
      
      if(task_table[task_id].interval)
        task_table[task_id].countdown = task_table[task_id].interval;
      else
        task_table[task_id].sta = TASK_STA_IDLE;
    }
  }
}

2. Header file: task.h:

#ifndef __TASK_H__
#define __TASK_H__

#include "main.h"

//变量申明
typedef void (*TASK_FUNC)(void);

void task_init(void);
void task_tick(void);
u16 task_create(u32 countdown, u32 interval, TASK_FUNC func_cb);
void task_delete(u16 id);
void task_handler(void);
#endif

3. Import the three files of GPIOdemo:

#include "bsp_led.h"  //led驱动
#include "bsp_delay.h"  //延时函数
#include "task.h"       //任务调度
#include "log.h"        //打印函数

4. Main function:

#include "main.h"
#include "task.h"

u16 task1_id,task2_id;
u16 num1,num2;


//任务1
void task1()
{
	num1++;
	printf("hellow task1 num:%d\r\n",num1);
	if(num1>20)
		task_delete(task1_id);
}


//任务2
void task2()
{
	num2 += 2;
	printf("hellow task2 num2:%d\r\n",num2);
	if(num2>30)
		task_delete(task2_id);
}
/**
*\*\brief  Main program
**/
int main(void)
{
  log_init();
  time6_init();
  task_init();
  num1 = num2 = 0;
	task1_id = task_create(1000,1000,task1); //创建任务1
	task2_id = task_create(1000,1000,task2);  //创建任务2
  while(1)
  {
    task_handler();
  }
}

After compiling and downloading, run the program as shown in the video below. In short, this is a very good task system that is short and concise. It is a good example for learning task creation. I would like to thank Mr. Xiong Jiayu for his detailed explanation.

任务系统

This post is from Domestic Chip Exchange

Latest reply

The overall structure is quite simple, you can cut it down and copy it.   Details Published on 2022-9-7 16:08
 
 

5998

Posts

6

Resources
2
 

In fact, I feel that whether it is a system or a state machine, for a single-core MCU, the essence feels the same, but the implementation method is different. It is executed through the state. In short, don't add a delay wait in while

This post is from Domestic Chip Exchange

Comments

The principles are the same, I think. This is the most useful one I've come across, so I'd like to recommend it here. It is also suitable for different microcontrollers, so it's very convenient to port.  Details Published on 2022-9-5 16:06
 
 
 

6818

Posts

11

Resources
3
 
Qintianqintian0303 posted on 2022-9-5 15:58 In fact, it feels that whether it is a system or a state machine, for a single-core MCU, the essence feels the same, but the implementation method is different, through the state to execute, ...

The principles are the same, I think. This is the most useful one I've come across, so I'd like to recommend it here. It is also suitable for different microcontrollers, so it's very convenient to port.

This post is from Domestic Chip Exchange

Comments

It is said that object-oriented programming is the most convenient, and the difference with process-oriented programming is the most obvious. It is indeed much more convenient to have a universal one. The big guy really has a lot of knowledge.  Details Published on 2022-9-5 16:21
 
 
 

5998

Posts

6

Resources
4
 
lugl4313820 posted on 2022-9-5 16:06 The principles are the same. I feel that this is a relatively easy-to-use one. I recommend it here. It is suitable for different microcontrollers. It can be said that transplantation...

It is said that object-oriented programming is the most convenient, and the difference with process-oriented programming is the most obvious. It is indeed much more convenient to have a universal one. The big guy really has a lot of knowledge.

This post is from Domestic Chip Exchange

Comments

Thank you for your praise...  Details Published on 2022-9-5 17:41
 
 
 

6818

Posts

11

Resources
5
 
Qintianqintian0303 posted on 2022-9-5 16:21 It is said that object-oriented programming is the most convenient, and the difference between process-oriented programming is the most obvious. It is indeed much more convenient to have a universal one. The boss is really...

Thank you for your praise...

This post is from Domestic Chip Exchange
 
 
 

2865

Posts

4

Resources
6
 

I don't see how task switching is implemented. If it is an operating system, it would be easy to explain.

This post is from Domestic Chip Exchange

Comments

This is achieved by polling. This is a simple task switching, without underlying operations, and cannot implement complex operations.  Details Published on 2022-9-6 11:55
 
 
 

6818

Posts

11

Resources
7
 
bigbat posted on 2022-9-6 09:36 I didn't see how task switching is implemented. If it is an operating system, it would be easy to explain

This is achieved by polling. This is a simple task switching, without underlying operations, and cannot implement complex operations.

This post is from Domestic Chip Exchange
 
 
 

6742

Posts

2

Resources
8
 

This is a bit like a miniature system, which is equivalent to taking a small part of the operating system.
However, N32G430 should support RTThread~

This post is from Domestic Chip Exchange
 
 
 

7422

Posts

2

Resources
9
 

The overall structure is quite simple, you can cut it down and copy it.

This post is from Domestic Chip Exchange
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list