3077 |
kaklik |
1 |
#define VERSION "0.1" |
|
|
2 |
#define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $" |
|
|
3 |
|
|
|
4 |
#include "main.h" |
|
|
5 |
|
|
|
6 |
#use i2c(master, sda=PIN_C4, scl=PIN_C3) |
|
|
7 |
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) //rcv TXD xmit RXD |
|
|
8 |
|
|
|
9 |
#define MPL3115_ADDR_R 0xC1 //addresa pro cteni |
|
|
10 |
#define MPL3115_ADDR_W 0xC0 |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
void mpl3115_setA (void) //nastavení pro výku |
|
|
14 |
{ |
|
|
15 |
|
|
|
16 |
|
|
|
17 |
i2c_start(); //STANDBY mode |
|
|
18 |
I2C_Write(MPL3115_ADDR_W); |
|
|
19 |
I2C_write(0x26); |
|
|
20 |
I2C_write(0xB8); |
|
|
21 |
i2c_stop(); |
|
|
22 |
|
|
|
23 |
i2c_start(); //PT_DATA_CFG set |
|
|
24 |
I2C_Write(MPL3115_ADDR_W); |
|
|
25 |
I2C_write(0x13); |
|
|
26 |
I2C_write(0x07); //hodnota |
|
|
27 |
i2c_stop(); |
|
|
28 |
|
|
|
29 |
i2c_start(); //ACTIVE mode |
|
|
30 |
I2C_Write(MPL3115_ADDR_W); |
|
|
31 |
I2C_write(0x26); |
|
|
32 |
I2C_write(0xB9); |
|
|
33 |
i2c_stop(); |
|
|
34 |
} |
|
|
35 |
|
|
|
36 |
void mpl3115_setP (void) //nastavení pro tlak |
|
|
37 |
{ |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
i2c_start(); //STANDBY mode |
|
|
41 |
I2C_Write(MPL3115_ADDR_W); |
|
|
42 |
I2C_write(0x26); |
|
|
43 |
I2C_write(0xB8); |
|
|
44 |
i2c_stop(); |
|
|
45 |
|
|
|
46 |
i2c_start(); //PT_DATA_CFG set |
|
|
47 |
I2C_Write(MPL3115_ADDR_W); |
|
|
48 |
I2C_write(0x13); |
|
|
49 |
I2C_write(0x07); //hodnota |
|
|
50 |
i2c_stop(); |
|
|
51 |
|
|
|
52 |
i2c_start(); //ACTIVE mode |
|
|
53 |
I2C_Write(MPL3115_ADDR_W); |
|
|
54 |
I2C_write(0x26); |
|
|
55 |
I2C_write(0x39); |
|
|
56 |
i2c_stop(); |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
|
|
|
60 |
byte mpl3115_read (byte reg) |
|
|
61 |
{ |
|
|
62 |
byte i; |
|
|
63 |
|
|
|
64 |
i2c_start(); |
|
|
65 |
I2C_Write(MPL3115_ADDR_W); |
|
|
66 |
I2C_write(reg); |
|
|
67 |
i2c_start(); |
|
|
68 |
I2C_Write(MPL3115_ADDR_R); |
|
|
69 |
i=i2c_read(0); |
|
|
70 |
i2c_stop(); |
|
|
71 |
|
|
|
72 |
return i; |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
float mpl3115_T (void) //teplota ve stupnich |
|
|
77 |
{ |
|
|
78 |
int m; |
|
|
79 |
float l, t; |
|
|
80 |
|
|
|
81 |
m = mpl3115_read (0x04); |
|
|
82 |
l = (float)(mpl3115_read(0x05)>>4)/16.0; |
|
|
83 |
t = (float)(M + L); |
|
|
84 |
return t; |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
float mpl3115_A (void) //vyska v m |
|
|
88 |
{ |
|
|
89 |
|
|
|
90 |
int16 m, c; |
|
|
91 |
float l, a; |
|
|
92 |
|
|
|
93 |
m = mpl3115_read (0x01); |
|
|
94 |
c = mpl3115_read (0x02); |
|
|
95 |
l = (float)(mpl3115_read(0x03)>>4)/16.0; |
|
|
96 |
a = (float)((m << 8)|c) + l; |
|
|
97 |
return a; |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
|
|
|
101 |
float mpl3115_P (void) //tlak v Pa |
|
|
102 |
{ |
|
|
103 |
unsigned int32 m; |
|
|
104 |
unsigned int16 c; |
|
|
105 |
unsigned int l; |
|
|
106 |
float p, l1; |
|
|
107 |
|
|
|
108 |
m = mpl3115_read (0x01); |
|
|
109 |
c = mpl3115_read (0x02); |
|
|
110 |
l = mpl3115_read(0x03); |
|
|
111 |
|
|
|
112 |
l1= (float)(l>>4)/4.0; |
|
|
113 |
p = (float)((m << 10)|(c<<2)|(l>>6)) + l1; |
|
|
114 |
|
|
|
115 |
return p; |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
void main() |
|
|
119 |
{ |
|
|
120 |
|
|
|
121 |
float p, t, a; |
|
|
122 |
printf("\r\nMLAB Barometer and Altimeter demo code \r\n",); |
|
|
123 |
printf("Temperature [deg C] Preassure [Pa] Altitude [m] \r\n",); |
|
|
124 |
|
|
|
125 |
while(TRUE) |
|
|
126 |
{ |
|
|
127 |
mpl3115_setP(); //nastaveni pro tlak a teplotu |
|
|
128 |
delay_ms (500); |
|
|
129 |
t=mpl3115_T(); |
|
|
130 |
p=mpl3115_P(); |
|
|
131 |
|
|
|
132 |
mpl3115_setA(); //nastaveni pro vysku a teplotu |
|
|
133 |
delay_ms (500); |
|
|
134 |
a=mpl3115_A(); |
|
|
135 |
|
|
|
136 |
printf("%3.1f %6.1f %5.2f \r\n", t, p, a); |
|
|
137 |
|
|
|
138 |
delay_ms (1000); |
|
|
139 |
} |
|
|
140 |
} |
|
|
141 |
|
|
|
142 |
|