Rev 3133 Rev 3136
Line 1... Line 1...
1 /**** Automatic weather station 01A ****/ 1 /**** Automatic weather station 01A ****/
2 #define VERSION "0.1" 2 #define VERSION "0.2"
3 #define ID "$Id: main.c 3133 2013-07-04 11:36:46Z kaklik $" 3 #define ID "$Id: main.c 3136 2013-07-07 21:55:22Z kaklik $"
4 #include "main.h" 4 #include "main.h"
5 #include ".\common\dbloader.h" 5 #include ".\common\dbloader.h"
6 #include <string.h> 6 #include <string.h>
7   7  
8 #CASE // Case sensitive compiler 8 #CASE // Case sensitive compiler
Line 26... Line 26...
26 #define CSN_SPI PIN_C2 // preassure sensor connection 26 #define CSN_SPI PIN_C2 // preassure sensor connection
27 #include "..\MPL115A1.c" 27 #include "..\MPL115A1.c"
28   28  
29 unsigned int16 timer0_overflow_count; 29 unsigned int16 timer0_overflow_count;
30 unsigned int16 timer1_overflow_count; 30 unsigned int16 timer1_overflow_count;
-   31 unsigned int16 timer0_overflow_count_last;
-   32 unsigned int16 timer0_last;
31 float anemo_max; 33 unsigned int16 anemo_count_max;
32   34  
33 int1 barometer_present; 35 int1 barometer_present;
34   36  
35 float anemo_compute() -  
36 { -  
37 float anemo; -  
38 anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(((timer1_overflow_count * 0xFFFF) + get_timer1())/32768.0); // pulses per second calculation -  
39 anemo = anemo / 10.5; // frequency divided by anemomether constant. -  
40 return anemo; -  
41 } -  
42   -  
43 #int_TIMER1 37 #int_TIMER1
44 void TIMER1_isr(void) 38 void TIMER1_isr(void)
45 { 39 {
-   40 // 32.768 kHz crystal, 16bit counter => every 2secs interrupt
46 float anemo; 41 unsigned int16 anemo_count;
-   42 unsigned int16 timer0 = get_timer0();
-   43 anemo_count = (((timer0_overflow_count - timer0_overflow_count_last) << 8) + (timer0 - timer0_last));
-   44 timer0_overflow_count_last = timer0_overflow_count;
47 anemo = anemo_compute(); 45 timer0_last = timer0;
48 if (anemo > anemo_max) anemo_max=anemo; 46 if (anemo_count > anemo_count_max) anemo_count_max=anemo_count;
49   47  
50 timer1_overflow_count++; 48 timer1_overflow_count++;
51 } 49 }
52   50  
53 #int_TIMER0 // anemometr pulses counting timer owerflow 51 #int_TIMER0 // anemometr pulses counting timer owerflow
Line 88... Line 86...
88   86  
89   87  
90 void main() 88 void main()
91 { 89 {
92 unsigned int16 seq=0; 90 unsigned int16 seq=0;
-   91 timer0_overflow_count=0;
-   92 timer1_overflow_count=0;
-   93 timer0_overflow_count_last=0;
-   94 timer0_last=0;
93   95  
94 setup_oscillator(OSC_8MHZ); // pri prouziti bootloaderu neni treba nastavovat 96 setup_oscillator(OSC_8MHZ); // pri prouziti bootloaderu neni treba nastavovat
95 setup_wdt(WDT_2304MS); 97 setup_wdt(WDT_2304MS);
96 restart_wdt(); //---WDT 98 restart_wdt(); //---WDT
97 setup_adc_ports(NO_ANALOGS|VSS_VDD); 99 setup_adc_ports(NO_ANALOGS|VSS_VDD);
Line 102... Line 104...
102 setup_ccp1(CCP_OFF); 104 setup_ccp1(CCP_OFF);
103 setup_comparator(NC_NC_NC_NC); // This device COMP currently not supported by the PICWizard 105 setup_comparator(NC_NC_NC_NC); // This device COMP currently not supported by the PICWizard
104 setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64); 106 setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
105 output_high(CSN_SPI); 107 output_high(CSN_SPI);
106 int1 repeat; 108 int1 repeat;
107 float anemo; -  
108   109  
109 welcome(); // welcome print and device indentification 110 welcome(); // welcome print and device indentification
110 111
111 enable_interrupts(INT_TIMER1); // interrupts used for anemometer readings 112 enable_interrupts(INT_TIMER1); // interrupts used for anemometer readings
112 enable_interrupts(INT_TIMER0); 113 enable_interrupts(INT_TIMER0);
Line 123... Line 124...
123 124
124 // anemometer init 125 // anemometer init
125 set_timer0(0); 126 set_timer0(0);
126 set_timer1(0); 127 set_timer1(0);
127 timer0_overflow_count=0; 128 timer0_overflow_count=0;
128 anemo=0; -  
129 repeat=TRUE; 129 repeat=TRUE;
130 130
131 restart_wdt(); //---WDT 131 restart_wdt(); //---WDT
132 delay_ms(1000); 132 delay_ms(1000);
133   133  
Line 169... Line 169...
169   169  
170 char output[8]; // Output buffer 170 char output[8]; // Output buffer
171 int8 check=0; // Checksum is calculated between '$' and '*' 171 int8 check=0; // Checksum is calculated between '$' and '*'
172 float SHT_temp1=0,SHT_hum1=0; 172 float SHT_temp1=0,SHT_hum1=0;
173 float SHT_temp2=0,SHT_hum2=0; 173 float SHT_temp2=0,SHT_hum2=0;
174 float local_temp; 174 int16 local_temp;
175 float barometer_temperature, barometer_pressure; 175 float barometer_temperature;
-   176 float barometer_pressure;
176 float anemo; 177 float anemo;
177   178  
178   -  
179 { // printf 179 { // printf
180   -  
181 local_temp = ds1820_read()+27315; 180 local_temp = (int16)ds1820_read();
182 sht_rd(SHT_temp1,SHT_hum1); 181 sht_rd(SHT_temp1,SHT_hum1);
183 SHT_temp1 = (SHT_temp1 + 273.15)*100; 182 //SHT_temp1 = (SHT_temp1 + 273.15)*100;
184 183
185 SHT_temp2 = SHT25_get_temp(); 184 SHT_temp2 = SHT25_get_temp();
186 SHT_hum2 = SHT25_get_hum(); 185 SHT_hum2 = SHT25_get_hum();
187 SHT_temp2 = (SHT_temp2 + 273.15)*100; 186 //SHT_temp2 = (SHT_temp2 + 273.15)*100;
188 if (barometer_present == TRUE) 187 if (barometer_present == TRUE)
189 { 188 {
190 barometer_temperature = (MPL_get_temperature() + 273.15)*100; 189 barometer_temperature = MPL_get_temperature();
191 barometer_pressure = MPL_get_pressure() * 10.0; // conversion to hectopascals 190 barometer_pressure = MPL_get_pressure() * 10.0; // conversion to hectopascals
192 } 191 }
193 else 192 else
194 { 193 {
195 barometer_temperature = 0; 194 barometer_temperature = 0;
Line 197... Line 196...
197 } 196 }
198   197
199 delay_us(SEND_DELAY); 198 delay_us(SEND_DELAY);
200 putc('$'); 199 putc('$');
201 delay_us(SEND_DELAY); 200 delay_us(SEND_DELAY);
-   201
202 sprintf(output,"AWS%s \0",VER); 202 sprintf(output,"AWS%s \0",VER);
203 print_slow(output, &check); 203 print_slow(output, &check);
204 sprintf(output,"%Lu \0", seq); 204 sprintf(output,"%Lu \0", seq);
205 print_slow(output, &check); 205 print_slow(output, &check);
206 sprintf(output,"%5.0f \0", local_temp ); 206 sprintf(output,"%Ld \0", local_temp);
207 print_slow(output, &check); 207 print_slow(output, &check);
208 sprintf(output,"%5.0f \0", SHT_temp1); 208 sprintf(output,"%3.1f \0", SHT_temp1);
209 print_slow(output, &check); 209 print_slow(output, &check);
210 sprintf(output,"%3.1f \0", SHT_hum1); 210 sprintf(output,"%3.1f \0", SHT_hum1);
211 print_slow(output, &check); 211 print_slow(output, &check);
212 sprintf(output,"%5.0f \0", SHT_temp2); 212 sprintf(output,"%3.1f \0", SHT_temp2);
213 print_slow(output, &check); 213 print_slow(output, &check);
214 sprintf(output,"%3.1f \0", SHT_hum2); 214 sprintf(output,"%3.1f \0", SHT_hum2);
215 print_slow(output, &check); 215 print_slow(output, &check);
216 sprintf(output,"%5.0f \0", barometer_temperature); 216 sprintf(output,"%3.1f \0", barometer_temperature);
217 print_slow(output, &check); 217 print_slow(output, &check);
218 sprintf(output,"%5.1f \0", barometer_pressure); 218 sprintf(output,"%5.1f \0", barometer_pressure);
219 print_slow(output, &check); 219 print_slow(output, &check);
220 220  
-   221 // optimization: (timer1_overflow_count << 16)/32768.0 = timer1_overflow_count << 1, so we can use int16 (and not int32)
221 //anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(((timer1_overflow_count * 0xFFFF) + get_timer1())/32768.0); // pulses per second calculation 222 anemo = ((float)((timer0_overflow_count << 8) + get_timer0()))/((float)(timer1_overflow_count << 1) + (float)(get_timer1())/32768.0); // pulses per second calculation
222 //anemo = anemo / 10.5; // frequency divided by anemomether constant. 223 anemo = anemo / 10.5; // frequency divided by anemomether constant.
223 sprintf(output,"%3.1f \0", anemo_compute()); -  
224   224  
225 timer0_overflow_count=0; -  
226 timer1_overflow_count=0; -  
227 set_timer0(0); 225 set_timer0(0);
228 set_timer1(0); 226 set_timer1(0);
-   227 timer0_overflow_count=0;
-   228 timer1_overflow_count=0;
-   229 timer0_overflow_count_last=0;
-   230 timer0_last=0;
229 231
-   232 sprintf(output,"%3.1f \0", anemo);
230 print_slow(output, &check); 233 print_slow(output, &check);
231 //sprintf(output,"%3.1f \0", anemo_max); -  
232 //print_slow(output, &check); -  
233   234
-   235 if (anemo_count_max > 0)
-   236 {
-   237 // anemo_max comptutation; >>1 is division by two, which comes from the 2secs interval from timer1
-   238 anemo = (float)(anemo_count_max >> 1) / 10.5; // frequency divided by anemomether constant.
234 //anemo_max = 0; 239 anemo_count_max = 0;
-   240 }
-   241  
-   242 sprintf(output,"%3.1f \0", anemo);
-   243 print_slow(output, &check);
235   244
236 sprintf(output,"*%X\r\n\0", check); 245 sprintf(output,"*%X\r\n\0", check);
237 print_slow(output, &check); 246 print_slow(output, &check);
238   247  
239 delay_us(SEND_DELAY); 248 delay_us(SEND_DELAY);