2773 |
kaklik |
1 |
/* |
|
|
2 |
MPL115A1 sparkfun breakout baropressure meter |
|
|
3 |
SDN : pin 7 |
|
|
4 |
CSN : pin 10 |
|
|
5 |
SDI/MOSI : pin 11 |
|
|
6 |
SDO/MISO : pin 12 |
|
|
7 |
SCK : pin 13 |
|
|
8 |
*/ |
|
|
9 |
|
|
|
10 |
// the sensor communicates using SPI, so include the library: |
|
|
11 |
#include <SPI.h> |
|
|
12 |
|
|
|
13 |
#define PRESH 0x80 |
|
|
14 |
#define PRESL 0x82 |
|
|
15 |
#define TEMPH 0x84 |
|
|
16 |
#define TEMPL 0x86 |
|
|
17 |
|
|
|
18 |
#define A0MSB 0x88 |
|
|
19 |
#define A0LSB 0x8A |
|
|
20 |
#define B1MSB 0x8C |
|
|
21 |
#define B1LSB 0x8E |
|
|
22 |
#define B2MSB 0x90 |
|
|
23 |
#define B2LSB 0x92 |
|
|
24 |
#define C12MSB 0x94 |
|
|
25 |
#define C12LSB 0x96 |
|
|
26 |
|
|
|
27 |
#define CONVERT 0x24 |
|
|
28 |
|
|
|
29 |
#define chipSelectPin 10 |
|
|
30 |
#define shutDown 7 |
|
|
31 |
|
|
|
32 |
float A0_; |
|
|
33 |
float B1_; |
|
|
34 |
float B2_; |
|
|
35 |
float C12_; |
|
|
36 |
|
|
|
37 |
void setup() { |
|
|
38 |
Serial.begin(115200); |
|
|
39 |
|
|
|
40 |
// start the SPI library: |
|
|
41 |
SPI.begin(); |
|
|
42 |
|
|
|
43 |
// initalize the data ready and chip select pins: |
|
|
44 |
pinMode(shutDown, OUTPUT); |
|
|
45 |
digitalWrite(shutDown, HIGH); |
|
|
46 |
pinMode(chipSelectPin, OUTPUT); |
|
|
47 |
digitalWrite(chipSelectPin, HIGH); |
|
|
48 |
delay (10); |
|
|
49 |
|
|
|
50 |
// read registers that contain the chip-unique parameters to do the math |
|
|
51 |
unsigned int A0H = readRegister(A0MSB); |
|
|
52 |
unsigned int A0L = readRegister(A0LSB); |
|
|
53 |
A0_ = (A0H << 5) + (A0L >> 3) + (A0L & 0x07) / 8.0; |
|
|
54 |
|
|
|
55 |
unsigned int B1H = readRegister(B1MSB); |
|
|
56 |
unsigned int B1L = readRegister(B1LSB); |
|
|
57 |
B1_ = ( ( ( (B1H & 0x1F) * 0x100)+B1L) / 8192.0) - 3 ; |
|
|
58 |
|
|
|
59 |
unsigned int B2H = readRegister(B2MSB); |
|
|
60 |
unsigned int B2L = readRegister(B2LSB); |
|
|
61 |
B2_ = ( ( ( (B2H - 0x80) << 8) + B2L) / 16384.0 ) - 2 ; |
|
|
62 |
|
|
|
63 |
unsigned int C12H = readRegister(C12MSB); |
|
|
64 |
unsigned int C12L = readRegister(C12LSB); |
|
|
65 |
C12_ = ( ( ( C12H * 0x100 ) + C12L) / 16777216.0 ) ; |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
void loop() { |
|
|
69 |
Serial.print("de druk is : "); |
|
|
70 |
Serial.println(baropPessure()); |
|
|
71 |
delay(1000); |
|
|
72 |
} |
|
|
73 |
|
|
|
74 |
//Read registers |
|
|
75 |
unsigned int readRegister(byte thisRegister ) { |
|
|
76 |
unsigned int result = 0; // result to return |
|
|
77 |
digitalWrite(chipSelectPin, LOW); |
|
|
78 |
delay(10); |
|
|
79 |
SPI.transfer(thisRegister); |
|
|
80 |
result = SPI.transfer(0x00); |
|
|
81 |
digitalWrite(chipSelectPin, HIGH); |
|
|
82 |
return(result); |
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
//read pressure |
|
|
86 |
float baropPessure(){ |
|
|
87 |
digitalWrite(chipSelectPin, LOW); |
|
|
88 |
delay(3); |
|
|
89 |
SPI.transfer(0x24); |
|
|
90 |
SPI.transfer(0x00); |
|
|
91 |
digitalWrite(chipSelectPin, HIGH); |
|
|
92 |
delay(3); |
|
|
93 |
digitalWrite(chipSelectPin, LOW); |
|
|
94 |
SPI.transfer(PRESH); |
|
|
95 |
unsigned int presH = SPI.transfer(0x00); |
|
|
96 |
delay(3); |
|
|
97 |
SPI.transfer(PRESL); |
|
|
98 |
unsigned int presL = SPI.transfer(0x00); |
|
|
99 |
delay(3); |
|
|
100 |
SPI.transfer(TEMPH); |
|
|
101 |
unsigned int tempH = SPI.transfer(0x00); |
|
|
102 |
delay(3); |
|
|
103 |
SPI.transfer(TEMPL); |
|
|
104 |
unsigned int tempL = SPI.transfer(0x00); |
|
|
105 |
delay(3); |
|
|
106 |
SPI.transfer(0x00); |
|
|
107 |
delay(3); |
|
|
108 |
digitalWrite(chipSelectPin, HIGH); |
|
|
109 |
|
|
|
110 |
unsigned long press = ((presH *256) + presL)/64; |
|
|
111 |
unsigned long temp = ((tempH *256) + tempL)/64; |
|
|
112 |
|
|
|
113 |
float pressure = A0_+(B1_+C12_*temp)*press+B2_*temp; |
|
|
114 |
float preskPa = pressure* (65.0/1023.0)+50.0; |
|
|
115 |
|
|
|
116 |
return(preskPa); |
|
|
117 |
} |
|
|
118 |
|