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