Gradually migrate to FreeRTOS

This commit is contained in:
aixiao 2024-06-04 09:54:09 +08:00
parent db774bfd76
commit 92c0ee6bd8
15 changed files with 143 additions and 161 deletions

View File

@ -1,14 +1,16 @@
# 基于 Raspberry Pico / Pico W 的厨房危险(火灾)报警 Pico W WiFi传输暂时不玩 # 基于 Raspberry Pico / Pico W 的厨房危险(火灾)报警 Pico W WiFi传输暂时不玩
* 使用 DS18B20温度传感器
* 使用 CH4 N55A甲烷气体传感器(进口) (弃用)
* 使用 PASCO2V01 CO2二氧化碳传感器模块(进口模块暂时买不到!)
* 使用 MH-Z14B CO2二氧化碳传感器模块(国产)(0 - 5000ppm)
* 使用 ZE07-CO CO一氧化碳传感器模块(国产)
* 使用 HC-12 433MHZ传感器模块(国产)
* 使用 ZC05/ZC13 可燃气体(天然气 CH4)传感器模块(国产)
## Build * 使用 DS18B20温度传感器
``` * 使用 CH4 N55A甲烷气体传感器(进口) (弃用)
* 使用 PASCO2V01 CO2二氧化碳传感器模块(进口模块暂时买不到!)
* 使用 MH-Z14B CO2二氧化碳传感器模块(国产)(0 - 5000ppm)
* 使用 ZE07-CO CO一氧化碳传感器模块(国产)
* 使用 HC-12 433MHZ传感器模块(国产)
* 使用 ZC05/ZC13 可燃气体(天然气 CH4)传感器模块(国产)
## 源码构建
```bash
# 使用WSL Debian GNU/Linux 12 (bookworm) 构建 # 使用WSL Debian GNU/Linux 12 (bookworm) 构建
# 确保Pico-SDK环境变量 # 确保Pico-SDK环境变量
export PICO_SDK_PATH=/mnt/c/Users/niuyuling/Desktop/raspberry-pico/SDK/pico-sdk export PICO_SDK_PATH=/mnt/c/Users/niuyuling/Desktop/raspberry-pico/SDK/pico-sdk
@ -29,7 +31,7 @@
# 树莓派Zero W 433MHZ HC-12接收服务端构建 # 树莓派Zero W 433MHZ HC-12接收服务端构建
apt install libmysqlclient-dev apt install libmysqlclient-dev
cd ~ cd ~
git clone https://github.com/WiringPi/WiringPi.git git clone https://github.com/WiringPi/WiringPi.git
cd WiringPi cd WiringPi
@ -39,7 +41,8 @@
./hc-12 ./hc-12
``` ```
## display
## 展示
![brief](HARDWARE/IMG/db.jpg) ![brief](HARDWARE/IMG/db.jpg)
![brief](HARDWARE/IMG/display.png) ![brief](HARDWARE/IMG/display.png)

View File

@ -15,14 +15,12 @@ void DS18B20(void *pvParameters)
_printTaskStackHighWaterMark("DS18B20"); _printTaskStackHighWaterMark("DS18B20");
while (1) while (1) {
{
one_wire.single_device_read_rom(address); one_wire.single_device_read_rom(address);
one_wire.convert_temperature(address, true, false); one_wire.convert_temperature(address, true, false);
TEMPERATURE = one_wire.temperature(address); TEMPERATURE = one_wire.temperature(address);
//printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x Temperature: %3.1f°C\n", address.rom[0], address.rom[1], address.rom[2], address.rom[3], printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x Temperature: %3.1f°C\n", address.rom[0], address.rom[1], address.rom[2], address.rom[3], address.rom[4], address.rom[5], address.rom[6], address.rom[7], one_wire.temperature(address));
// address.rom[4], address.rom[5], address.rom[6], address.rom[7], one_wire.temperature(address));
// 发送数据到队列 // 发送数据到队列
xQueueSend(xQueue, &TEMPERATURE, portMAX_DELAY); xQueueSend(xQueue, &TEMPERATURE, portMAX_DELAY);
@ -32,5 +30,5 @@ void DS18B20(void *pvParameters)
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时 vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
} }
return ; return;
} }

View File

