11/*
22 SCP1000 Barometric Pressure Sensor Display
3-
3+
44 Serves the output of a Barometric Pressure Sensor as a web page.
55 Uses the SPI library. For details on the sensor, see:
66 http://www.sparkfun.com/commerce/product_info.php?products_id=8161
77 http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
8-
8+
99 This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
1010 http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
11-
11+
1212 Circuit:
1313 SCP1000 sensor attached to pins 6,7, and 11 - 13:
1414 DRDY: pin 6
1515 CSB: pin 7
1616 MOSI: pin 11
1717 MISO: pin 12
1818 SCK: pin 13
19-
19+
2020 created 31 July 2010
2121 by Tom Igoe
2222 */
2828
2929// assign a MAC address for the ethernet controller.
3030// fill in your address here:
31- byte mac[] = {
32- 0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED };
31+ byte mac[] = {
32+ 0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED
33+ };
3334// assign an IP address for the controller:
34- IPAddress ip (192 ,168 ,1 , 20 );
35- IPAddress gateway (192 ,168 ,1 , 1 );
35+ IPAddress ip (192 , 168 , 1 , 20 );
36+ IPAddress gateway (192 , 168 , 1 , 1 );
3637IPAddress subnet (255 , 255 , 255 , 0 );
3738
3839
3940// Initialize the Ethernet server library
40- // with the IP address and port you want to use
41+ // with the IP address and port you want to use
4142// (port 80 is default for HTTP):
4243EthernetServer server (80 );
4344
@@ -49,7 +50,7 @@ const int TEMPERATURE = 0x21; //16 bit temperature reading
4950
5051// pins used for the connection with the sensor
5152// the others you need are controlled by the SPI library):
52- const int dataReadyPin = 6 ;
53+ const int dataReadyPin = 6 ;
5354const int chipSelectPin = 7 ;
5455
5556float temperature = 0.0 ;
@@ -83,9 +84,9 @@ void setup() {
8384
8485}
8586
86- void loop () {
87+ void loop () {
8788 // check for a reading no more than once a second.
88- if (millis () - lastReadingTime > 1000 ){
89+ if (millis () - lastReadingTime > 1000 ) {
8990 // if there's a reading ready, read it:
9091 // don't do anything until the data ready pin is high:
9192 if (digitalRead (dataReadyPin) == HIGH) {
@@ -109,13 +110,13 @@ void getData() {
109110 temperature = (float )tempData / 20.0 ;
110111
111112 // Read the pressure data highest 3 bits:
112- byte pressureDataHigh = readRegister (0x1F , 1 );
113+ byte pressureDataHigh = readRegister (0x1F , 1 );
113114 pressureDataHigh &= 0b00000111 ; // you only needs bits 2 to 0
114115
115116 // Read the pressure data lower 16 bits:
116- unsigned int pressureDataLow = readRegister (0x20 , 2 );
117+ unsigned int pressureDataLow = readRegister (0x20 , 2 );
117118 // combine the two parts into one 19-bit number:
118- pressure = ((pressureDataHigh << 16 ) | pressureDataLow)/ 4 ;
119+ pressure = ((pressureDataHigh << 16 ) | pressureDataLow) / 4 ;
119120
120121 Serial.print (" Temperature: " );
121122 Serial.print (temperature);
@@ -149,13 +150,13 @@ void listenForEthernetClients() {
149150 client.println (" <br />" );
150151 client.print (" Pressure: " + String (pressure));
151152 client.print (" Pa" );
152- client.println (" <br />" );
153+ client.println (" <br />" );
153154 break ;
154155 }
155156 if (c == ' \n ' ) {
156157 // you're starting a new line
157158 currentLineIsBlank = true ;
158- }
159+ }
159160 else if (c != ' \r ' ) {
160161 // you've gotten a character on the current line
161162 currentLineIsBlank = false ;
@@ -167,7 +168,7 @@ void listenForEthernetClients() {
167168 // close the connection:
168169 client.stop ();
169170 }
170- }
171+ }
171172
172173
173174// Send a write command to SCP1000
@@ -179,20 +180,20 @@ void writeRegister(byte registerName, byte registerValue) {
179180 registerName |= 0b00000010 ; // Write command
180181
181182 // take the chip select low to select the device:
182- digitalWrite (chipSelectPin, LOW);
183+ digitalWrite (chipSelectPin, LOW);
183184
184185 SPI.transfer (registerName); // Send register location
185186 SPI.transfer (registerValue); // Send value to record into register
186187
187188 // take the chip select high to de-select:
188- digitalWrite (chipSelectPin, HIGH);
189+ digitalWrite (chipSelectPin, HIGH);
189190}
190191
191192
192193// Read register from the SCP1000:
193194unsigned int readRegister (byte registerName, int numBytes) {
194195 byte inByte = 0 ; // incoming from the SPI read
195- unsigned int result = 0 ; // result to return
196+ unsigned int result = 0 ; // result to return
196197
197198 // SCP1000 expects the register name in the upper 6 bits
198199 // of the byte:
@@ -201,22 +202,22 @@ unsigned int readRegister(byte registerName, int numBytes) {
201202 registerName &= 0b11111100 ; // Read command
202203
203204 // take the chip select low to select the device:
204- digitalWrite (chipSelectPin, LOW);
205+ digitalWrite (chipSelectPin, LOW);
205206 // send the device the register you want to read:
206- int command = SPI.transfer (registerName);
207+ int command = SPI.transfer (registerName);
207208 // send a value of 0 to read the first byte returned:
208- inByte = SPI.transfer (0x00 );
209-
209+ inByte = SPI.transfer (0x00 );
210+
210211 result = inByte;
211- // if there's more than one byte returned,
212+ // if there's more than one byte returned,
212213 // shift the first byte then get the second byte:
213- if (numBytes > 1 ){
214+ if (numBytes > 1 ) {
214215 result = inByte << 8 ;
215- inByte = SPI.transfer (0x00 );
216- result = result |inByte;
216+ inByte = SPI.transfer (0x00 );
217+ result = result | inByte;
217218 }
218219 // take the chip select high to de-select:
219- digitalWrite (chipSelectPin, HIGH);
220+ digitalWrite (chipSelectPin, HIGH);
220221 // return the result:
221222 return (result);
222223}
0 commit comments