2
SCP1000 Barometric Pressure Sensor Display
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/
9
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
10
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
13
SCP1000 sensor attached to pins 6, 7, 10 - 13:
21
modified 14 August 2010
25
// the sensor communicates using SPI, so include the library:
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
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;
43
// start the SPI library:
46
// initalize the data ready and chip select pins:
47
pinMode(dataReadyPin, INPUT);
48
pinMode(chipSelectPin, OUTPUT);
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:
59
//Select High Resolution Mode
60
writeRegister(0x03, 0x0A);
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);
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);
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
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;
82
// display the temperature:
83
Serial.println("\tPressure [Pa]=" + String(pressure));
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);
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:
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:
117
// take the chip select high to de-select:
118
digitalWrite(chipSelectPin, HIGH);
119
// return the result:
124
//Sends a write command to SCP1000
126
void writeRegister(byte thisRegister, byte thisValue) {
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;
134
// take the chip select low to select the device:
135
digitalWrite(chipSelectPin, LOW);
137
SPI.transfer(dataToSend); //Send register location
138
SPI.transfer(thisValue); //Send value to record into register
140
// take the chip select high to de-select:
141
digitalWrite(chipSelectPin, HIGH);