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.
Fork of mbed_main by
Barometer.cpp
00001 #include "mbed.h" 00002 #include "Barometer.h" 00003 00004 #define WEATHER_Barometer 0xee 00005 #define xpow(x, y) ((long)1 << y) 00006 00007 00008 Barometer::Barometer (PinName p_sda, PinName p_scl, Barometer_oss p_oss) : i2c(p_sda, p_scl) { 00009 init(p_oss); 00010 } 00011 00012 Barometer::Barometer (I2C& p_i2c, Barometer_oss p_oss) : i2c(p_i2c) { 00013 init(p_oss); 00014 } 00015 00016 00017 float Barometer::get_temperature() { 00018 return temperature; 00019 } 00020 00021 float Barometer::get_pressure() { 00022 return pressure; 00023 } 00024 00025 float Barometer::get_altitude_m() { 00026 altitude = 44330*(1-pow((pressure/1013.25),(0.190295))); 00027 return altitude; 00028 } 00029 00030 void Barometer::update () { 00031 long t, p, ut, up, x1, x2, x3, b3, b5, b6; 00032 unsigned long b4, b7; 00033 00034 twi_writechar(WEATHER_Barometer, 0xf4, 0x2e); 00035 wait(0.01); 00036 ut = twi_readshort(WEATHER_Barometer, 0xf6); 00037 00038 twi_writechar(WEATHER_Barometer, 0xf4, 0x34 | (oss << 6)); 00039 wait(0.05); 00040 up = twi_readlong(WEATHER_Barometer, 0xf6) >> (8 - oss); 00041 00042 x1 = (ut - ac6) * ac5 / xpow(2, 15); 00043 x2 = (long)mc * xpow(2, 11) / (x1 + md); 00044 b5 = x1 + x2; 00045 t = (b5 + 8) / xpow(2, 4); 00046 temperature = (float)t / 10.0; 00047 00048 b6 = b5 - 4000; 00049 x1 = (b2 * (b6 * b6 / xpow(2, 12))) / xpow(2, 11); 00050 x2 = ac2 * b6 / xpow(2, 11); 00051 x3 = x1 + x2; 00052 b3 = ((((unsigned long)ac1 * 4 + x3) << oss) + 2) / 4; 00053 x1 = ac3 * b6 / xpow(2, 13); 00054 x2 = (b1 * (b6 * b6 / xpow(2, 12))) / xpow(2, 16); 00055 x3 = ((x1 + x2) + 2) / xpow(2, 2); 00056 b4 = ac4 * (unsigned long)(x3 + 32768) / xpow(2, 15); 00057 b7 = ((unsigned long)up - b3) * (50000 >> oss); 00058 if (b7 < (unsigned long)0x80000000) { 00059 p = (b7 * 2) / b4; 00060 } else { 00061 p = (b7 / b4) * 2; 00062 } 00063 x1 = (p / xpow(2, 8)) * (p / xpow(2, 8)); 00064 x1 = (x1 * 3038) / xpow(2, 16); 00065 x2 = (-7357 * p) / xpow(2, 16); 00066 p = p + (x1 + x2 + 3791) / xpow(2, 4); 00067 pressure = (float)p / 100.0; 00068 } 00069 00070 void Barometer::init (Barometer_oss p_oss) { 00071 ac1 = twi_readshort(WEATHER_Barometer, 0xaa); 00072 ac2 = twi_readshort(WEATHER_Barometer, 0xac); 00073 ac3 = twi_readshort(WEATHER_Barometer, 0xae); 00074 ac4 = twi_readshort(WEATHER_Barometer, 0xb0); 00075 ac5 = twi_readshort(WEATHER_Barometer, 0xb2); 00076 ac6 = twi_readshort(WEATHER_Barometer, 0xb4); 00077 b1 = twi_readshort(WEATHER_Barometer, 0xb6); 00078 b2 = twi_readshort(WEATHER_Barometer, 0xb8); 00079 mb = twi_readshort(WEATHER_Barometer, 0xba); 00080 mc = twi_readshort(WEATHER_Barometer, 0xbc); 00081 md = twi_readshort(WEATHER_Barometer, 0xbe); 00082 oss = p_oss; 00083 } 00084 00085 unsigned short Barometer::twi_readshort (int id, int addr) { 00086 unsigned short i; 00087 00088 i2c.start(); 00089 i2c.write(id); 00090 i2c.write(addr); 00091 00092 i2c.start(); 00093 i2c.write(id | 1); 00094 i = i2c.read(1) << 8; 00095 i |= i2c.read(0); 00096 i2c.stop(); 00097 00098 return i; 00099 } 00100 00101 unsigned long Barometer::twi_readlong (int id, int addr) { 00102 unsigned long i; 00103 00104 i2c.start(); 00105 i2c.write(id); 00106 i2c.write(addr); 00107 00108 i2c.start(); 00109 i2c.write(id | 1); 00110 i = i2c.read(1) << 16; 00111 i |= i2c.read(1) << 8; 00112 i |= i2c.read(0); 00113 i2c.stop(); 00114 00115 return i; 00116 } 00117 00118 void Barometer::twi_writechar (int id, int addr, int dat) { 00119 00120 i2c.start(); 00121 i2c.write(id); 00122 i2c.write(addr); 00123 i2c.write(dat); 00124 i2c.stop(); 00125 }
Generated on Thu Jul 21 2022 15:06:12 by
1.7.2
