依据以上文档,写FreeRTOS程序
时间: 2024-12-23 15:08:17 浏览: 11
要将上述代码转换为使用FreeRTOS的程序,我们需要对代码进行一些结构调整,以适应FreeRTOS的任务调度机制。以下是基于FreeRTOS的示例程序:
```c
#include "FreeRTOS.h"
#include "task.h"
#include "delay.h"
#include "sys.h"
#include "lcd.h"
#include "touch.h"
#include "gui.h"
#include "test.h"
#include "DS2431.h"
#include "DS2807.h"
#include "adc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "key.h"
#include "led.h"
#include "usart.h"
// 定义任务句柄
TaskHandle_t xTask1Handle = NULL;
TaskHandle_t xTask2Handle = NULL;
// 全局变量
uint8_t led_state = 0;
uint8_t led_count = 0;
uint8_t sensor1 = 0;
uint8_t sensor2 = 0;
unsigned char arr1_id[8];
unsigned char arr2_id[8];
unsigned char write_data[128];
unsigned char read_data[128];
unsigned char status;
unsigned char position[5] = { 12, 41, 65, 89, 12 };
unsigned char position_data[5];
// 任务1:处理按键和LED
void vTask1(void *pvParameters)
{
(void)pvParameters;
while (1)
{
if (KEY0 == 1)
{
vTaskDelay(pdMS_TO_TICKS(20)); // 去抖动
if (++led_count > 25)
{
led_count = 0;
if (led_state)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
}
else
{
GPIO_SetBits(GPIOA, GPIO_Pin_8);
}
led_state = !led_state;
}
}
else
{
led_count = 0;
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
}
vTaskDelay(pdMS_TO_TICKS(10)); // 降低CPU占用率
}
}
// 任务2:处理传感器读取和显示
void vTask2(void *pvParameters)
{
(void)pvParameters;
while (1)
{
if (KEY0 == 0)
{
GPIO_SetBits(GPIOA, GPIO_Pin_4);
vTaskDelay(pdMS_TO_TICKS(150)); // 去抖动
if (DS2431_ReadRom(arr1_id) == 0)
{
printf("DS2431 found with serial number: %02X %02X %02X %02X %02X %02X %02X %02X\r\n",
arr1_id[0], arr1_id[1], arr1_id[2], arr1_id[3],
arr1_id[4], arr1_id[5], arr1_id[6], arr1_id[7]);
Show_Str(3, 20, GREEN, YELLOW, (u8 *)"DS28E07+ A ID:", 24, 1);
sprintf((char *)arr1_id, "%02X%02X%02X%02X%02X%02X%02X%02X\r\n",
arr1_id[0], arr1_id[1], arr1_id[2], arr1_id[3],
arr1_id[4], arr1_id[5], arr1_id[6], arr1_id[7]);
Show_Str(1, 40, RED, RED, arr1_id, 15, 1);
sensor1 = 1;
}
else
{
printf("DS2431_ReadRom failed,请检查硬件连线 \r\n");
Show_Str(3, 20, BLUE, BLUE, (u8 *)" A IC is error!", 24, 1);
sensor1 = 0;
}
vTaskDelay(pdMS_TO_TICKS(30));
if (DS2807_ReadRom(arr2_id) == 0)
{
printf("DS2807 found with serial number: %02X %02X %02X %02X %02X %02X %02X %02X\r\n",
arr2_id[0], arr2_id[1], arr2_id[2], arr2_id[3],
arr2_id[4], arr2_id[5], arr2_id[6], arr2_id[7]);
Show_Str(3, 60, GREEN, YELLOW, (u8 *)"DS28E07+ B ID:", 24, 1);
sprintf((char *)arr2_id, "%02X%02X%02X%02X%02X%02X%02X%02X\r\n",
arr2_id[0], arr2_id[1], arr2_id[2], arr2_id[3],
arr2_id[4], arr2_id[5], arr2_id[6], arr2_id[7]);
Show_Str(1, 80, RED, RED, arr2_id, 15, 1);
sensor2 = 1;
}
else
{
printf("DS2807_ReadRom failed,请检查硬件连线 \r\n");
Show_Str(3, 60, BLUE, BLUE, (u8 *)" B IC is error!", 24, 1);
sensor2 = 0;
}
if (sensor1 == 1 && sensor2 == 1)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
vTaskDelay(pdMS_TO_TICKS(300));
GPIO_SetBits(GPIOB, GPIO_Pin_7); // PASS
vTaskDelay(pdMS_TO_TICKS(300));
GPIO_ResetBits(GPIOB, GPIO_Pin_7);
GPIO_ResetBits(GPIOC, GPIO_Pin_7);
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_7);
GPIO_SetBits(GPIOC, GPIO_Pin_7); // PASS
vTaskDelay(pdMS_TO_TICKS(300));
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
}
}
vTaskDelay(pdMS_TO_TICKS(100)); // 降低CPU占用率
}
}
int main(void)
{
/* 初始化write_data 装ascii */
for (int idx = 0; idx < 128; idx++)
{
write_data[idx] = idx;
}
Stm32_Clock_Init(9); /* 系统时钟设置 */
delay_init(72); // 延时初始化
uart_init(72, 9600); /* 串口初始化为9600 */
KEY_Init();
LED_Init();
Adc_Init();
DS2431_Init();
DS2807_Init();
LCD_Init(); // 液晶屏初始化
LCD_Clear(BLACK); // 清屏
POINT_COLOR = WHITE;
// 创建任务
xTaskCreate(vTask1, "Task1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, &xTask1Handle);
xTaskCreate(vTask2, "Task2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, &xTask2Handle);
// 启动调度器
vTaskStartScheduler();
// 如果调度器退出(不应该发生),则进入死循环
for (;;)
{
;
}
}
```
### 说明
1. **任务创建**:在`main`函数中创建了两个任务`vTask1`和`vTask2`,分别处理按键和LED以及传感器读取和显示。
2. **任务调度**:使用`vTaskStartScheduler()`启动FreeRTOS调度器。
3. **任务延迟**:使用`vTaskDelay(pdMS_TO_TICKS(ms))`来实现任务延迟,替代原有的`delay_ms(ms)`。
4. **任务优先级**:任务优先级设置为`tskIDLE_PRIORITY + 1`,确保任务优先级高于空闲任务。
这样,程序就可以在FreeRTOS环境下运行,并且能够更好地管理多个任务。
阅读全文