- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "timer.h"
- #include "lcd.h"
- #include "key.h"
- #include "beep.h"
- #include "FreeRTOS.h"
- #include "task.h" //任务的
- #include "queue.h" //内存池的
- #include "malloc.h" //队列的
- #include "semphr.h" //信号量
- #include "event_groups.h" //事件组
- #define START_TASK_PRIO 1 //任务优先级
- #define START_STK_SIZE 256 //任务堆栈大小
- TaskHandle_t StartTask_Handler; //任务句柄
- void start_task(void *pvParameters); //任务函数
- #define LED_TASK_PRIO 2 //任务优先级
- #define LED_STK_SIZE 128 //任务堆栈大小
- TaskHandle_t LedTask_Handler; //任务句柄
- void led_task(void *pvParameters); //任务函数
- #define TASK1_TASK_PRIO 3 //任务优先级
- #define TASK1_STK_SIZE 256 //任务堆栈大小
- TaskHandle_t Task1Task_Handler; //任务句柄
- void task1_task(void *pvParameters); //任务函数
- #define TASK2_TASK_PRIO 4 //任务优先级
- #define TASK2_STK_SIZE 128 //任务堆栈大小
- TaskHandle_t Task2Task_Handler; //任务句柄
- void task2_task(void *pvParameters); //任务函数
- /*事件的句柄*/
- static EventGroupHandle_t Event_Handler = NULL;
- #define EVENT1 (0x01 << 1)
- #define EVENT2 (0x02 << 2)
- //LCD刷屏时使用的颜色
- int lcd_discolor[14] = { WHITE, BLACK, BLUE, BRED,
- GRED, GBLUE, RED, MAGENTA,
- GREEN, CYAN, YELLOW, BROWN,
- BRRED, GRAY
- };
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
- delay_init(168); //初始化延时函数
- uart_init(115200); //初始化串口
- LED_Init(); //初始化LED端口
- KEY_Init();
- BEEP_Init();
- LCD_Init(); //初始化LCD
- my_mem_init(SRAMIN); //初始化内存池
- POINT_COLOR = RED;
- LCD_ShowString(30, 10, 200, 16, 16, "ATK STM32F407");
- LCD_ShowString(30, 30, 200, 16, 16, "FreeRTOS 测试");
- LCD_ShowString(30, 50, 200, 16, 16, "任务状态查询");
- //创建开始任务
- xTaskCreate((TaskFunction_t)start_task, //任务函数
- (const char *)"start_task", //任务名称
- (uint16_t)START_STK_SIZE, //任务堆栈大小
- (void *)NULL, //传递给任务函数的参数
- (UBaseType_t)START_TASK_PRIO, //任务优先级
- (TaskHandle_t *)&StartTask_Handler); //任务句柄
- vTaskStartScheduler(); //开启任务调度
- }
- //开始任务任务函数
- void start_task(void *pvParameters)
- {
- taskENTER_CRITICAL(); //进入临界区
- //创建事件
- Event_Handler = xEventGroupCreate();
- if (Event_Handler != NULL)
- printf("创建事假成功\r\n\n");
- //创建LED任务
- xTaskCreate((TaskFunction_t)led_task,
- (const char *)"led_task",
- (uint16_t)LED_STK_SIZE,
- (void *)NULL,
- (UBaseType_t)LED_TASK_PRIO,
- (TaskHandle_t *)&LedTask_Handler);
- printf("led0任务函数创建成功\r\n");
- //创建TASK1任务
- xTaskCreate((TaskFunction_t)task1_task,
- (const char *)"task1_task",
- (uint16_t)TASK1_STK_SIZE,
- (void *)NULL,
- (UBaseType_t)TASK1_TASK_PRIO,
- (TaskHandle_t *)&Task1Task_Handler);
- printf("task1任务函数创建成功\r\n");
- //创建TASK2任务
- xTaskCreate((TaskFunction_t)task2_task,
- (const char *)"task2_task",
- (uint16_t)TASK2_STK_SIZE,
- (void *)NULL,
- (UBaseType_t)TASK2_TASK_PRIO,
- (TaskHandle_t *)&Task2Task_Handler);
- printf("task2任务函数创建成功\r\n");
- vTaskDelete(StartTask_Handler); //删除开始任务
- taskEXIT_CRITICAL(); //退出临界区
- }
- //led0任务函数,低优先级
- void led_task(void *pvParameters)
- {
- while (1)
- {
- LED0 = ! LED0;
- vTaskDelay(300);
- }
- }
- //task1任务函数,中优先级
- void task1_task(void *pvParameters)
- {
- EventBits_t r_event = pdPASS;
- while (1)
- {
- r_event = xEventGroupWaitBits(Event_Handler, //事件的句柄
- EVENT1 | EVENT2, //感兴趣的事件
- pdTRUE, //退出时是否清除事件位
- pdTRUE, //是否满足所有事件
- portMAX_DELAY); //超时时间,一直等所有事件都满足
- if(((EVENT1|EVENT2) & r_event) == (EVENT1|EVENT2))
- printf("KEY1和KEY2都按下\r\n");
- else
- printf("事件错误\r\n");
-
- }
- }
- //task2任务函数,高优先级
- void task2_task(void *pvParameters)
- {
- EventBits_t r_event = pdPASS;
- while (1)
- {
- if(KEY_Scan(1)==KEY1_PRES)
- {
- xEventGroupSetBits(Event_Handler,EVENT1);
-
- if(r_event == pdTRUE)
- printf("KEY1按下,设置事件1\r\n");
-
- }else if(KEY_Scan(1)==KEY2_PRES)
- {
- xEventGroupSetBits(Event_Handler,EVENT2);
-
- if(r_event == pdTRUE)
- printf("KEY2按下,设置事件2\r\n");
-
- }
-
- vTaskDelay(20);
- }
- }