找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2256|回复: 0
收起左侧

LPS22HB STM32驱动程序

[复制链接]
ID:761468 发表于 2020-5-28 11:43 | 显示全部楼层 |阅读模式
51hei.png
单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "LPS22HB_Driver.h"
  3. #ifdef  USE_FULL_ASSERT_LPS22HB
  4. #include <stdio.h>
  5. #endif

  6. /** @addtogroup Environmental_Sensor
  7. * @{
  8. */

  9. /** @defgroup LPS22HB_DRIVER
  10. * @brief LPS22HB DRIVER
  11. * @{
  12. */

  13. /** @defgroup LPS22HB_Imported_Function_Prototypes
  14. * @{
  15. */

  16. extern uint8_t Sensor_IO_Write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite );
  17. extern uint8_t Sensor_IO_Read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead );

  18. /**
  19. * @}
  20. */

  21. /** @defgroup LPS22HB_Private_Function_Prototypes
  22. * @{
  23. */

  24. /**
  25. * @}
  26. */

  27. /** @defgroup LPS22HB_Private_Functions
  28. * @{
  29. */

  30. /**
  31. * @}
  32. */

  33. /** @defgroup LPS22HB_Public_Functions
  34. * @{
  35. */

  36. /*******************************************************************************
  37. * Function Name   : LPS22HB_ReadReg
  38. * Description   : Generic Reading function. It must be fullfilled with either
  39. *         : I2C or SPI reading functions
  40. * Input       : Register Address
  41. * Output      : Data Read
  42. * Return      : None
  43. *******************************************************************************/
  44. LPS22HB_Error_et LPS22HB_ReadReg( void *handle, uint8_t RegAddr, uint16_t NumByteToRead, uint8_t *Data )
  45. {

  46.   if ( Sensor_IO_Read( handle, RegAddr, Data, NumByteToRead ) )
  47.     return LPS22HB_ERROR;
  48.   else
  49.     return LPS22HB_OK;
  50. }

  51. /*******************************************************************************
  52. * Function Name   : LPS22HB_WriteReg
  53. * Description   : Generic Writing function. It must be fullfilled with either
  54. *         : I2C or SPI writing function
  55. * Input       : Register Address, Data to be written
  56. * Output      : None
  57. * Return      : None
  58. *******************************************************************************/
  59. LPS22HB_Error_et LPS22HB_WriteReg( void *handle, uint8_t RegAddr, uint16_t NumByteToWrite, uint8_t *Data )
  60. {

  61.   if ( Sensor_IO_Write( handle, RegAddr, Data, NumByteToWrite ) )
  62.     return LPS22HB_ERROR;
  63.   else
  64.     return LPS22HB_OK;
  65. }

  66. /**
  67. * @brief  Read identification code by WHO_AM_I register
  68. * @param  *handle Device handle.
  69. * @param  Buffer to empty by Device identification Value.
  70. * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
  71. */
  72. LPS22HB_Error_et LPS22HB_Get_DeviceID(void *handle, uint8_t* deviceid)
  73. {
  74.   if(LPS22HB_ReadReg(handle, LPS22HB_WHO_AM_I_REG, 1, deviceid))
  75.     return LPS22HB_ERROR;

  76.   return LPS22HB_OK;
  77. }


  78. /**
  79. * @brief  Get the LPS22HB driver version.
  80. * @param  None
  81. * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
  82. */
  83. LPS22HB_Error_et LPS22HB_Get_DriverVersion(LPS22HB_DriverVersion_st *Version)
  84. {
  85.   Version->Major = LPS22HB_DriverVersion_Major;
  86.   Version->Minor = LPS22HB_DriverVersion_Minor;
  87.   Version->Point = LPS22HB_DriverVersion_Point;

  88.   return LPS22HB_OK;
  89. }


  90. /**
  91. * @brief  Set LPS22HB Low Power or Low Noise Mode Configuration
  92. * @param  *handle Device handle.
  93. * @param  LPS22HB_LowNoise or LPS22HB_LowPower mode
  94. * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
  95. */
  96. LPS22HB_Error_et LPS22HB_Set_PowerMode(void *handle, LPS22HB_PowerMode_et mode)
  97. {
  98.   uint8_t tmp;

  99.   LPS22HB_assert_param(IS_LPS22HB_PowerMode(mode));

  100.   if(LPS22HB_ReadReg(handle, LPS22HB_RES_CONF_REG, 1, &tmp))
  101.     return LPS22HB_ERROR;

  102.   tmp &= ~LPS22HB_LCEN_MASK;
  103.   tmp |= (uint8_t)mode;

  104.   if(LPS22HB_WriteReg(handle, LPS22HB_RES_CONF_REG, 1, &tmp))
  105.     return LPS22HB_ERROR;

  106.   return LPS22HB_OK;
  107. }

  108. /**
  109. * @brief  Get LPS22HB Power Mode
  110. * @param  *handle Device handle.
  111. * @param   Buffer to empty with Mode: Low Noise or Low Current
  112. * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
  113. */
  114. LPS22HB_Error_et LPS22HB_Get_PowerMode(void *handle, LPS22HB_PowerMode_et* mode)
  115. {
  116.   uint8_t tmp;

  117.   if(LPS22HB_ReadReg(handle, LPS22HB_RES_CONF_REG, 1, &tmp))
  118.     return LPS22HB_ERROR;

  119.   *mode = (LPS22HB_PowerMode_et)(tmp & LPS22HB_LCEN_MASK);

  120.   return LPS22HB_OK;
  121. }


  122. /**
  123. * @brief  Set LPS22HB Output Data Rate
  124. * @param  *handle Device handle.
  125. * @param  Output Data Rate
  126. * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
  127. */
  128. LPS22HB_Error_et LPS22HB_Set_Odr(void *handle, LPS22HB_Odr_et odr)
  129. {
  130.   uint8_t tmp;


  131.   LPS22HB_assert_param(IS_LPS22HB_ODR(odr));

  132.   if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
  133.     return LPS22HB_ERROR;

  134.   tmp &= ~LPS22HB_ODR_MASK;
  135.   tmp |= (uint8_t)odr;

  136.   if(LPS22HB_WriteReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
  137.     return LPS22HB_ERROR;

  138.   return LPS22HB_OK;
  139. }

  140. /**
  141. * @brief  Get LPS22HB Output Data Rate
  142. * @param  *handle Device handle.
  143. * @param  Buffer to empty with Output Data Rate
  144. * @retval  Error Code [LPS22HB_ERROR, LPS22HB_OK]
  145. */
  146. LPS22HB_Error_et LPS22HB_Get_Odr(void *handle, LPS22HB_Odr_et* odr)
  147. {
  148.   uint8_t tmp;

  149.   if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
  150.     return LPS22HB_ERROR;

  151.   *odr = (LPS22HB_Odr_et)(tmp & LPS22HB_ODR_MASK);

  152.   return LPS22HB_OK;
  153. }

  154. /**
  155. * @brief  Enable/Disale low-pass filter on LPS22HB pressure data
  156. * @param  *handle Device handle.
  157. * @param  state: enable or disable
  158. * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
  159. */
  160. LPS22HB_Error_et LPS22HB_Set_LowPassFilter(void *handle, LPS22HB_State_et state)
  161. {
  162.   uint8_t tmp;

  163.   LPS22HB_assert_param(IS_LPS22HB_State(state));

  164.   if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
  165.     return LPS22HB_ERROR;

  166.   tmp &= ~LPS22HB_LPFP_MASK;
  167.   tmp |= ((uint8_t)state)<<LPS22HB_LPFP_BIT;


  168.   if(LPS22HB_WriteReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
  169.     return LPS22HB_ERROR;


  170.   return LPS22HB_OK;
  171. }


  172. /**
  173. * @brief  Set low-pass filter cutoff configuration on LPS22HB pressure data
  174. * @param  *handle Device handle.
  175. * @param  Filter Cutoff ODR/9 or ODR/20
  176. * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
  177. */
  178. LPS22HB_Error_et LPS22HB_Set_LowPassFilterCutoff(void *handle, LPS22HB_LPF_Cutoff_et cutoff){

  179.   uint8_t tmp;

  180.   LPS22HB_assert_param(IS_LPS22HB_LPF_Cutoff(cutoff));

  181.   if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
  182.     return LPS22HB_ERROR;

  183.   tmp &= ~LPS22HB_LPFP_CUTOFF_MASK;
  184.   tmp |= (uint8_t)cutoff;



  185.   if(LPS22HB_WriteReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
  186.     return LPS22HB_ERROR;


  187.   return LPS22HB_OK;

  188. }

  189. /**
  190. * @brief  Set Block Data Mode
  191. * @detail It is recommended to set BDU bit to ??
  192. * @detail This feature avoids reading LSB and MSB related to different samples.
  193. * @param  *handle Device handle.
  194. * @param  LPS22HB_BDU_CONTINUOUS_UPDATE, LPS22HB_BDU_NO_UPDATE
  195. * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
  196. */

  197. LPS22HB_Error_et LPS22HB_Set_Bdu(void *handle, LPS22HB_Bdu_et bdu)
  198. {
  199.   uint8_t tmp;


  200.   LPS22HB_assert_param(IS_LPS22HB_BDUMode(bdu));

  201.   if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
  202.     return LPS22HB_ERROR;

  203.   tmp &= ~LPS22HB_BDU_MASK;
  204.   tmp |= ((uint8_t)bdu);


  205.   if(LPS22HB_WriteReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
  206.   return LPS22HB_OK;

  207.    return LPS22HB_OK;
  208. }

  209. /**
  210. * @brief  Get Block Data Mode
  211. * @param  *handle Device handle.
  212. * @param Buffer to empty whit the bdu mode read from sensor
  213. * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
  214. */
  215. LPS22HB_Error_et LPS22HB_Get_Bdu(void *handle, LPS22HB_Bdu_et* bdu)
  216. {
  217.   uint8_t tmp;

  218.   if(LPS22HB_ReadReg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
  219.     return LPS22HB_ERROR;

  220.   *bdu = (LPS22HB_Bdu_et)(tmp & LPS22HB_BDU_MASK);

  221.   return LPS22HB_OK;
  222. }

  223. /**
  224. * @brief  Set SPI mode: 3 Wire Interface or 4 Wire Interface
  225. * @param  *handle Device handle.
  226. * @param LPS22HB_SPI_3_WIRE, LPS22HB_SPI_4_WIRE
  227. * @retval Error Code [LPS22HB_ERROR, LPS22HB_OK]
  228. */
  229. LPS22HB_Error_et LPS22HB_Set_SpiInterface(void *handle, LPS22HB_SPIMode_et spimode)
  230. {
  231.   uint8_t tmp;


  232.   LPS22HB_assert_param(IS_LPS22HB_SPIMode(spimode));
  233. ……………………

  234. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
LPS22HB_lib.zip (17.32 KB, 下载次数: 35)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表