2980 |
kaklik |
1 |
|
|
|
2 |
//Meteorologicka cast |
|
|
3 |
#define VERSION "0.1" |
|
|
4 |
#define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $" |
|
|
5 |
|
|
|
6 |
#include <main.h> |
|
|
7 |
#include <math.h> |
|
|
8 |
#include <string.h> |
|
|
9 |
|
|
|
10 |
#include "SHT25.h" |
|
|
11 |
#include "MPL3115.h" |
|
|
12 |
#include "HMC5883L.h" |
|
|
13 |
|
|
|
14 |
unsigned int16 timer0_overflow_count; |
|
|
15 |
float anemo=0; |
|
|
16 |
|
|
|
17 |
#int_TIMER1 |
|
|
18 |
void TIMER1_isr(void) |
|
|
19 |
{ |
|
|
20 |
// 32.768 kHz krystal pro timer1 oscilátor |
|
|
21 |
anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(32768.0/0xFFFF); // pocet pulzu za 1s |
|
|
22 |
anemo = anemo / 2; //pocet otacek za sekundu |
|
|
23 |
|
|
|
24 |
timer0_overflow_count=0; //nulovani |
|
|
25 |
set_timer0(0); |
|
|
26 |
set_timer1(0); |
|
|
27 |
|
|
|
28 |
} |
|
|
29 |
|
|
|
30 |
#int_TIMER0 //pro preteceni èítaèe pùlzù od anemometru (RA4) |
|
|
31 |
void TIMER0_isr(void) |
|
|
32 |
{ |
|
|
33 |
timer0_overflow_count++; |
|
|
34 |
} |
|
|
35 |
|
|
|
36 |
|
|
|
37 |
float azimut (void) //vypocet azimutu smeru vetru |
|
|
38 |
{ |
|
|
39 |
signed int16 X,Y,Z; |
|
|
40 |
X= mag_readX(); |
|
|
41 |
Y= mag_readY(); |
|
|
42 |
Z= mag_readZ(); |
|
|
43 |
|
|
|
44 |
float a, b; |
|
|
45 |
a=(float)Y/X; |
|
|
46 |
b=atan(a); |
|
|
47 |
b = (b/3.14)*180; |
|
|
48 |
b=abs(b); |
|
|
49 |
|
|
|
50 |
if(X==0) //osetreni proti deleni 0 |
|
|
51 |
{ |
|
|
52 |
if(Y>0) |
|
|
53 |
{ |
|
|
54 |
b=90; |
|
|
55 |
} |
|
|
56 |
else |
|
|
57 |
{ |
|
|
58 |
b=270; |
|
|
59 |
} |
|
|
60 |
} |
|
|
61 |
else |
|
|
62 |
{ |
|
|
63 |
if(X>0) |
|
|
64 |
{ |
|
|
65 |
if(Y>=0) |
|
|
66 |
{ |
|
|
67 |
b=180+b; |
|
|
68 |
|
|
|
69 |
} |
|
|
70 |
else |
|
|
71 |
{ |
|
|
72 |
b=180-b; |
|
|
73 |
|
|
|
74 |
} |
|
|
75 |
} |
|
|
76 |
else |
|
|
77 |
{ |
|
|
78 |
if(Y>=0) |
|
|
79 |
{ |
|
|
80 |
b=360-b; |
|
|
81 |
|
|
|
82 |
} |
|
|
83 |
else |
|
|
84 |
{ |
|
|
85 |
b=b; |
|
|
86 |
|
|
|
87 |
} |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
} |
|
|
91 |
|
|
|
92 |
return b; |
|
|
93 |
|
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
|
|
|
97 |
void uvitani(void) // uvodni zprava |
|
|
98 |
{ |
|
|
99 |
printf("\r\n\r\n# Meteorologicka stanice %s (C) 2013 www.mlab.cz \r\n",VERSION); |
|
|
100 |
printf("\r\n %s \r\n",ID);// Welcome message |
|
|
101 |
printf("# ver poradi "); |
|
|
102 |
printf("altimet_t[°C] altimet_a[m] altimet_p[Pa] "); |
|
|
103 |
printf("sht_t[°C] sht_h[%%] Anemo[m/s]check\r\n\r\n"); |
|
|
104 |
} |
|
|
105 |
|
|
|
106 |
void main() |
|
|
107 |
{ |
|
|
108 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
|
|
109 |
setup_adc(ADC_CLOCK_DIV_2); |
|
|
110 |
setup_spi(SPI_SS_DISABLED); |
|
|
111 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
|
|
112 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); |
|
|
113 |
setup_timer_2(T2_DISABLED,0,1); |
|
|
114 |
setup_ccp1(CCP_OFF); |
|
|
115 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
|
|
116 |
|
|
|
117 |
|
|
|
118 |
float altimet_t; //teplota z MPL3115 |
|
|
119 |
float altimet_p; //tlak z MPL3115 |
|
|
120 |
float altimet_a; //vyska z MPL3115 |
|
|
121 |
float sht25_t; //teplota z SHT25 |
|
|
122 |
float sht25_h; //relativni vlhkost z SHT25 |
|
|
123 |
float smer_v; //smer vetru |
|
|
124 |
|
|
|
125 |
unsigned int8 sht_config; |
|
|
126 |
|
|
|
127 |
//nastaveni SHT25 |
|
|
128 |
SHT25_soft_reset(); |
|
|
129 |
sht_config = SHT25_RH12_T14 | SHT25_HEATER_OFF; //vypnuti topeni v SHT25 |
|
|
130 |
SHT25_setup(sht_config); |
|
|
131 |
|
|
|
132 |
//nastavení pøeruení pro anemometr |
|
|
133 |
enable_interrupts(INT_TIMER1); |
|
|
134 |
enable_interrupts(INT_TIMER0); |
|
|
135 |
enable_interrupts(GLOBAL); |
|
|
136 |
|
|
|
137 |
|
|
|
138 |
//uvitani(); |
|
|
139 |
set_mag(); //nastaveni magnetometru pro smer vetru |
|
|
140 |
|
|
|
141 |
// vynulovani promenych pro anemometr |
|
|
142 |
set_timer0(0); |
|
|
143 |
set_timer1(0); |
|
|
144 |
timer0_overflow_count=0; |
|
|
145 |
anemo=0; |
|
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
while(TRUE) |
|
|
150 |
{ |
|
|
151 |
char dataOUT[60]; |
|
|
152 |
int i=0; |
|
|
153 |
|
|
|
154 |
mpl3115_setP(); //nastaveni pro tlak a teplotu |
|
|
155 |
delay_ms (500); |
|
|
156 |
altimet_t=mpl3115_T(); |
|
|
157 |
altimet_p=mpl3115_P(); |
|
|
158 |
|
|
|
159 |
mpl3115_setA(); //nastaveni pro vysku a teplotu |
|
|
160 |
delay_ms (500); |
|
|
161 |
altimet_a=mpl3115_A(); |
|
|
162 |
|
|
|
163 |
sht25_t=SHT25_get_temp(); //mereni hodnot z SHT25 |
|
|
164 |
sht25_h=SHT25_get_hum(); |
|
|
165 |
|
|
|
166 |
smer_v=azimut(); //vrati azimut aktualniho smeru vetru |
|
|
167 |
|
|
|
168 |
sprintf(dataOUT, " %6.2f %7.2f %9.2f %6.2f %6.2f %5.2f %6.2f \n\r", altimet_t, altimet_a, altimet_p, sht25_t, sht25_h, smer_v, anemo); |
|
|
169 |
|
|
|
170 |
printf(dataOUT); //zaslani hodnot na seriovou linku |
|
|
171 |
|
|
|
172 |
delay_ms(1000); |
|
|
173 |
} |
|
|
174 |
} |
|
|
175 |
|