Danger-alarm/SOFTWARE-FreeRTOS/Source/main.cpp

120 lines
3.1 KiB
C++
Raw Normal View History

2024-06-03 16:27:41 +08:00
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
2024-06-03 18:59:42 +08:00
#include "queue.h"
2024-06-03 16:27:41 +08:00
/* Standard demo includes. */
#include "TimerDemo.h"
#include "QueueOverwrite.h"
#include "EventGroupsDemo.h"
#include "IntSemTest.h"
#include "TaskNotify.h"
2024-06-03 18:59:42 +08:00
#include "main.hpp"
2024-06-03 16:27:41 +08:00
#include "lwipopts.h"
2024-06-03 18:59:42 +08:00
#include "common.hpp"
#include "ds18b20.hpp"
#include "ZC13.hpp"
#include "MHZ14B.hpp"
2024-06-03 16:27:41 +08:00
#ifndef PICO_DEFAULT_LED_PIN
2024-06-04 09:54:09 +08:00
#warning pio/hello_pio example requires a board with a regular LED
#define PICO_DEFAULT_LED_PIN 25
2024-06-03 16:27:41 +08:00
#endif
void Led_Blinky(void *pvParameters)
{
(void)pvParameters;
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
2024-06-04 09:54:09 +08:00
2024-06-03 18:59:42 +08:00
_printTaskStackHighWaterMark("Led_Blinky");
2024-06-03 16:27:41 +08:00
2024-06-03 18:59:42 +08:00
float TEMPERATURE;
2024-06-04 09:54:09 +08:00
2024-06-03 16:27:41 +08:00
while (1) {
vTaskDelay(pdMS_TO_TICKS(500));
gpio_put(LED_PIN, 1);
2024-06-04 09:54:09 +08:00
2024-06-03 16:27:41 +08:00
vTaskDelay(pdMS_TO_TICKS(500));
gpio_put(LED_PIN, 0);
2024-06-04 09:54:09 +08:00
2024-06-03 18:59:42 +08:00
// 从队列接收数据
if (xQueueReceive(xQueue, &TEMPERATURE, portMAX_DELAY) == pdPASS) {
// 处理接收到的数据
printf("%f\n", TEMPERATURE);
}
//_printTaskStackHighWaterMark("Led_Blinky");
2024-06-03 16:27:41 +08:00
}
}
void Read_Onboard_Temperature(void *pvParameters)
{
(void)pvParameters;
adc_init();
adc_set_temp_sensor_enabled(true);
adc_select_input(4); // Input 4 is the onboard temperature sensor.
2024-06-04 09:54:09 +08:00
2024-06-03 18:59:42 +08:00
//_printTaskStackHighWaterMark("Read_Onboard_Temperature");
2024-06-03 16:27:41 +08:00
while (1) {
const float conversionFactor = 3.3f / (1 << 12);
float adc = (float)adc_read() * conversionFactor;
float tempC = 27.0f - (adc - 0.706f) / 0.001721f;
printf("Onboard temperature %.02f°C %.02f°F\n", tempC, (tempC * 9 / 5 + 32));
2024-06-04 09:54:09 +08:00
2024-06-03 18:59:42 +08:00
//_printTaskStackHighWaterMark("Read_Onboard_Temperature");
2024-06-04 09:54:09 +08:00
2024-06-03 16:27:41 +08:00
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
}
int main(void)
{
stdio_init_all();
2024-06-03 18:59:42 +08:00
sleep_ms(3000);
2024-06-03 16:27:41 +08:00
//set_sys_clock_khz(250000, true);
2024-06-04 09:54:09 +08:00
2024-06-03 18:59:42 +08:00
// 创建队列
xQueue = xQueueCreate(10, sizeof(long));
2024-06-04 09:54:09 +08:00
2024-06-03 18:59:42 +08:00
// 创建任务
2024-06-03 16:27:41 +08:00
BaseType_t xReturned;
TaskHandle_t Led_Blinky_xHandle = NULL;
2024-06-03 18:59:42 +08:00
TaskHandle_t DS18B20_xHandle = NULL;
TaskHandle_t CH4_xHandle = NULL;
TaskHandle_t CO2_xHandle = NULL;
2024-06-03 16:27:41 +08:00
// 板载LED闪烁
xReturned = xTaskCreate(Led_Blinky, "Blinky task", 512, NULL, tskIDLE_PRIORITY, &Led_Blinky_xHandle);
2024-06-04 09:54:09 +08:00
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
2024-06-03 16:27:41 +08:00
printf("Blinky Task Error!");
}
2024-06-03 18:59:42 +08:00
// DS18B20
xReturned = xTaskCreate(DS18B20, "DS18B20 task", 1024, NULL, tskIDLE_PRIORITY, &DS18B20_xHandle);
2024-06-04 09:54:09 +08:00
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
2024-06-03 18:59:42 +08:00
printf("DS18B20() Task Error!");
}
// CH4
xReturned = xTaskCreate(CH4, "CH4 task", 1024, NULL, tskIDLE_PRIORITY, &CH4_xHandle);
2024-06-04 09:54:09 +08:00
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
2024-06-03 18:59:42 +08:00
printf("CH4() Task Error!");
}
// CO2
xReturned = xTaskCreate(CO2, "CO2 task", 1024, NULL, tskIDLE_PRIORITY, &CO2_xHandle);
2024-06-04 09:54:09 +08:00
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
2024-06-03 18:59:42 +08:00
printf("CO2() Task Error!");
}
2024-06-04 09:54:09 +08:00
2024-06-03 16:27:41 +08:00
vTaskStartScheduler();
return 0;
}