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