15_事件标志组

事件标志组

事件标志组简介

image-20240328164814667

image-20240328164824211

image-20240328164834754

事件标志组与队列、信号量的区别?

image-20240328164856020

事件标志组相关API函数介绍

image-20240328164910723

动态方式创建事件标志组API函数

image-20240328164928879

清除事件标志位API函数

image-20240328164941593

设置事件标志位API函数

image-20240328164955109

等待事件标志位API函数

image-20240328165014466

同步函数

image-20240328165036159

事件标志组实验

image-20240328165052875

代码

freertos_demo.c

/**
 ****************************************************************************************************
 * @file        freertos.c
 * @author      正点原子团队(ALIENTEK)
 * @version     V1.4
 * @date        2022-01-04
 * @brief       FreeRTOS 移植实验
 * @license     Copyright (c) 2020-2032, 广州市星翼电子科技有限公司
 ****************************************************************************************************
 * @attention
 *
 * 实验平台:正点原子 探索者F407开发板
 * 在线视频:www.yuanzige.com
 * 技术论坛:www.openedv.com
 * 公司网址:www.alientek.com
 * 购买地址:openedv.taobao.com
 *
 ****************************************************************************************************
 */

#include "freertos_demo.h"
#include "./SYSTEM/usart/usart.h"
#include "./BSP/LED/led.h"
#include "./BSP/LCD/lcd.h"
#include "./BSP/KEY/key.h"
#include "./SYSTEM/delay/delay.h"
#include "./MALLOC/malloc.h"
/*FreeRTOS*********************************************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "event_groups.h"

/******************************************************************************************************/
/*FreeRTOS配置*/

/* START_TASK 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define START_TASK_STACK_SIZE 	128
#define START_TASK_PRIO			1
TaskHandle_t start_task_handler;

#define TASK1_STACK_SIZE 		128
#define TASK1_PRIO				2
TaskHandle_t task1_handler;

#define TASK2_STACK_SIZE 		128
#define TASK2_PRIO				3
TaskHandle_t task2_handler;

EventGroupHandle_t event_group_handle;

#define EVENT_BIT0 (1 << 0)
#define EVENT_BIT1 (1 << 1)

void start_task( void * pvParameters );

void task1( void * pvParameters )
{
	uint8_t key;
	while(1)
	{
		key = key_scan(0);
		if(key == KEY0_PRES)
		{
			xEventGroupSetBits(event_group_handle, EVENT_BIT0); //设置事件标志组bit0为1
		}else if(key == KEY1_PRES)
		{
			xEventGroupSetBits(event_group_handle, EVENT_BIT1); //设置事件标志组bit1为1
		}
		vTaskDelay(10);
	}
}

void task2( void * pvParameters )
{
	EventBits_t event_bit;
	while(1)
	{
		event_bit = xEventGroupWaitBits(event_group_handle, 		/* 事件标志组句柄 */
							EVENT_BIT0 | EVENT_BIT1, 	/* 等待事件标志组的bit0和bit1位 */
							pdTRUE,						/* 成功等待到事件标志位后,清除事件标志组中的bit0和bit1位 */
							pdTRUE,						/* 等待事件标志组的bit0和bit1位都置1,就成立 */
							portMAX_DELAY);             /* 死等 */
		printf("等待到的事件标志位值为:%#x\r\n",event_bit);
	}
}

/******************************************************************************************************/

/**
 * @brief       FreeRTOS例程入口函数
 * @param       无
 * @retval      无
 */
void freertos_demo(void)
{
    xTaskCreate(start_task, "start_task", START_TASK_STACK_SIZE, NULL, START_TASK_PRIO, &start_task_handler);
	vTaskStartScheduler();
}

void start_task( void * pvParameters )
{
	taskENTER_CRITICAL(); //进入临界区
	event_group_handle = xEventGroupCreate(); //创建事件标志组
	if(event_group_handle != NULL)
	{
		printf("事件标志组创建成功!!!\r\n");
	}
	xTaskCreate(task1, "task1", TASK1_STACK_SIZE, NULL, TASK1_PRIO, &task1_handler);
	xTaskCreate(task2, "task2", TASK2_STACK_SIZE, NULL, TASK2_PRIO, &task2_handler);
    vTaskDelete(NULL);
	taskEXIT_CRITICAL(); //退出临界区
}

posted @ 2024-03-28 20:26  爱吃冰激凌的黄某某  阅读(6)  评论(0编辑  收藏  举报