1 |
/**** Automatic weather station 01A ****/ |
1 |
/**** Automatic weather station 01A ****/ |
2 |
#define VERSION "0.1" |
2 |
#define VERSION "0.1" |
3 |
#define ID "$Id: main.c 2825 2013-03-10 14:57:58Z kaklik $" |
3 |
#define ID "$Id: main.c 2826 2013-03-10 15:27:21Z kaklik $" |
4 |
#include "main.h" |
4 |
#include "main.h" |
5 |
#include <string.h> |
5 |
#include <string.h> |
6 |
|
6 |
|
7 |
#CASE // Case sensitive compiler |
7 |
#CASE // Case sensitive compiler |
8 |
|
8 |
|
9 |
#define SEND_DELAY 50 // Time between two characters on RS232 |
9 |
#define SEND_DELAY 50 // Time between two characters on RS232 |
10 |
|
10 |
|
11 |
char VER[4]=VERSION; // Buffer for concatenate of a version string |
11 |
char VER[4]=VERSION; // Buffer for concatenate of a version string |
12 |
|
12 |
|
13 |
#define ONE_WIRE_PIN PIN_C7 // DS18B20 sensor connection |
13 |
#define ONE_WIRE_PIN PIN_B1 // DS18B20 sensor connection |
14 |
#include "..\ds1820.c" |
14 |
#include "..\ds1820.c" |
15 |
|
15 |
|
16 |
#define sht_data_pin PIN_D0 // SHT11 sensor connection |
16 |
#define sht_data_pin PIN_D0 // SHT11 sensor connection |
17 |
#define sht_clk_pin PIN_D1 |
17 |
#define sht_clk_pin PIN_D1 |
18 |
#include "..\SHT.c" |
18 |
#include "..\SHT.c" |
19 |
|
19 |
|
20 |
#define CSN_SPI PIN_C2 // preassure sensor connection |
20 |
#define CSN_SPI PIN_C2 // preassure sensor connection |
21 |
#include "..\MPL115A1.c" |
21 |
#include "..\MPL115A1.c" |
22 |
|
22 |
|
23 |
unsigned int16 timer0_overflow_count=0; |
23 |
unsigned int16 timer0_overflow_count=0; |
24 |
float anemo=0; |
24 |
float anemo=0; |
25 |
|
25 |
|
26 |
|
26 |
|
27 |
#int_TIMER1 |
27 |
#int_TIMER1 |
28 |
void TIMER1_isr(void) |
28 |
void TIMER1_isr(void) |
29 |
{ |
29 |
{ |
30 |
output_toggle(PIN_D1); |
30 |
output_toggle(PIN_D1); |
31 |
anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(32768.0/0xFFFF); |
31 |
anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(32768.0/0xFFFF); |
32 |
timer0_overflow_count=0; |
32 |
timer0_overflow_count=0; |
33 |
set_timer0(0); |
33 |
set_timer0(0); |
34 |
} |
34 |
} |
35 |
|
35 |
|
36 |
#int_TIMER0 |
36 |
#int_TIMER0 |
37 |
void TIMER0_isr(void) |
37 |
void TIMER0_isr(void) |
38 |
{ |
38 |
{ |
39 |
timer0_overflow_count++; |
39 |
timer0_overflow_count++; |
40 |
} |
40 |
} |
41 |
|
41 |
|
42 |
|
42 |
|
43 |
|
43 |
|
44 |
void welcome(void) // Welcome message |
44 |
void welcome(void) // Welcome message |
45 |
{ |
45 |
{ |
46 |
char REV[50]=ID; // Buffer for concatenate of a version string |
46 |
char REV[50]=ID; // Buffer for concatenate of a version string |
47 |
|
47 |
|
48 |
if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0; |
48 |
if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0; |
49 |
printf("\r\n\r\n# AWS01A %s (C) 2013 www.mlab.cz \r\n",VER); // Welcome message |
49 |
printf("\r\n\r\n# AWS01A %s (C) 2013 www.mlab.cz \r\n",VER); // Welcome message |
50 |
printf("#%s\r\n",&REV[4]); |
50 |
printf("#%s\r\n",&REV[4]); |
51 |
// printf("#\r\n"); |
51 |
// printf("#\r\n"); |
52 |
// printf("# commands: h, c, o, l, x, i, r, a, s, u\r\n"); |
52 |
// printf("# commands: h, c, o, l, x, i, r, a, s, u\r\n"); |
53 |
// printf("# h_eat, c_old, o_pen, l_ock, x_open, "); |
53 |
// printf("# h_eat, c_old, o_pen, l_ock, x_open, "); |
54 |
// printf("i_nfo, r_epeat, a_uto, s_single, u_pdate\r\n"); |
54 |
// printf("i_nfo, r_epeat, a_uto, s_single, u_pdate\r\n"); |
55 |
// printf("#\r\n"); |
55 |
// printf("#\r\n"); |
56 |
printf("# ver seq temp[K] hum_temp[K] hum[%%] "); |
56 |
printf("# ver seq temp[K] hum_temp[K] hum[%%] "); |
57 |
printf("bar_temp[K] pressure[hPa] check\r\n\r\n"); |
57 |
printf("bar_temp[K] pressure[hPa] check\r\n\r\n"); |
58 |
|
58 |
|
59 |
//---WDT |
59 |
//---WDT |
60 |
restart_wdt(); |
60 |
restart_wdt(); |
61 |
} |
61 |
} |
62 |
|
62 |
|
63 |
void main() |
63 |
void main() |
64 |
{ |
64 |
{ |
65 |
unsigned int16 seq=0; |
65 |
unsigned int16 seq=0; |
66 |
|
66 |
|
67 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
67 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
68 |
setup_adc(ADC_CLOCK_DIV_2); |
68 |
setup_adc(ADC_CLOCK_DIV_2); |
69 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
69 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
70 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); |
70 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); |
71 |
setup_timer_2(T2_DISABLED,0,1); |
71 |
setup_timer_2(T2_DISABLED,0,1); |
72 |
setup_ccp1(CCP_OFF); |
72 |
setup_ccp1(CCP_OFF); |
73 |
setup_comparator(NC_NC_NC_NC); // This device COMP currently not supported by the PICWizard |
73 |
setup_comparator(NC_NC_NC_NC); // This device COMP currently not supported by the PICWizard |
74 |
setup_oscillator(OSC_8MHZ); |
74 |
setup_oscillator(OSC_8MHZ); |
75 |
setup_wdt(WDT_1152MS|WDT_DIV_16); |
75 |
setup_wdt(WDT_1152MS|WDT_DIV_16); |
76 |
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64); |
76 |
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64); |
77 |
output_high(CSN_SPI); |
77 |
output_high(CSN_SPI); |
78 |
|
78 |
|
79 |
|
79 |
|
80 |
delay_ms(100); |
80 |
delay_ms(100); |
81 |
|
81 |
|
82 |
welcome(); |
82 |
welcome(); |
83 |
|
83 |
|
84 |
sht_init(); |
84 |
sht_init(); |
85 |
MPL_init(); // get correction coefficients from the sensor |
85 |
MPL_init(); // get correction coefficients from the sensor |
86 |
|
86 |
|
87 |
// enable_interrupts(INT_TIMER1); // interrupts used for anemometer readings |
87 |
// enable_interrupts(INT_TIMER1); // interrupts used for anemometer readings |
88 |
// enable_interrupts(INT_TIMER0); |
88 |
// enable_interrupts(INT_TIMER0); |
89 |
// enable_interrupts(GLOBAL); |
89 |
// enable_interrupts(GLOBAL); |
90 |
|
90 |
|
91 |
while (TRUE) |
91 |
while (TRUE) |
92 |
{ |
92 |
{ |
93 |
char output[8]; // Output buffer |
93 |
char output[8]; // Output buffer |
94 |
int8 j; // String pointer |
94 |
int8 j; // String pointer |
95 |
int8 check=0; // Checksum is calculated between '$' and '*' |
95 |
int8 check=0; // Checksum is calculated between '$' and '*' |
96 |
float SHT_temp,SHT_hum; |
96 |
float SHT_temp,SHT_hum; |
97 |
float local_temp; |
97 |
float local_temp; |
98 |
float barometer_temperature, barometer_pressure; |
98 |
float barometer_temperature, barometer_pressure; |
99 |
|
99 |
|
100 |
delay_ms(100); |
100 |
delay_ms(100); |
101 |
{ // printf |
101 |
{ // printf |
102 |
|
102 |
|
103 |
local_temp = ds1820_read()+273.15; |
103 |
local_temp = ds1820_read()+273.15; |
104 |
sht_rd(SHT_temp,SHT_hum); |
104 |
sht_rd(SHT_temp,SHT_hum); |
105 |
SHT_temp += 273.15; |
105 |
SHT_temp += 273.15; |
106 |
// barometer_temperature = MPL_get_temperature() + 273.15; |
106 |
// barometer_temperature = MPL_get_temperature() + 273.15; |
107 |
// barometer_pressure = MPL_get_pressure() * 10.0; // conversion to hectopascals |
107 |
// barometer_pressure = MPL_get_pressure() * 10.0; // conversion to hectopascals |
108 |
|
108 |
|
109 |
delay_us(SEND_DELAY); |
109 |
delay_us(SEND_DELAY); |
110 |
putc('$'); |
110 |
putc('$'); |
111 |
delay_us(SEND_DELAY); |
111 |
delay_us(SEND_DELAY); |
112 |
sprintf(output,"AWS%s \0",VER); |
112 |
sprintf(output,"AWS%s \0",VER); |
113 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
113 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
114 |
sprintf(output,"%Lu \0", seq); |
114 |
sprintf(output,"%Lu \0", seq); |
115 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
115 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
116 |
sprintf(output,"%f \0", local_temp ); |
116 |
sprintf(output,"%f \0", local_temp ); |
117 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
117 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
118 |
sprintf(output,"%f \0", SHT_temp); |
118 |
sprintf(output,"%f \0", SHT_temp); |
119 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
119 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
120 |
sprintf(output,"%3.1f \0", SHT_hum); |
120 |
sprintf(output,"%3.1f \0", SHT_hum); |
121 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
121 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
122 |
sprintf(output,"%f \0", barometer_temperature); |
122 |
sprintf(output,"%f \0", barometer_temperature); |
123 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
123 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
124 |
sprintf(output,"%5.1f \0", barometer_pressure); |
124 |
sprintf(output,"%5.1f \0", barometer_pressure); |
125 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
125 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
126 |
sprintf(output,"%3.1f \0", anemo); |
126 |
sprintf(output,"%3.1f \0", anemo); |
127 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
127 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j]); check^=output[j++]; } |
128 |
sprintf(output,"*%X\r\n\0", check); |
128 |
sprintf(output,"*%X\r\n\0", check); |
129 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j++]); } |
129 |
j=0; while(output[j]!=0) { delay_us(SEND_DELAY); putc(output[j++]); } |
130 |
delay_us(SEND_DELAY); |
130 |
delay_us(SEND_DELAY); |
131 |
} |
131 |
} |
132 |
|
132 |
|
133 |
//---WDT |
133 |
//---WDT |
134 |
restart_wdt(); |
134 |
restart_wdt(); |
135 |
seq++; // Increment the number of measurement |
135 |
seq++; // Increment the number of measurement |
136 |
} |
136 |
} |
137 |
} |
137 |
} |
138 |
#include ".\common\dbloader.h" |
138 |
#include ".\common\dbloader.h" |
139 |
//#include "dbloader.c" // Space reservation for the BootLoader |
139 |
//#include "dbloader.c" // Space reservation for the BootLoader |