micro_climate/Drivers/HP203B/hp203b.c

94 lines
3.1 KiB
C
Raw Normal View History

#include "hp203b.h"
#include "i2c.h"
#include "pdebug.h"
#include "assertions.h"
#include "filter.h"
#include "cmsis_os.h"
uint8_t hp20X_SOFT_RST = 0x06; //<2F><><EFBFBD><EFBFBD>λ
uint8_t hp20X_READ_P = 0x30; //ѹ<><D1B9>
uint8_t hp20X_READ_A = 0x31; //<2F>߶<EFBFBD>
uint8_t hp20X_READ_T = 0x32; //<2F><EFBFBD>
uint8_t hp20X_READ_PT = 0x10; //<2F><EFBFBD>ѹ<EFBFBD><D1B9>
uint8_t hp20X_READ_AT = 0x11; //<2F>¶Ⱥ<C2B6><C8BA><EFBFBD>
/****************************
*<EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>HP203_soft_reset
*<EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>hp203<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λָ<EFBFBD><EFBFBD>
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*<EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD>
*****************************/
void HP203_soft_reset(void)
{
HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, &hp20X_SOFT_RST, 1, 0xffff);//0xec,0x06----<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
osDelay(30); //<2F><>ʱ20MS thread<61><64><EFBFBD><EFBFBD>+10
}
/****************************
*<EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>Hp203bReadPressureTemperature
*<EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ѹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ρ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Press--<EFBFBD><EFBFBD>ѹֵ
* Altitude--<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Temp--<EFBFBD><EFBFBD>
*<EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD>
*****************************/
void Hp203bReadPressureTemperature(double *Hp203bPressure,double *Hp203bAltitude,double *Hp203bTemp)
{
uint8_t Osr_Cfg = HP20X_CONVERT_OSR1024; //0x08
uint8_t DataBuf[6];
long Hp203b_Temp;
long Hp203b_Pressure;
long Hp203b_Altitude;
uint8_t status = 0xFF;
uint8_t command = HP20X_WR_CONVERT_CMD | Osr_Cfg;
status = HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, &command, 1, 0xffff);
// while(status != HAL_OK){}
if(HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, &command, 1, 0xffff) == HAL_OK) //0x40 | 0x08 = 0x48----010: fixed; 010: OSR = 1024; 00: sensor pressure and temperature channel;
{
osDelay(74); //<2F><>ʱ64MS <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD>OSRֵ<52><D6B5><EFBFBD><EFBFBD> thread<61><64><EFBFBD><EFBFBD>+10
HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, &hp20X_READ_PT, 1, 0xffff); //<2F><><EFBFBD>0x10----<2D><>ȡ<EFBFBD>¶Ⱥ<C2B6>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
HAL_I2C_Master_Receive(&hi2c3, HP20X_ADDRESSCMD, DataBuf, 6, 0xffff); //<2F><>ȡ6<C8A1><36><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>ݣ<EFBFBD>3<EFBFBD><33><EFBFBD>ֽڵ<D6BD><DAB5><EFBFBD><C2B6><EFBFBD><EFBFBD>ݺ<EFBFBD>3<EFBFBD><33><EFBFBD>ֽڵ<D6BD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Hp203b_Temp = DataBuf[0]; //<2F><>3<EFBFBD><33><EFBFBD>ֽڵ<D6BD><DAB5><EFBFBD><C2B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϳ<EFBFBD>24λ<34><CEBB><EFBFBD><EFBFBD><C2B6><EFBFBD><EFBFBD><EFBFBD>
Hp203b_Temp <<= 8;
Hp203b_Temp |= DataBuf[1];
Hp203b_Temp <<= 8;
Hp203b_Temp |= DataBuf[2];
if(Hp203b_Temp > 0x7FFFFF) //<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>ֵ
Hp203b_Temp = Hp203b_Temp - 16777216;
*Hp203bTemp = (double)Hp203b_Temp / 100; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><C2B6><EFBFBD><EFBFBD><EFBFBD>
Hp203b_Pressure = DataBuf[3]; //<2F><>3<EFBFBD><33><EFBFBD>ֽڵ<D6BD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϳ<EFBFBD>24λ<34><CEBB>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Hp203b_Pressure <<= 8;
Hp203b_Pressure |= DataBuf[4];
Hp203b_Pressure <<= 8;
Hp203b_Pressure |= DataBuf[5];
*Hp203bPressure = (double)Hp203b_Pressure / 100; //<2F><><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, &hp20X_READ_A, 1, 0xffff); //<2F><><EFBFBD>0x31----<2D><>ȡ<EFBFBD>߶<EFBFBD><DFB6><EFBFBD><EFBFBD><EFBFBD>
HAL_I2C_Master_Receive(&hi2c3, HP20X_ADDRESSCMD, DataBuf, 3, 0xffff); //<2F><>ȡ3<C8A1><33><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>ݣ<EFBFBD>3<EFBFBD><33><EFBFBD>ֽڵĸ߶<C4B8><DFB6><EFBFBD><EFBFBD><EFBFBD>
Hp203b_Altitude = DataBuf[0]; //<2F><>3<EFBFBD><33><EFBFBD>ֽڵĸ߶<C4B8><DFB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϳ<EFBFBD>24λ<34>ĸ߶<C4B8><DFB6><EFBFBD><EFBFBD><EFBFBD>
Hp203b_Altitude <<= 8;
Hp203b_Altitude |= DataBuf[1];
Hp203b_Altitude <<= 8;
Hp203b_Altitude |= DataBuf[2];
if(Hp203b_Altitude > 0x7FFFFF) //<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>ֵ
Hp203b_Altitude = Hp203b_Altitude - 16777216;
*Hp203bAltitude = (double)Hp203b_Altitude / 100; //<2F><><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><DFB6><EFBFBD><EFBFBD><EFBFBD>
}
}