Rev Author Line No. Line
3136 kaklik 1 /**** Automatic weather station 01A ****/
2 #define VERSION "0.2"
3 #define ID "$Id: main.c 3136 2013-07-07 21:55:22Z kaklik $"
4 #include "main.h"
5 #include ".\common\dbloader.h"
6 #include <string.h>
7  
8 #CASE // Case sensitive compiler
9  
10 #define SEND_DELAY 50 // Time between two characters on RS232
11 #define RESPONSE_DELAY 100 // Reaction time after receiving a command
12 #define MEASURE_DELAY 1000 // Delay to a next measurement
13  
14 char VER[4]=VERSION; // Buffer for concatenate of a version string
15  
16 #define ONE_WIRE_PIN PIN_B1 // DS18B20 sensor connection
17 #include "..\ds1820.c"
18  
19 #define sht_data_pin PIN_D0 // SHT11 sensor connection
20 #define sht_clk_pin PIN_D1
21 #include "..\SHT.c"
22  
23 #use i2c(master, sda=PIN_D2, scl=PIN_D3)
24 #include "..\SHT25.h"
25  
26 #define CSN_SPI PIN_C2 // preassure sensor connection
27 #include "..\MPL115A1.c"
28  
29 unsigned int16 timer0_overflow_count;
30 unsigned int16 timer1_overflow_count;
31 unsigned int16 timer0_overflow_count_last;
32 unsigned int16 timer0_last;
33 unsigned int16 anemo_count_max;
34  
35 int1 barometer_present;
36  
37 #int_TIMER1
38 void TIMER1_isr(void)
39 {
40 // 32.768 kHz crystal, 16bit counter => every 2secs interrupt
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;
45 timer0_last = timer0;
46 if (anemo_count > anemo_count_max) anemo_count_max=anemo_count;
47  
48 timer1_overflow_count++;
49 }
50  
51 #int_TIMER0 // anemometr pulses counting timer owerflow
52 void TIMER0_isr(void)
53 {
54 timer0_overflow_count++;
55 }
56  
57 /*#int_default
58 void default_isr()
59 {
60 printf("Unexplained interrupt\r\n");
61 }
62 */
63 void welcome(void) // Welcome message
64 {
65 char REV[50]=ID; // Buffer for concatenate of a version string
66  
67 if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0;
68 printf("\r\n\r\n# AWS01A %s (C) 2013 www.mlab.cz \r\n",VER); // Welcome message
69 printf("#%s\r\n",&REV[4]);
70 // printf("# ver seq ");
71 // printf("#temp[mK] hum_temp[mK] hum[%%] ");
72 // printf("bar_temp[mK] pressure[hPa] Anemo[m/s]check\r\n\r\n");
73 }
74  
75 void print_slow(char *output, int8 *check)
76 {
77 int8 j; // String pointer
78 j=0;
79 while(output[j]!=0)
80 {
81 delay_us(SEND_DELAY);
82 putc(output[j]);
83 *check^=output[j++];
84 }
85 }
86  
87  
88 void main()
89 {
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;
95  
96 setup_oscillator(OSC_8MHZ); // pri prouziti bootloaderu neni treba nastavovat
97 setup_wdt(WDT_2304MS);
98 restart_wdt(); //---WDT
99 setup_adc_ports(NO_ANALOGS|VSS_VDD);
100 setup_adc(ADC_CLOCK_DIV_2);
101 setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
102 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
103 setup_timer_2(T2_DISABLED,0,1);
104 setup_ccp1(CCP_OFF);
105 setup_comparator(NC_NC_NC_NC); // This device COMP currently not supported by the PICWizard
106 setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
107 output_high(CSN_SPI);
108 int1 repeat;
109  
110 welcome(); // welcome print and device indentification
111  
112 enable_interrupts(INT_TIMER1); // interrupts used for anemometer readings
113 enable_interrupts(INT_TIMER0);
114 enable_interrupts(GLOBAL);
115  
116 restart_wdt(); //---WDT
117  
118 // barometer init
119 barometer_present = MPL_init(); // get correction coefficients from the sensor
120  
121 sht_init();
122  
123 SHT25_soft_reset();
124  
125 // anemometer init
126 set_timer0(0);
127 set_timer1(0);
128 timer0_overflow_count=0;
129 repeat=TRUE;
130  
131 restart_wdt(); //---WDT
132 delay_ms(1000);
133  
134 while (TRUE)
135 {
136 do
137 {
138 delay_ms(RESPONSE_DELAY);
139 //---WDT
140 restart_wdt();
141 } while (!kbhit()&&!repeat);
142  
143 //---WDT
144 restart_wdt();
145  
146 { // Retrieve command
147 char ch='k';
148  
149 if(kbhit()) ch=getc();
150  
151 switch (ch)
152 {
153 case 'i':
154 welcome(); // Information about version, etc...
155 break; // Only when dome is closed
156  
157 case 's':
158 repeat=FALSE; // Single measure mode
159 break;
160  
161 case 'r':
162 repeat=TRUE; // Repeat mode
163 break;
164  
165 case 'u':
166 reset_cpu(); // Update firmware
167 }
168 }
169  
170 char output[8]; // Output buffer
171 int8 check=0; // Checksum is calculated between '$' and '*'
172 float SHT_temp1=0,SHT_hum1=0;
173 float SHT_temp2=0,SHT_hum2=0;
174 int16 local_temp;
175 float barometer_temperature;
176 float barometer_pressure;
177 float anemo;
178  
179 { // printf
180 local_temp = (int16)ds1820_read();
181 sht_rd(SHT_temp1,SHT_hum1);
182 //SHT_temp1 = (SHT_temp1 + 273.15)*100;
183  
184 SHT_temp2 = SHT25_get_temp();
185 SHT_hum2 = SHT25_get_hum();
186 //SHT_temp2 = (SHT_temp2 + 273.15)*100;
187 if (barometer_present == TRUE)
188 {
189 barometer_temperature = MPL_get_temperature();
190 barometer_pressure = MPL_get_pressure() * 10.0; // conversion to hectopascals
191 }
192 else
193 {
194 barometer_temperature = 0;
195 barometer_pressure = 0;
196 }
197  
198 delay_us(SEND_DELAY);
199 putc('$');
200 delay_us(SEND_DELAY);
201  
202 sprintf(output,"AWS%s \0",VER);
203 print_slow(output, &check);
204 sprintf(output,"%Lu \0", seq);
205 print_slow(output, &check);
206 sprintf(output,"%Ld \0", local_temp);
207 print_slow(output, &check);
208 sprintf(output,"%3.1f \0", SHT_temp1);
209 print_slow(output, &check);
210 sprintf(output,"%3.1f \0", SHT_hum1);
211 print_slow(output, &check);
212 sprintf(output,"%3.1f \0", SHT_temp2);
213 print_slow(output, &check);
214 sprintf(output,"%3.1f \0", SHT_hum2);
215 print_slow(output, &check);
216 sprintf(output,"%3.1f \0", barometer_temperature);
217 print_slow(output, &check);
218 sprintf(output,"%5.1f \0", barometer_pressure);
219 print_slow(output, &check);
220  
221 // optimization: (timer1_overflow_count << 16)/32768.0 = timer1_overflow_count << 1, so we can use int16 (and not int32)
222 anemo = ((float)((timer0_overflow_count << 8) + get_timer0()))/((float)(timer1_overflow_count << 1) + (float)(get_timer1())/32768.0); // pulses per second calculation
223 anemo = anemo / 10.5; // frequency divided by anemomether constant.
224  
225 set_timer0(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;
231  
232 sprintf(output,"%3.1f \0", anemo);
233 print_slow(output, &check);
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.
239 anemo_count_max = 0;
240 }
241  
242 sprintf(output,"%3.1f \0", anemo);
243 print_slow(output, &check);
244  
245 sprintf(output,"*%X\r\n\0", check);
246 print_slow(output, &check);
247  
248 delay_us(SEND_DELAY);
249 }
250  
251 //---WDT
252 restart_wdt();
253 seq++; // Increment the number of measurement
254 delay_ms(MEASURE_DELAY);
255 }
256 }
257