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:
//私有的靜態成員內部類,實現了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
-
單片機
+關注
關注
6039文章
44585瀏覽量
636483 -
STM32
+關注
關注
2270文章
10910瀏覽量
356599 -
多線程
+關注
關注
0文章
278瀏覽量
20018
發布評論請先 登錄
相關推薦
評論