From 8c78f265c6af0618d30c74066d57251b8b7f8478 Mon Sep 17 00:00:00 2001 From: kkira Date: Thu, 18 Jun 2026 00:00:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D1=80=D0=B0=D0=B9=D0=B2=D0=B5=D1=80=20MH?= =?UTF-8?q?-Z19C=20=D1=81=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8=20=D0=BF=D0=BE=D0=BA=D0=B0=D0=B7=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20CO2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STM32_CO2_NODE/App/Models/co2_data.hpp | 3 + STM32_CO2_NODE/Core/Src/freertos.c | 3 +- STM32_CO2_NODE/Drivers/MHZ19C/mhz19c.cpp | 72 +++++++++++++++++++++++- STM32_CO2_NODE/Drivers/MHZ19C/mhz19c.hpp | 23 +++++++- 4 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 STM32_CO2_NODE/App/Models/co2_data.hpp diff --git a/STM32_CO2_NODE/App/Models/co2_data.hpp b/STM32_CO2_NODE/App/Models/co2_data.hpp new file mode 100644 index 0000000..0fb5c45 --- /dev/null +++ b/STM32_CO2_NODE/App/Models/co2_data.hpp @@ -0,0 +1,3 @@ +void test_cpp() +{ +} diff --git a/STM32_CO2_NODE/Core/Src/freertos.c b/STM32_CO2_NODE/Core/Src/freertos.c index 8eb7ba5..9abe1d8 100644 --- a/STM32_CO2_NODE/Core/Src/freertos.c +++ b/STM32_CO2_NODE/Core/Src/freertos.c @@ -26,7 +26,8 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "queue.h" -#include "co2_data.hpp" + +#include "../../App/Models/co2_data.hpp" /* USER CODE END Includes */ diff --git a/STM32_CO2_NODE/Drivers/MHZ19C/mhz19c.cpp b/STM32_CO2_NODE/Drivers/MHZ19C/mhz19c.cpp index b3a5689..d5e913d 100644 --- a/STM32_CO2_NODE/Drivers/MHZ19C/mhz19c.cpp +++ b/STM32_CO2_NODE/Drivers/MHZ19C/mhz19c.cpp @@ -1,5 +1,75 @@ #include "mhz19c.hpp" -MHZ19C::MHZ19C() +extern "C" +{ +#include "cmsis_os.h" +} + +MHZ19C::MHZ19C(UART_HandleTypeDef* huart) + : uart_(huart) { } + + +bool MHZ19C::ReadCO2(uint16_t& ppm) +{ + const uint8_t cmd[9] = + { + 0xFF, + 0x01, + 0x86, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x79 }; + + if (HAL_UART_Transmit( + uart_, + const_cast(cmd), + sizeof(cmd), + 100) != HAL_OK) + { + return false; } + + if (HAL_UART_Receive_DMA( + uart_, + rxBuffer_, + 9) != HAL_OK) + { + return false; } + + osDelay(50); + + if (rxBuffer_[0] != 0xFF || + rxBuffer_[1] != 0x86) + { + return false; } + + ppm = + (static_cast(rxBuffer_[2]) << 8) | + static_cast(rxBuffer_[3]); + + return true; } + + +bool MHZ19C::Calibrate() +{ + const uint8_t cmd[9] = + { + 0xFF, + 0x01, + 0x87, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x78 }; + + return HAL_UART_Transmit( + uart_, + (uint8_t*)cmd, + sizeof(cmd), + 100) == HAL_OK; } diff --git a/STM32_CO2_NODE/Drivers/MHZ19C/mhz19c.hpp b/STM32_CO2_NODE/Drivers/MHZ19C/mhz19c.hpp index 7545731..6ef8347 100644 --- a/STM32_CO2_NODE/Drivers/MHZ19C/mhz19c.hpp +++ b/STM32_CO2_NODE/Drivers/MHZ19C/mhz19c.hpp @@ -1,7 +1,26 @@ #pragma once +#include + +extern "C" +{ +#include "usart.h" +} + + class MHZ19C { public: - MHZ19C(); -}; + + explicit MHZ19C(UART_HandleTypeDef* huart); + + bool ReadCO2(uint16_t& ppm); + + bool Calibrate(); + +private: + + UART_HandleTypeDef* uart_; + + uint8_t txBuffer_[9]; + uint8_t rxBuffer_[9]; };