void BSP_ACC_Init(lps25hb_ctx_t *dev_ctx)
{
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.handle = &hi2c1;
}
void BSP_ACC_Config(lps25hb_ctx_t *dev_ctx)
{
/* Check device ID */
whoamI = 0;
lps25hb_device_id_get(&dev_ctx, &whoamI);
if ( whoamI != LPS25HB_ID )
while(1); /*manage here device not found */
/* Restore default configuration */
lps25hb_reset_set(&dev_ctx, PROPERTY_ENABLE);
do {
lps25hb_reset_get(&dev_ctx, &rst);
} while (rst);
/* Enable Block Data Update */
lps25hb_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
/* Set Output Data Rate */
lps25hb_data_rate_set(&dev_ctx, LPS25HB_ODR_1Hz);
}
2.5气压器的处理测试程序
void BSP_BARO_handle(lps25hb_ctx_t *dev_ctx);
{
while(1)
{
HAL_IWDG_Refresh(&hiwdg);
/* Read output only if new value is available */
lps25hb_reg_t reg;
lps25hb_status_get(&dev_ctx, ®.status_reg);
if (reg.status_reg.p_da)
{
memset(data_raw_pressure.u8bit, 0x00, sizeof(int32_t));
lps25hb_pressure_raw_get(dev_ctx, data_raw_pressure.u8bit);
pressure_hPa = lps25hb_from_l***_to_hpa( data_raw_pressure.i32bit);
}
if (reg.status_reg.t_da)
{
memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t));
lps25hb_temperature_raw_get(dev_ctx, data_raw_temperature.u8bit);
temperature_degC = lps25hb_from_l***_to_degc( data_raw_temperature.i16bit);
}
HAL_Delay(500);
}
}
3、重写IIC的读写程序
以为加速度传感器的程序为例,当然气压器的IIC读写程序可以类似仿写。
读写时将从机的地址要写入
#define LIS2HH12_I2C_ADD_L 0x3DU
/*
* @brief Write generic device register (platform dependent)
*
* @param handle customizable argument. In this examples is used in
* order to select the correct sensor bus handler.
* @param reg register to write
* @param bufp pointer to data to write in register reg
* @param len number of consecutive register to write
*
*/
static int32_t platform_write(void *handle, uint8_t reg, uint8_t *bufp,
uint16_t len)
{
if (handle == &hi2c1)
{
HAL_I2C_Mem_Write(handle, LIS2HH12_I2C_ADD_L, reg,
I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
}
return 0;
}
/*
* @brief Read generic device register (platform dependent)
*
* @param handle customizable argument. In this examples is used in
* order to select the correct sensor bus handler.
* @param reg register to read
* @param bufp pointer to buffer that store the data read
* @param len number of consecutive register to read
*
*/
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
uint16_t len)
{
if (handle == &hi2c1)
{
HAL_I2C_Mem_Read(handle, LIS2HH12_I2C_ADD_L, reg,
I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
}
return 0;
}
void BSP_ACC_Init(lps25hb_ctx_t *dev_ctx)
{
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.handle = &hi2c1;
}
void BSP_ACC_Config(lps25hb_ctx_t *dev_ctx)
{
/* Check device ID */
whoamI = 0;
lps25hb_device_id_get(&dev_ctx, &whoamI);
if ( whoamI != LPS25HB_ID )
while(1); /*manage here device not found */
/* Restore default configuration */
lps25hb_reset_set(&dev_ctx, PROPERTY_ENABLE);
do {
lps25hb_reset_get(&dev_ctx, &rst);
} while (rst);
/* Enable Block Data Update */
lps25hb_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
/* Set Output Data Rate */
lps25hb_data_rate_set(&dev_ctx, LPS25HB_ODR_1Hz);
}
2.5气压器的处理测试程序
void BSP_BARO_handle(lps25hb_ctx_t *dev_ctx);
{
while(1)
{
HAL_IWDG_Refresh(&hiwdg);
/* Read output only if new value is available */
lps25hb_reg_t reg;
lps25hb_status_get(&dev_ctx, ®.status_reg);
if (reg.status_reg.p_da)
{
memset(data_raw_pressure.u8bit, 0x00, sizeof(int32_t));
lps25hb_pressure_raw_get(dev_ctx, data_raw_pressure.u8bit);
pressure_hPa = lps25hb_from_l***_to_hpa( data_raw_pressure.i32bit);
}
if (reg.status_reg.t_da)
{
memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t));
lps25hb_temperature_raw_get(dev_ctx, data_raw_temperature.u8bit);
temperature_degC = lps25hb_from_l***_to_degc( data_raw_temperature.i16bit);
}
HAL_Delay(500);
}
}
3、重写IIC的读写程序
以为加速度传感器的程序为例,当然气压器的IIC读写程序可以类似仿写。
读写时将从机的地址要写入
#define LIS2HH12_I2C_ADD_L 0x3DU
/*
* @brief Write generic device register (platform dependent)
*
* @param handle customizable argument. In this examples is used in
* order to select the correct sensor bus handler.
* @param reg register to write
* @param bufp pointer to data to write in register reg
* @param len number of consecutive register to write
*
*/
static int32_t platform_write(void *handle, uint8_t reg, uint8_t *bufp,
uint16_t len)
{
if (handle == &hi2c1)
{
HAL_I2C_Mem_Write(handle, LIS2HH12_I2C_ADD_L, reg,
I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
}
return 0;
}
/*
* @brief Read generic device register (platform dependent)
*
* @param handle customizable argument. In this examples is used in
* order to select the correct sensor bus handler.
* @param reg register to read
* @param bufp pointer to buffer that store the data read
* @param len number of consecutive register to read
*
*/
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
uint16_t len)
{
if (handle == &hi2c1)
{
HAL_I2C_Mem_Read(handle, LIS2HH12_I2C_ADD_L, reg,
I2C_MEMADD_SIZE_8BIT, bufp, len, 1000);
}
return 0;
}