@ -15,7 +15,6 @@
#define DS18B20_PIN 15 // DS18B20 引脚 #define DS18B20_PIN 15 // DS18B20 引脚
extern QueueHandle_t xQueue; extern QueueHandle_t xQueue;
extern void DS18B20(void *pvParameters); extern void DS18B20(void *pvParameters);

View File

@ -7,8 +7,8 @@
#include "uart_rx.pio.h" #include "uart_rx.pio.h"
#define HC_12_PIO pio1 #define HC_12_PIO pio1
#define HC_12_PIO_TX_PIN 16 // 接 HC-12 RX #define HC_12_PIO_TX_PIN 16 // 接 HC-12 RX
#define HC_12_PIO_RX_PIN 15 // 接 HC-12 TX #define HC_12_PIO_RX_PIN 15 // 接 HC-12 TX
#define HC_12_PIO_SM_TX 0 #define HC_12_PIO_SM_TX 0
#define HC_12_PIO_SM_RX 1 #define HC_12_PIO_SM_RX 1
#define HC_12_PIO_SERIAL_BAUD 9600 #define HC_12_PIO_SERIAL_BAUD 9600

View File

@ -1,7 +1,6 @@
#include "MHZ14B.hpp" #include "MHZ14B.hpp"
#include "common.hpp" #include "common.hpp"
void MH_Z14B_INIT() void MH_Z14B_INIT()
{ {
// 初始化UART // 初始化UART
@ -16,7 +15,6 @@ void MH_Z14B_INIT()
static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK) static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK)
{ {
// 0x86 读气体浓度值 // 0x86 读气体浓度值
uint8_t CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 }; uint8_t CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
uart_write_blocking(UART1, CMD, 9); uart_write_blocking(UART1, CMD, 9);
@ -61,14 +59,12 @@ void CO2(void *pvParameters)
MH_Z14B_INIT(); MH_Z14B_INIT();
_printTaskStackHighWaterMark("MH_Z14B"); _printTaskStackHighWaterMark("MH_Z14B");
while(1) while (1) {
{
CO2_DATA = MH_Z14B(&MH_Z14B_DATA_IS_OK); CO2_DATA = MH_Z14B(&MH_Z14B_DATA_IS_OK);
if (CO2_DATA != -1 && MH_Z14B_DATA_IS_OK == 1) { if (CO2_DATA != -1 && MH_Z14B_DATA_IS_OK == 1) {
printf("CO2 Concentration: %d ppm\n", CO2_DATA); printf("CO2 Concentration: %d ppm\n", CO2_DATA);
} }
//_printTaskStackHighWaterMark("MH_Z14B");
_printTaskStackHighWaterMark("MH_Z14B");
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时 vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
} }
} }

View File

