You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BarometricPressureSensor.ino 4.8KB

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. SCP1000 Barometric Pressure Sensor Display
  3. Shows the output of a Barometric Pressure Sensor on a
  4. Uses the SPI library. For details on the sensor, see:
  5. http://www.sparkfun.com/commerce/product_info.php?products_id=8161
  6. http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
  7. This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
  8. http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
  9. Circuit:
  10. SCP1000 sensor attached to pins 6, 7, 10 - 13:
  11. DRDY: pin 6
  12. CSB: pin 7
  13. MOSI: pin 11
  14. MISO: pin 12
  15. SCK: pin 13
  16. created 31 July 2010
  17. modified 14 August 2010
  18. by Tom Igoe
  19. */
  20. // the sensor communicates using SPI, so include the library:
  21. #include <SPI.h>
  22. //Sensor's memory register addresses:
  23. const int PRESSURE = 0x1F; //3 most significant bits of pressure
  24. const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
  25. const int TEMPERATURE = 0x21; //16 bit temperature reading
  26. const byte READ = 0b11111100; // SCP1000's read command
  27. const byte WRITE = 0b00000010; // SCP1000's write command
  28. // pins used for the connection with the sensor
  29. // the other you need are controlled by the SPI library):
  30. const int dataReadyPin = 6;
  31. const int chipSelectPin = 7;
  32. void setup() {
  33. Serial.begin(9600);
  34. // start the SPI library:
  35. SPI.begin();
  36. // initalize the data ready and chip select pins:
  37. pinMode(dataReadyPin, INPUT);
  38. pinMode(chipSelectPin, OUTPUT);
  39. //Configure SCP1000 for low noise configuration:
  40. writeRegister(0x02, 0x2D);
  41. writeRegister(0x01, 0x03);
  42. writeRegister(0x03, 0x02);
  43. // give the sensor time to set up:
  44. delay(100);
  45. }
  46. void loop() {
  47. //Select High Resolution Mode
  48. writeRegister(0x03, 0x0A);
  49. // don't do anything until the data ready pin is high:
  50. if (digitalRead(dataReadyPin) == HIGH) {
  51. //Read the temperature data
  52. int tempData = readRegister(0x21, 2);
  53. // convert the temperature to celsius and display it:
  54. float realTemp = (float)tempData / 20.0;
  55. Serial.print("Temp[C]=");
  56. Serial.print(realTemp);
  57. //Read the pressure data highest 3 bits:
  58. byte pressure_data_high = readRegister(0x1F, 1);
  59. pressure_data_high &= 0b00000111; //you only needs bits 2 to 0
  60. //Read the pressure data lower 16 bits:
  61. unsigned int pressure_data_low = readRegister(0x20, 2);
  62. //combine the two parts into one 19-bit number:
  63. long pressure = ((pressure_data_high << 16) | pressure_data_low)/4;
  64. // display the temperature:
  65. Serial.println("\tPressure [Pa]=" + String(pressure));
  66. }
  67. }
  68. //Read from or write to register from the SCP1000:
  69. unsigned int readRegister(byte thisRegister, int bytesToRead ) {
  70. byte inByte = 0; // incoming byte from the SPI
  71. unsigned int result = 0; // result to return
  72. Serial.print(thisRegister, BIN);
  73. Serial.print("\t");
  74. // SCP1000 expects the register name in the upper 6 bits
  75. // of the byte. So shift the bits left by two bits:
  76. thisRegister = thisRegister << 2;
  77. // now combine the address and the command into one byte
  78. byte dataToSend = thisRegister & READ;
  79. Serial.println(thisRegister, BIN);
  80. // gain control of the SPI port
  81. // and configure settings
  82. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  83. // take the chip select low to select the device:
  84. digitalWrite(chipSelectPin, LOW);
  85. // send the device the register you want to read:
  86. SPI.transfer(dataToSend);
  87. // send a value of 0 to read the first byte returned:
  88. result = SPI.transfer(0x00);
  89. // decrement the number of bytes left to read:
  90. bytesToRead--;
  91. // if you still have another byte to read:
  92. if (bytesToRead > 0) {
  93. // shift the first byte left, then get the second byte:
  94. result = result << 8;
  95. inByte = SPI.transfer(0x00);
  96. // combine the byte you just got with the previous one:
  97. result = result | inByte;
  98. // decrement the number of bytes left to read:
  99. bytesToRead--;
  100. }
  101. // take the chip select high to de-select:
  102. digitalWrite(chipSelectPin, HIGH);
  103. // release control of the SPI port
  104. SPI.endTransaction();
  105. // return the result:
  106. return(result);
  107. }
  108. //Sends a write command to SCP1000
  109. void writeRegister(byte thisRegister, byte thisValue) {
  110. // SCP1000 expects the register address in the upper 6 bits
  111. // of the byte. So shift the bits left by two bits:
  112. thisRegister = thisRegister << 2;
  113. // now combine the register address and the command into one byte:
  114. byte dataToSend = thisRegister | WRITE;
  115. // gain control of the SPI port
  116. // and configure settings
  117. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  118. // take the chip select low to select the device:
  119. digitalWrite(chipSelectPin, LOW);
  120. SPI.transfer(dataToSend); //Send register location
  121. SPI.transfer(thisValue); //Send value to record into register
  122. // take the chip select high to de-select:
  123. digitalWrite(chipSelectPin, HIGH);
  124. // release control of the SPI port
  125. SPI.endTransaction();
  126. }