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

95 lines
2.7 KiB
C++
Raw Normal View History

2024-06-03 18:59:42 +08:00
#include "MHZ14B.hpp"
#include "common.hpp"
#include "HC-12.hpp"
2024-06-03 18:59:42 +08:00
void MH_Z14B_INIT()
{
// 初始化UART
uart_init(UART1, BAUD_RATE);
gpio_set_function(UART1_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART1_RX_PIN, GPIO_FUNC_UART);
uart_set_hw_flow(UART1, false, false);
uart_set_format(UART1, DATA_BITS, STOP_BITS, PARITY);
return ;
2024-06-03 18:59:42 +08:00
}
// CO2
static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK)
{
// 0x86 读气体浓度值
uint8_t CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
uart_write_blocking(UART1, CMD, 9);
2024-08-23 08:08:37 +08:00
//sleep_ms(200);
vTaskDelay(pdMS_TO_TICKS(200)); // 非阻塞延时
2024-06-03 18:59:42 +08:00
// 读取
uint8_t CO2_DATA[9] = { 0 };
uart_read_blocking(UART1, CO2_DATA, 9);
// CO2 浓度
uint16_t CO2_CONC = (256 * CO2_DATA[2]) + CO2_DATA[3];
// 校验
uint8_t CHECKSUM = (0xFF - (CO2_DATA[1] + CO2_DATA[2] + CO2_DATA[3] + CO2_DATA[4] + \
CO2_DATA[5] + CO2_DATA[6] + CO2_DATA[7])) + 1;
2024-06-03 18:59:42 +08:00
if (CO2_DATA[8] == CHECKSUM && CO2_DATA[1] == 0x86) {
//printf("CHECKSUM: %X = %X\n", CO2_DATA[8], CHECKSUM);
//printf("CO2 Concentration: %d ppm\n", CO2_CONC);
*MH_Z14B_DATA_IS_OK = 1;
2024-09-08 18:39:35 +08:00
}
/*
else {
2024-06-03 18:59:42 +08:00
// 校准传感器 零点 ZERO
uint8_t ZERO[] = { 0XFF, 0X01, 0X87, 0X00, 0X00, 0X00, 0X00, 0X00, 0X78 };
uart_write_blocking(UART1, ZERO, 9);
2024-08-23 08:08:37 +08:00
//sleep_ms(200);
vTaskDelay(pdMS_TO_TICKS(200)); // 非阻塞延时
2024-06-03 18:59:42 +08:00
// 校准传感器 跨度点 SPAN
uint8_t SPAN[] = { 0XFF, 0X01, 0X88, 0X07, 0XD0, 0X00, 0X00, 0X00, 0XA0 };
uart_write_blocking(UART1, SPAN, 9);
2024-08-23 08:08:37 +08:00
//sleep_ms(200);
vTaskDelay(pdMS_TO_TICKS(200)); // 非阻塞延时
2024-06-03 18:59:42 +08:00
*MH_Z14B_DATA_IS_OK = 0;
printf("CO2 concentration reading failed!\n");
}
2024-09-08 18:39:35 +08:00
*/
2024-06-03 18:59:42 +08:00
2024-06-03 18:59:42 +08:00
return CO2_CONC;
}
void CO2(void *pvParameters)
{
2024-08-23 08:08:37 +08:00
uint16_t CO2_DATA = 0;
2024-06-03 18:59:42 +08:00
int MH_Z14B_DATA_IS_OK = 0;
2024-08-23 08:08:37 +08:00
char CO2_DATA_TEMP[BUFER] = { 0 };
2024-06-03 18:59:42 +08:00
MH_Z14B_INIT();
_printTaskStackHighWaterMark("CO2");
2024-06-04 09:54:09 +08:00
while (1) {
2024-06-03 18:59:42 +08:00
CO2_DATA = MH_Z14B(&MH_Z14B_DATA_IS_OK);
if (CO2_DATA != -1 && MH_Z14B_DATA_IS_OK == 1) {
2024-06-03 18:59:42 +08:00
printf("CO2 Concentration: %d ppm\n", CO2_DATA);
2024-08-23 08:08:37 +08:00
snprintf(CO2_DATA_TEMP, BUFER, "CO2 Concentration: %d ppm\n", CO2_DATA);
_HC_12(CO2_DATA_TEMP);
2024-08-23 08:08:37 +08:00
memset(CO2_DATA_TEMP, 0, BUFER);
2024-09-08 18:39:35 +08:00
} else {
printf("CO2 reading failed!!!");
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
2024-06-03 18:59:42 +08:00
}
//_printTaskStackHighWaterMark("CO2");
watchdog_update(); // 喂狗
2024-08-23 08:08:37 +08:00
vTaskDelay(pdMS_TO_TICKS(5000)); // 非阻塞延时
printf("\n");
2024-06-03 18:59:42 +08:00
}
return ;
2024-06-04 09:54:09 +08:00
}