mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-07 01:54:26 +01:00
Modified Barometric Pressure Sensor to make it easier for beginners to understand.
This commit is contained in:
parent
00989e9778
commit
aab8563d1e
@ -1,33 +1,37 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
SCP1000 Barometric Pressure Sensor Display
|
SCP1000 Barometric Pressure Sensor Display
|
||||||
|
|
||||||
Shows the output of a Barometric Pressure Sensor on a
|
Shows the output of a Barometric Pressure Sensor on a
|
||||||
Uses the SPI library. For details on the sensor, see:
|
Uses the SPI library. For details on the sensor, see:
|
||||||
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
|
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
|
||||||
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
|
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
|
||||||
|
|
||||||
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
|
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
|
||||||
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
|
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
|
||||||
|
|
||||||
Circuit:
|
Circuit:
|
||||||
SCP1000 sensor attached to pins 6, 7, 10 - 13:
|
SCP1000 sensor attached to pins 6, 7, 10 - 13:
|
||||||
DRDY: pin 6
|
DRDY: pin 6
|
||||||
CSB: pin 7
|
CSB: pin 7
|
||||||
MOSI: pin 11 MOSI, MISO, SCK pins are different depending on the
|
MOSI: pin 11
|
||||||
MISO: pin 12 board type. See your board's documentation for details
|
MISO: pin 12
|
||||||
SCK: pin 13
|
SCK: pin 13
|
||||||
|
|
||||||
created 31 July 2010
|
created 31 July 2010
|
||||||
by Tom Igoe
|
modified 14 August 2010
|
||||||
*/
|
by Tom Igoe
|
||||||
|
*/
|
||||||
|
|
||||||
// the sensor communicates using SPI, so include the library:
|
// the sensor communicates using SPI, so include the library:
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
|
|
||||||
//Sensor's memory register addresses:
|
//Sensor's memory register addresses:
|
||||||
const int PRESSURE = 0x1F; //3 most significant bits of pressure
|
const int PRESSURE = 0x1F; // 3 most significant bits of pressure
|
||||||
const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
|
const int PRESSURE_LSB = 0x20; // 16 least significant bits of pressure
|
||||||
const int TEMPERATURE = 0x21; //16 bit temperature reading
|
const int TEMPERATURE = 0x21; // 16 bit temperature reading
|
||||||
|
cont byte READ = 0b11111100; // SCP1000's read command
|
||||||
|
const byte WRITE = 0b00000010; // SCP1000's write command
|
||||||
|
|
||||||
// pins used for the connection with the sensor
|
// pins used for the connection with the sensor
|
||||||
// the other you need are controlled by the SPI library):
|
// the other you need are controlled by the SPI library):
|
||||||
@ -81,55 +85,34 @@ void loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Sends a write command to SCP1000
|
//Read from or write to register from the SCP1000:
|
||||||
|
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
|
||||||
void writeRegister(byte registerName, byte registerValue) {
|
byte inByte = 0; // incoming byte from the SPI
|
||||||
// SCP1000 expects the reguster name in the upper 6 bits
|
|
||||||
// of the byte:
|
|
||||||
registerName <<= 2;
|
|
||||||
// command goes in the lower two bits:
|
|
||||||
registerName |= 0b00000010; //Write command
|
|
||||||
|
|
||||||
// take the chip select low to select the device:
|
|
||||||
digitalWrite(chipSelectPin, LOW);
|
|
||||||
|
|
||||||
SPI.transfer(registerName); //Send register location
|
|
||||||
SPI.transfer(registerValue); //Send value to record into register
|
|
||||||
|
|
||||||
// take the chip select high to de-select:
|
|
||||||
digitalWrite(chipSelectPin, HIGH); //Select SPI device
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//Read register from the SCP1000:
|
|
||||||
unsigned int readRegister(byte registerName, int numBytes) {
|
|
||||||
byte inByte = 0; // incoming from the SPI read
|
|
||||||
unsigned int result = 0; // result to return
|
unsigned int result = 0; // result to return
|
||||||
|
|
||||||
// SCP1000 expects the reguster name in the upper 6 bits
|
// SCP1000 expects the register name in the upper 6 bits
|
||||||
// of the byte:
|
// of the byte. So shift the bits left by two bits:
|
||||||
registerName <<= 2;
|
thisRegister = thisRegister << 2;
|
||||||
// command goes in the lower two bits:
|
// now combine the address and the command into one byte
|
||||||
registerName &= 0b11111100; //Read command
|
dataToSend = thisRegister & READ;
|
||||||
|
|
||||||
// take the chip select low to select the device:
|
// take the chip select low to select the device:
|
||||||
digitalWrite(chipSelectPin, LOW);
|
digitalWrite(chipSelectPin, LOW);
|
||||||
// send the device the register you want to read:
|
// send the device the register you want to read:
|
||||||
int command = SPI.transfer(registerName);
|
SPI.transfer(dataToSend);
|
||||||
// send a value of 0 to read the forst byte returned:
|
// send a value of 0 to read the first byte returned:
|
||||||
|
result = SPI.transfer(0x00);
|
||||||
|
// decrement the number of bytes left to read:
|
||||||
|
bytesToRead--;
|
||||||
|
// if you still have another byte to read:
|
||||||
|
if (bytesToRead > 0) {
|
||||||
|
// shift the first byte left, then get the second byte:
|
||||||
|
result = result << 8;
|
||||||
inByte = SPI.transfer(0x00);
|
inByte = SPI.transfer(0x00);
|
||||||
|
// combine the byte you just got with the previous one:
|
||||||
// if there's more than one byte returned,
|
result = result | inByte;
|
||||||
// shift the first byte then get the second byte:
|
// decrement the number of bytes left to read:
|
||||||
switch(numBytes) {
|
bytesToRead--;
|
||||||
case 1:
|
|
||||||
result = inByte;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
result = inByte << 8;
|
|
||||||
inByte = SPI.transfer(0x00);
|
|
||||||
result = result |inByte;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
// take the chip select high to de-select:
|
// take the chip select high to de-select:
|
||||||
digitalWrite(chipSelectPin, HIGH);
|
digitalWrite(chipSelectPin, HIGH);
|
||||||
@ -138,13 +121,22 @@ unsigned int readRegister(byte registerName, int numBytes) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Sends a write command to SCP1000
|
||||||
|
|
||||||
|
void writeRegister(byte thisRegister, byte thisValue) {
|
||||||
|
|
||||||
|
// SCP1000 expects the register address in the upper 6 bits
|
||||||
|
// of the byte. So shift the bits left by two bits:
|
||||||
|
thisRegister = thisRegister << 2;
|
||||||
|
// now combine the register address and the command into one byte:
|
||||||
|
dataToSend = thisRegister | WRITE;
|
||||||
|
|
||||||
|
// take the chip select low to select the device:
|
||||||
|
digitalWrite(chipSelectPin, LOW);
|
||||||
|
|
||||||
|
SPI.transfer(dataToSend); //Send register location
|
||||||
|
SPI.transfer(thisValue); //Send value to record into register
|
||||||
|
|
||||||
|
// take the chip select high to de-select:
|
||||||
|
digitalWrite(chipSelectPin, HIGH);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user