mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-30 19:52:13 +01:00
Modified Barometric Pressure Sensor to make it easier for beginners to understand.
This commit is contained in:
parent
00989e9778
commit
aab8563d1e
@ -1,3 +1,4 @@
|
||||
|
||||
/*
|
||||
SCP1000 Barometric Pressure Sensor Display
|
||||
|
||||
@ -13,11 +14,12 @@
|
||||
SCP1000 sensor attached to pins 6, 7, 10 - 13:
|
||||
DRDY: pin 6
|
||||
CSB: pin 7
|
||||
MOSI: pin 11 MOSI, MISO, SCK pins are different depending on the
|
||||
MISO: pin 12 board type. See your board's documentation for details
|
||||
MOSI: pin 11
|
||||
MISO: pin 12
|
||||
SCK: pin 13
|
||||
|
||||
created 31 July 2010
|
||||
modified 14 August 2010
|
||||
by Tom Igoe
|
||||
*/
|
||||
|
||||
@ -28,6 +30,8 @@
|
||||
const int PRESSURE = 0x1F; // 3 most significant bits of pressure
|
||||
const int PRESSURE_LSB = 0x20; // 16 least significant bits of pressure
|
||||
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
|
||||
// the other you need are controlled by the SPI library):
|
||||
@ -81,55 +85,34 @@ void loop() {
|
||||
}
|
||||
}
|
||||
|
||||
//Sends a write command to SCP1000
|
||||
|
||||
void writeRegister(byte registerName, byte registerValue) {
|
||||
// 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
|
||||
//Read from or write to register from the SCP1000:
|
||||
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
|
||||
byte inByte = 0; // incoming byte from the SPI
|
||||
unsigned int result = 0; // result to return
|
||||
|
||||
// SCP1000 expects the reguster name in the upper 6 bits
|
||||
// of the byte:
|
||||
registerName <<= 2;
|
||||
// command goes in the lower two bits:
|
||||
registerName &= 0b11111100; //Read command
|
||||
// SCP1000 expects the register name in the upper 6 bits
|
||||
// of the byte. So shift the bits left by two bits:
|
||||
thisRegister = thisRegister << 2;
|
||||
// now combine the address and the command into one byte
|
||||
dataToSend = thisRegister & READ;
|
||||
|
||||
// take the chip select low to select the device:
|
||||
digitalWrite(chipSelectPin, LOW);
|
||||
// send the device the register you want to read:
|
||||
int command = SPI.transfer(registerName);
|
||||
// send a value of 0 to read the forst byte returned:
|
||||
inByte = SPI.transfer(0x00);
|
||||
|
||||
// if there's more than one byte returned,
|
||||
// shift the first byte then get the second byte:
|
||||
switch(numBytes) {
|
||||
case 1:
|
||||
result = inByte;
|
||||
break;
|
||||
case 2:
|
||||
result = inByte << 8;
|
||||
SPI.transfer(dataToSend);
|
||||
// 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);
|
||||
// combine the byte you just got with the previous one:
|
||||
result = result | inByte;
|
||||
break;
|
||||
// decrement the number of bytes left to read:
|
||||
bytesToRead--;
|
||||
}
|
||||
// take the chip select high to de-select:
|
||||
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