mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-17 22:23:10 +03:00
Modified Barometric Pressure Sensor to make it easier for beginners to understand.
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
SCP1000 Barometric Pressure Sensor Display
|
SCP1000 Barometric Pressure Sensor Display
|
||||||
|
|
||||||
@ -13,11 +14,12 @@
|
|||||||
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
|
||||||
|
modified 14 August 2010
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -28,6 +30,8 @@
|
|||||||
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:
|
||||||
inByte = SPI.transfer(0x00);
|
result = SPI.transfer(0x00);
|
||||||
|
// decrement the number of bytes left to read:
|
||||||
// if there's more than one byte returned,
|
bytesToRead--;
|
||||||
// shift the first byte then get the second byte:
|
// if you still have another byte to read:
|
||||||
switch(numBytes) {
|
if (bytesToRead > 0) {
|
||||||
case 1:
|
// shift the first byte left, then get the second byte:
|
||||||
result = inByte;
|
result = result << 8;
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
result = inByte << 8;
|
|
||||||
inByte = SPI.transfer(0x00);
|
inByte = SPI.transfer(0x00);
|
||||||
|
// combine the byte you just got with the previous one:
|
||||||
result = result | inByte;
|
result = result | inByte;
|
||||||
break;
|
// decrement the number of bytes left to read:
|
||||||
|
bytesToRead--;
|
||||||
}
|
}
|
||||||
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user