当前位置:   article > 正文

树莓派3b+通过i2c驱动bmp280大气压模块_bmp280驱动程序树莓派

bmp280驱动程序树莓派

一、树莓派引脚图

二、bmp280简介

1、

2、模块引脚

三、打开i2c

1、

2、

四、代码实现  bmp280.C

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <wiringPi.h>
  4. #include <wiringPiI2C.h>
  5. unsigned short dig_t1, dig_p1;
  6. signed short dig_t2, dig_t3, dig_p2, dig_p3, dig_p4;
  7. signed short dig_p5, dig_p6, dig_p7, dig_p8, dig_p9;
  8. float temp, pres, var1, var2;
  9. int pres_int = 0, bmp280_flag = -1;
  10. unsigned short msb, lsb, xlsb, pres_h = 0, pres_l = 0, pres_flo = 0;
  11. int i2c_fd;
  12. int Raspi_BMP280_Init()
  13. {
  14. //system("sudo ulimit -n 4096");
  15. wiringPiSetup();
  16. i2c_fd = wiringPiI2CSetup(0x76);
  17. if (wiringPiI2CReadReg8(i2c_fd, 0xd0) == 0x58)
  18. {
  19. system("sudo i2cset -y 1 0x76 0xff 0xf4");
  20. dig_t1 = (wiringPiI2CReadReg8(i2c_fd, 0x89) << 8)
  21. | wiringPiI2CReadReg8(i2c_fd, 0x88);
  22. dig_t2 = (wiringPiI2CReadReg8(i2c_fd, 0x8b) << 8)
  23. | wiringPiI2CReadReg8(i2c_fd, 0x8a);
  24. dig_t3 = (wiringPiI2CReadReg8(i2c_fd, 0x8d) << 8)
  25. | wiringPiI2CReadReg8(i2c_fd, 0x8c);
  26. dig_p1 = (wiringPiI2CReadReg8(i2c_fd, 0x8f) << 8)
  27. | wiringPiI2CReadReg8(i2c_fd, 0x8e);
  28. dig_p2 = (wiringPiI2CReadReg8(i2c_fd, 0x91) << 8)
  29. | wiringPiI2CReadReg8(i2c_fd, 0x90);
  30. dig_p3 = (wiringPiI2CReadReg8(i2c_fd, 0x93) << 8)
  31. | wiringPiI2CReadReg8(i2c_fd, 0x92);
  32. dig_p4 = (wiringPiI2CReadReg8(i2c_fd, 0x95) << 8)
  33. | wiringPiI2CReadReg8(i2c_fd, 0x94);
  34. dig_p5 = (wiringPiI2CReadReg8(i2c_fd, 0x97) << 8)
  35. | wiringPiI2CReadReg8(i2c_fd, 0x96);
  36. dig_p6 = (wiringPiI2CReadReg8(i2c_fd, 0x99) << 8)
  37. | wiringPiI2CReadReg8(i2c_fd, 0x98);
  38. dig_p7 = (wiringPiI2CReadReg8(i2c_fd, 0x9b) << 8)
  39. | wiringPiI2CReadReg8(i2c_fd, 0x9a);
  40. dig_p8 = (wiringPiI2CReadReg8(i2c_fd, 0x9d) << 8)
  41. | wiringPiI2CReadReg8(i2c_fd, 0x9c);
  42. dig_p9 = (wiringPiI2CReadReg8(i2c_fd, 0x9f) << 8)
  43. | wiringPiI2CReadReg8(i2c_fd, 0x9e);
  44. return 0;
  45. }
  46. return -1;
  47. }
  48. float Raspi_BMP280_Read_Pressure()
  49. {
  50. msb = wiringPiI2CReadReg8(i2c_fd, 0xf7);
  51. lsb = wiringPiI2CReadReg8(i2c_fd, 0xf8);
  52. xlsb = wiringPiI2CReadReg8(i2c_fd, 0xf9);
  53. pres = (msb << 16 | lsb << 8 | xlsb) >> 4;
  54. msb = wiringPiI2CReadReg8(i2c_fd, 0xfa);
  55. lsb = wiringPiI2CReadReg8(i2c_fd, 0xfb);
  56. xlsb = wiringPiI2CReadReg8(i2c_fd, 0xfc);
  57. temp = (msb << 16 | lsb << 8 | xlsb) >> 4;
  58. var1 = (temp / 16384.0 - dig_t1 / 1024.0)*(dig_t2);
  59. var2 = ((temp / 131072.0 - dig_t1 / 8192.0)*(temp / 131072.0 - dig_t1 / 8192.0))*dig_t3;
  60. temp = var1 + var2;
  61. temp /= 5120.0;
  62. var1 = (temp / 2.0) - 64000.0;
  63. var2 = var1 * var1*(dig_p6) / 32768.0;
  64. var2 = var2 + var1 * (dig_p5)*2.0;
  65. var2 = (var2 / 4.0) + ((dig_p4)*65536.0);
  66. var1 = (dig_p3)*var1*var1 / 524288.0 + (dig_p2)*var1 / 524288.0;
  67. var1 = (1.0 + var1 / 32768.0)*(dig_p1);
  68. pres = 1048576.0 - pres;
  69. pres = (pres - (var2 / 4096.0))*6250.0 / var1;
  70. var1 = (dig_p9)*pres*pres / 2147483648.0;
  71. var2 = pres * (dig_p8) / 32768.0;
  72. pres = pres + (var1 + var2 + (dig_p7)) / 16.0;
  73. return pres;
  74. }
  75. int main()
  76. {
  77. float pre;
  78. while (Raspi_BMP280_Init() != 0);
  79. while (1)
  80. {
  81. pre = Raspi_BMP280_Read_Pressure();
  82. printf("%.2f\n", pre);
  83. }
  84. }

 

五、编译运行bmp280.c

1、编译

sudo gcc bmp280.c -o bmp280 -lwiringPi

2、运行

./bmp280

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/647951
推荐阅读
相关标签
  

闽ICP备14008679号