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