一、Cubemx 配置
选好芯片型号后,先进行sys的配置
时钟配置,f103c8t6标准板子只有高速,低速可以不用
打开板载led
usart1配置 选择异步,其他默认
freertos配置,版本选择v2,v1没有事件相关函数
生成工程
二、Eevent
freertos.c
/* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "stdio.h" #include "usart.h" #include "event_groups.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ int fputc(int ch,FILE *f) { HAL_UART_Transmit(&huart1,(uint8_t*)&ch,1,0xFFFF); return ch; } /* USER CODE END PTD */
/* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ // 定义事件组句柄 EventGroupHandle_t myEventGroup; // 定义事件位 #define EVENT_BIT_1 (1 << 0) #define EVENT_BIT_2 (1 << 1) /* USER CODE END FunctionPrototypes */
/* USER CODE BEGIN RTOS_TIMERS */ /* start timers, add new ones, ... */ // 初始化事件组 myEventGroup = xEventGroupCreate(); /* USER CODE END RTOS_TIMERS */
void StartTask02(void *argument) { /* USER CODE BEGIN StartTask02 */ /* Infinite loop */ for(;;) { HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);// open led 4s osDelay(4000); HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);// close led 4s osDelay(4000); xEventGroupSetBits(myEventGroup, EVENT_BIT_1); } /* USER CODE END StartTask02 */ }
void StartTask03(void *argument) { /* USER CODE BEGIN StartTask03 */ /* Infinite loop */ for(;;) { printf("task2 ok!!\r\n"); // 触发事件2 xEventGroupSetBits(myEventGroup, EVENT_BIT_2); osDelay(4000); } /* USER CODE END StartTask03 */ }
void StartDefaultTask(void *argument) { /* USER CODE BEGIN StartDefaultTask */ /* Infinite loop */ for(;;) { EventBits_t eventBits = xEventGroupWaitBits(myEventGroup, EVENT_BIT_1 | EVENT_BIT_2, pdTRUE, pdFALSE, portMAX_DELAY); if ((eventBits & (EVENT_BIT_1 | EVENT_BIT_2)) == (EVENT_BIT_1 | EVENT_BIT_2)) { // 所有指定的事件都已发生 // 执行任务1的操作 printf("task2 and task3 over!!\r\n"); // 重置事件位,以便下次等待 xEventGroupClearBits(myEventGroup, EVENT_BIT_1 | EVENT_BIT_2); } osDelay(4000); } /* USER CODE END StartDefaultTask */ }
运行效果
链接:https://pan.baidu.com/s/1ASV5C88yGT28ogq6OmUXIQ?pwd=ebuq 提取码:ebuq