//-----------------------------------創建計數型信號量-------------------------------
SemaphoreHandle_t CountSemaphore;//計數型信號量
CountSemaphore=xSemaphoreCreateCounting(255,0);//創建計數型信號量
BaseType_t err;
u8 semavalue;
if(CountSemaphore!=NULL) //計數型信號量創建成功
{
err=xSemaphoreGive(CountSemaphore);//釋放計數型信號量
if(err==pdFALSE)
{
printf("信號量釋放失敗!!!\r\n");
}
xSemaphoreTake(CountSemaphore,portMAX_DELAY); //等待數值信號量釋放
semavalue=uxSemaphoreGetCount(CountSemaphore); //獲取計數型信號量值
}
//-----------------------------------二值信號量-------------------------------
SemaphoreHandle_t BinarySemaphore; //二值信號量句柄
BinarySemaphore=xSemaphoreCreateBinary();//創建二值信號量
if(BinarySemaphore!=NULL)xSemaphoreGive(BinarySemaphore);//二值信號量創建成功以後要先釋放一下
BaseType_t err=pdFALSE;
if(BinarySemaphore!=NULL)
{
err=xSemaphoreTake(BinarySemaphore,portMAX_DELAY); //獲取信號量
if(err==pdTRUE) //獲取信號量成功
{}
}
xSemaphoreGive(BinarySemaphore); //釋放信號量
extern SemaphoreHandle_t BinarySemaphore; //二值信號量句柄
BaseType_t xHigherPriorityTaskWoken;
if(BinarySemaphore!=NULL)
xSemaphoreGiveFromISR(BinarySemaphore,&xHigherPriorityTaskWoken); //釋放二值信號量
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);//如果需要的話進行一次任務切換
//-----------------------------------互斥信號量------------------------------
SemaphoreHandle_t MutexSemaphore; //聲明互斥信號量句柄
MutexSemaphore=xSemaphoreCreateMutex();//創建互斥信號量
xSemaphoreTake(MutexSemaphore,portMAX_DELAY); //獲取互斥信號量
xSemaphoreGive(MutexSemaphore); //釋放信號量
//-----------------------------------軟件定時器------------------------------
TimerHandle_t AutoReloadTimer_Handle; //週期定時器句柄
TimerHandle_t OneShotTimer_Handle; //單次定時器句柄
void AutoReloadCallback(TimerHandle_t xTimer); //週期定時器回調函數
void OneShotCallback(TimerHandle_t xTimer); //單次定時器回調函數
AutoReloadTimer_Handle=xTimerCreate((const char* )"AutoReloadTimer",
(TickType_t )1000,
(UBaseType_t )pdTRUE,
(void* )1,
(TimerCallbackFunction_t)AutoReloadCallback); //週期定時器,週期1s(1000個時鐘節拍),週期模式
//創建單次定時器
OneShotTimer_Handle=xTimerCreate((const char* )"OneShotTimer",
(TickType_t )2000,
(UBaseType_t )pdFALSE,
(void* )2,
(TimerCallbackFunction_t)OneShotCallback); //單次定時器,週期2s(2000個時鐘節拍),單次模式
xTimerStop(AutoReloadTimer_Handle,0); //關閉週期定時器
xTimerStop(OneShotTimer_Handle,0); //關閉單次定時器
xTimerStart(OneShotTimer_Handle,0); //開啓單次定時器
xTimerStart(AutoReloadTimer_Handle,0); //開啓週期定時器
//週期定時器的回調函數
void AutoReloadCallback(TimerHandle_t xTimer)
{
static u8 tmr1_num=0;
tmr1_num++; //週期定時器執行次數加1
}
//單次定時器的回調函數
void OneShotCallback(TimerHandle_t xTimer)
{
static u8 tmr2_num = 0;
tmr2_num++; //週期定時器執行次數加1
LED1=!LED1;
printf("定時器2運行結束\r\n");
}
//-----------------------------------事件標誌組(位)------------------------------
//個人理解爲標誌位的集合
EventGroupHandle_t EventGroupHandler; //事件標誌組句柄
#define EVENTBIT_0 (1<<0) //事件位
#define EVENTBIT_1 (1<<1)
#define EVENTBIT_2 (1<<2)
#define EVENTBIT_ALL (EVENTBIT_0|EVENTBIT_1|EVENTBIT_2)
EventGroupHandler=xEventGroupCreate(); //創建事件標誌組
if(EventGroupHandler!=NULL)
{
xEventGroupSetBits(EventGroupHandler,EVENTBIT_1);
xEventGroupSetBits(EventGroupHandler,EVENTBIT_2);
}
EventBits_t EventValue;
if(EventGroupHandler!=NULL)
{
//等待事件組中的相應事件位
EventValue=xEventGroupWaitBits((EventGroupHandle_t )EventGroupHandler,
(EventBits_t )EVENTBIT_ALL,//指定要置位的
(BaseType_t )pdTRUE, //清除
(BaseType_t )pdTRUE,//全部置位
(TickType_t )portMAX_DELAY);//死等
}
EventBits_t NewValue;
NewValue=xEventGroupGetBits(EventGroupHandler); //獲取當前標誌位被置位的與值
//-----------------------------------隊列------------------------------
#include "queue.h"
#define MESSAGE_Q_NUM 4 //發送數據的消息隊列的數量
QueueHandle_t Message_Queue; //信息隊列句柄
Message_Queue=xQueueCreate(MESSAGE_Q_NUM,200); //創建消息Message_Queue,隊列項長度是消息長度
BaseType_t xHigherPriorityTaskWoken;
BaseType_t err=xQueueSend(Message_Queue,USART_RX_BUF,10);
xQueueReceive(Message_Queue,&key,portMAX_DELAY)
//就向隊列發送接收到的數據
if(Message_Queue!=NULL)
{
xQueueSendFromISR(Message_Queue,USART_RX_BUF,&xHigherPriorityTaskWoken);//向隊列中發送數據可以
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);//如果需要的話進行一次任務切換
}
//-----------------------------------內存------------------------------
u8 *buffer;
buffer=pvPortMalloc(30);//申請內存,30個字節
if(buffer!=NULL)vPortFree(buffer); //釋放內存
buffer=NULL;
u32 freemem;
freemem=xPortGetFreeHeapSize(); //獲取剩餘內存大小,單位字節
//每個任務的堆棧有限,大量使用內存,動態申請最靠譜