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