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