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