1
1
/*
2
2
SCP1000 Barometric Pressure Sensor Display
3
-
3
+
4
4
Shows the output of a Barometric Pressure Sensor on a
5
5
Uses the SPI library. For details on the sensor, see:
6
6
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
7
7
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
8
-
8
+
9
9
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
10
10
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
11
-
11
+
12
12
Circuit:
13
13
SCP1000 sensor attached to pins 6, 7, 10 - 13:
14
14
DRDY: pin 6
15
15
CSB: pin 7
16
16
MOSI: pin 11
17
17
MISO: pin 12
18
18
SCK: pin 13
19
-
19
+
20
20
created 31 July 2010
21
21
modified 14 August 2010
22
22
by Tom Igoe
@@ -33,7 +33,7 @@ cont byte READ = 0b00000000; // SCP1000's read command
33
33
const byte WRITE = 0b00000010 ; // SCP1000's write command
34
34
// pins used for the connection with the sensor
35
35
// the other you need are controlled by the SPI library):
36
- const int dataReadyPin = 6 ;
36
+ const int dataReadyPin = 6 ;
37
37
const int chipSelectPin = 7 ;
38
38
39
39
void setup () {
@@ -70,13 +70,13 @@ void loop() {
70
70
71
71
72
72
// Read the pressure data highest 3 bits:
73
- byte pressure_data_high = readRegister (0x1F , 1 );
73
+ byte pressure_data_high = readRegister (0x1F , 1 );
74
74
pressure_data_high &= 0b00000111 ; // you only needs bits 2 to 0
75
75
76
76
// Read the pressure data lower 16 bits:
77
- unsigned int pressure_data_low = readRegister (0x20 , 2 );
77
+ unsigned int pressure_data_low = readRegister (0x20 , 2 );
78
78
// combine the two parts into one 19-bit number:
79
- long pressure = ((pressure_data_high << 16 ) | pressure_data_low)/ 4 ;
79
+ long pressure = ((pressure_data_high << 16 ) | pressure_data_low) / 4 ;
80
80
81
81
// display the temperature:
82
82
Serial.println (" \t Pressure [Pa]=" + String (pressure));
@@ -86,7 +86,7 @@ void loop() {
86
86
// Read from or write to register from the SCP1000:
87
87
unsigned int readRegister (byte thisRegister, int bytesToRead ) {
88
88
byte inByte = 0 ; // incoming byte from the SPI
89
- unsigned int result = 0 ; // result to return
89
+ unsigned int result = 0 ; // result to return
90
90
91
91
// SCP1000 expects the register name in the upper 6 bits
92
92
// of the byte. So shift the bits left by two bits:
@@ -95,25 +95,25 @@ unsigned int readRegister(byte thisRegister, int bytesToRead ) {
95
95
dataToSend = thisRegister & READ;
96
96
97
97
// take the chip select low to select the device:
98
- digitalWrite (chipSelectPin, LOW);
98
+ digitalWrite (chipSelectPin, LOW);
99
99
// send the device the register you want to read:
100
- SPI.transfer (dataToSend);
100
+ SPI.transfer (dataToSend);
101
101
// send a value of 0 to read the first byte returned:
102
- result = SPI.transfer (0x00 );
102
+ result = SPI.transfer (0x00 );
103
103
// decrement the number of bytes left to read:
104
104
bytesToRead--;
105
105
// if you still have another byte to read:
106
106
if (bytesToRead > 0 ) {
107
- // shift the first byte left, then get the second byte:
107
+ // shift the first byte left, then get the second byte:
108
108
result = result << 8 ;
109
- inByte = SPI.transfer (0x00 );
109
+ inByte = SPI.transfer (0x00 );
110
110
// combine the byte you just got with the previous one:
111
111
result = result | inByte;
112
112
// decrement the number of bytes left to read:
113
113
bytesToRead--;
114
114
}
115
115
// take the chip select high to de-select:
116
- digitalWrite (chipSelectPin, HIGH);
116
+ digitalWrite (chipSelectPin, HIGH);
117
117
// return the result:
118
118
return (result);
119
119
}
@@ -130,13 +130,13 @@ void writeRegister(byte thisRegister, byte thisValue) {
130
130
dataToSend = thisRegister | WRITE;
131
131
132
132
// take the chip select low to select the device:
133
- digitalWrite (chipSelectPin, LOW);
133
+ digitalWrite (chipSelectPin, LOW);
134
134
135
135
SPI.transfer (dataToSend); // Send register location
136
136
SPI.transfer (thisValue); // Send value to record into register
137
137
138
138
// take the chip select high to de-select:
139
- digitalWrite (chipSelectPin, HIGH);
139
+ digitalWrite (chipSelectPin, HIGH);
140
140
}
141
141
142
142
0 commit comments