649浏览
查看: 649|回复: 0

[进阶] BMP388传感器如何和UNO SPI接线?

[复制链接]
BMP388传感器如何和UNO SPI接线?图1


csPin接uno的任意其他数字引脚即可,这里选择4号引脚,传感器CSB接UNO D4。

代码:
  1. /*!
  2. * @file  interruptDataDrdy.ino
  3. * @brief  Demonstrate ready data (temperature/pressure) interrupt
  4. * @details  When measured data, the sensor will generate a 2.5 ms pulse signal by INT in the non-interrupt
  5. * [url=home.php?mod=space&uid=821650]@N[/url]  register locked state.
  6. * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
  7. * @license  The MIT License (MIT)
  8. * @author  [qsjhyy](yihuan.huang@dfrobot.com)
  9. * @version  V1.0
  10. * @date  2021-04-30
  11. * @url  https://github.com/DFRobot/DFRobot_BMP3XX
  12. */
  13. #include <DFRobot_BMP3XX.h>
  14. /**
  15. * Select chip version BMP388/BMP390L
  16. * Select I2C communication interface, please comment out SPI interface.
  17. * I2C communication address settings: eSDOGND: connect SDO pin to GND, I2C address is 0×76 now.
  18. *                   eSDOVDD: Connect SDO pin to VDDIO (3v3), I2C address is 0×77 now
  19. * Notice: If using Gravity products, default I2C communication address is: 0×77(eSDOVDD)
  20. */
  21. //DFRobot_BMP388_I2C sensor(&Wire, sensor.eSDOVDD);
  22. //DFRobot_BMP390L_I2C sensor(&Wire, sensor.eSDOVDD);
  23. /**
  24. * Select the chip version BMP388/BMP390L
  25. * Select I2C communication interface, please comment out SPI interface.
  26. * Set up digital pin according to the on-board pin connected with SPI chip-select pin.
  27. * Notice: csPin used here is D3 digital pin on ESP32, other non-conflicting pins can also be selected
  28. * as external interrupt pins.
  29. */
  30. uint8_t csPin = 4;
  31. DFRobot_BMP388_SPI sensor(&SPI, csPin);
  32. // DFRobot_BMP390L_SPI sensor(&SPI, csPin);
  33. /* If you do not need to eliminate the absolute difference of measurement, please comment the following line */
  34. #define CALIBRATE_ABSOLUTE_DIFFERENCE
  35. /* Interrupt flag */
  36. volatile uint8_t flag = 0;
  37. /* External interrupt flag */
  38. void interrupt()
  39. {
  40.   if(flag ==0){
  41.     flag = 1;
  42.   }
  43. }
  44. void setup(void)
  45. {
  46.   Serial.begin(115200);
  47.   int rslt;
  48.   while( ERR_OK != (rslt = sensor.begin()) ){
  49.     if(ERR_DATA_BUS == rslt){
  50.       Serial.println("Data bus error!!!");
  51.     }else if(ERR_IC_VERSION == rslt){
  52.       Serial.println("Chip versions do not match!!!");
  53.     }
  54.     delay(3000);
  55.   }
  56.   Serial.println("Begin ok!");
  57.   /**
  58.    * Interrupt configuration
  59.    * mode The interrupt mode needs to set. The following modes add up to mode:
  60.    *      Interrupt pin output mode: eINTPinPP: Push pull, eINTPinOD: Open drain
  61.    *      Interrupt pin active level: eINTPinActiveLevelLow: Active low, eINTPinActiveLevelHigh: Active high
  62.    *      Register interrupt latch: eINTLatchDIS: Disable, eINTLatchEN: Enable
  63.    *      FIFO water level reached interrupt: eIntFWtmDis: Disable, eIntFWtmEn: Enable
  64.    *      FIFO full interrupt: eINTFFullDIS: Disable, eINTFFullEN: Enable
  65.    *      Interrupt pin initial (invalid, non-interrupt) level: eINTInitialLevelLOW: Low, eINTInitialLevelHIGH: High
  66.    *      Temperature/pressure data ready interrupt: eINTDataDrdyDIS: Disable, eINTDataDrdyEN: Enable
  67.    * Notice: In non-latching mode (eINTLatchDIS), interrupt signal is 2.5 ms pulse signal
  68.    * Note: When using eINTPinActiveLevelLow (Active low interrupt pin), you need to use eINTInitialLevelHIGH (Initial
  69.    *       level of interrupt pin is high). Please use “FALLING” to trigger the following interrupt.
  70.    *       When using eINTPinActiveLevelHigh (Active low interrupt pin), you need to use eINTInitialLevelLOW (Initial
  71.    *       level of interrupt pin is high). Please use “RISING” to trigger the following interrupt.
  72.    */
  73.   sensor.setINTMode(sensor.eINTPinPP |
  74.                     sensor.eINTPinActiveLevelHigh |
  75.                     sensor.eINTLatchDIS |
  76.                     sensor.eIntFWtmDis |
  77.                     sensor.eINTFFullDIS |
  78.                     sensor.eINTInitialLevelLOW |
  79.                     sensor.eINTDataDrdyEN);
  80.   delay(100);
  81.   #ifdef CALIBRATE_ABSOLUTE_DIFFERENCE
  82.   /**
  83.    * Calibrate the sensor according to the current altitude
  84.    * In this example, we use an altitude of 540 meters in Wenjiang District of Chengdu (China).
  85.    * Please change to the local altitude when using it.
  86.    * If this interface is not called, the measurement data will not eliminate the absolute difference
  87.    * Note: This interface is only valid for the first call
  88.    */
  89.   if( sensor.calibratedAbsoluteDifference(540.0) ){
  90.     Serial.println("Absolute difference base value set successfully!");
  91.   }
  92.   #endif
  93.   #if defined(ESP32) || defined(ESP8266)
  94.     // D6 pin is used as interrupt pin by default, other non-conflicting pins can also be selected as external interrupt pins.
  95.     attachInterrupt(digitalPinToInterrupt(D6)/* Query the interrupt number of the D6 pin */,interrupt,CHANGE);
  96.   #elif defined(Arduino_SAM_ZERO)
  97.     // Pin 5 is used as interrupt pin by default, other non-conflicting pins can also be selected as external interrupt pins
  98.     attachInterrupt(digitalPinToInterrupt(5)/* Query the interrupt number of the 5 pin */,interrupt,CHANGE);
  99.   #else
  100.     /* The Correspondence Table of AVR Series Arduino Interrupt Pins And Terminal Numbers
  101.      * ---------------------------------------------------------------------------------------
  102.      * |                                        |  DigitalPin  | 2  | 3  |                   |
  103.      * |    Uno, Nano, Mini, other 328-based    |--------------------------------------------|
  104.      * |                                        | Interrupt No | 0  | 1  |                   |
  105.      * |-------------------------------------------------------------------------------------|
  106.      * |                                        |    Pin       | 2  | 3  | 21 | 20 | 19 | 18 |
  107.      * |               Mega2560                 |--------------------------------------------|
  108.      * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  | 5  |
  109.      * |-------------------------------------------------------------------------------------|
  110.      * |                                        |    Pin       | 3  | 2  | 0  | 1  | 7  |    |
  111.      * |    Leonardo, other 32u4-based          |--------------------------------------------|
  112.      * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  |    |
  113.      * |--------------------------------------------------------------------------------------
  114.      * ---------------------------------------------------------------------------------------------------------------------------------------------
  115.      *                      The Correspondence Table of micro:bit Interrupt Pins And Terminal Numbers
  116.      * ---------------------------------------------------------------------------------------------------------------------------------------------
  117.      * |             micro:bit                       | DigitalPin |P0-P20 can be used as an external interrupt                                     |
  118.      * |  (When using as an external interrupt,      |---------------------------------------------------------------------------------------------|
  119.      * |no need to set it to input mode with pinMode)|Interrupt No|Interrupt number is a pin digital value, such as P0 interrupt number 0, P1 is 1 |
  120.      * |-------------------------------------------------------------------------------------------------------------------------------------------|
  121.      */
  122.     attachInterrupt(/*Interrupt No*/1,interrupt,CHANGE);   // Open the external interrupt 0, connect INT1/2 to the digital pin of the main control:
  123.                                                            // UNO(2), Mega2560(2), Leonardo(3), microbit(P0).
  124.   #endif
  125.   /* Get the sampling period of the current measurement mode, unit: us */
  126.   float sampingPeriodus = sensor.getSamplingPeriodUS();
  127.   Serial.print("samping period : ");
  128.   Serial.print(sampingPeriodus);
  129.   Serial.println(" us");
  130.   /* Get the sampling frequency of the current measurement mode, unit: Hz */
  131.   float sampingFrequencyHz = 1000000 / sampingPeriodus;
  132.   Serial.print("samping frequency : ");
  133.   Serial.print(sampingFrequencyHz);
  134.   Serial.println(" Hz");
  135.   Serial.println();
  136.   delay(1000);
  137. }
  138. void loop()
  139. {
  140.   if(flag == 1){
  141.     flag = 0;
  142.     /* When data is ready and the interrupt is triggered, read altitude, unit: m */
  143.     float altitude = sensor.readAltitudeM();
  144.     Serial.print("Altitude : ");
  145.     Serial.print(altitude);
  146.     Serial.println(" m");
  147.   }
  148. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail