1+
12/*
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 MOSI, MISO, SCK pins are different depending on the
17- MISO: pin 12 board type. See your board's documentation for details
18- SCK: pin 13
19-
20- created 31 July 2010
21- by Tom Igoe
22- */
3+ SCP1000 Barometric Pressure Sensor Display
4+
5+ Shows the output of a Barometric Pressure Sensor on a
6+ Uses the SPI library. For details on the sensor, see:
7+ http://www.sparkfun.com/commerce/product_info.php?products_id=8161
8+ http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
9+
10+ This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
11+ http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
12+
13+ Circuit:
14+ SCP1000 sensor attached to pins 6, 7, 10 - 13:
15+ DRDY: pin 6
16+ CSB: pin 7
17+ MOSI: pin 11
18+ MISO: pin 12
19+ SCK: pin 13
20+
21+ created 31 July 2010
22+ modified 14 August 2010
23+ by Tom Igoe
24+ */
2325
2426// the sensor communicates using SPI, so include the library:
2527#include < SPI . h>
2628
2729// Sensor's memory register addresses:
28- const int PRESSURE = 0x1F ; // 3 most significant bits of pressure
29- const int PRESSURE_LSB = 0x20 ; // 16 least significant bits of pressure
30- const int TEMPERATURE = 0x21 ; // 16 bit temperature reading
30+ const int PRESSURE = 0x1F ; // 3 most significant bits of pressure
31+ const int PRESSURE_LSB = 0x20 ; // 16 least significant bits of pressure
32+ const int TEMPERATURE = 0x21 ; // 16 bit temperature reading
33+ cont byte READ = 0b11111100; // SCP1000's read command
34+ const byte WRITE = 0b00000010; // SCP1000's write command
3135
3236// pins used for the connection with the sensor
3337// the other you need are controlled by the SPI library):
3438const int dataReadyPin = 6 ;
3539const int chipSelectPin = 7 ;
3640
3741void setup () {
38- Serial . begin(9600 );
42+ Serial . begin(9600 );
3943
40- // start the SPI library:
41- SPI . begin();
44+ // start the SPI library:
45+ SPI . begin();
4246
43- // initalize the data ready and chip select pins:
44- pinMode(dataReadyPin, INPUT );
45- pinMode(chipSelectPin, OUTPUT );
47+ // initalize the data ready and chip select pins:
48+ pinMode(dataReadyPin, INPUT );
49+ pinMode(chipSelectPin, OUTPUT );
4650
47- // Configure SCP1000 for low noise configuration:
48- writeRegister(0x02 , 0x2D );
49- writeRegister(0x01 , 0x03 );
50- writeRegister(0x03 , 0x02 );
51- // give the sensor time to set up:
52- delay (100 );
51+ // Configure SCP1000 for low noise configuration:
52+ writeRegister(0x02 , 0x2D );
53+ writeRegister(0x01 , 0x03 );
54+ writeRegister(0x03 , 0x02 );
55+ // give the sensor time to set up:
56+ delay (100 );
5357}
5458
5559void loop () {
56- // Select High Resolution Mode
57- writeRegister(0x03 , 0x0A );
60+ // Select High Resolution Mode
61+ writeRegister(0x03 , 0x0A );
5862
59- // don't do anything until the data ready pin is high:
60- if (digitalRead(dataReadyPin) == HIGH ) {
61- // Read the temperature data
62- int tempData = readRegister(0x21 , 2 );
63+ // don't do anything until the data ready pin is high:
64+ if (digitalRead(dataReadyPin) == HIGH ) {
65+ // Read the temperature data
66+ int tempData = readRegister(0x21 , 2 );
6367
64- // convert the temperature to celsius and display it:
65- float realTemp = (float )tempData / 20.0 ;
66- Serial . print(" Temp[C]=" );
67- Serial . print(realTemp);
68+ // convert the temperature to celsius and display it:
69+ float realTemp = (float )tempData / 20.0 ;
70+ Serial . print(" Temp[C]=" );
71+ Serial . print(realTemp);
6872
6973
70- // Read the pressure data highest 3 bits:
71- byte pressure_data_high = readRegister(0x1F , 1 );
72- pressure_data_high &= 0b00000111; // you only needs bits 2 to 0
74+ // Read the pressure data highest 3 bits:
75+ byte pressure_data_high = readRegister(0x1F , 1 );
76+ pressure_data_high &= 0b00000111; // you only needs bits 2 to 0
7377
74- // Read the pressure data lower 16 bits:
75- unsigned int pressure_data_low = readRegister(0x20 , 2 );
76- // combine the two parts into one 19-bit number:
77- long pressure = ((pressure_data_high << 16 ) | pressure_data_low)/ 4 ;
78+ // Read the pressure data lower 16 bits:
79+ unsigned int pressure_data_low = readRegister(0x20 , 2 );
80+ // combine the two parts into one 19-bit number:
81+ long pressure = ((pressure_data_high << 16 ) | pressure_data_low)/ 4 ;
7882
79- // display the temperature:
80- Serial . println(" \t Pressure [Pa]=" + String (pressure));
81- }
83+ // display the temperature:
84+ Serial . println(" \t Pressure [Pa]=" + String (pressure));
85+ }
8286}
8387
84- // Sends a write command to SCP1000
85-
86- void writeRegister (byte registerName , byte registerValue ) {
87- // SCP1000 expects the reguster name in the upper 6 bits
88- // of the byte:
89- registerName <<= 2 ;
90- // command goes in the lower two bits:
91- registerName |= 0b00000010; // Write command
92-
93- // take the chip select low to select the device:
94- digitalWrite(chipSelectPin, LOW );
95-
96- SPI . transfer(registerName); // Send register location
97- SPI . transfer(registerValue); // Send value to record into register
98-
99- // take the chip select high to de-select:
100- digitalWrite(chipSelectPin, HIGH ); // Select SPI device
88+ // Read from or write to register from the SCP1000:
89+ unsigned int readRegister (byte thisRegister , int bytesToRead ) {
90+ byte inByte = 0 ; // incoming byte from the SPI
91+ unsigned int result = 0 ; // result to return
92+
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+ dataToSend = thisRegister & READ ;
98+
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);
101121}
102122
103123
104- // Read register from the SCP1000:
105- unsigned int readRegister (byte registerName , int numBytes ) {
106- byte inByte = 0 ; // incoming from the SPI read
107- unsigned int result = 0 ; // result to return
108-
109- // SCP1000 expects the reguster name in the upper 6 bits
110- // of the byte:
111- registerName <<= 2 ;
112- // command goes in the lower two bits:
113- registerName &= 0b11111100; // Read command
114-
115- // take the chip select low to select the device:
116- digitalWrite(chipSelectPin, LOW );
117- // send the device the register you want to read:
118- int command = SPI . transfer(registerName);
119- // send a value of 0 to read the forst byte returned:
120- inByte = SPI . transfer(0x00 );
121-
122- // if there's more than one byte returned,
123- // shift the first byte then get the second byte:
124- switch (numBytes) {
125- case 1 :
126- result = inByte;
127- break ;
128- case 2 :
129- result = inByte << 8 ;
130- inByte = SPI . transfer(0x00 );
131- result = result | inByte;
132- break ;
133- }
134- // take the chip select high to de-select:
135- digitalWrite(chipSelectPin, HIGH );
136- // return the result:
137- return (result);
138- }
139-
140-
141-
142-
143-
144-
145-
124+ // Sends a write command to SCP1000
146125
126+ void writeRegister (byte thisRegister , byte thisValue ) {
147127
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+ dataToSend = thisRegister | WRITE ;
148133
134+ // take the chip select low to select the device:
135+ digitalWrite(chipSelectPin, LOW );
149136
137+ SPI . transfer(dataToSend); // Send register location
138+ SPI . transfer(thisValue); // Send value to record into register
150139
140+ // take the chip select high to de-select:
141+ digitalWrite(chipSelectPin, HIGH );
142+ }
0 commit comments