测试demo
创建两个任务 任务一 按键按下 释放二值信号量 任务二获取 并打印执行次数
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#include "key.h"
#include "time.h"
#include "queue.h"
#include "semphr.h"
#define START_TASK_PRIO 1 //任务优先级
#define START_STK_SIZE 120 //任务堆栈
TaskHandle_t Start_Handler;
void start_task(void *pvParameters);
#define Task1_TASK_PRIO 2 //任务优先级
#define Task1_STK_SIZE 120 //任务堆栈
TaskHandle_t Task1_Handler;
void task1_task(void *pvParameters);
#define Task2_TASK_PRIO 3 //任务优先级
#define Task2_STK_SIZE 120 //任务堆栈
TaskHandle_t Task2_Handler;
void task2_task(void *pvParameters);
#define KEYSG_Q_NUM 1 //队列数为1
#define MESSAGE_Q_NUM 4
SemaphoreHandle_t erzhixinhaoliang;//二值信号量句柄
//*******************下面注释掉的代码是通过 位带 操作实现IO口控制**************************************
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
delay_init(168); //初始化延时函数
KEY_Init();
uart_init(115200);
LED_Init(); //初始化LED端口
TIM3_Int_Init(5000-1,8400-1);
while(1)
{
xTaskCreate((TaskFunction_t) start_task,
(char *) "start_task",
(uint16_t) START_STK_SIZE,
(void *) NULL,
(UBaseType_t) START_TASK_PRIO,
(TaskHandle_t *) &Start_Handler ); /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
vTaskStartScheduler();// 开启任务调度器 注意一定要开启
}
}
//创建开始任务
void start_task(void *pvParameters)
{
taskENTER_CRITICAL();//进入临界区
erzhixinhaoliang=xSemaphoreCreateBinary();//创建二值信号量
if(erzhixinhaoliang==NULL)
{
printf("Bin sem create failed\r\n");
}
//创建task1
xTaskCreate((TaskFunction_t) task1_task,
(char *) "task1_task",
(uint16_t) Task1_STK_SIZE,
(void *) NULL,
(UBaseType_t) Task1_TASK_PRIO,
(TaskHandle_t *) &Task1_Handler ); /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
//创建task2 按键处理任务
xTaskCreate((TaskFunction_t) task2_task,
(char *) "task2_task",
(uint16_t) Task2_STK_SIZE,
(void *) NULL,
(UBaseType_t) Task2_TASK_PRIO,
(TaskHandle_t *) &Task2_Handler ); /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
vTaskDelete(Start_Handler); //删除开始任务
taskEXIT_CRITICAL();//退出临界区
}
void task1_task(void *pvParameters)
{
u8 key;
while(1)
{
key=KEY_Scan(0);
if(key==4)
{
if(erzhixinhaoliang!=NULL);
{
xSemaphoreGive(erzhixinhaoliang);
}
}
LED0=!LED0;
vTaskDelay(10);//10个时钟节拍 10ms
}
}
void task2_task(void *pvParameters)
{
u8 key;
u8 count=0;
BaseType_t err;
while(1)
{
count++;
if(erzhixinhaoliang!=NULL)
{
xSemaphoreTake(erzhixinhaoliang,portMAX_DELAY);//获取信号量 阻塞时间为死等
printf("count =%d\r\n",count);
}
else
{
vTaskDelay(10);
}
}
}