2752 |
kaklik |
1 |
/**** Automatic weather station 01A ****/ |
|
|
2 |
#define VERSION "0.1" |
2806 |
kakl |
3 |
#define ID "$Id: main.c 3083 2013-06-18 10:50:31Z kaklik $" |
2751 |
kaklik |
4 |
#include "main.h" |
2828 |
kaklik |
5 |
#include ".\common\dbloader.h" |
2752 |
kaklik |
6 |
#include <string.h> |
2751 |
kaklik |
7 |
|
2752 |
kaklik |
8 |
#CASE // Case sensitive compiler |
|
|
9 |
|
|
|
10 |
#define SEND_DELAY 50 // Time between two characters on RS232 |
|
|
11 |
|
|
|
12 |
char VER[4]=VERSION; // Buffer for concatenate of a version string |
|
|
13 |
|
2826 |
kaklik |
14 |
#define ONE_WIRE_PIN PIN_B1 // DS18B20 sensor connection |
2751 |
kaklik |
15 |
#include "..\ds1820.c" |
|
|
16 |
|
2781 |
kaklik |
17 |
#define sht_data_pin PIN_D0 // SHT11 sensor connection |
2779 |
kaklik |
18 |
#define sht_clk_pin PIN_D1 |
2768 |
kaklik |
19 |
#include "..\SHT.c" |
|
|
20 |
|
2916 |
kaklik |
21 |
#use i2c(master, sda=PIN_D2, scl=PIN_D3) |
2913 |
kaklik |
22 |
#include "..\SHT25.h" |
|
|
23 |
|
2781 |
kaklik |
24 |
#define CSN_SPI PIN_C2 // preassure sensor connection |
2779 |
kaklik |
25 |
#include "..\MPL115A1.c" |
2768 |
kaklik |
26 |
|
2915 |
kaklik |
27 |
unsigned int16 timer0_overflow_count; |
|
|
28 |
float anemo; |
2782 |
kaklik |
29 |
|
2907 |
kaklik |
30 |
int1 barometer_present; |
2782 |
kaklik |
31 |
|
|
|
32 |
#int_TIMER1 |
|
|
33 |
void TIMER1_isr(void) |
|
|
34 |
{ |
2907 |
kaklik |
35 |
// wind speed calculation 32.768 kHz crystal on timer1 oscilator expected. |
|
|
36 |
anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(32768.0/0xFFFF); // pulses per second calculation |
|
|
37 |
anemo = anemo / 10.5; // frequency divided by anemomether constant. |
|
|
38 |
|
|
|
39 |
timer0_overflow_count=0; |
|
|
40 |
set_timer0(0); |
|
|
41 |
set_timer1(0); |
|
|
42 |
output_toggle(PIN_E0); |
2782 |
kaklik |
43 |
} |
|
|
44 |
|
3083 |
kaklik |
45 |
#int_TIMER0 // anemometr pulses counting timer owerflow |
2782 |
kaklik |
46 |
void TIMER0_isr(void) |
|
|
47 |
{ |
|
|
48 |
timer0_overflow_count++; |
|
|
49 |
} |
|
|
50 |
|
2913 |
kaklik |
51 |
/*#int_default |
2907 |
kaklik |
52 |
void default_isr() |
2843 |
kaklik |
53 |
{ |
|
|
54 |
printf("Unexplained interrupt\r\n"); |
|
|
55 |
} |
2913 |
kaklik |
56 |
*/ |
2752 |
kaklik |
57 |
void welcome(void) // Welcome message |
|
|
58 |
{ |
|
|
59 |
char REV[50]=ID; // Buffer for concatenate of a version string |
|
|
60 |
|
|
|
61 |
if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0; |
2780 |
kaklik |
62 |
printf("\r\n\r\n# AWS01A %s (C) 2013 www.mlab.cz \r\n",VER); // Welcome message |
2752 |
kaklik |
63 |
printf("#%s\r\n",&REV[4]); |
2913 |
kaklik |
64 |
// printf("# ver seq "); |
|
|
65 |
// printf("#temp[mK] hum_temp[mK] hum[%%] "); |
|
|
66 |
// printf("bar_temp[mK] pressure[hPa] Anemo[m/s]check\r\n\r\n"); |
2752 |
kaklik |
67 |
} |
|
|
68 |
|
2751 |
kaklik |
69 |
void main() |
|
|
70 |
{ |
2752 |
kaklik |
71 |
unsigned int16 seq=0; |
2751 |
kaklik |
72 |
|
2848 |
kakl |
73 |
setup_oscillator(OSC_8MHZ); // pri prouziti bootloaderu neni treba nastavovat |
|
|
74 |
setup_wdt(WDT_2304MS); |
2843 |
kaklik |
75 |
restart_wdt(); //---WDT |
2822 |
kaklik |
76 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
2751 |
kaklik |
77 |
setup_adc(ADC_CLOCK_DIV_2); |
2848 |
kakl |
78 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
2907 |
kaklik |
79 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); |
2751 |
kaklik |
80 |
setup_timer_2(T2_DISABLED,0,1); |
|
|
81 |
setup_ccp1(CCP_OFF); |
2823 |
kaklik |
82 |
setup_comparator(NC_NC_NC_NC); // This device COMP currently not supported by the PICWizard |
2779 |
kaklik |
83 |
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64); |
|
|
84 |
output_high(CSN_SPI); |
2782 |
kaklik |
85 |
|
2834 |
kaklik |
86 |
welcome(); // welcome print and device indentification |
2848 |
kakl |
87 |
|
2897 |
kakl |
88 |
enable_interrupts(INT_TIMER1); // interrupts used for anemometer readings |
|
|
89 |
enable_interrupts(INT_TIMER0); |
2907 |
kaklik |
90 |
enable_interrupts(GLOBAL); |
2848 |
kakl |
91 |
|
|
|
92 |
restart_wdt(); //---WDT |
|
|
93 |
|
2915 |
kaklik |
94 |
// barometer init |
|
|
95 |
barometer_present = MPL_init(); // get correction coefficients from the sensor |
|
|
96 |
|
2848 |
kakl |
97 |
sht_init(); |
2915 |
kaklik |
98 |
|
2913 |
kaklik |
99 |
SHT25_soft_reset(); |
|
|
100 |
|
2915 |
kaklik |
101 |
// anemometer init |
|
|
102 |
set_timer0(0); |
|
|
103 |
set_timer1(0); |
|
|
104 |
timer0_overflow_count=0; |
|
|
105 |
anemo=0; |
|
|
106 |
|
2843 |
kaklik |
107 |
restart_wdt(); //---WDT |
2822 |
kaklik |
108 |
|
2751 |
kaklik |
109 |
while (TRUE) |
|
|
110 |
{ |
2768 |
kaklik |
111 |
char output[8]; // Output buffer |
|
|
112 |
int8 j; // String pointer |
|
|
113 |
int8 check=0; // Checksum is calculated between '$' and '*' |
2907 |
kaklik |
114 |
float SHT_temp1=0,SHT_hum1=0; |
|
|
115 |
float SHT_temp2=0,SHT_hum2=0; |
2779 |
kaklik |
116 |
float local_temp; |
|
|
117 |
float barometer_temperature, barometer_pressure; |
2768 |
kaklik |
118 |
|
2835 |
kaklik |
119 |
delay_ms(1000); |
2752 |
kaklik |
120 |
{ // printf |
|
|
121 |
|
2835 |
kaklik |
122 |
local_temp = ds1820_read()+27315; |
2907 |
kaklik |
123 |
sht_rd(SHT_temp1,SHT_hum1); |
|
|
124 |
SHT_temp1 = (SHT_temp1 + 273.15)*100; |
2779 |
kaklik |
125 |
|
2913 |
kaklik |
126 |
SHT_temp2 = SHT25_get_temp(); |
|
|
127 |
SHT_hum2 = SHT25_get_hum(); |
2915 |
kaklik |
128 |
SHT_temp2 = (SHT_temp2 + 273.15)*100; |
|
|
129 |
|
2907 |
kaklik |
130 |
if (barometer_present == TRUE) |
|
|
131 |
{ |
|
|
132 |
barometer_temperature = (MPL_get_temperature() + 273.15)*100; |
|
|
133 |
barometer_pressure = MPL_get_pressure() * 10.0; // conversion to hectopascals |
|
|
134 |
} |
|
|
135 |
else |
|
|
136 |
{ |
|
|
137 |
barometer_temperature = 0; |
|
|
138 |
barometer_pressure = 0; |
|
|
139 |
} |
|
|
140 |
|
2752 |
kaklik |
141 |
delay_us(SEND_DELAY); |
|
|
142 |
putc('$'); |
|
|
143 |
delay_us(SEND_DELAY); |
|
|
144 |
sprintf(output,"AWS%s \0",VER); |
|
|
145 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
|
|
146 |
sprintf(output,"%Lu \0", seq); |
|
|
147 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
2835 |
kaklik |
148 |
sprintf(output,"%5.0f \0", local_temp ); |
2752 |
kaklik |
149 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
2907 |
kaklik |
150 |
sprintf(output,"%5.0f \0", SHT_temp1); |
2768 |
kaklik |
151 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
2907 |
kaklik |
152 |
sprintf(output,"%3.1f \0", SHT_hum1); |
2768 |
kaklik |
153 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
2907 |
kaklik |
154 |
sprintf(output,"%5.0f \0", SHT_temp2); |
|
|
155 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
|
|
156 |
sprintf(output,"%3.1f \0", SHT_hum2); |
|
|
157 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
2837 |
kaklik |
158 |
sprintf(output,"%5.0f \0", barometer_temperature); |
2779 |
kaklik |
159 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
|
|
160 |
sprintf(output,"%5.1f \0", barometer_pressure); |
|
|
161 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
2782 |
kaklik |
162 |
sprintf(output,"%3.1f \0", anemo); |
|
|
163 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
2752 |
kaklik |
164 |
sprintf(output,"*%X\r\n\0", check); |
|
|
165 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j++]); } |
|
|
166 |
delay_us(SEND_DELAY); |
|
|
167 |
} |
2907 |
kaklik |
168 |
|
2752 |
kaklik |
169 |
//---WDT |
|
|
170 |
restart_wdt(); |
|
|
171 |
seq++; // Increment the number of measurement |
2751 |
kaklik |
172 |
} |
|
|
173 |
} |
2828 |
kaklik |
174 |
|