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