freeRtos事件组的使用

2023-12-20 11:02:49

一.事件组的使用?

1.创建事件

2.设置事件?

3.等待事件?

4.同步点?

二.事件组等待事件?

创建3个任务,任务1:累加100000000,然后设置事件bit 0;任务2:累减5000000次,然后设置事件bit 1;任务3:等待。

static int sum = 0;
static int dec = 0;
static volatile int flagCalcEnd = 0;
static volatile int flagUARTused = 0;
static QueueHandle_t xQueueCalcHandle;
static QueueHandle_t xQueueUARTcHandle;

static EventGroupHandle_t xEventGroupCalc;


void Task1Function(void * param)
{
	volatile int i = 0;
	while (1)
	{
		for (i = 0; i < 10000000; i++)
			sum++;
		xQueueSend(xQueueCalcHandle,&sum,0);//写入数据
		/*设置事件0*/
		xEventGroupSetBits(xEventGroupCalc,(1<<0));
	}
}

void Task2Function(void * param)
{
	volatile int i = 0;
	while (1)
	{
		for (i = 0; i < 10000000; i++)
			dec++;
		xQueueSend(xQueueCalcHandle,&dec,0);//写入数据
		/*设置事件1*/
		xEventGroupSetBits(xEventGroupCalc,(1<<1));
	}
}

void Task3Function(void * param)
{
	int val1,val2;
	while (1)
	{
		/*等待事件*/
		xEventGroupWaitBits(xEventGroupCalc,(1<<0)||(1<<1),pdTRUE,,pdTRUE,为portMAX_DELAY);
		//等待上面两个事件
		xQueueReceive(xQueueCalcHandle,&val1,0);
		xQueueReceive(xQueueCalcHandle,&val2,0);
		printf("val1 = %d,val2= %d\r\n",val1,val2);
	}
}



/*-----------------------------------------------------------*/

int main( void )
{
	TaskHandle_t xHandleTask1;
		
#ifdef DEBUG
  debug();
#endif

	prvSetupHardware();

	printf("Hello, world!\r\n");

	/*创建事件组*/
	 xEventGroupCalc=xEventGroupCreate();
	
	xQueueCalcHandle = xQueueCreate(2, sizeof(int));
	if (xQueueCalcHandle == NULL)
	{
		printf("can not create queue\r\n");
	}


	xTaskCreate(Task1Function, "Task1", 100, NULL, 1, &xHandleTask1);
	xTaskCreate(Task2Function, "Task2", 100, NULL, 1, NULL);
	xTaskCreate(Task3Function, "Task3", 100, NULL, 1, NULL);

	/* Start the scheduler. */
	vTaskStartScheduler();

	/* Will only get here if there was not enough heap space to create the
	idle task. */
	return 0;
}

文章来源:https://blog.csdn.net/lovesjj_1011/article/details/135019974
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。