bmp280.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. BMP280.h
  3. Bosch BMP280 pressure sensor library for the Arduino microcontroller.
  4. This library uses I2C connection.
  5. Uses floating-point equations from BMP280 datasheet.
  6. modified by mhafuzul islam
  7. version 1.01 16/9/2014 initial version
  8. Our example code uses the "pizza-eating" license. You can do anything
  9. you like with this code. No really, anything. If you find it useful,
  10. buy me italian pizza someday.
  11. */
  12. #ifndef BMP280_h
  13. #define BMP280_h
  14. #if defined(ARDUINO) && ARDUINO >= 100
  15. #include "Arduino.h"
  16. #else
  17. #include "WProgram.h"
  18. #endif
  19. // #define _debugSerial
  20. // #define _debugTestData
  21. class BMP280
  22. {
  23. public:
  24. BMP280(); // base type
  25. char begin();
  26. char begin(int sdaPin, int sclPin);
  27. // call pressure.begin() to initialize BMP280 before use
  28. // returns 1 if success, 0 if failure (i2C connection problem.)
  29. short getOversampling(void);
  30. char setOversampling(short oss);
  31. char startMeasurment(void);
  32. // command BMP280 to start a pressure measurement
  33. // oversampling: 0 - 3 for oversampling value
  34. // returns (number of ms to wait) for success, 0 for fail
  35. char calcTemperature(double &T, double &uT);
  36. // calculation the true temperature from the given uncalibrated Temperature
  37. char calcPressure(double &P, double uP);
  38. //calculation for measuring pressure.
  39. double sealevel(double P, double A);
  40. // convert absolute pressure to sea-level pressure
  41. // P: absolute pressure (mbar)
  42. // A: current altitude (meters)
  43. // returns sealevel pressure in mbar
  44. double altitude(double P, double P0);
  45. // convert absolute pressure to altitude (given baseline pressure; sea-level, runway, etc.)
  46. // P: absolute pressure (mbar)
  47. // P0: fixed baseline pressure (mbar)
  48. // returns signed altitude in meters
  49. char getError(void);
  50. // If any library command fails, you can retrieve an extended
  51. // error code using this command. Errors are from the wire library:
  52. // 0 = Success
  53. // 1 = Data too long to fit in transmit buffer
  54. // 2 = Received NACK on transmit of address
  55. // 3 = Received NACK on transmit of data
  56. // 4 = Other error
  57. char getTemperatureAndPressure(double& T,double& P);
  58. private:
  59. char readCalibration();
  60. // Retrieve calibration data from device:
  61. // The BMP280 includes factory calibration data stored on the device.
  62. // Each device has different numbers, these must be retrieved and
  63. // used in the calculations when taking measurements.
  64. char readInt(char address, double &value);
  65. // read an signed int (16 bits) from a BMP280 register
  66. // address: BMP280 register address
  67. // value: external signed int for returned value (16 bits)
  68. // returns 1 for success, 0 for fail, with result in value
  69. char readUInt(char address, double &value);
  70. // read an unsigned int (16 bits) from a BMP280 register
  71. // address: BMP280 register address
  72. // value: external unsigned int for returned value (16 bits)
  73. // returns 1 for success, 0 for fail, with result in value
  74. char readBytes(unsigned char *values, char length);
  75. // read a number of bytes from a BMP280 register
  76. // values: array of char with register address in first location [0]
  77. // length: number of bytes to read back
  78. // returns 1 for success, 0 for fail, with read bytes in values[] array
  79. char writeBytes(unsigned char *values, char length);
  80. // write a number of bytes to a BMP280 register (and consecutive subsequent registers)
  81. // values: array of char with register address in first location [0]
  82. // length: number of bytes to write
  83. // returns 1 for success, 0 for fail
  84. char getUnPT(double &uP, double &uT);
  85. //get uncalibrated UP and UT value.
  86. //int dig_T2 , dig_T3 , dig_T4 , dig_P2 , dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9;
  87. //unsigned int dig_P1 , dig_T1 ;
  88. double dig_T1, dig_T2 , dig_T3 , dig_T4 , dig_P1, dig_P2 , dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9;
  89. short oversampling, oversampling_t;
  90. double t_fine;
  91. char error;
  92. };
  93. #define BMP280_ADDR 0x76 // 7-bit address
  94. #define BMP280_REG_CONTROL 0xF4
  95. #define BMP280_REG_RESULT_PRESSURE 0xF7 // 0xF7(msb) , 0xF8(lsb) , 0xF9(xlsb) : stores the pressure data.
  96. #define BMP280_REG_RESULT_TEMPRERATURE 0xFA // 0xFA(msb) , 0xFB(lsb) , 0xFC(xlsb) : stores the temperature data.
  97. #define BMP280_COMMAND_TEMPERATURE 0x2E
  98. #define BMP280_COMMAND_PRESSURE0 0x25
  99. #define BMP280_COMMAND_PRESSURE1 0x29
  100. #define BMP280_COMMAND_PRESSURE2 0x2D
  101. #define BMP280_COMMAND_PRESSURE3 0x31
  102. #define BMP280_COMMAND_PRESSURE4 0x5D
  103. #define BMP280_COMMAND_OVERSAMPLING_MAX 0xF5
  104. #endif