92 lines
1.3 KiB
C++
92 lines
1.3 KiB
C++
#include "mhz19c.hpp"
|
|
|
|
MHZ19C::MHZ19C(UART_HandleTypeDef* huart)
|
|
: uart(huart)
|
|
{
|
|
BuildReadFrame();
|
|
}
|
|
|
|
void MHZ19C::BuildReadFrame()
|
|
{
|
|
txFrame[0] = 0xFF;
|
|
txFrame[1] = 0x01;
|
|
txFrame[2] = 0x86;
|
|
txFrame[3] = 0x00;
|
|
txFrame[4] = 0x00;
|
|
txFrame[5] = 0x00;
|
|
txFrame[6] = 0x00;
|
|
txFrame[7] = 0x00;
|
|
txFrame[8] = Checksum(txFrame);
|
|
}
|
|
|
|
uint8_t MHZ19C::Checksum(uint8_t* data)
|
|
{
|
|
uint8_t sum = 0;
|
|
|
|
for(int i = 1; i < 8; i++)
|
|
{
|
|
sum += data[i];
|
|
}
|
|
|
|
return 0xFF - sum + 1;
|
|
}
|
|
|
|
bool MHZ19C::ReadCO2(uint16_t& ppm)
|
|
{
|
|
if(HAL_UART_Transmit(
|
|
uart,
|
|
txFrame,
|
|
9,
|
|
100)
|
|
!= HAL_OK)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if(HAL_UART_Receive(
|
|
uart,
|
|
rxFrame,
|
|
9,
|
|
100)
|
|
!= HAL_OK)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if(rxFrame[0] != 0xFF)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
ppm =
|
|
(rxFrame[2] << 8) |
|
|
rxFrame[3];
|
|
|
|
return true;
|
|
}
|
|
|
|
void MHZ19C::CalibrateZero()
|
|
{
|
|
uint8_t cmd[9] =
|
|
{
|
|
0xFF,
|
|
0x01,
|
|
0x87,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
0
|
|
};
|
|
|
|
cmd[8] = Checksum(cmd);
|
|
|
|
HAL_UART_Transmit(
|
|
uart,
|
|
cmd,
|
|
9,
|
|
100
|
|
);
|
|
}
|