~ubuntu-branches/debian/experimental/arduino/experimental

« back to all changes in this revision

Viewing changes to libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor/BarometricPressureSensor.pde

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2012-03-11 18:19:42 UTC
  • mfrom: (1.1.5) (5.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20120311181942-be2clnbz1gcehixb
Tags: 1:1.0.1~rc1+dfsg-1
New upstream release, experimental.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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
17
 
 MISO: pin 12
18
 
 SCK: pin 13
19
 
 
20
 
 created 31 July 2010
21
 
 modified 14 August 2010
22
 
 by Tom Igoe
23
 
 */
24
 
 
25
 
// the sensor communicates using SPI, so include the library:
26
 
#include <SPI.h>
27
 
 
28
 
//Sensor's memory register addresses:
29
 
const int PRESSURE = 0x1F;      //3 most significant bits of pressure
30
 
const int PRESSURE_LSB = 0x20;  //16 least significant bits of pressure
31
 
const int TEMPERATURE = 0x21;   //16 bit temperature reading
32
 
cont byte READ = 0b00000000;     // SCP1000's read command
33
 
const byte WRITE = 0b00000010;   // SCP1000's write command
34
 
// pins used for the connection with the sensor
35
 
// the other you need are controlled by the SPI library):
36
 
const int dataReadyPin = 6; 
37
 
const int chipSelectPin = 7;
38
 
 
39
 
void setup() {
40
 
  Serial.begin(9600);
41
 
 
42
 
  // start the SPI library:
43
 
  SPI.begin();
44
 
 
45
 
  // initalize the  data ready and chip select pins:
46
 
  pinMode(dataReadyPin, INPUT);
47
 
  pinMode(chipSelectPin, OUTPUT);
48
 
 
49
 
  //Configure SCP1000 for low noise configuration:
50
 
  writeRegister(0x02, 0x2D);
51
 
  writeRegister(0x01, 0x03);
52
 
  writeRegister(0x03, 0x02);
53
 
  // give the sensor time to set up:
54
 
  delay(100);
55
 
}
56
 
 
57
 
void loop() {
58
 
  //Select High Resolution Mode
59
 
  writeRegister(0x03, 0x0A);
60
 
 
61
 
  // don't do anything until the data ready pin is high:
62
 
  if (digitalRead(dataReadyPin) == HIGH) {
63
 
    //Read the temperature data
64
 
    int tempData = readRegister(0x21, 2);
65
 
 
66
 
    // convert the temperature to celsius and display it:
67
 
    float realTemp = (float)tempData / 20.0;
68
 
    Serial.print("Temp[C]=");
69
 
    Serial.print(realTemp);
70
 
 
71
 
 
72
 
    //Read the pressure data highest 3 bits:
73
 
    byte  pressure_data_high = readRegister(0x1F, 1);   
74
 
    pressure_data_high &= 0b00000111; //you only needs bits 2 to 0
75
 
 
76
 
    //Read the pressure data lower 16 bits:
77
 
    unsigned int pressure_data_low = readRegister(0x20, 2);    
78
 
    //combine the two parts into one 19-bit number:
79
 
    long pressure = ((pressure_data_high << 16) | pressure_data_low)/4;
80
 
 
81
 
    // display the temperature:
82
 
    Serial.println("\tPressure [Pa]=" + String(pressure));
83
 
  }
84
 
}
85
 
 
86
 
//Read from or write to register from the SCP1000:
87
 
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
88
 
  byte inByte = 0;           // incoming byte from the SPI
89
 
  unsigned int result = 0;   // result to return 
90
 
 
91
 
  // SCP1000 expects the register name in the upper 6 bits
92
 
  // of the byte. So shift the bits left by two bits:
93
 
  thisRegister = thisRegister << 2;
94
 
  // now combine the address and the command into one byte
95
 
  dataToSend = thisRegister & READ;
96
 
 
97
 
  // take the chip select low to select the device:
98
 
  digitalWrite(chipSelectPin, LOW); 
99
 
  // send the device the register you want to read:
100
 
  SPI.transfer(dataToSend); 
101
 
  // send a value of 0 to read the first byte returned:
102
 
  result = SPI.transfer(0x00); 
103
 
  // decrement the number of bytes left to read:
104
 
  bytesToRead--;
105
 
  // if you still have another byte to read:
106
 
  if (bytesToRead > 0) {
107
 
    // shift the first byte left, then get the second byte: 
108
 
    result = result << 8;
109
 
    inByte = SPI.transfer(0x00); 
110
 
    // combine the byte you just got with the previous one:
111
 
    result = result | inByte;
112
 
    // decrement the number of bytes left to read:
113
 
    bytesToRead--;
114
 
  }
115
 
  // take the chip select high to de-select:
116
 
  digitalWrite(chipSelectPin, HIGH); 
117
 
  // return the result:
118
 
  return(result);
119
 
}
120
 
 
121
 
 
122
 
//Sends a write command to SCP1000
123
 
 
124
 
void writeRegister(byte thisRegister, byte thisValue) {
125
 
 
126
 
  // SCP1000 expects the register address in the upper 6 bits
127
 
  // of the byte. So shift the bits left by two bits:
128
 
  thisRegister = thisRegister << 2;
129
 
  // now combine the register address and the command into one byte:
130
 
  dataToSend = thisRegister | WRITE;
131
 
 
132
 
  // take the chip select low to select the device:
133
 
  digitalWrite(chipSelectPin, LOW); 
134
 
 
135
 
  SPI.transfer(dataToSend); //Send register location
136
 
  SPI.transfer(thisValue);  //Send value to record into register
137
 
 
138
 
  // take the chip select high to de-select:
139
 
  digitalWrite(chipSelectPin, HIGH); 
140
 
}
141
 
 
142
 
 
143