2 Sample code for the BMP085
9 #include <linux/i2c-dev.h>
10 #include <linux/i2c.h>
11 #include <sys/ioctl.h>
14 #define BMP085_I2C_ADDRESS 0x77
16 const unsigned char BMP085_OVERSAMPLING_SETTING = 3;
18 // Calibration values - These are stored in the BMP085
22 unsigned short int ac4;
23 unsigned short int ac5;
24 unsigned short int ac6;
33 unsigned int temperature, pressure;
36 // Open a connection to the bmp085
38 int bmp085_i2c_Begin()
41 char *fileName = "/dev/i2c-0";
43 // Open port for reading and writing
44 if ((fd = open(fileName, O_RDWR)) < 0)
47 // Set the port options and set the address of the device
48 if (ioctl(fd, I2C_SLAVE, BMP085_I2C_ADDRESS) < 0) {
56 // Read two words from the BMP085 and supply it as a 16 bit integer
57 __s32 bmp085_i2c_Read_Int(int fd, __u8 address)
59 __s32 res = i2c_smbus_read_word_data(fd, address);
65 // Convert result to 16 bits and swap bytes
66 res = ((res<<8) & 0xFF00) | ((res>>8) & 0xFF);
71 //Write a byte to the BMP085
72 void bmp085_i2c_Write_Byte(int fd, __u8 address, __u8 value)
74 if (i2c_smbus_write_byte_data(fd, address, value) < 0) {
80 // Read a block of data BMP085
81 void bmp085_i2c_Read_Block(int fd, __u8 address, __u8 length, __u8 *values)
83 if(i2c_smbus_read_i2c_block_data(fd, address,length,values)<0) {
90 void bmp085_Calibration()
92 int fd = bmp085_i2c_Begin();
93 ac1 = bmp085_i2c_Read_Int(fd,0xAA);
94 ac2 = bmp085_i2c_Read_Int(fd,0xAC);
95 ac3 = bmp085_i2c_Read_Int(fd,0xAE);
96 ac4 = bmp085_i2c_Read_Int(fd,0xB0);
97 ac5 = bmp085_i2c_Read_Int(fd,0xB2);
98 ac6 = bmp085_i2c_Read_Int(fd,0xB4);
99 b1 = bmp085_i2c_Read_Int(fd,0xB6);
100 b2 = bmp085_i2c_Read_Int(fd,0xB8);
101 mb = bmp085_i2c_Read_Int(fd,0xBA);
102 mc = bmp085_i2c_Read_Int(fd,0xBC);
103 md = bmp085_i2c_Read_Int(fd,0xBE);
107 // Read the uncompensated temperature value
108 unsigned int bmp085_ReadUT()
111 int fd = bmp085_i2c_Begin();
113 // Write 0x2E into Register 0xF4
114 // This requests a temperature reading
115 bmp085_i2c_Write_Byte(fd,0xF4,0x2E);
117 // Wait at least 4.5ms
120 // Read the two byte result from address 0xF6
121 ut = bmp085_i2c_Read_Int(fd,0xF6);
123 // Close the i2c file
129 // Read the uncompensated pressure value
130 unsigned int bmp085_ReadUP()
133 int fd = bmp085_i2c_Begin();
135 // Write 0x34+(BMP085_OVERSAMPLING_SETTING<<6) into register 0xF4
136 // Request a pressure reading w/ oversampling setting
137 bmp085_i2c_Write_Byte(fd,0xF4,0x34 + (BMP085_OVERSAMPLING_SETTING<<6));
139 // Wait for conversion, delay time dependent on oversampling setting
140 usleep((2 + (3<<BMP085_OVERSAMPLING_SETTING)) * 1000);
142 // Read the three byte result from 0xF6
143 // 0xF6 = MSB, 0xF7 = LSB and 0xF8 = XLSB
145 bmp085_i2c_Read_Block(fd, 0xF6, 3, values);
147 up = (((unsigned int) values[0] << 16) | ((unsigned int) values[1] << 8) | (unsigned int) values[2]) >> (8-BMP085_OVERSAMPLING_SETTING);
152 // Calculate pressure given uncalibrated pressure
153 // Value returned will be in units of XXXXX
154 unsigned int bmp085_GetPressure(unsigned int up)
156 int x1, x2, x3, b3, b6, p;
161 x1 = (b2 * (b6 * b6)>>12)>>11;
164 b3 = (((((int)ac1)*4 + x3)<<BMP085_OVERSAMPLING_SETTING) + 2)>>2;
168 x2 = (b1 * ((b6 * b6)>>12))>>16;
169 x3 = ((x1 + x2) + 2)>>2;
170 b4 = (ac4 * (unsigned int)(x3 + 32768))>>15;
172 b7 = ((unsigned int)(up - b3) * (50000>>BMP085_OVERSAMPLING_SETTING));
178 x1 = (p>>8) * (p>>8);
179 x1 = (x1 * 3038)>>16;
180 x2 = (-7357 * p)>>16;
181 p += (x1 + x2 + 3791)>>4;
186 // Calculate temperature given uncalibrated temperature
187 // Value returned will be in units of 0.1 deg C
188 unsigned int bmp085_GetTemperature(unsigned int ut)
192 x1 = (((int)ut - (int)ac6)*(int)ac5) >> 15;
193 x2 = ((int)mc << 11)/(x1 + md);
196 unsigned int result = ((b5 + 8)>>4);
201 int main(int argc, char **argv)
203 bmp085_Calibration();
204 temperature = bmp085_GetTemperature(bmp085_ReadUT());
205 pressure = bmp085_GetPressure(bmp085_ReadUP());
207 printf("Temperature (C)\t\t%0.1f\n", ((double)temperature)/10);
208 printf("Pressure (mBar)\t%0.2f\n", ((double)pressure)/100);
209 // printf("Pressure (mmHg)\t%0.2f\n", ((double)pressure)/100*0.75006375541921);
210 // printf("Pressure (mmHg)\t\t%0.2f\n", ((double)pressure)/100*0.750061683);