Files

88 lines
1.6 KiB
C
Raw Permalink Normal View History

2024-09-26 22:32:20 +08:00
/*
* Baro_MS4525DO.c
*
* Created on: Jun 22, 2020
* Author: matth
*/
#include "Baro_MS4525DO.h"
bool Baro_MS4525_init(Baro_MS4525_t *baro, const char *name, I2C_DEV_t *dev, int Pmax, int Pmin, Baro_MS4525_type typeAB)
{
baro->name = name;
baro->_dev = dev;
baro->Prange = 6895*(Pmax-Pmin);
baro->Pmin = 6895*Pmin;
baro->typeAB = typeAB;
baro->status = 0u;
return true;
}
bool Baro_MS4525_update(Baro_MS4525_t *baro)
{
bool rslt;
rslt = false;
uint8_t buff[4];
if (!I2C_DEV_begin(baro->_dev,1)) {
baro->status = 1u;
return rslt;
}
if (I2C_DEV_receive(baro->_dev, buff, 4))
{
uint16_t bridge_data = ((buff[0] & 0x3F) << 8) + buff[1];
uint8_t status = ((buff[0] & 0xC0) >> 6);
uint16_t temp = (buff[2] << 3) + ((buff[3] & 0xE) >> 5);
if (status == 0x00)
{
switch (baro->typeAB)
{
case Baro_MS4525_typeA:
if (bridge_data<0x666 || bridge_data>0x399A)
{
status = 3;
}
else
{
baro->pressure = (bridge_data-0x666)*(int64_t)(baro->Prange)/(0x399A-0x666)+baro->Pmin;
}
break;
default:
if (bridge_data<0x333 || bridge_data>0x3CCB)
{
status = 3;
}
else
{
baro->pressure = (bridge_data-0x333)*(int64_t)(baro->Prange)/(0x3CCB-0x333)+baro->Pmin;
}
break;
}
baro->temp = temp*200u/2047u-50;
if (status == 0)
{
baro->status = 0;
rslt = true;
}
else
{
baro->status = status;
}
}
else
{
baro->status = (status<<2);
}
}
else
{
baro->status = 2u;
}
I2C_DEV_end(baro->_dev);
return rslt;
}