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.

144 satır
4.4KB

  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. // take the chip select low to select the device:
  81. digitalWrite(chipSelectPin, LOW);
  82. // send the device the register you want to read:
  83. SPI.transfer(dataToSend);
  84. // send a value of 0 to read the first byte returned:
  85. result = SPI.transfer(0x00);
  86. // decrement the number of bytes left to read:
  87. bytesToRead--;
  88. // if you still have another byte to read:
  89. if (bytesToRead > 0) {
  90. // shift the first byte left, then get the second byte:
  91. result = result << 8;
  92. inByte = SPI.transfer(0x00);
  93. // combine the byte you just got with the previous one:
  94. result = result | inByte;
  95. // decrement the number of bytes left to read:
  96. bytesToRead--;
  97. }
  98. // take the chip select high to de-select:
  99. digitalWrite(chipSelectPin, HIGH);
  100. // return the result:
  101. return(result);
  102. }
  103. //Sends a write command to SCP1000
  104. void writeRegister(byte thisRegister, byte thisValue) {
  105. // SCP1000 expects the register address in the upper 6 bits
  106. // of the byte. So shift the bits left by two bits:
  107. thisRegister = thisRegister << 2;
  108. // now combine the register address and the command into one byte:
  109. byte dataToSend = thisRegister | WRITE;
  110. // take the chip select low to select the device:
  111. digitalWrite(chipSelectPin, LOW);
  112. SPI.transfer(dataToSend); //Send register location
  113. SPI.transfer(thisValue); //Send value to record into register
  114. // take the chip select high to de-select:
  115. digitalWrite(chipSelectPin, HIGH);
  116. }