@ -12,26 +12,28 @@ int ZC13_INIT()
return 0; return 0;
} }
int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) { int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
{
for (int i = 0; i < DATA_LEN; i++) { for (int i = 0; i < DATA_LEN; i++) {
uart_tx_program_putc(pio, sm, DATA[i]); uart_tx_program_putc(pio, sm, DATA[i]);
sleep_ms(3); sleep_ms(3);
} }
return 0; return 0;
} }
int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) { int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
{
char c = '\0'; char c = '\0';
int received_count = 0; int received_count = 0;
int timeout_ms = 100; // 设置较长的超时时间 int timeout_ms = 100; // 设置较长的超时时间
while (received_count < DATA_LEN && timeout_ms > 0) { while (received_count < DATA_LEN && timeout_ms > 0) {
if (uart_rx_program_available(pio, sm)) { if (uart_rx_program_available(pio, sm)) {
c = uart_rx_program_getc(pio, sm); c = uart_rx_program_getc(pio, sm);
DATA[received_count++] = c; DATA[received_count++] = c;
printf("0x%X ", c); //printf("0x%X ", c);
if (c == '\n') { if (c == '\n') {
// 接收到换行符,停止接收数据 // 接收到换行符,停止接收数据
break; break;
@ -58,15 +60,14 @@ int ZC13(const char *model)
// 发送指令 // 发送指令
uint8_t CH4_CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 }; uint8_t CH4_CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
ZC13_PIO_UART_TX_DATA(ZC13_PIO, ZC13_PIO_SM_TX, CH4_CMD, sizeof(CH4_CMD)); ZC13_PIO_UART_TX_DATA(ZC13_PIO, ZC13_PIO_SM_TX, CH4_CMD, sizeof(CH4_CMD));
sleep_ms(200); sleep_ms(200);
// 接收指令 // 接收指令
CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9); CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9);
//printf("\n");
printf("\n"); // printf("%d\n", CH4_DATA_LEN);
printf("%d\n", CH4_DATA_LEN);
uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节 uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节
uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节 uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节
@ -77,23 +78,23 @@ int ZC13(const char *model)
printf("CH4 sensor malfunction!\n"); printf("CH4 sensor malfunction!\n");
} else { } else {
if (CH4_DATA[1] == 0X86) { if (CH4_DATA[1] == 0X86) {
if ( 0 == strcasecmp(model, "ZC05")) { if (0 == strcasecmp(model, "ZC05")) {
// 计算气体浓度值 // 计算气体浓度值
uint16_t gasConcentration = (highByte & 0x3F) * 256 + lowByte; uint16_t gasConcentration = (highByte & 0x3F) * 256 + lowByte;
// 输出气体浓度值 // 输出气体浓度值
printf("CH4 Concentration: %uppm\n", gasConcentration); printf("CH4 Concentration: %uppm\n", gasConcentration);
return gasConcentration; return gasConcentration;
} }
if ( 0 == strcasecmp(model, "ZC13")) { if (0 == strcasecmp(model, "ZC13")) {
// 计算气体浓度值 // 计算气体浓度值
uint16_t gasConcentration = (highByte & 0x1F) * 256 + lowByte; uint16_t gasConcentration = (highByte & 0x1F) * 256 + lowByte;
// 输出气体浓度值 // 输出气体浓度值
printf("CH4 Concentration: %uppm\n", gasConcentration); printf("CH4 Concentration: %uppm\n", gasConcentration);
return gasConcentration; return gasConcentration;
} }
} }
} }
@ -104,11 +105,11 @@ void CH4(void *pvParameters)
{ {
ZC13_INIT(); ZC13_INIT();
_printTaskStackHighWaterMark("ZC13"); _printTaskStackHighWaterMark("ZC13");
while(1)
{ while (1) {
ZC13("ZC05"); ZC13("ZC05");
_printTaskStackHighWaterMark("ZC13"); //_printTaskStackHighWaterMark("ZC13");
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时 vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
} }
} }

View File

@ -15,8 +15,8 @@
#include "uart_rx.pio.h" #include "uart_rx.pio.h"
#define ZC13_PIO pio0 #define ZC13_PIO pio0
#define ZC13_PIO_TX_PIN 19 // 接ZC13 4 TX PIN #define ZC13_PIO_TX_PIN 19 // 接ZC13 4 TX PIN
#define ZC13_PIO_RX_PIN 20 // 接ZC13 5 RX PIN #define ZC13_PIO_RX_PIN 20 // 接ZC13 5 RX PIN
#define ZC13_PIO_SM_TX 0 #define ZC13_PIO_SM_TX 0
#define ZC13_PIO_SM_RX 1 #define ZC13_PIO_SM_RX 1
#define ZC13_PIO_SERIAL_BAUD 9600 #define ZC13_PIO_SERIAL_BAUD 9600

View File

@ -34,31 +34,31 @@ void vApplicationTickHook(void)
/* The full demo includes a software timer demo/test that requires /* The full demo includes a software timer demo/test that requires
prodding periodically from the tick interrupt. */ prodding periodically from the tick interrupt. */
/*完整的演示包括软件定时器演示/测试,需要 /*完整的演示包括软件定时器演示/测试,需要
*/ */
#if (mainENABLE_TIMER_DEMO == 1) #if (mainENABLE_TIMER_DEMO == 1)
vTimerPeriodicISRTests(); vTimerPeriodicISRTests();
#endif #endif
/* Call the periodic queue overwrite from ISR demo. */ /* Call the periodic queue overwrite from ISR demo. */
/*调用ISR演示中的周期性队列覆盖*/ /*调用ISR演示中的周期性队列覆盖 */
#if (mainENABLE_QUEUE_OVERWRITE == 1) #if (mainENABLE_QUEUE_OVERWRITE == 1)
vQueueOverwritePeriodicISRDemo(); vQueueOverwritePeriodicISRDemo();
#endif #endif
/* Call the periodic event group from ISR demo. */ /* Call the periodic event group from ISR demo. */
/*从ISR演示中调用定期事件组*/ /*从ISR演示中调用定期事件组 */
#if (mainENABLE_EVENT_GROUP == 1) #if (mainENABLE_EVENT_GROUP == 1)
vPeriodicEventGroupsProcessing(); vPeriodicEventGroupsProcessing();
#endif #endif
/* Call the code that uses a mutex from an ISR. */ /* Call the code that uses a mutex from an ISR. */
/*从ISR调用使用互斥的代码*/ /*从ISR调用使用互斥的代码 */
#if (mainENABLE_INTERRUPT_SEMAPHORE == 1) #if (mainENABLE_INTERRUPT_SEMAPHORE == 1)
vInterruptSemaphorePeriodicTest(); vInterruptSemaphorePeriodicTest();
#endif #endif
/* Call the code that 'gives' a task notification from an ISR. */ /* Call the code that 'gives' a task notification from an ISR. */
/*调用从ISR“发出”任务通知的代码*/ /*调用从ISR“发出”任务通知的代码 */
#if (mainENABLE_TASK_NOTIFY == 1) #if (mainENABLE_TASK_NOTIFY == 1)
xNotifyTaskFromISR(); xNotifyTaskFromISR();
#endif #endif
@ -66,16 +66,12 @@ void vApplicationTickHook(void)
#endif #endif
} }
void _printTaskStackHighWaterMark(const char *task_name) void _printTaskStackHighWaterMark(const char *task_name)
{ {
TaskHandle_t currentTask = xTaskGetCurrentTaskHandle(); TaskHandle_t currentTask = xTaskGetCurrentTaskHandle();
if (currentTask != NULL) if (currentTask != NULL) {
{
printf("%s TASK STACK HIGH WATER MARK: %ld\n", task_name, uxTaskGetStackHighWaterMark(currentTask)); printf("%s TASK STACK HIGH WATER MARK: %ld\n", task_name, uxTaskGetStackHighWaterMark(currentTask));
} } else {
else
{
printf("FAILED TO GET CURRENT TASK HANDLE.\n"); printf("FAILED TO GET CURRENT TASK HANDLE.\n");
} }
} }

View File

@ -10,7 +10,6 @@
#include <stdio.h> #include <stdio.h>
#include "hardware/pio.h" #include "hardware/pio.h"
#define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0) #define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
static inline bool uart_rx_program_available(PIO pio, uint sm) static inline bool uart_rx_program_available(PIO pio, uint sm)

View File

@ -19,8 +19,8 @@
#include "MHZ14B.hpp" #include "MHZ14B.hpp"
#ifndef PICO_DEFAULT_LED_PIN #ifndef PICO_DEFAULT_LED_PIN
#warning pio/hello_pio example requires a board with a regular LED #warning pio/hello_pio example requires a board with a regular LED
#define PICO_DEFAULT_LED_PIN 25 #define PICO_DEFAULT_LED_PIN 25
#endif #endif
void Led_Blinky(void *pvParameters) void Led_Blinky(void *pvParameters)
@ -47,7 +47,6 @@ void Led_Blinky(void *pvParameters)
// 处理接收到的数据 // 处理接收到的数据
printf("%f\n", TEMPERATURE); printf("%f\n", TEMPERATURE);
} }
//_printTaskStackHighWaterMark("Led_Blinky"); //_printTaskStackHighWaterMark("Led_Blinky");
} }
@ -83,11 +82,9 @@ int main(void)
sleep_ms(3000); sleep_ms(3000);
//set_sys_clock_khz(250000, true); //set_sys_clock_khz(250000, true);
// 创建队列 // 创建队列
xQueue = xQueueCreate(10, sizeof(long)); xQueue = xQueueCreate(10, sizeof(long));
// 创建任务 // 创建任务
BaseType_t xReturned; BaseType_t xReturned;
TaskHandle_t Led_Blinky_xHandle = NULL; TaskHandle_t Led_Blinky_xHandle = NULL;
@ -97,31 +94,25 @@ int main(void)
// 板载LED闪烁 // 板载LED闪烁
xReturned = xTaskCreate(Led_Blinky, "Blinky task", 512, NULL, tskIDLE_PRIORITY, &Led_Blinky_xHandle); xReturned = xTaskCreate(Led_Blinky, "Blinky task", 512, NULL, tskIDLE_PRIORITY, &Led_Blinky_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
{
printf("Blinky Task Error!"); printf("Blinky Task Error!");
} }
// DS18B20 // DS18B20
xReturned = xTaskCreate(DS18B20, "DS18B20 task", 1024, NULL, tskIDLE_PRIORITY, &DS18B20_xHandle); xReturned = xTaskCreate(DS18B20, "DS18B20 task", 1024, NULL, tskIDLE_PRIORITY, &DS18B20_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
{
printf("DS18B20() Task Error!"); printf("DS18B20() Task Error!");
} }
// CH4 // CH4
xReturned = xTaskCreate(CH4, "CH4 task", 1024, NULL, tskIDLE_PRIORITY, &CH4_xHandle); xReturned = xTaskCreate(CH4, "CH4 task", 1024, NULL, tskIDLE_PRIORITY, &CH4_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
{
printf("CH4() Task Error!"); printf("CH4() Task Error!");
} }
// CO2 // CO2
xReturned = xTaskCreate(CO2, "CO2 task", 1024, NULL, tskIDLE_PRIORITY, &CO2_xHandle); xReturned = xTaskCreate(CO2, "CO2 task", 1024, NULL, tskIDLE_PRIORITY, &CO2_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
{
printf("CO2() Task Error!"); printf("CO2() Task Error!");
} }
vTaskStartScheduler(); vTaskStartScheduler();
return 0; return 0;

