欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 7195|回復: 0
收起左側

基于STM32F103的MDK5_RTOS創建線程

[復制鏈接]
ID:89763 發表于 2015-9-10 00:38 | 顯示全部樓層 |閱讀模式
前面已經正式介紹了如何建立一個帶有RTOS的工程,那么現在開始探究一下,如何在使用了RTOS的程序中建立線程。
經過查閱,發現想要創建一個線程的話,那么只要調用創建線程的函數就可以了。一下就是其 原型。
/// Create a thread and add it to Active Threads and set it tostate READY.
/// \param[in]    thread_def    thread definitionreferenced with \ref osThread.
/// \param[in]     argument     pointerthat is passed to the thread function as start argument.
/// \return thread ID for reference by other functions or NULLin case of error.
osThreadId osThreadCreate (const osThreadDef_t *thread_def,void *argument);


從上很容易的看出,想要創建線程,首先需要準備一個線程的結構體,和進入線程的時候的參數。
這里我們首先準備兩個 結構體,thread1,和thread2.

osThreadDef_t thread1 = {
.pthread = thread1_function,
.tpriority = osPriorityNormal,
.instances = 1,
.stacksize = 0,
};

osThreadDef_t thread2 = {
.pthread = thread2_function,
.tpriority =osPriorityNormal,
.instances = 1,
.stacksize = 0,
};

其中 pthread 所指的就是 線程的入口地址

當準備好 兩個 結構體之后,我們就可以 利用 osThreadCreate  來創建相應的線程了。

我們在線程1的入口函數中 寫led1的閃光燈,線程2的入口函數中寫led2的閃光燈。來觀察相應的實驗現象。

其完整的代碼如下。

#include "stm32f10x.h"
#include  
#include  

void gpio_init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef gpio_initStruct;
gpio_initStruct.GPIO_Mode =GPIO_Mode_Out_PP;
gpio_initStruct.GPIO_Speed =GPIO_Speed_50MHz;
gpio_initStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOA,&gpio_initStruct);
gpio_initStruct.GPIO_Pin =GPIO_Pin_3;
GPIO_Init(GPIOA,&gpio_initStruct);
}

void thread2_function(void const *args)
{
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
osDelay(500);
GPIO_SetBits(GPIOA, GPIO_Pin_3);
osDelay(500);
}
}

osThreadDef_t thread2 = {
.pthread =thread2_function,
.tpriority = osPriorityNormal,
.instances = 1,
.stacksize = 0
};

void thread1_function(void const *args)
{
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
osDelay(500);
GPIO_SetBits(GPIOA, GPIO_Pin_2);
osDelay(500);
}
}

osThreadDef_t thread1 ={
.pthread =thread1_function,
.tpriority = osPriorityNormal,
.instances = 1,
.stacksize = 0,
};

int main()
{
gpio_init();
osThreadCreate((const osThreadDef_t*)&thread1, NULL);

while(1);
}

回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表