赞
踩
- #include <stdio.h>
- #include <stdlib.h>
- #include <wiringPi.h>
- #include <wiringPiI2C.h>
-
- unsigned short dig_t1, dig_p1;
- signed short dig_t2, dig_t3, dig_p2, dig_p3, dig_p4;
- signed short dig_p5, dig_p6, dig_p7, dig_p8, dig_p9;
- float temp, pres, var1, var2;
- int pres_int = 0, bmp280_flag = -1;
- unsigned short msb, lsb, xlsb, pres_h = 0, pres_l = 0, pres_flo = 0;
-
- int i2c_fd;
-
- int Raspi_BMP280_Init()
- {
- //system("sudo ulimit -n 4096");
- wiringPiSetup();
- i2c_fd = wiringPiI2CSetup(0x76);
- if (wiringPiI2CReadReg8(i2c_fd, 0xd0) == 0x58)
- {
- system("sudo i2cset -y 1 0x76 0xff 0xf4");
- dig_t1 = (wiringPiI2CReadReg8(i2c_fd, 0x89) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x88);
-
- dig_t2 = (wiringPiI2CReadReg8(i2c_fd, 0x8b) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x8a);
-
- dig_t3 = (wiringPiI2CReadReg8(i2c_fd, 0x8d) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x8c);
-
- dig_p1 = (wiringPiI2CReadReg8(i2c_fd, 0x8f) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x8e);
-
- dig_p2 = (wiringPiI2CReadReg8(i2c_fd, 0x91) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x90);
-
- dig_p3 = (wiringPiI2CReadReg8(i2c_fd, 0x93) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x92);
- dig_p4 = (wiringPiI2CReadReg8(i2c_fd, 0x95) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x94);
- dig_p5 = (wiringPiI2CReadReg8(i2c_fd, 0x97) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x96);
- dig_p6 = (wiringPiI2CReadReg8(i2c_fd, 0x99) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x98);
- dig_p7 = (wiringPiI2CReadReg8(i2c_fd, 0x9b) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x9a);
- dig_p8 = (wiringPiI2CReadReg8(i2c_fd, 0x9d) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x9c);
- dig_p9 = (wiringPiI2CReadReg8(i2c_fd, 0x9f) << 8)
- | wiringPiI2CReadReg8(i2c_fd, 0x9e);
- return 0;
- }
- return -1;
- }
-
- float Raspi_BMP280_Read_Pressure()
- {
- msb = wiringPiI2CReadReg8(i2c_fd, 0xf7);
- lsb = wiringPiI2CReadReg8(i2c_fd, 0xf8);
- xlsb = wiringPiI2CReadReg8(i2c_fd, 0xf9);
- pres = (msb << 16 | lsb << 8 | xlsb) >> 4;
- msb = wiringPiI2CReadReg8(i2c_fd, 0xfa);
- lsb = wiringPiI2CReadReg8(i2c_fd, 0xfb);
- xlsb = wiringPiI2CReadReg8(i2c_fd, 0xfc);
- temp = (msb << 16 | lsb << 8 | xlsb) >> 4;
- var1 = (temp / 16384.0 - dig_t1 / 1024.0)*(dig_t2);
- var2 = ((temp / 131072.0 - dig_t1 / 8192.0)*(temp / 131072.0 - dig_t1 / 8192.0))*dig_t3;
- temp = var1 + var2;
- temp /= 5120.0;
-
- var1 = (temp / 2.0) - 64000.0;
- var2 = var1 * var1*(dig_p6) / 32768.0;
- var2 = var2 + var1 * (dig_p5)*2.0;
- var2 = (var2 / 4.0) + ((dig_p4)*65536.0);
- var1 = (dig_p3)*var1*var1 / 524288.0 + (dig_p2)*var1 / 524288.0;
- var1 = (1.0 + var1 / 32768.0)*(dig_p1);
-
- pres = 1048576.0 - pres;
- pres = (pres - (var2 / 4096.0))*6250.0 / var1;
- var1 = (dig_p9)*pres*pres / 2147483648.0;
- var2 = pres * (dig_p8) / 32768.0;
- pres = pres + (var1 + var2 + (dig_p7)) / 16.0;
- return pres;
- }
-
- int main()
- {
- float pre;
- while (Raspi_BMP280_Init() != 0);
- while (1)
- {
- pre = Raspi_BMP280_Read_Pressure();
- printf("%.2f\n", pre);
- }
- }
sudo gcc bmp280.c -o bmp280 -lwiringPi
./bmp280
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。