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