2024-06-03 16:27:41 +08:00
|
|
|
|
#include "ZC13.hpp"
|
2024-06-03 18:59:42 +08:00
|
|
|
|
#include "common.hpp"
|
2024-06-03 16:27:41 +08:00
|
|
|
|
|
|
|
|
|
int ZC13_INIT()
|
|
|
|
|
{
|
|
|
|
|
uint tx_offset = pio_add_program(ZC13_PIO, &uart_tx_program);
|
|
|
|
|
uart_tx_program_init(ZC13_PIO, ZC13_PIO_SM_TX, tx_offset, ZC13_PIO_TX_PIN, ZC13_PIO_SERIAL_BAUD);
|
|
|
|
|
|
|
|
|
|
uint rx_offset = pio_add_program(ZC13_PIO, &uart_rx_program);
|
|
|
|
|
uart_rx_program_init(ZC13_PIO, ZC13_PIO_SM_RX, rx_offset, ZC13_PIO_RX_PIN, ZC13_PIO_SERIAL_BAUD);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 09:54:09 +08:00
|
|
|
|
int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < DATA_LEN; i++) {
|
2024-06-03 16:27:41 +08:00
|
|
|
|
uart_tx_program_putc(pio, sm, DATA[i]);
|
|
|
|
|
sleep_ms(3);
|
|
|
|
|
}
|
2024-06-04 09:54:09 +08:00
|
|
|
|
|
|
|
|
|
return 0;
|
2024-06-03 16:27:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 09:54:09 +08:00
|
|
|
|
int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
|
|
|
|
|
{
|
2024-06-03 16:27:41 +08:00
|
|
|
|
char c = '\0';
|
|
|
|
|
int received_count = 0;
|
2024-06-04 09:54:09 +08:00
|
|
|
|
int timeout_ms = 100; // 设置较长的超时时间
|
2024-06-03 16:27:41 +08:00
|
|
|
|
|
|
|
|
|
while (received_count < DATA_LEN && timeout_ms > 0) {
|
|
|
|
|
if (uart_rx_program_available(pio, sm)) {
|
|
|
|
|
c = uart_rx_program_getc(pio, sm);
|
|
|
|
|
DATA[received_count++] = c;
|
2024-06-04 09:54:09 +08:00
|
|
|
|
//printf("0x%X ", c);
|
2024-06-03 16:27:41 +08:00
|
|
|
|
if (c == '\n') {
|
|
|
|
|
// 接收到换行符,停止接收数据
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 没有接收到数据,继续等待
|
|
|
|
|
sleep_ms(1);
|
|
|
|
|
timeout_ms--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (received_count == 0) {
|
|
|
|
|
// 没有接收到有效数据,可以进行相应的处理
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return received_count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ZC13(const char *model)
|
|
|
|
|
{
|
|
|
|
|
uint8_t CH4_DATA[270] = { 0 };
|
|
|
|
|
int CH4_DATA_LEN = 0;
|
|
|
|
|
|
|
|
|
|
// 发送指令
|
|
|
|
|
uint8_t CH4_CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
|
2024-06-04 09:54:09 +08:00
|
|
|
|
ZC13_PIO_UART_TX_DATA(ZC13_PIO, ZC13_PIO_SM_TX, CH4_CMD, sizeof(CH4_CMD));
|
2024-06-03 16:27:41 +08:00
|
|
|
|
sleep_ms(200);
|
|
|
|
|
|
2024-06-04 09:54:09 +08:00
|
|
|
|
// 接收指令
|
|
|
|
|
CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9);
|
2024-06-03 16:27:41 +08:00
|
|
|
|
|
2024-06-04 09:54:09 +08:00
|
|
|
|
//printf("\n");
|
|
|
|
|
// printf("%d\n", CH4_DATA_LEN);
|
2024-06-03 16:27:41 +08:00
|
|
|
|
|
|
|
|
|
uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节
|
|
|
|
|
uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节
|
|
|
|
|
|
|
|
|
|
// 判断最高位是否为 1
|
|
|
|
|
if (highByte & 0x80) {
|
|
|
|
|
// 最高位是 1,表示传感器故障
|
|
|
|
|
printf("CH4 sensor malfunction!\n");
|
|
|
|
|
} else {
|
|
|
|
|
if (CH4_DATA[1] == 0X86) {
|
2024-06-04 09:54:09 +08:00
|
|
|
|
if (0 == strcasecmp(model, "ZC05")) {
|
|
|
|
|
// 计算气体浓度值
|
|
|
|
|
uint16_t gasConcentration = (highByte & 0x3F) * 256 + lowByte;
|
|
|
|
|
// 输出气体浓度值
|
|
|
|
|
printf("CH4 Concentration: %uppm\n", gasConcentration);
|
|
|
|
|
|
|
|
|
|
return gasConcentration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (0 == strcasecmp(model, "ZC13")) {
|
|
|
|
|
// 计算气体浓度值
|
|
|
|
|
uint16_t gasConcentration = (highByte & 0x1F) * 256 + lowByte;
|
|
|
|
|
// 输出气体浓度值
|
|
|
|
|
printf("CH4 Concentration: %uppm\n", gasConcentration);
|
|
|
|
|
|
|
|
|
|
return gasConcentration;
|
|
|
|
|
}
|
2024-06-03 16:27:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-04 09:54:09 +08:00
|
|
|
|
|
2024-06-03 16:27:41 +08:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 18:59:42 +08:00
|
|
|
|
void CH4(void *pvParameters)
|
2024-06-03 16:27:41 +08:00
|
|
|
|
{
|
2024-06-03 18:59:42 +08:00
|
|
|
|
ZC13_INIT();
|
|
|
|
|
_printTaskStackHighWaterMark("ZC13");
|
2024-06-04 09:54:09 +08:00
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
|
2024-06-03 18:59:42 +08:00
|
|
|
|
ZC13("ZC05");
|
2024-06-04 09:54:09 +08:00
|
|
|
|
//_printTaskStackHighWaterMark("ZC13");
|
2024-06-04 15:45:40 +08:00
|
|
|
|
watchdog_update(); // 喂狗
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
|
2024-06-03 16:27:41 +08:00
|
|
|
|
}
|
2024-06-04 09:54:09 +08:00
|
|
|
|
}
|