View File

@ -7,8 +7,8 @@
#include "uart_rx.pio.h" #include "uart_rx.pio.h"
#define HC_12_PIO pio1 #define HC_12_PIO pio1
#define HC_12_PIO_TX_PIN 16 // 接 HC-12 RX #define HC_12_PIO_TX_PIN 16 // 接 HC-12 RX
#define HC_12_PIO_RX_PIN 15 // 接 HC-12 TX #define HC_12_PIO_RX_PIN 15 // 接 HC-12 TX
#define HC_12_PIO_SM_TX 0 #define HC_12_PIO_SM_TX 0
#define HC_12_PIO_SM_RX 1 #define HC_12_PIO_SM_RX 1
#define HC_12_PIO_SERIAL_BAUD 9600 #define HC_12_PIO_SERIAL_BAUD 9600

View File

@ -297,8 +297,6 @@ int main()
} }
watchdog_update(); // 喂狗 watchdog_update(); // 喂狗
// CH4 // CH4
FIFO_ = multicore_fifo_pop_blocking(); FIFO_ = multicore_fifo_pop_blocking();
if (FIFO_ == 1) { if (FIFO_ == 1) {
@ -326,21 +324,21 @@ int main()
} }
//_433_MHZ(addDigit(TEMPERATURE, SIGN_DS18B20)); //_433_MHZ(addDigit(TEMPERATURE, SIGN_DS18B20));
if (TEMPERATURE != 85) { if (TEMPERATURE != 85) {
char TEMPERATURE_TEMP[BUFER] = { 0 }; char TEMPERATURE_TEMP[BUFER] = { 0 };
sprintf(TEMPERATURE_TEMP, "Temperature: %.3f°C\n", TEMPERATURE); sprintf(TEMPERATURE_TEMP, "Temperature: %.3f°C\n", TEMPERATURE);
_HC_12(TEMPERATURE_TEMP); _HC_12(TEMPERATURE_TEMP);
TEMPERATURE = -1; TEMPERATURE = -1;
} }
} }
watchdog_update(); // 喂狗 watchdog_update(); // 喂狗
// ZC13 CH4 传感器 // ZC13 CH4 传感器
char CH4_DATA[BUFER] = { 0 }; char CH4_DATA[BUFER] = { 0 };
sprintf(CH4_DATA, "CH4 Concentration: %d\n", ZC13("ZC05")); sprintf(CH4_DATA, "CH4 Concentration: %d\n", ZC13("ZC05"));
_HC_12(CH4_DATA); _HC_12(CH4_DATA);
watchdog_update(); // 喂狗 watchdog_update(); // 喂狗
// ME2_CO CO // ME2_CO CO

