Rev Author Line No. Line
2917 jacho 1 //Jan Chroust 15.4.2013
2  
3  
2948 kaklik 4 #include "main.h"
2917 jacho 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, c;
93 float l, a;
94  
95 m= mpl3115_read (0x01);
96 c= mpl3115_read (0x02);
97 l= (float)(mpl3115_read(0x03)>>4)/16.0;
98 a = (float)((m << 8)|c) + l;
99 return a;
100 }
101  
102  
103 float mpl3115_P (void) //tlak v Pa
104 {
105 unsigned int32 m;
106 unsigned int16 c;
107 unsigned int l;
108 float p, l1;
109  
110 m= mpl3115_read (0x01);
111 c= mpl3115_read (0x02);
112 l=mpl3115_read(0x03);
113  
114 l1= (float)(l>>4)/4.0;
115 p = (float)((m << 10)|(c<<2)|(l>>6)) + l1;
116  
117 return p;
118 }
119  
120 void main()
121 {
122  
123 float p, t, a;
124 printf("Tlakomer nebo vyskomer \r\n",);
125  
126 while(TRUE)
127 {
128 mpl3115_setP(); //nastaveni pro tlak a teplotu
129 delay_ms (500);
130 t=mpl3115_T();
131 p=mpl3115_P();
132  
133 mpl3115_setA(); //nastaveni pro vysku a teplotu
134 delay_ms (500);
135 a=mpl3115_A();
136  
137  
138 printf("Teplota: %10.4f(stupne)\r\n", t);
139 printf("Tlak: %10.4f(Pa)\r\n", p);
140 printf("Výška: %10.4f(m)\r\n", a);
141  
142 delay_ms (1000);
143 }
144  
145  
146 }
147  
148