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