View File

@ -43,7 +43,7 @@
#define UART1_RX_PIN 4 // MH-Z14B R #define UART1_RX_PIN 4 // MH-Z14B R
#define UART0 uart0 #define UART0 uart0
#define UART0_TX_PIN 1 // #define UART0_TX_PIN 1 //
#define UART0_RX_PIN 0 // #define UART0_RX_PIN 0 //
#endif #endif

View File

@ -12,26 +12,28 @@ int ZC13_INIT()
return 0; return 0;
} }
int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) { int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
{
for (int i = 0; i < DATA_LEN; i++) { for (int i = 0; i < DATA_LEN; i++) {
uart_tx_program_putc(pio, sm, DATA[i]); uart_tx_program_putc(pio, sm, DATA[i]);
sleep_ms(3); sleep_ms(3);
} }
return 0; return 0;
} }
int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) { int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
{
char c = '\0'; char c = '\0';
int received_count = 0; int received_count = 0;
int timeout_ms = 100; // 设置较长的超时时间 int timeout_ms = 100; // 设置较长的超时时间
while (received_count < DATA_LEN && timeout_ms > 0) { while (received_count < DATA_LEN && timeout_ms > 0) {
if (uart_rx_program_available(pio, sm)) { if (uart_rx_program_available(pio, sm)) {
c = uart_rx_program_getc(pio, sm); c = uart_rx_program_getc(pio, sm);
DATA[received_count++] = c; DATA[received_count++] = c;
printf("0x%X ", c); printf("0x%X ", c);
if (c == '\n') { if (c == '\n') {
// 接收到换行符,停止接收数据 // 接收到换行符,停止接收数据
break; break;
@ -58,15 +60,14 @@ int ZC13(const char *model)
// 发送指令 // 发送指令
uint8_t CH4_CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 }; uint8_t CH4_CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
ZC13_PIO_UART_TX_DATA(ZC13_PIO, ZC13_PIO_SM_TX, CH4_CMD, sizeof(CH4_CMD)); ZC13_PIO_UART_TX_DATA(ZC13_PIO, ZC13_PIO_SM_TX, CH4_CMD, sizeof(CH4_CMD));
sleep_ms(200); sleep_ms(200);
// 接收指令 // 接收指令
CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9); CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9);
printf("\n"); printf("\n");
printf("%d\n", CH4_DATA_LEN); printf("%d\n", CH4_DATA_LEN);
uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节 uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节
uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节 uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节
@ -77,23 +78,23 @@ int ZC13(const char *model)
printf("CH4 sensor malfunction!\n"); printf("CH4 sensor malfunction!\n");
} else { } else {
if (CH4_DATA[1] == 0X86) { if (CH4_DATA[1] == 0X86) {
if ( 0 == strcasecmp(model, "ZC05")) { if (0 == strcasecmp(model, "ZC05")) {
// 计算气体浓度值 // 计算气体浓度值
uint16_t gasConcentration = (highByte & 0x3F) * 256 + lowByte; uint16_t gasConcentration = (highByte & 0x3F) * 256 + lowByte;
// 输出气体浓度值 // 输出气体浓度值
printf("CH4 Concentration: %uppm\n", gasConcentration); printf("CH4 Concentration: %uppm\n", gasConcentration);
return gasConcentration; return gasConcentration;
} }
if ( 0 == strcasecmp(model, "ZC13")) { if (0 == strcasecmp(model, "ZC13")) {
// 计算气体浓度值 // 计算气体浓度值
uint16_t gasConcentration = (highByte & 0x1F) * 256 + lowByte; uint16_t gasConcentration = (highByte & 0x1F) * 256 + lowByte;
// 输出气体浓度值 // 输出气体浓度值
printf("CH4 Concentration: %uppm\n", gasConcentration); printf("CH4 Concentration: %uppm\n", gasConcentration);
return gasConcentration; return gasConcentration;
} }
} }
} }

View File

@ -9,8 +9,8 @@
#include "uart_rx.pio.h" #include "uart_rx.pio.h"
#define ZC13_PIO pio0 #define ZC13_PIO pio0
#define ZC13_PIO_TX_PIN 19 // 接ZC13 4 TX PIN #define ZC13_PIO_TX_PIN 19 // 接ZC13 4 TX PIN
#define ZC13_PIO_RX_PIN 20 // 接ZC13 5 RX PIN #define ZC13_PIO_RX_PIN 20 // 接ZC13 5 RX PIN
#define ZC13_PIO_SM_TX 0 #define ZC13_PIO_SM_TX 0
#define ZC13_PIO_SM_RX 1 #define ZC13_PIO_SM_RX 1
#define ZC13_PIO_SERIAL_BAUD 9600 #define ZC13_PIO_SERIAL_BAUD 9600