2978 |
kaklik |
1 |
#define VERSION "0.1" |
|
|
2 |
#define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $" |
2917 |
jacho |
3 |
|
2948 |
kaklik |
4 |
#include "main.h" |
2917 |
jacho |
5 |
|
2978 |
kaklik |
6 |
#use i2c(master, sda=PIN_B0, scl=PIN_B1) |
2917 |
jacho |
7 |
#use rs232(baud=9600,parity=N,xmit=PIN_B3,rcv=PIN_B2,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 |
|
2978 |
kaklik |
81 |
m = mpl3115_read (0x04); |
|
|
82 |
l = (float)(mpl3115_read(0x05)>>4)/16.0; |
2917 |
jacho |
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 |
|
2978 |
kaklik |
93 |
m = mpl3115_read (0x01); |
|
|
94 |
c = mpl3115_read (0x02); |
|
|
95 |
l = (float)(mpl3115_read(0x03)>>4)/16.0; |
2917 |
jacho |
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 |
|
2978 |
kaklik |
108 |
m = mpl3115_read (0x01); |
|
|
109 |
c = mpl3115_read (0x02); |
|
|
110 |
l = mpl3115_read(0x03); |
2917 |
jacho |
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("Tlakomer nebo vyskomer \r\n",); |
|
|
123 |
|
|
|
124 |
while(TRUE) |
|
|
125 |
{ |
|
|
126 |
mpl3115_setP(); //nastaveni pro tlak a teplotu |
|
|
127 |
delay_ms (500); |
|
|
128 |
t=mpl3115_T(); |
|
|
129 |
p=mpl3115_P(); |
|
|
130 |
|
|
|
131 |
mpl3115_setA(); //nastaveni pro vysku a teplotu |
|
|
132 |
delay_ms (500); |
|
|
133 |
a=mpl3115_A(); |
|
|
134 |
|
|
|
135 |
|
2978 |
kaklik |
136 |
printf("Teplota: %1.4f\r\n", t); |
2917 |
jacho |
137 |
printf("Tlak: %10.4f(Pa)\r\n", p); |
|
|
138 |
printf("Výka: %10.4f(m)\r\n", a); |
|
|
139 |
|
2978 |
kaklik |
140 |
delay_ms (1000); |
|
|
141 |
} |
2917 |
jacho |
142 |
} |
|
|
143 |
|
|
|
144 |
|