- /* Includes ------------------------------------------------------------------*/
- #include "bmp085.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- //uint8_t oss;
- int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;
- uint16_t ac4, ac5, ac6;
- int32_t ut, up, BMP085_Temperature, BMP085_Pressure;
- BMP085_OSS_TypeDef oss;
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief BMP085¶á¼ÆËãÏμêyoˉêy£¬òÔ±ã½øDD¼ÆËãÕæÕyμÄζèoíÑ1Ç¿
- * @param ÎT
- * @retval ÎT
- */
- void BMP085_Read_Calibration_Data(void)
- {
- uint8_t BMP085_Calibration_Data_Buffer[BMP085_CAL_COE_NUMBER];
- if(I2C1_Read_NBytes(BMP085_ADDR, BMP085_CAL_COE_STA_ADDR, BMP085_CAL_COE_NUMBER, BMP085_Calibration_Data_Buffer) == I2C_FAIL)
- {
- BMP085_TIMEOUT_UserCallback();
- }
- ac1 = (((int16_t)BMP085_Calibration_Data_Buffer[0]) << 8) + BMP085_Calibration_Data_Buffer[1];
- ac2 = (((int16_t)BMP085_Calibration_Data_Buffer[2]) << 8) + BMP085_Calibration_Data_Buffer[3];
- ac3 = (((int16_t)BMP085_Calibration_Data_Buffer[4]) << 8) + BMP085_Calibration_Data_Buffer[5];
- ac4 = (((uint16_t)BMP085_Calibration_Data_Buffer[6]) << 8) + BMP085_Calibration_Data_Buffer[7];
- ac5 = (((uint16_t)BMP085_Calibration_Data_Buffer[8]) << 8) + BMP085_Calibration_Data_Buffer[9];
- ac6 = (((uint16_t)BMP085_Calibration_Data_Buffer[10]) << 8) + BMP085_Calibration_Data_Buffer[11];
- b1 = (((int16_t)BMP085_Calibration_Data_Buffer[12]) << 8) + BMP085_Calibration_Data_Buffer[13];
- b2 = (((int16_t)BMP085_Calibration_Data_Buffer[14]) << 8) + BMP085_Calibration_Data_Buffer[15];
- mb = (((int16_t)BMP085_Calibration_Data_Buffer[16]) << 8) + BMP085_Calibration_Data_Buffer[17];
- mc = (((int16_t)BMP085_Calibration_Data_Buffer[18]) << 8) + BMP085_Calibration_Data_Buffer[19];
- md = (((int16_t)BMP085_Calibration_Data_Buffer[20]) << 8) + BMP085_Calibration_Data_Buffer[21];
- }
- /**
- * @brief éèÖÃ2éÑùÄ£ê½oˉêy
- * @param oss_ValueéèÖÃμÄ2éÑùÄ£ê½
- * @retval ÎT
- */
- void BMP085_Set_OSS(BMP085_OSS_TypeDef oss_Value)
- {
- oss = oss_Value;
- }
- /**
- * @brief BMP0853õê¼»ˉoˉêy£¬°ü਼ÆËãÏμêyoí2éÑùÄ£ê½
- * @param oss_ValueéèÖÃμÄ2éÑùÄ£ê½
- * @retval ÎT
- */
- void BMP085_Init(BMP085_OSS_TypeDef oss_Value)
- {
- BMP085_Read_Calibration_Data();
- BMP085_Set_OSS(oss_Value);
- }
- /**
- * @brief BMP085¶áÎ′D£ÕyμÄζèÖμ
- * @param ÎT
- * @retval ÎT
- */
- void BMP085_Read_UnComPensated_Temperature(void)
- {
- uint8_t BMP085_Temperature_Data_Buffer[BMP085_TEMPERATURE_NUMBER];
- uint8_t BMP085_Cmd = BMP085_TEMPERATURE_CMD;
- if(I2C1_Write_NBytes(BMP085_ADDR, BMP085_CMD_ADDR, 1, &BMP085_Cmd) == I2C_FAIL)
- {
- BMP085_TIMEOUT_UserCallback();
- }
- Delay(BMP085_TEMPERATURE_DEL);
- if(I2C1_Read_NBytes(BMP085_ADDR, BMP085_VALUE_MSB_ADDR, BMP085_TEMPERATURE_NUMBER, BMP085_Temperature_Data_Buffer) == I2C_FAIL)
- {
- BMP085_TIMEOUT_UserCallback();
- }
- ut = (((int32_t)BMP085_Temperature_Data_Buffer[0]) << 8) + BMP085_Temperature_Data_Buffer[1];
- }
- /**
- * @brief BMP085¶áÎ′D£ÕyμÄÑ1Ç¿Öμ
- * @param ÎT
- * @retval ÎT
- */
- void BMP085_Read_Uncompensated_Pressure(void)
- {
- uint8_t BMP085_Pressure_Data_Buffer[BMP085_PRESSURE_NUMBER];
- uint8_t BMP085_Cmd = BMP085_PRESSURE_CMD + (oss << 6);
- if(I2C1_Write_NBytes(BMP085_ADDR, BMP085_CMD_ADDR, 1, &BMP085_Cmd) == I2C_FAIL)
- {
- BMP085_TIMEOUT_UserCallback();
- }
- switch(oss)
- {
- case ULTRA_LOW_POWER:
- Delay(BMP085_PRESSURE_ULP_DEL);
- break;
- case STANDARD:
- Delay(BMP085_PRESSURE_STD_DEL);
- break;
- case HIGH_RESOLUTION:
- Delay(BMP085_PRESSURE_HR_DEL);
- break;
- case ULTRA_HIGH_RESOLUTION:
- Delay(BMP085_PRESSURE_UHR_DEL);
- break;
- }
- if(I2C1_Read_NBytes(BMP085_ADDR, BMP085_VALUE_MSB_ADDR, BMP085_PRESSURE_NUMBER, BMP085_Pressure_Data_Buffer) == I2C_FAIL)
- {
- BMP085_TIMEOUT_UserCallback();
- }
- up = ((((int32_t)BMP085_Pressure_Data_Buffer[0]) << 16) + (((int32_t)BMP085_Pressure_Data_Buffer[1]) << 8) + BMP085_Pressure_Data_Buffer[2]) >> (8 - oss);
- }
- /**
- * @brief ¼ÆËãÕæÕyμÄζèÑ1Ç¿Öμ
- * @param ÎT
- * @retval ÎT
- */
- void BMP085_Calculate_Temp_Pres(void)
- {
- int32_t x1, x2, x3, b3, b5, b6, b7;
- uint32_t b4;
- x1 = (ut - ac6) * ac5 / 32768;
- x2 = mc * 2048 / (x1 + md);
- b5 = x1 + x2;
- BMP085_Temperature = (b5 + 8) / 16;
- b6 = b5 - 4000;
- x1 = (b2 * (b6 * b6 / 4096)) / 2048;
- x2 = ac2 * b6 / 2048;
- x3 = x1 + x2;
- b3 = (((ac1 * 4 + x3) << oss) + 2) / 4;
- x1 = ac3 * b6 / 8192;
- x2 = (b1 * (b6 * b6 / 4096)) / 65536;
- x3 = ((x1 + x2) + 2) / 4;
- b4 = ac4 * (uint32_t)(x3 + 32768) / 32768;
- b7 = ((uint32_t)up - b3) * (50000 >> oss);
- if(b7 < 0x80000000)
- {
- BMP085_Pressure = (b7 * 2) / b4;
- }
- else
- {
- BMP085_Pressure = (b7 / b4) * 2;
- }
- x1 = (BMP085_Pressure / 256) * (BMP085_Pressure / 256);
- x1 = (x1 * 3038) / 65536;
- x2 = ( -7357 * BMP085_Pressure) / 65536;
- BMP085_Pressure = BMP085_Pressure + (x1 + x2 + 3791) / 16;
- }
- /**
- * @brief BMP085μÄI2C2ù×÷3¬ê±oˉêy£¬′Ë′|Î′¶¨òåÄúèY
- * @param ÎT
- * @retval ÎT
- */
- void BMP085_TIMEOUT_UserCallback(void)
- {
- while (1)
- {
- }
- }
- /* Define to prevent recursive inclusion -------------------------------------*/
- #ifndef __BMP085_H
- #define __BMP085_H
- /* Includes ------------------------------------------------------------------*/
- #include "i2c1.h"
- #include "main.h"
- /* Exported types ------------------------------------------------------------*/
- /*BMP085¼ì2aÆøÑ1Ä£ê½*/
- typedef enum
- {
- ULTRA_LOW_POWER = 0,
- STANDARD = 1,
- HIGH_RESOLUTION = 2,
- ULTRA_HIGH_RESOLUTION = 3
- }BMP085_OSS_TypeDef;
- /* Exported constants --------------------------------------------------------*/
- /*BMP085Æ÷¼tμØÖ·*/
- #define BMP085_ADDR 0xEE
- /*BMP085¼ÆËãÏμêy′æ·ÅμØÖ·Æeê¼*/
- #define BMP085_CAL_COE_STA_ADDR 0xAA
- /*BMP085¼ÆËãÏμêy×Ö½úêy*/
- #define BMP085_CAL_COE_NUMBER 22
- /*BMP085μÄÃüáî¼Ä′æÆ÷μØÖ·*/
- #define BMP085_CMD_ADDR 0xF4
- /*BMP085μļì2aζèÃüáî*/
- #define BMP085_TEMPERATURE_CMD 0x2E
- /*BMP085μļì2aÆøÑ1Ãüáî*/
- #define BMP085_PRESSURE_CMD 0x34
- /*BMP085¼ì2aÖμ¼Ä′æÆ÷*/
- #define BMP085_VALUE_MSB_ADDR 0xF6
- #define BMP085_VALUE_LSB_ADDR 0xF7
- #define BMP085_VALUE_XLSB_ADDR 0xF8
- /*BMP085ζèÖμ×Ö½ú3¤¶è*/
- #define BMP085_TEMPERATURE_NUMBER 2
- /*BMP085ÆøÑ1Öμ×Ö½ú3¤¶è*/
- #define BMP085_PRESSURE_NUMBER 3
- /*BMP085¼ì2aζèóÃê±,μ¥λÎa0.1ms*/
- #define BMP085_TEMPERATURE_DEL 45
- /*BMP0852»í¬¼ì2aÆøÑ1Ä£ê½ÏÂóÃê±£¬μ¥λÎa0.1ms*/
- #define BMP085_PRESSURE_ULP_DEL 45
- #define BMP085_PRESSURE_STD_DEL 75
- #define BMP085_PRESSURE_HR_DEL 135
- #define BMP085_PRESSURE_UHR_DEL 255
- /* Exported macro ------------------------------------------------------------*/
- /* Exported functions ------------------------------------------------------- */
- void BMP085_Read_Calibration_Data(void);
- void BMP085_Set_OSS(BMP085_OSS_TypeDef oss_Value);
- void BMP085_Init(BMP085_OSS_TypeDef oss_Value);
- void BMP085_Read_UnComPensated_Temperature(void);
- void BMP085_Read_Uncompensated_Pressure(void);
- void BMP085_Calculate_Temp_Pres(void);
- void BMP085_TIMEOUT_UserCallback(void);
- #endif
复制代码
|