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