2024-06-03 18:59:42 +08:00
|
|
|
|
#include "MHZ14B.hpp"
|
|
|
|
|
#include "common.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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
// 读取
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 校准传感器 跨度点 (SPAN)
|
|
|
|
|
uint8_t SPAN[] = { 0XFF, 0X01, 0X88, 0X07, 0XD0, 0X00, 0X00, 0X00, 0XA0 };
|
|
|
|
|
uart_write_blocking(UART1, SPAN, 9);
|
|
|
|
|
sleep_ms(200);
|
|
|
|
|
|
|
|
|
|
*MH_Z14B_DATA_IS_OK = 0;
|
|
|
|
|
printf("CO2 concentration reading failed!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CO2_CONC;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CO2(void *pvParameters)
|
|
|
|
|
{
|
|
|
|
|
uint16_t CO2_DATA = -1;
|
|
|
|
|
int MH_Z14B_DATA_IS_OK = 0;
|
|
|
|
|
MH_Z14B_INIT();
|
2024-06-04 15:45:40 +08:00
|
|
|
|
_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) {
|
|
|
|
|
printf("CO2 Concentration: %d ppm\n", CO2_DATA);
|
|
|
|
|
}
|
2024-06-04 15:45:40 +08:00
|
|
|
|
//_printTaskStackHighWaterMark("CO2");
|
|
|
|
|
watchdog_update(); // 喂狗
|
|
|
|
|
|
2024-06-03 18:59:42 +08:00
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
|
2024-06-04 15:45:40 +08:00
|
|
|
|
printf("\n");
|
2024-06-03 18:59:42 +08:00
|
|
|
|
}
|
2024-06-04 09:54:09 +08:00
|
|
|
|
}
|