Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
BMP280/BMP280.cpp@1:94d75a291f23, 2021-09-06 (annotated)
- Committer:
- louatayehh
- Date:
- Mon Sep 06 11:30:37 2021 +0000
- Revision:
- 1:94d75a291f23
- Parent:
- 0:c23b55e3ca7d
Capteur 1 : BMP280
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
louatayehh | 1:94d75a291f23 | 1 | //*****************Capteur BMP280 - pression atmosphérique ***************************// |
louatayehh | 0:c23b55e3ca7d | 2 | |
louatayehh | 0:c23b55e3ca7d | 3 | #include "mbed.h" |
louatayehh | 0:c23b55e3ca7d | 4 | #include "BMP280.h" |
louatayehh | 0:c23b55e3ca7d | 5 | |
louatayehh | 0:c23b55e3ca7d | 6 | //Constructeur |
louatayehh | 0:c23b55e3ca7d | 7 | BMP280::BMP280(PinName sda, PinName scl, char slave_adr) |
louatayehh | 0:c23b55e3ca7d | 8 | : |
louatayehh | 0:c23b55e3ca7d | 9 | i2c_p(new I2C(sda, scl)), |
louatayehh | 0:c23b55e3ca7d | 10 | i2c(*i2c_p), |
louatayehh | 1:94d75a291f23 | 11 | address(slave_adr<<1), // décalage adresse I2C |
louatayehh | 0:c23b55e3ca7d | 12 | t_fine(0) |
louatayehh | 0:c23b55e3ca7d | 13 | { |
louatayehh | 0:c23b55e3ca7d | 14 | initialize(); |
louatayehh | 0:c23b55e3ca7d | 15 | } |
louatayehh | 0:c23b55e3ca7d | 16 | |
louatayehh | 0:c23b55e3ca7d | 17 | //Constructeur |
louatayehh | 0:c23b55e3ca7d | 18 | BMP280::BMP280(I2C &i2c_obj, char slave_adr) |
louatayehh | 0:c23b55e3ca7d | 19 | : |
louatayehh | 0:c23b55e3ca7d | 20 | i2c_p(NULL), |
louatayehh | 0:c23b55e3ca7d | 21 | i2c(i2c_obj), |
louatayehh | 0:c23b55e3ca7d | 22 | address(slave_adr<<1), |
louatayehh | 0:c23b55e3ca7d | 23 | t_fine(0) |
louatayehh | 0:c23b55e3ca7d | 24 | { |
louatayehh | 0:c23b55e3ca7d | 25 | initialize(); |
louatayehh | 0:c23b55e3ca7d | 26 | } |
louatayehh | 0:c23b55e3ca7d | 27 | |
louatayehh | 0:c23b55e3ca7d | 28 | // Destructeur |
louatayehh | 0:c23b55e3ca7d | 29 | BMP280::~BMP280() |
louatayehh | 0:c23b55e3ca7d | 30 | { |
louatayehh | 0:c23b55e3ca7d | 31 | if (NULL != i2c_p) |
louatayehh | 0:c23b55e3ca7d | 32 | delete i2c_p; |
louatayehh | 0:c23b55e3ca7d | 33 | } |
louatayehh | 0:c23b55e3ca7d | 34 | |
louatayehh | 0:c23b55e3ca7d | 35 | void BMP280::initialize() |
louatayehh | 0:c23b55e3ca7d | 36 | { |
louatayehh | 0:c23b55e3ca7d | 37 | char cmd[18]; |
louatayehh | 0:c23b55e3ca7d | 38 | |
louatayehh | 0:c23b55e3ca7d | 39 | //cmd[0] = 0xf2; // ctrl_hum |
louatayehh | 0:c23b55e3ca7d | 40 | //cmd[1] = 0x01; // suréchantillonage humidité x1 |
louatayehh | 0:c23b55e3ca7d | 41 | //i2c.write(address, cmd, 2); |
louatayehh | 0:c23b55e3ca7d | 42 | |
louatayehh | 0:c23b55e3ca7d | 43 | cmd[0] = 0xf4; // ctrl_meas |
louatayehh | 0:c23b55e3ca7d | 44 | //cmd[1] = 0x27; // suréchantillonage température x1, suréchantillonage pression x1, Mode Normal |
louatayehh | 0:c23b55e3ca7d | 45 | cmd[1] = 0b01010111; // suréchantillonage température x2 010, suréchantillonage pression x16 101, Mode Normal 11 |
louatayehh | 0:c23b55e3ca7d | 46 | i2c.write(address, cmd, 2); |
louatayehh | 0:c23b55e3ca7d | 47 | |
louatayehh | 0:c23b55e3ca7d | 48 | cmd[0] = 0xf5; // config |
louatayehh | 0:c23b55e3ca7d | 49 | cmd[1] = 0b10111100; // Période Standby 1000ms, Filtre x16 |
louatayehh | 0:c23b55e3ca7d | 50 | i2c.write(address, cmd, 2); |
louatayehh | 0:c23b55e3ca7d | 51 | |
louatayehh | 0:c23b55e3ca7d | 52 | cmd[0] = 0x88; // read dig_T regs |
louatayehh | 0:c23b55e3ca7d | 53 | i2c.write(address, cmd, 1); |
louatayehh | 0:c23b55e3ca7d | 54 | i2c.read(address, cmd, 6); |
louatayehh | 0:c23b55e3ca7d | 55 | |
louatayehh | 0:c23b55e3ca7d | 56 | dig_T1 = (cmd[1] << 8) | cmd[0]; |
louatayehh | 0:c23b55e3ca7d | 57 | dig_T2 = (cmd[3] << 8) | cmd[2]; |
louatayehh | 0:c23b55e3ca7d | 58 | dig_T3 = (cmd[5] << 8) | cmd[4]; |
louatayehh | 0:c23b55e3ca7d | 59 | |
louatayehh | 0:c23b55e3ca7d | 60 | DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n\r", dig_T1, dig_T2, dig_T3); |
louatayehh | 0:c23b55e3ca7d | 61 | DEBUG_PRINT("dig_T = %d, %d, %d\n\r", dig_T1, dig_T2, dig_T3); |
louatayehh | 0:c23b55e3ca7d | 62 | |
louatayehh | 0:c23b55e3ca7d | 63 | cmd[0] = 0x8E; // read dig_P regs |
louatayehh | 0:c23b55e3ca7d | 64 | i2c.write(address, cmd, 1); |
louatayehh | 0:c23b55e3ca7d | 65 | i2c.read(address, cmd, 18); |
louatayehh | 0:c23b55e3ca7d | 66 | |
louatayehh | 0:c23b55e3ca7d | 67 | dig_P1 = (cmd[ 1] << 8) | cmd[ 0]; |
louatayehh | 0:c23b55e3ca7d | 68 | dig_P2 = (cmd[ 3] << 8) | cmd[ 2]; |
louatayehh | 0:c23b55e3ca7d | 69 | dig_P3 = (cmd[ 5] << 8) | cmd[ 4]; |
louatayehh | 0:c23b55e3ca7d | 70 | dig_P4 = (cmd[ 7] << 8) | cmd[ 6]; |
louatayehh | 0:c23b55e3ca7d | 71 | dig_P5 = (cmd[ 9] << 8) | cmd[ 8]; |
louatayehh | 0:c23b55e3ca7d | 72 | dig_P6 = (cmd[11] << 8) | cmd[10]; |
louatayehh | 0:c23b55e3ca7d | 73 | dig_P7 = (cmd[13] << 8) | cmd[12]; |
louatayehh | 0:c23b55e3ca7d | 74 | dig_P8 = (cmd[15] << 8) | cmd[14]; |
louatayehh | 0:c23b55e3ca7d | 75 | dig_P9 = (cmd[17] << 8) | cmd[16]; |
louatayehh | 0:c23b55e3ca7d | 76 | |
louatayehh | 0:c23b55e3ca7d | 77 | DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9); |
louatayehh | 1:94d75a291f23 | 78 | /* |
louatayehh | 0:c23b55e3ca7d | 79 | cmd[0] = 0xA1; // read dig_H regs |
louatayehh | 0:c23b55e3ca7d | 80 | i2c.write(address, cmd, 1); |
louatayehh | 0:c23b55e3ca7d | 81 | i2c.read(address, cmd, 1); |
louatayehh | 0:c23b55e3ca7d | 82 | cmd[1] = 0xE1; // read dig_H regs |
louatayehh | 0:c23b55e3ca7d | 83 | i2c.write(address, &cmd[1], 1); |
louatayehh | 0:c23b55e3ca7d | 84 | i2c.read(address, &cmd[1], 7); |
louatayehh | 0:c23b55e3ca7d | 85 | |
louatayehh | 0:c23b55e3ca7d | 86 | dig_H1 = cmd[0]; |
louatayehh | 0:c23b55e3ca7d | 87 | dig_H2 = (cmd[2] << 8) | cmd[1]; |
louatayehh | 0:c23b55e3ca7d | 88 | dig_H3 = cmd[3]; |
louatayehh | 0:c23b55e3ca7d | 89 | dig_H4 = (cmd[4] << 4) | (cmd[5] & 0x0f); |
louatayehh | 0:c23b55e3ca7d | 90 | dig_H5 = (cmd[6] << 4) | ((cmd[5]>>4) & 0x0f); |
louatayehh | 0:c23b55e3ca7d | 91 | dig_H6 = cmd[7]; |
louatayehh | 0:c23b55e3ca7d | 92 | |
louatayehh | 0:c23b55e3ca7d | 93 | DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6); |
louatayehh | 1:94d75a291f23 | 94 | */ |
louatayehh | 0:c23b55e3ca7d | 95 | } |
louatayehh | 0:c23b55e3ca7d | 96 | |
louatayehh | 0:c23b55e3ca7d | 97 | float BMP280::getTemperature() |
louatayehh | 0:c23b55e3ca7d | 98 | { |
louatayehh | 0:c23b55e3ca7d | 99 | int32_t temp_raw; |
louatayehh | 0:c23b55e3ca7d | 100 | float tempf; |
louatayehh | 0:c23b55e3ca7d | 101 | char cmd[4]; |
louatayehh | 0:c23b55e3ca7d | 102 | |
louatayehh | 0:c23b55e3ca7d | 103 | cmd[0] = 0xfa; // temp_msb |
louatayehh | 0:c23b55e3ca7d | 104 | i2c.write(address, cmd, 1); |
louatayehh | 0:c23b55e3ca7d | 105 | i2c.read(address, &cmd[1], 3); |
louatayehh | 0:c23b55e3ca7d | 106 | |
louatayehh | 0:c23b55e3ca7d | 107 | temp_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4); |
louatayehh | 0:c23b55e3ca7d | 108 | DEBUG_PRINT("\r\ntemp_raw:%d",temp_raw); |
louatayehh | 0:c23b55e3ca7d | 109 | |
louatayehh | 0:c23b55e3ca7d | 110 | int32_t temp1, temp2,temp; |
louatayehh | 0:c23b55e3ca7d | 111 | |
louatayehh | 0:c23b55e3ca7d | 112 | temp1 =((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11; |
louatayehh | 0:c23b55e3ca7d | 113 | temp2 =(((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14; |
louatayehh | 0:c23b55e3ca7d | 114 | DEBUG_PRINT(" temp1:%d temp2:%d",temp1, temp2); |
louatayehh | 0:c23b55e3ca7d | 115 | t_fine = temp1+temp2; |
louatayehh | 0:c23b55e3ca7d | 116 | DEBUG_PRINT(" t_fine:%d",t_fine); |
louatayehh | 0:c23b55e3ca7d | 117 | temp = (t_fine * 5 + 128) >> 8; |
louatayehh | 0:c23b55e3ca7d | 118 | tempf = (float)temp; |
louatayehh | 0:c23b55e3ca7d | 119 | DEBUG_PRINT(" tempf:%f",tempf); |
louatayehh | 0:c23b55e3ca7d | 120 | |
louatayehh | 0:c23b55e3ca7d | 121 | return (tempf/100.0f); |
louatayehh | 0:c23b55e3ca7d | 122 | } |
louatayehh | 0:c23b55e3ca7d | 123 | |
louatayehh | 0:c23b55e3ca7d | 124 | float BMP280::getPressure() |
louatayehh | 0:c23b55e3ca7d | 125 | { |
louatayehh | 0:c23b55e3ca7d | 126 | uint32_t press_raw; |
louatayehh | 0:c23b55e3ca7d | 127 | float pressf; |
louatayehh | 0:c23b55e3ca7d | 128 | char cmd[4]; |
louatayehh | 0:c23b55e3ca7d | 129 | |
louatayehh | 0:c23b55e3ca7d | 130 | cmd[0] = 0xf7; // press_msb |
louatayehh | 0:c23b55e3ca7d | 131 | i2c.write(address, cmd, 1); |
louatayehh | 0:c23b55e3ca7d | 132 | i2c.read(address, &cmd[1], 3); |
louatayehh | 0:c23b55e3ca7d | 133 | |
louatayehh | 0:c23b55e3ca7d | 134 | press_raw = (cmd[1] << 12) | (cmd[2] << 4) | (cmd[3] >> 4); |
louatayehh | 0:c23b55e3ca7d | 135 | |
louatayehh | 0:c23b55e3ca7d | 136 | int32_t var1, var2; |
louatayehh | 0:c23b55e3ca7d | 137 | uint32_t press; |
louatayehh | 0:c23b55e3ca7d | 138 | |
louatayehh | 0:c23b55e3ca7d | 139 | var1 = (t_fine >> 1) - 64000; |
louatayehh | 0:c23b55e3ca7d | 140 | var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6; |
louatayehh | 0:c23b55e3ca7d | 141 | var2 = var2 + ((var1 * dig_P5) << 1); |
louatayehh | 0:c23b55e3ca7d | 142 | var2 = (var2 >> 2) + (dig_P4 << 16); |
louatayehh | 0:c23b55e3ca7d | 143 | var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18; |
louatayehh | 0:c23b55e3ca7d | 144 | var1 = ((32768 + var1) * dig_P1) >> 15; |
louatayehh | 0:c23b55e3ca7d | 145 | if (var1 == 0) { |
louatayehh | 0:c23b55e3ca7d | 146 | return 0; |
louatayehh | 0:c23b55e3ca7d | 147 | } |
louatayehh | 0:c23b55e3ca7d | 148 | press = (((1048576 - press_raw) - (var2 >> 12))) * 3125; |
louatayehh | 0:c23b55e3ca7d | 149 | if(press < 0x80000000) { |
louatayehh | 0:c23b55e3ca7d | 150 | press = (press << 1) / var1; |
louatayehh | 0:c23b55e3ca7d | 151 | } else { |
louatayehh | 0:c23b55e3ca7d | 152 | press = (press / var1) * 2; |
louatayehh | 0:c23b55e3ca7d | 153 | } |
louatayehh | 0:c23b55e3ca7d | 154 | var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12; |
louatayehh | 0:c23b55e3ca7d | 155 | var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13; |
louatayehh | 0:c23b55e3ca7d | 156 | press = (press + ((var1 + var2 + dig_P7) >> 4)); |
louatayehh | 0:c23b55e3ca7d | 157 | |
louatayehh | 0:c23b55e3ca7d | 158 | pressf = (float)press; |
louatayehh | 0:c23b55e3ca7d | 159 | return (pressf/100.0f); |
louatayehh | 0:c23b55e3ca7d | 160 | } |