找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1723|回复: 0
打印 上一主题 下一主题
收起左侧

bmp085气压传感器与STM32F031的I2C代码调试结果

[复制链接]
跳转到指定楼层
楼主
ID:410324 发表于 2018-10-16 08:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "bmp085.h"

  3. /* Private typedef -----------------------------------------------------------*/
  4. /* Private define ------------------------------------------------------------*/
  5. /* Private macro -------------------------------------------------------------*/
  6. /* Private variables ---------------------------------------------------------*/
  7. //uint8_t oss;
  8. int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;
  9. uint16_t ac4, ac5, ac6;
  10. int32_t ut, up, BMP085_Temperature, BMP085_Pressure;
  11. BMP085_OSS_TypeDef oss;

  12. /* Private function prototypes -----------------------------------------------*/
  13. /* Private functions ---------------------------------------------------------*/
  14. /**
  15.   * @brief  BMP085¶á¼ÆËãÏμêyoˉêy£¬òÔ±ã½øDD¼ÆËãÕæÕyμÄζèoíÑ1Ç¿
  16.   * @param  ÎT
  17.   * @retval ÎT
  18.   */
  19. void BMP085_Read_Calibration_Data(void)
  20. {
  21.   uint8_t BMP085_Calibration_Data_Buffer[BMP085_CAL_COE_NUMBER];

  22.   if(I2C1_Read_NBytes(BMP085_ADDR, BMP085_CAL_COE_STA_ADDR, BMP085_CAL_COE_NUMBER, BMP085_Calibration_Data_Buffer) == I2C_FAIL)
  23.   {
  24.     BMP085_TIMEOUT_UserCallback();
  25.   }

  26.   ac1 = (((int16_t)BMP085_Calibration_Data_Buffer[0]) << 8) + BMP085_Calibration_Data_Buffer[1];
  27.   ac2 = (((int16_t)BMP085_Calibration_Data_Buffer[2]) << 8) + BMP085_Calibration_Data_Buffer[3];
  28.   ac3 = (((int16_t)BMP085_Calibration_Data_Buffer[4]) << 8) + BMP085_Calibration_Data_Buffer[5];
  29.   ac4 = (((uint16_t)BMP085_Calibration_Data_Buffer[6]) << 8) + BMP085_Calibration_Data_Buffer[7];
  30.   ac5 = (((uint16_t)BMP085_Calibration_Data_Buffer[8]) << 8) + BMP085_Calibration_Data_Buffer[9];
  31.   ac6 = (((uint16_t)BMP085_Calibration_Data_Buffer[10]) << 8) + BMP085_Calibration_Data_Buffer[11];
  32.   b1 = (((int16_t)BMP085_Calibration_Data_Buffer[12]) << 8) + BMP085_Calibration_Data_Buffer[13];
  33.   b2 = (((int16_t)BMP085_Calibration_Data_Buffer[14]) << 8) + BMP085_Calibration_Data_Buffer[15];
  34.   mb = (((int16_t)BMP085_Calibration_Data_Buffer[16]) << 8) + BMP085_Calibration_Data_Buffer[17];
  35.   mc = (((int16_t)BMP085_Calibration_Data_Buffer[18]) << 8) + BMP085_Calibration_Data_Buffer[19];
  36.   md = (((int16_t)BMP085_Calibration_Data_Buffer[20]) << 8) + BMP085_Calibration_Data_Buffer[21];
  37. }

  38. /**
  39.   * @brief  éèÖÃ2éÑùÄ£ê½oˉêy
  40.   * @param  oss_ValueéèÖÃμÄ2éÑùÄ£ê½
  41.   * @retval ÎT
  42.   */
  43. void BMP085_Set_OSS(BMP085_OSS_TypeDef oss_Value)
  44. {
  45.   oss = oss_Value;
  46. }

  47. /**
  48.   * @brief  BMP0853õê¼»ˉoˉêy£¬°ü਼ÆËãÏμêyoí2éÑùÄ£ê½
  49.   * @param  oss_ValueéèÖÃμÄ2éÑùÄ£ê½
  50.   * @retval ÎT
  51.   */
  52. void BMP085_Init(BMP085_OSS_TypeDef oss_Value)
  53. {
  54.   BMP085_Read_Calibration_Data();
  55.   BMP085_Set_OSS(oss_Value);
  56. }

  57. /**
  58.   * @brief  BMP085¶áÎ′D£ÕyμÄζèÖμ
  59.   * @param  ÎT
  60.   * @retval ÎT
  61.   */
  62. void BMP085_Read_UnComPensated_Temperature(void)
  63. {
  64.   uint8_t BMP085_Temperature_Data_Buffer[BMP085_TEMPERATURE_NUMBER];

  65.   uint8_t BMP085_Cmd = BMP085_TEMPERATURE_CMD;

  66.   if(I2C1_Write_NBytes(BMP085_ADDR, BMP085_CMD_ADDR, 1, &BMP085_Cmd) == I2C_FAIL)
  67.   {
  68.     BMP085_TIMEOUT_UserCallback();
  69.   }

  70.   Delay(BMP085_TEMPERATURE_DEL);

  71.   if(I2C1_Read_NBytes(BMP085_ADDR, BMP085_VALUE_MSB_ADDR, BMP085_TEMPERATURE_NUMBER, BMP085_Temperature_Data_Buffer) == I2C_FAIL)
  72.   {
  73.     BMP085_TIMEOUT_UserCallback();
  74.   }

  75.   ut = (((int32_t)BMP085_Temperature_Data_Buffer[0]) << 8) + BMP085_Temperature_Data_Buffer[1];
  76. }

  77. /**
  78.   * @brief  BMP085¶áÎ′D£ÕyμÄÑ1Ç¿Öμ
  79.   * @param  ÎT
  80.   * @retval ÎT
  81.   */
  82. void BMP085_Read_Uncompensated_Pressure(void)
  83. {
  84.   uint8_t BMP085_Pressure_Data_Buffer[BMP085_PRESSURE_NUMBER];

  85.   uint8_t BMP085_Cmd = BMP085_PRESSURE_CMD + (oss << 6);

  86.   if(I2C1_Write_NBytes(BMP085_ADDR, BMP085_CMD_ADDR, 1, &BMP085_Cmd) == I2C_FAIL)
  87.   {
  88.     BMP085_TIMEOUT_UserCallback();
  89.   }

  90.   switch(oss)
  91.   {
  92.     case ULTRA_LOW_POWER:
  93.       Delay(BMP085_PRESSURE_ULP_DEL);
  94.       break;
  95.     case STANDARD:
  96.       Delay(BMP085_PRESSURE_STD_DEL);
  97.       break;
  98.     case HIGH_RESOLUTION:
  99.       Delay(BMP085_PRESSURE_HR_DEL);
  100.       break;
  101.     case ULTRA_HIGH_RESOLUTION:
  102.       Delay(BMP085_PRESSURE_UHR_DEL);
  103.       break;
  104.   }

  105.   if(I2C1_Read_NBytes(BMP085_ADDR, BMP085_VALUE_MSB_ADDR, BMP085_PRESSURE_NUMBER, BMP085_Pressure_Data_Buffer) == I2C_FAIL)
  106.   {
  107.     BMP085_TIMEOUT_UserCallback();
  108.   }

  109.   up = ((((int32_t)BMP085_Pressure_Data_Buffer[0]) << 16) + (((int32_t)BMP085_Pressure_Data_Buffer[1]) << 8) + BMP085_Pressure_Data_Buffer[2]) >> (8 - oss);
  110. }

  111. /**
  112.   * @brief  ¼ÆËãÕæÕyμÄζèÑ1Ç¿Öμ
  113.   * @param  ÎT
  114.   * @retval ÎT
  115.   */
  116. void BMP085_Calculate_Temp_Pres(void)
  117. {
  118.   int32_t x1, x2, x3, b3, b5, b6, b7;
  119.   uint32_t b4;

  120.   x1 = (ut - ac6) * ac5 / 32768;
  121.   x2 = mc * 2048 / (x1 + md);
  122.   b5 = x1 + x2;
  123.   BMP085_Temperature = (b5 + 8) / 16;

  124.   b6 = b5 - 4000;
  125.   x1 = (b2 * (b6 * b6 / 4096)) / 2048;
  126.   x2 = ac2 * b6 / 2048;
  127.   x3 = x1 + x2;
  128.   b3 = (((ac1 * 4 + x3) << oss) + 2) / 4;
  129.   x1 = ac3 * b6 / 8192;
  130.   x2 = (b1 * (b6 * b6 / 4096)) / 65536;
  131.   x3 = ((x1 + x2) + 2) / 4;
  132.   b4 = ac4 * (uint32_t)(x3 + 32768) / 32768;
  133.   b7 = ((uint32_t)up - b3) * (50000 >> oss);
  134.   if(b7 < 0x80000000)
  135.   {
  136.     BMP085_Pressure = (b7 * 2) / b4;
  137.   }
  138.   else
  139.   {
  140.     BMP085_Pressure = (b7 / b4) * 2;
  141.   }
  142.   x1 = (BMP085_Pressure / 256) * (BMP085_Pressure / 256);
  143.   x1 = (x1 * 3038) / 65536;
  144.   x2 = ( -7357 * BMP085_Pressure) / 65536;
  145.   BMP085_Pressure = BMP085_Pressure + (x1 + x2 + 3791) / 16;
  146. }

  147. /**
  148.   * @brief  BMP085μÄI2C2ù×÷3¬ê±oˉêy£¬′Ë′|Î′¶¨òåÄúèY
  149.   * @param  ÎT
  150.   * @retval ÎT
  151.   */
  152. void BMP085_TIMEOUT_UserCallback(void)
  153. {
  154.   while (1)
  155.   {   
  156.   }
  157. }

  158. /* Define to prevent recursive inclusion -------------------------------------*/
  159. #ifndef __BMP085_H
  160. #define __BMP085_H

  161. /* Includes ------------------------------------------------------------------*/
  162. #include "i2c1.h"
  163. #include "main.h"

  164. /* Exported types ------------------------------------------------------------*/
  165. /*BMP085¼ì2aÆøÑ1Ä£ê½*/
  166. typedef enum
  167. {
  168.   ULTRA_LOW_POWER                                 = 0,
  169.   STANDARD                                        = 1,
  170.   HIGH_RESOLUTION                                 = 2,
  171.   ULTRA_HIGH_RESOLUTION                           = 3

  172. }BMP085_OSS_TypeDef;

  173. /* Exported constants --------------------------------------------------------*/
  174. /*BMP085Æ÷¼tμØÖ·*/
  175. #define BMP085_ADDR                               0xEE

  176. /*BMP085¼ÆËãÏμêy′æ·ÅμØÖ·Æeê¼*/
  177. #define BMP085_CAL_COE_STA_ADDR                   0xAA
  178. /*BMP085¼ÆËãÏμêy×Ö½úêy*/
  179. #define BMP085_CAL_COE_NUMBER                     22

  180. /*BMP085μÄÃüáî¼Ä′æÆ÷μØÖ·*/
  181. #define BMP085_CMD_ADDR                          0xF4

  182. /*BMP085μļì2aζèÃüáî*/
  183. #define BMP085_TEMPERATURE_CMD                   0x2E

  184. /*BMP085μļì2aÆøÑ1Ãüáî*/
  185. #define BMP085_PRESSURE_CMD                      0x34

  186. /*BMP085¼ì2aÖμ¼Ä′æÆ÷*/
  187. #define BMP085_VALUE_MSB_ADDR                    0xF6
  188. #define BMP085_VALUE_LSB_ADDR                    0xF7
  189. #define BMP085_VALUE_XLSB_ADDR                   0xF8

  190. /*BMP085ζèÖμ×Ö½ú3¤¶è*/
  191. #define BMP085_TEMPERATURE_NUMBER                 2

  192. /*BMP085ÆøÑ1Öμ×Ö½ú3¤¶è*/
  193. #define BMP085_PRESSURE_NUMBER                    3

  194. /*BMP085¼ì2aζèóÃê±,μ¥λÎa0.1ms*/
  195. #define BMP085_TEMPERATURE_DEL                    45

  196. /*BMP0852»í¬¼ì2aÆøÑ1Ä£ê½ÏÂóÃê±£¬μ¥λÎa0.1ms*/
  197. #define BMP085_PRESSURE_ULP_DEL                   45
  198. #define BMP085_PRESSURE_STD_DEL                   75
  199. #define BMP085_PRESSURE_HR_DEL                    135
  200. #define BMP085_PRESSURE_UHR_DEL                   255

  201. /* Exported macro ------------------------------------------------------------*/
  202. /* Exported functions ------------------------------------------------------- */
  203. void BMP085_Read_Calibration_Data(void);
  204. void BMP085_Set_OSS(BMP085_OSS_TypeDef oss_Value);
  205. void BMP085_Init(BMP085_OSS_TypeDef oss_Value);
  206. void BMP085_Read_UnComPensated_Temperature(void);
  207. void BMP085_Read_Uncompensated_Pressure(void);
  208. void BMP085_Calculate_Temp_Pres(void);
  209. void BMP085_TIMEOUT_UserCallback(void);

  210. #endif
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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