Project

General

Profile

Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / SPI / examples / BarometricPressureSensor / BarometricPressureSensor.ino @ 58d82c77

History | View | Annotate | Download (4.44 KB)

1
/*
2
 SCP1000 Barometric Pressure Sensor Display
3
 
4
 Shows the output of a Barometric Pressure Sensor on a
5
 Uses the SPI library. For details on the sensor, see:
6
 http://www.sparkfun.com/commerce/product_info.php?products_id=8161
7
 http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
8
 
9
 This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
10
 http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
11
 
12
 Circuit:
13
 SCP1000 sensor attached to pins 6, 7, 10 - 13:
14
 DRDY: pin 6
15
 CSB: pin 7
16
 MOSI: pin 11
17
 MISO: pin 12
18
 SCK: pin 13
19
 
20
 created 31 July 2010
21
 modified 14 August 2010
22
 by Tom Igoe
23
 */
24

    
25
// the sensor communicates using SPI, so include the library:
26
#include <SPI.h>
27

    
28
//Sensor's memory register addresses:
29
const int PRESSURE = 0x1F;      //3 most significant bits of pressure
30
const int PRESSURE_LSB = 0x20;  //16 least significant bits of pressure
31
const int TEMPERATURE = 0x21;   //16 bit temperature reading
32
const byte READ = 0b11111100;     // SCP1000's read command
33
const byte WRITE = 0b00000010;   // SCP1000's write command
34

    
35
// pins used for the connection with the sensor
36
// the other you need are controlled by the SPI library):
37
const int dataReadyPin = 6;
38
const int chipSelectPin = 7;
39

    
40
void setup() {
41
  Serial.begin(9600);
42

    
43
  // start the SPI library:
44
  SPI.begin();
45

    
46
  // initalize the  data ready and chip select pins:
47
  pinMode(dataReadyPin, INPUT);
48
  pinMode(chipSelectPin, OUTPUT);
49

    
50
  //Configure SCP1000 for low noise configuration:
51
  writeRegister(0x02, 0x2D);
52
  writeRegister(0x01, 0x03);
53
  writeRegister(0x03, 0x02);
54
  // give the sensor time to set up:
55
  delay(100);
56
}
57

    
58
void loop() {
59
  //Select High Resolution Mode
60
  writeRegister(0x03, 0x0A);
61

    
62
  // don't do anything until the data ready pin is high:
63
  if (digitalRead(dataReadyPin) == HIGH) {
64
    //Read the temperature data
65
    int tempData = readRegister(0x21, 2);
66

    
67
    // convert the temperature to celsius and display it:
68
    float realTemp = (float)tempData / 20.0;
69
    Serial.print("Temp[C]=");
70
    Serial.print(realTemp);
71

    
72

    
73
    //Read the pressure data highest 3 bits:
74
    byte  pressure_data_high = readRegister(0x1F, 1);
75
    pressure_data_high &= 0b00000111; //you only needs bits 2 to 0
76

    
77
    //Read the pressure data lower 16 bits:
78
    unsigned int pressure_data_low = readRegister(0x20, 2);
79
    //combine the two parts into one 19-bit number:
80
    long pressure = ((pressure_data_high << 16) | pressure_data_low)/4;
81

    
82
    // display the temperature:
83
    Serial.println("\tPressure [Pa]=" + String(pressure));
84
  }
85
}
86

    
87
//Read from or write to register from the SCP1000:
88
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
89
  byte inByte = 0;           // incoming byte from the SPI
90
  unsigned int result = 0;   // result to return
91
  Serial.print(thisRegister, BIN);
92
  Serial.print("\t");
93
  // SCP1000 expects the register name in the upper 6 bits
94
  // of the byte. So shift the bits left by two bits:
95
  thisRegister = thisRegister << 2;
96
  // now combine the address and the command into one byte
97
  byte dataToSend = thisRegister & READ;
98
  Serial.println(thisRegister, BIN);
99
  // take the chip select low to select the device:
100
  digitalWrite(chipSelectPin, LOW);
101
  // send the device the register you want to read:
102
  SPI.transfer(dataToSend);
103
  // send a value of 0 to read the first byte returned:
104
  result = SPI.transfer(0x00);
105
  // decrement the number of bytes left to read:
106
  bytesToRead--;
107
  // if you still have another byte to read:
108
  if (bytesToRead > 0) {
109
    // shift the first byte left, then get the second byte:
110
    result = result << 8;
111
    inByte = SPI.transfer(0x00);
112
    // combine the byte you just got with the previous one:
113
    result = result | inByte;
114
    // decrement the number of bytes left to read:
115
    bytesToRead--;
116
  }
117
  // take the chip select high to de-select:
118
  digitalWrite(chipSelectPin, HIGH);
119
  // return the result:
120
  return(result);
121
}
122

    
123

    
124
//Sends a write command to SCP1000
125

    
126
void writeRegister(byte thisRegister, byte thisValue) {
127

    
128
  // SCP1000 expects the register address in the upper 6 bits
129
  // of the byte. So shift the bits left by two bits:
130
  thisRegister = thisRegister << 2;
131
  // now combine the register address and the command into one byte:
132
  byte dataToSend = thisRegister | WRITE;
133

    
134
  // take the chip select low to select the device:
135
  digitalWrite(chipSelectPin, LOW);
136

    
137
  SPI.transfer(dataToSend); //Send register location
138
  SPI.transfer(thisValue);  //Send value to record into register
139

    
140
  // take the chip select high to de-select:
141
  digitalWrite(chipSelectPin, HIGH);
142
}
143