其他分享
首页 > 其他分享> > FreeRTOS-事件标志组

FreeRTOS-事件标志组

作者:互联网

FreeRTOS-事件标志组

事件标志组表示方法

typedef TickType_t EventBits_t;
#if( configUSE_16_BIT_TICKS == 1 )
	typedef uint16_t TickType_t;
	#define portMAX_DELAY ( TickType_t ) 0xffff
#else
	typedef uint32_t TickType_t;
	#define portMAX_DELAY ( TickType_t ) 0xffffffffUL
	#define portTICK_TYPE_IS_ATOMIC 1
#endif

事件标志组相关API

xEventGroupCreate()

xEventGroupSetBits()

xEventGroupClearBits()

xEventGroupWaitBits()

用法示例

实验目的:创建3个事件,分别等待bit0置位,等待bit1置位,等待bit0和bit1均置位,通过按键将bit0和bit1置位
u8 keyFlag=0;
void key_task(void* pvParameters)
{
	EventBits_t Ebit;
	while(1)
	{
		
		if (Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) == KEY_ON)
		{
			if ( (keyFlag&0x01) == 0x01)
				keyFlag &= ~(0x01<<0);
			else
			  keyFlag |= (0x01<<0);
			
			if ( (keyFlag&0x01) == 0x01)
			{
				Ebit = xEventGroupSetBits(Event_Handler,(0x01<<0));
				
				printf("current Ebit = %#x\r\n",Ebit);
			}
			else
			{
				Ebit = xEventGroupClearBits(Event_Handler,(0x01<<0));
				printf("current Ebit = %#x\r\n",Ebit);
			}
		
		}
		
		if (Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN) == KEY_ON)
		{
			if ( (keyFlag&0x02) == 0x02)
				keyFlag &= ~(0x01<<1);
			else
				keyFlag |= (0x01<<1);
			
			if ( (keyFlag&0x02) == 0x02)
			{
				Ebit = xEventGroupSetBits(Event_Handler,(0x01<<1));
				printf("current Ebit = %#x\r\n",Ebit);
			}
			else
			{
				Ebit = xEventGroupClearBits(Event_Handler,(0x01<<1));
				printf("current Ebit = %#x\r\n",Ebit);
			}
			
		}
		vTaskDelay(10);
	}
}

void event1_task(void* pvParameters)
{
	while(1)
	{
		if ( (keyFlag&0x01) == 0x01)
			LED0 = 0;
		else
			LED0 = 1;
		xEventGroupWaitBits(Event_Handler,(0x01<<0),pdFALSE,pdTRUE,portMAX_DELAY);
		printf("event1 task running!!\r\n");
		vTaskDelay(1000);
	}
}


void event2_task(void* pvParameters)
{
	while(1)
	{
		if ( (keyFlag&0x02) == 0x02)
			LED1 = 0;
		else
			LED1 = 1;
		xEventGroupWaitBits(Event_Handler,(0x01<<1),pdFALSE,pdTRUE,portMAX_DELAY);
		printf("event2 task running!!\r\n");
		vTaskDelay(1000);
	}
}


void event3_task(void* pvParameters)
{
	while(1)
	{
		if ( (keyFlag&0x03) == 0x03)
			LED2 = 0;
		else
			LED2 = 1;
		xEventGroupWaitBits(Event_Handler,(0x01<<1)|(0x01<<0),pdFALSE,pdTRUE,portMAX_DELAY);
		printf("event3 task running!!\r\n");
		vTaskDelay(1000);
	}
}

void start_task(void *pvParameters)
{				
	 Event_Handler = xEventGroupCreate();
	
		xTaskCreate((TaskFunction_t )key_task,            //任务函数
                (const char*    )"key_task",          //任务名称
                (uint16_t       )KEY_STACK_SIZE,        //任务堆栈大小
                (void*          )NULL,                  //传递给任务函数的参数
                (UBaseType_t    )KEY_TASK_PRIO,       //任务优先级
                (TaskHandle_t*  )&KeyTask_Handler);   //任务句柄  	
								
	 xTaskCreate((TaskFunction_t )event1_task,            //任务函数
                (const char*    )"event1_task",          //任务名称
                (uint16_t       )EVENT1_STACK_SIZE,        //任务堆栈大小
                (void*          )NULL,                  //传递给任务函数的参数
                (UBaseType_t    )EVENT1_TASK_PRIO,       //任务优先级
                (TaskHandle_t*  )&Event1Task_Handler);   //任务句柄  
								
	 xTaskCreate((TaskFunction_t )event2_task,            //任务函数
                (const char*    )"event2_task",          //任务名称
                (uint16_t       )EVENT2_STACK_SIZE,        //任务堆栈大小
                (void*          )NULL,                  //传递给任务函数的参数
                (UBaseType_t    )EVENT2_TASK_PRIO,       //任务优先级
                (TaskHandle_t*  )&Event2Task_Handler);   //任务句柄 

		 xTaskCreate((TaskFunction_t )event3_task,            //任务函数
                (const char*    )"event3_task",          //任务名称
                (uint16_t       )EVENT3_STACK_SIZE,        //任务堆栈大小
                (void*          )NULL,                  //传递给任务函数的参数
                (UBaseType_t    )EVENT3_TASK_PRIO,       //任务优先级
                (TaskHandle_t*  )&Event3Task_Handler);   //任务句柄 
								
	 vTaskDelete(StartTask_Handler);
}

标签:标志,FreeRTOS,EventBits,0x01,任务,Handler,事件
来源: https://blog.csdn.net/dhejsb/article/details/120207514