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 |
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 |
54 |
void TIMER0_isr(void) |
52 |
void TIMER0_isr(void) |
55 |
{ |
53 |
{ |
56 |
timer0_overflow_count++; |
54 |
timer0_overflow_count++; |
57 |
} |
55 |
} |
58 |
|
56 |
|
59 |
/*#int_default |
57 |
/*#int_default |
60 |
void default_isr() |
58 |
void default_isr() |
61 |
{ |
59 |
{ |
62 |
printf("Unexplained interrupt\r\n"); |
60 |
printf("Unexplained interrupt\r\n"); |
63 |
} |
61 |
} |
64 |
*/ |
62 |
*/ |
65 |
void welcome(void) // Welcome message |
63 |
void welcome(void) // Welcome message |
66 |
{ |
64 |
{ |
67 |
char REV[50]=ID; // Buffer for concatenate of a version string |
65 |
char REV[50]=ID; // Buffer for concatenate of a version string |
68 |
|
66 |
|
69 |
if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0; |
67 |
if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0; |
70 |
printf("\r\n\r\n# AWS01A %s (C) 2013 www.mlab.cz \r\n",VER); // Welcome message |
68 |
printf("\r\n\r\n# AWS01A %s (C) 2013 www.mlab.cz \r\n",VER); // Welcome message |
71 |
printf("#%s\r\n",&REV[4]); |
69 |
printf("#%s\r\n",&REV[4]); |
72 |
// printf("# ver seq "); |
70 |
// printf("# ver seq "); |
73 |
// printf("#temp[mK] hum_temp[mK] hum[%%] "); |
71 |
// printf("#temp[mK] hum_temp[mK] hum[%%] "); |
74 |
// printf("bar_temp[mK] pressure[hPa] Anemo[m/s]check\r\n\r\n"); |
72 |
// printf("bar_temp[mK] pressure[hPa] Anemo[m/s]check\r\n\r\n"); |
75 |
} |
73 |
} |
76 |
|
74 |
|
77 |
void print_slow(char *output, int8 *check) |
75 |
void print_slow(char *output, int8 *check) |
78 |
{ |
76 |
{ |
79 |
int8 j; // String pointer |
77 |
int8 j; // String pointer |
80 |
j=0; |
78 |
j=0; |
81 |
while(output[j]!=0) |
79 |
while(output[j]!=0) |
82 |
{ |
80 |
{ |
83 |
delay_us(SEND_DELAY); |
81 |
delay_us(SEND_DELAY); |
84 |
putc(output[j]); |
82 |
putc(output[j]); |
85 |
*check^=output[j++]; |
83 |
*check^=output[j++]; |
86 |
} |
84 |
} |
87 |
} |
85 |
} |
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); |
98 |
setup_adc(ADC_CLOCK_DIV_2); |
100 |
setup_adc(ADC_CLOCK_DIV_2); |
99 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
101 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
100 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); |
102 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); |
101 |
setup_timer_2(T2_DISABLED,0,1); |
103 |
setup_timer_2(T2_DISABLED,0,1); |
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); |
113 |
enable_interrupts(GLOBAL); |
114 |
enable_interrupts(GLOBAL); |
114 |
|
115 |
|
115 |
restart_wdt(); //---WDT |
116 |
restart_wdt(); //---WDT |
116 |
|
117 |
|
117 |
// barometer init |
118 |
// barometer init |
118 |
barometer_present = MPL_init(); // get correction coefficients from the sensor |
119 |
barometer_present = MPL_init(); // get correction coefficients from the sensor |
119 |
|
120 |
|
120 |
sht_init(); |
121 |
sht_init(); |
121 |
|
122 |
|
122 |
SHT25_soft_reset(); |
123 |
SHT25_soft_reset(); |
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 |
|
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 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; |
196 |
barometer_pressure = 0; |
195 |
barometer_pressure = 0; |
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); |
240 |
} |
249 |
} |
241 |
|
250 |
|
242 |
//---WDT |
251 |
//---WDT |
243 |
restart_wdt(); |
252 |
restart_wdt(); |
244 |
seq++; // Increment the number of measurement |
253 |
seq++; // Increment the number of measurement |
245 |
delay_ms(MEASURE_DELAY); |
254 |
delay_ms(MEASURE_DELAY); |
246 |
} |
255 |
} |
247 |
} |
256 |
} |
248 |
|
257 |
|