95 lines
2.7 KiB
C++
95 lines
2.7 KiB
C++
#include "MHZ14B.hpp"
|
||
#include "common.hpp"
|
||
#include "HC-12.hpp"
|
||
|
||
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 ;
|
||
}
|
||
|
||
// 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);
|
||
//sleep_ms(200);
|
||
vTaskDelay(pdMS_TO_TICKS(200)); // 非阻塞延时
|
||
|
||
// 读取
|
||
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;
|
||
|
||
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;
|
||
}
|
||
/*
|
||
else {
|
||
// 校准传感器 零点 (ZERO)
|
||
uint8_t ZERO[] = { 0XFF, 0X01, 0X87, 0X00, 0X00, 0X00, 0X00, 0X00, 0X78 };
|
||
uart_write_blocking(UART1, ZERO, 9);
|
||
//sleep_ms(200);
|
||
vTaskDelay(pdMS_TO_TICKS(200)); // 非阻塞延时
|
||
|
||
// 校准传感器 跨度点 (SPAN)
|
||
uint8_t SPAN[] = { 0XFF, 0X01, 0X88, 0X07, 0XD0, 0X00, 0X00, 0X00, 0XA0 };
|
||
uart_write_blocking(UART1, SPAN, 9);
|
||
//sleep_ms(200);
|
||
vTaskDelay(pdMS_TO_TICKS(200)); // 非阻塞延时
|
||
|
||
*MH_Z14B_DATA_IS_OK = 0;
|
||
printf("CO2 concentration reading failed!\n");
|
||
}
|
||
*/
|
||
|
||
|
||
return CO2_CONC;
|
||
}
|
||
|
||
void CO2(void *pvParameters)
|
||
{
|
||
uint16_t CO2_DATA = 0;
|
||
int MH_Z14B_DATA_IS_OK = 0;
|
||
char CO2_DATA_TEMP[BUFER] = { 0 };
|
||
MH_Z14B_INIT();
|
||
_printTaskStackHighWaterMark("CO2");
|
||
|
||
while (1) {
|
||
CO2_DATA = MH_Z14B(&MH_Z14B_DATA_IS_OK);
|
||
if (CO2_DATA != -1 && MH_Z14B_DATA_IS_OK == 1) {
|
||
printf("CO2 Concentration: %d ppm\n", CO2_DATA);
|
||
|
||
snprintf(CO2_DATA_TEMP, BUFER, "CO2 Concentration: %d ppm\n", CO2_DATA);
|
||
_HC_12(CO2_DATA_TEMP);
|
||
memset(CO2_DATA_TEMP, 0, BUFER);
|
||
} else {
|
||
printf("CO2 reading failed!!!");
|
||
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
|
||
}
|
||
|
||
//_printTaskStackHighWaterMark("CO2");
|
||
watchdog_update(); // 喂狗
|
||
|
||
vTaskDelay(pdMS_TO_TICKS(5000)); // 非阻塞延时
|
||
printf("\n");
|
||
}
|
||
|
||
return ;
|
||
}
|