通过创建软件定时器实现500ms中断,执行x次后停止并删除定时器。 宏:
#define configTICK_RATE_HZ (( TickType_t )1000)
#define configSUPPORT_DYNAMIC_ALLOCATION 1
#define configUSE_TIMERS 1
main.c
#include "AppTask.h"
static void BSP_Init(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
LED_GPIO_Config();
USART_Config();
}
int main(void)
{
BaseType_t xReturn = pdPASS;
BSP_Init();
xReturn = xTaskCreate((TaskFunction_t) (AppCreate_Task),
(const char*) "AppCreate_Task",
(uint16_t) 512,
(void*) NULL,
(UBaseType_t) 1,
(TaskHandle_t) (&AppCreateTask_Handle));
if(xReturn == pdPASS)
{
printf("AppCreate_Task Create Succeed!\r\n");
vTaskStartScheduler();
}
else{printf("AppCreate_Task Create Fail!\r\n");}
while(1);
}
AppTask.h
#ifndef _AppTask_H_
#define _AppTask_H_
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "event_groups.h"
#include "timers.h"
#include "bsp_led.h"
#include "bsp_usart.h"
extern TaskHandle_t AppCreateTask_Handle;
extern void AppCreate_Task(void* parameter);
#endif
AppTask.c**
#include "AppTask.h"
TaskHandle_t AppCreateTask_Handle = NULL;
static TimerHandle_t Timer_Handle = NULL;
static void Timer_Callback(void* parameter);
void AppCreate_Task(void* parameter)
{
taskENTER_CRITICAL();
Timer_Handle = xTimerCreate((const char*)"Timer",\
(TickType_t) 500,\
(UBaseType_t) pdTRUE,\
(void*) 1,\
(TimerCallbackFunction_t) Timer_Callback);
xTimerStart(Timer_Handle,0);
vTaskDelete(AppCreateTask_Handle);
taskEXIT_CRITICAL();
}
static void Timer_Callback(void* parameter)
{
static int i = 0;
TickType_t tick_num2;
LED1_TOGGLE;
tick_num2 = xTaskGetTickCount();
printf("Timer Run...%d\r\n",tick_num2);
i++;
if(i > 5)
{
xTimerStop(Timer_Handle,0);
xTimerDelete(Timer_Handle,0);
printf("删除定时器");
}
}
演示现象:
|