电子说
stm32多线程 单片机如何实现多线程
STM 32系列是专门应用在高性能、低成本、低功耗的嵌入式应用设计的ARM Corte-M0,M0+,M3,M4和M7内核,是主流的嵌入式单片机之一。
多线程通常是指从计算机软件或硬件上实现多个线程并发执行的技术,多线程技术有利于提升计算机整体处理性能。
基于STM32单片机的多线程源代码实例1:
#include “Hal_Led/Hal_Led.h”
#include “Hal_delay/delay.h”
#include “Hal_Key/Hal_Key.h”
#include “ringbuffer.h”
#define APP_LED2_BLINK_EVENT 0x0001
#define HAL_LED1_BLINK_EVENT 0x0001
#define TASK_NO_TASK_RUNNING 0xFF
unsigned short Hal_ProcessEvent( unsigned char task_id, unsigned short events );
unsigned short App_ProcessEvent( unsigned char task_id, unsigned short events );
typedef unsigned short uint16;
typedef unsigned char uint8;
#define TASK_CNT 2 //定义线程的个数
//定义函数指针
typedef unsigned short (*pTaskEventHandlerFn)( unsigned char task_id, unsigned short events );
//线程函数表
const pTaskEventHandlerFn tasksArr[] =
{
Hal_ProcessEvent,
App_ProcessEvent
};
const unsigned char tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[ 0] );
//uint16 *tasksEvents;
uint16 tasksEvents[TASK_CNT] = { 0}; //每个线程有16位位域空间用于设置事件
static uint8 activeTaskID = 0xFF; //当前任务ID,指示作用
#define SUCCESS 0x00
#define FAILURE 0x01
#define INVALID_TASK 0x02
uint8 osal_set_event( uint8 task_id, uint16 event_flag )
{
if ( task_id 《 tasksCnt )
{
tasksEvents[task_id] |= event_flag; // Stuff the event bit(s)
return ( SUCCESS );
}
else
{
return ( INVALID_TASK );
}
}
/**
* @brief 程序入口
* @param none
* @return none
*/
int main( void)
{
unsigned short taskID = 0;
uint8 idx = 0;
SystemInit(); //系统时钟初始化
delayInit( 72); //滴答定时器初始化
Led_Init(); //LED初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
osal_set_event( 0, HAL_LED1_BLINK_EVENT);
osal_set_event( 1, APP_LED2_BLINK_EVENT);
while( 1)
{
do
{
if(tasksEvents[idx]) //轮训获知哪个线程有事件需要进行处理
{
break;
}
}
while (++idx 《 tasksCnt);
if (idx 《 tasksCnt)
{
uint16 events;
events = tasksEvents[idx];
tasksEvents[idx] = 0; // 清除事件数组中的事件
activeTaskID = idx;
events = (tasksArr[idx])( idx, events ); //调用线程函数
activeTaskID = TASK_NO_TASK_RUNNING;
tasksEvents[idx] |= events; // 添加未处理的事件到本线程的事件组中
}
delayMs( 1000);
}
}
/**
* @brief 应用层处理
* @param none
* @r
*/
unsigned short Hal_ProcessEvent( unsigned char task_id, unsigned short events )
{
if ( events & HAL_LED1_BLINK_EVENT )
{
Led_Reverse( 1);
return events ^ HAL_LED1_BLINK_EVENT; //清除事件
}
}
/**
* @brief 硬件控制线程
* @param none
* @r
*/
unsigned short App_ProcessEvent( unsigned char task_id, unsigned short events )
{
if ( events & APP_LED2_BLINK_EVENT )
{
Led_Reverse( 2);
return events ^ APP_LED2_BLINK_EVENT; //清除事件
}
基于STM32单片机的多线程源代码实例2:
public class SimpleThread {
//私有的静态成员内部类,实现了Runnable接口
private static class ThreadMessage implements Runnable{
public void run(){
String[] info = {“消息1”,“消息2”, “消息3”, “消息4”};
try {
for(int i=0;i《info.length;i++){
Thread.sleep(4000);
displayThreadMessage(info[i]);
}
} catch (InterruptedException e) {
displayThreadMessage(“不能正常工作”);
}
}
}
//显示消息,消息是当前线程的名字
static void displayThreadMessage(String message){
String threadName = Thread.currentThread().getName();
//格式化输出线程消息
System.out.format(“%s: %s%n”, threadName, message);
}
public static void main(String[] args) throws InterruptedException {
// 中断ThreadMessage线程之前延迟的毫秒数(默认是一分钟)
long delay =1000 * 60;
//如果有命令行参数,那么在命令行参数中给出推迟的时间
if(args.length》0){
try {
delay =Long.parseLong(args[0])*1000;
} catch (NumberFormatException e) {
System.err.println(“参数必须是整数”);
System.exit(1);
}
}
displayThreadMessage(“启动线程ThreadMessage.。。”);
long startTime = System.currentTimeMillis();
Thread t = new Thread(new ThreadMessage());
t.start();
displayThreadMessage(“等待线程ThreadMessage结速。。。”);
//循环直到ThreadMessage线程退出
while(t.isAlive()){
displayThreadMessage(“继续等待线程ThreadMessage.。。”);
//最多等待3秒钟ThreadMessage线程结速
t.join(3000);
//如果线程t运行的时间超过delay指定时间
if(((System.currentTimeMillis() - startTime) 》 delay) && t.isAlive()){
displayThreadMessage(“线程ThreadMessage运行时间太久了,不想等待!”);
t.interrupt();
t.join();
}
}
displayThreadMessage(“结束线程ThreadMessage!!!”);
}
}
基于STM32单片机的多线程源代码实例3:
#include “Hal_Led/Hal_Led.h”
#include “Hal_delay/delay.h”
#include “Hal_Key/Hal_Key.h”
#include “ringbuffer.h”
#define APP_LED2_BLINK_EVENT 0x0001
#define HAL_LED1_BLINK_EVENT 0x0001
#define TASK_NO_TASK_RUNNING 0xFF
unsigned short Hal_ProcessEvent( unsigned char task_id, unsigned short events );
unsigned short App_ProcessEvent( unsigned char task_id, unsigned short events );
typedef unsigned short uint16;
typedef unsigned char uint8;
#define TASK_CNT 2 // 定义线程的个数
// 定义函数指针
typedef unsigned short (*pTaskEventHandlerFn)( unsigned char task_id, unsigned short events );
// 线程函数表
const pTaskEventHandlerFn tasksArr[] =
{
Hal_ProcessEvent,
App_ProcessEvent
};
const unsigned char tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
//uint16 *tasksEvents;
uint16 tasksEvents[TASK_CNT] = {0}; // 每个线程有 16 位位域空间用于设置事件
staTIc uint8 acTIveTaskID = 0xFF; // 当前任务 ID,指示作用
#define SUCCESS 0x00
#define FAILURE 0x01
#define INVALID_TASK 0x02
uint8 osal_set_event( uint8 task_id, uint16 event_flag )
{
if ( task_id 《 tasksCnt )
{
tasksEvents[task_id] |= event_flag; // Stuff the event bit(s)
return ( SUCCESS );
}
else
{
return ( INVALID_TASK );
}
}
/**
* @brief 程序入口
* @param none
* @return none
*/
int main(void)
{
unsigned short taskID = 0;
uint8 idx = 0;
SystemInit(); // 系统时钟初始化
delayInit(72); // 滴答定时器初始化
Led_Init(); //LED 初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
osal_set_event(0, HAL_LED1_BLINK_EVENT);
osal_set_event(1, APP_LED2_BLINK_EVENT);
while(1)
{
do
{
if(tasksEvents[idx]) // 轮训获知哪个线程有事件需要进行处理
{
break;
}
}
while (++idx 《 tasksCnt);
if (idx 《 tasksCnt)
{
uint16 events;
events = tasksEvents[idx];
tasksEvents[idx] = 0; // 清除事件数组中的事件
acTIveTaskID = idx;
events = (tasksArr[idx])( idx, events ); // 调用线程函数
activeTaskID = TASK_NO_TASK_RUNNING;
tasksEvents[idx] |= events; // 添加未处理的事件到本线程的事件组中
}
delayMs(1000);
}
}
/**
* @brief 应用层处理
* @param none
* @r
*/
unsigned short Hal_ProcessEvent( unsigned char task_id, unsigned short events )
{
if ( events & HAL_LED1_BLINK_EVENT )
{
Led_Reverse(1);
return events ^ HAL_LED1_BLINK_EVENT; // 清除事件
}
}
/**
* @brief 硬件控制线程
* @param none
* @r
*/
unsigned short App_ProcessEvent( unsigned char task_id, unsigned short events )
{
if ( events & APP_LED2_BLINK_EVENT )
{
Led_Reverse(2);
return events ^ APP_LED2_BLINK_EVENT; // 清除事件
以上是关于STM32单片机的源代码,希望对用户有所帮助。
本文整合自百度百科、与非网、CSDN-辣条boy
责编AJX
全部0条评论
快来发表一下你的评论吧 !