单片机源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "LPS22HB_Driver.h"
- #ifdef USE_FULL_ASSERT_LPS22HB
- #include <stdio.h>
- #endif
- /** @addtogroup Environmental_Sensor
- * @{
- */
- /** @defgroup LPS22HB_DRIVER
- * @brief LPS22HB DRIVER
- * @{
- */
- /** @defgroup LPS22HB_Imported_Function_Prototypes
- * @{
- */
- extern uint8_t Sensor_IO_Write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
- extern uint8_t Sensor_IO_Read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );
- /**
- * @}
- */
- /** @defgroup LPS22HB_Private_Function_Prototypes
- * @{
- */
- /**
- * @}
- */
- /** @defgroup LPS22HB_Private_Functions
- * @{
- */
- /**
- * @}
- */
- /** @defgroup LPS22HB_Public_Functions
- * @{
- */
- /*******************************************************************************
- * Function Name : LPS22HB_ReadReg
- * Description : Generic Reading function. It must be fullfilled with either
- * : I2C or SPI reading functions
- * Input : Register Address
- * Output : Data Read
- * Return : None
- *******************************************************************************/
- LPS22HB_Error_et LPS22HB_ReadReg( void *handle, uint8_t RegAddr, uint16_t NumByteToRead, uint8_t *Data )
- {
- if ( Sensor_IO_Read( handle, RegAddr, Data, NumByteToRead ) )
- return LPS22HB_ERROR;
- else
- return LPS22HB_OK;
- }
- /*******************************************************************************
- * Function Name : LPS22HB_WriteReg
- * Description : Generic Writing function. It must be fullfilled with either
- * : I2C or SPI writing function
- * Input : Register Address, Data to be written
- * Output : None
- * Return : None
- *******************************************************************************/
- LPS22HB_Error_et LPS22HB_WriteReg( void *handle, uint8_t RegAddr, uint16_t NumByteToWrite, uint8_t *Data )
- {
- if ( Sensor_IO_Write( handle, RegAddr, Data, NumByteToWrite ) )
- return LPS22HB_ERROR;
- else
- return LPS22HB_OK;
- }
- /**
- * @brief Read identification code by WHO_AM_I register
- * @param *handle Device handle.
- * @param Buffer to empty by Device identification Value.
- * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
- */
- LPS22HB_Error_et LPS22HB_Get_DeviceID(void *handle, uint8_t* deviceid)
- {
- if(LPS22HB_ReadReg(handle, LPS22HB_WHO_AM_I_REG, 1, deviceid))
- return LPS22HB_ERROR;
- return LPS22HB_OK;
- }
- /**
- * @brief Get the LPS22HB driver version.
- * @param None
- * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
- */
- LPS22HB_Error_et LPS22HB_Get_DriverVersion(LPS22HB_DriverVersion_st *Version)
- {
- Version->Major = LPS22HB_DriverVersion_Major;
- Version->Minor = LPS22HB_DriverVersion_Minor;
- Version->Point = LPS22HB_DriverVersion_Point;
- return LPS22HB_OK;
- }
- /**
- * @brief Set LPS22HB Low Power or Low Noise Mode Configuration
- * @param *handle Device handle.
- * @param LPS22HB_LowNoise or LPS22HB_LowPower mode
- * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
- */
- LPS22HB_Error_et LPS22HB_Set_PowerMode(void *handle, LPS22HB_PowerMode_et mode)
- {
- uint8_t tmp;
- LPS22HB_assert_param(IS_LPS22HB_PowerMode(mode));
- if(LPS22HB_ReadReg(handle, LPS22HB_RES_CONF_REG, 1, &tmp))
- return LPS22HB_ERROR;
- tmp &= ~LPS22HB_LCEN_MASK;
- tmp |= (uint8_t)mode;
- if(LPS22HB_WriteReg(handle, LPS22HB_RES_CONF_REG, 1, &tmp))
- return LPS22HB_ERROR;
- return LPS22HB_OK;
- }
- /**
- * @brief Get LPS22HB Power Mode
- * @param *handle Device handle.
- * @param Buffer to empty with Mode: Low Noise or Low Current
- * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
- */
- LPS22HB_Error_et LPS22HB_Get_PowerMode(void *handle, LPS22HB_PowerMode_et* mode)
- {
- uint8_t tmp;
- if(LPS22HB_ReadReg(handle, LPS22HB_RES_CONF_REG, 1, &tmp))
- return LPS22HB_ERROR;
- *mode = (LPS22HB_PowerMode_et)(tmp & LPS22HB_LCEN_MASK);
- return LPS22HB_OK;
- }
- /**
- * @brief Set LPS22HB Output Data Rate
- * @param *handle Device handle.
- * @param Output Data Rate
- * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
- */
- LPS22HB_Error_et LPS22HB_Set_Odr(void *handle, LPS22HB_Odr_et odr)
- {
- uint8_t tmp;
- LPS22HB_assert_param(IS_LPS22HB_ODR(odr));
- if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
- return LPS22HB_ERROR;
- tmp &= ~LPS22HB_ODR_MASK;
- tmp |= (uint8_t)odr;
- if(LPS22HB_WriteReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
- return LPS22HB_ERROR;
- return LPS22HB_OK;
- }
- /**
- * @brief Get LPS22HB Output Data Rate
- * @param *handle Device handle.
- * @param Buffer to empty with Output Data Rate
- * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
- */
- LPS22HB_Error_et LPS22HB_Get_Odr(void *handle, LPS22HB_Odr_et* odr)
- {
- uint8_t tmp;
- if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
- return LPS22HB_ERROR;
- *odr = (LPS22HB_Odr_et)(tmp & LPS22HB_ODR_MASK);
- return LPS22HB_OK;
- }
- /**
- * @brief Enable/Disale low-pass filter on LPS22HB pressure data
- * @param *handle Device handle.
- * @param state: enable or disable
- * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
- */
- LPS22HB_Error_et LPS22HB_Set_LowPassFilter(void *handle, LPS22HB_State_et state)
- {
- uint8_t tmp;
- LPS22HB_assert_param(IS_LPS22HB_State(state));
- if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
- return LPS22HB_ERROR;
- tmp &= ~LPS22HB_LPFP_MASK;
- tmp |= ((uint8_t)state)<<LPS22HB_LPFP_BIT;
- if(LPS22HB_WriteReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
- return LPS22HB_ERROR;
- return LPS22HB_OK;
- }
- /**
- * @brief Set low-pass filter cutoff configuration on LPS22HB pressure data
- * @param *handle Device handle.
- * @param Filter Cutoff ODR/9 or ODR/20
- * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
- */
- LPS22HB_Error_et LPS22HB_Set_LowPassFilterCutoff(void *handle, LPS22HB_LPF_Cutoff_et cutoff){
- uint8_t tmp;
- LPS22HB_assert_param(IS_LPS22HB_LPF_Cutoff(cutoff));
- if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
- return LPS22HB_ERROR;
- tmp &= ~LPS22HB_LPFP_CUTOFF_MASK;
- tmp |= (uint8_t)cutoff;
- if(LPS22HB_WriteReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
- return LPS22HB_ERROR;
- return LPS22HB_OK;
- }
- /**
- * @brief Set Block Data Mode
- * @detail It is recommended to set BDU bit to ??
- * @detail This feature avoids reading LSB and MSB related to different samples.
- * @param *handle Device handle.
- * @param LPS22HB_BDU_CONTINUOUS_UPDATE, LPS22HB_BDU_NO_UPDATE
- * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
- */
- LPS22HB_Error_et LPS22HB_Set_Bdu(void *handle, LPS22HB_Bdu_et bdu)
- {
- uint8_t tmp;
- LPS22HB_assert_param(IS_LPS22HB_BDUMode(bdu));
- if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
- return LPS22HB_ERROR;
- tmp &= ~LPS22HB_BDU_MASK;
- tmp |= ((uint8_t)bdu);
- if(LPS22HB_WriteReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
- return LPS22HB_OK;
- return LPS22HB_OK;
- }
- /**
- * @brief Get Block Data Mode
- * @param *handle Device handle.
- * @param Buffer to empty whit the bdu mode read from sensor
- * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
- */
- LPS22HB_Error_et LPS22HB_Get_Bdu(void *handle, LPS22HB_Bdu_et* bdu)
- {
- uint8_t tmp;
- if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
- return LPS22HB_ERROR;
- *bdu = (LPS22HB_Bdu_et)(tmp & LPS22HB_BDU_MASK);
- return LPS22HB_OK;
- }
- /**
- * @brief Set SPI mode: 3 Wire Interface or 4 Wire Interface
- * @param *handle Device handle.
- * @param LPS22HB_SPI_3_WIRE, LPS22HB_SPI_4_WIRE
- * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
- */
- LPS22HB_Error_et LPS22HB_Set_SpiInterface(void *handle, LPS22HB_SPIMode_et spimode)
- {
- uint8_t tmp;
- LPS22HB_assert_param(IS_LPS22HB_SPIMode(spimode));
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
LPS22HB_lib.zip
(17.32 KB, 下载次数: 35)
|