Rev Author Line No. Line
2752 kaklik 1 /**** Automatic weather station 01A ****/
2 #define VERSION "0.1"
2806 kakl 3 #define ID "$Id: main.c 3133 2013-07-04 11:36:46Z 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
3133 kaklik 11 #define RESPONSE_DELAY 100 // Reaction time after receiving a command
12 #define MEASURE_DELAY 1000 // Delay to a next measurement
2752 kaklik 13  
14 char VER[4]=VERSION; // Buffer for concatenate of a version string
15  
2826 kaklik 16 #define ONE_WIRE_PIN PIN_B1 // DS18B20 sensor connection
2751 kaklik 17 #include "..\ds1820.c"
18  
2781 kaklik 19 #define sht_data_pin PIN_D0 // SHT11 sensor connection
2779 kaklik 20 #define sht_clk_pin PIN_D1
2768 kaklik 21 #include "..\SHT.c"
22  
2916 kaklik 23 #use i2c(master, sda=PIN_D2, scl=PIN_D3)
2913 kaklik 24 #include "..\SHT25.h"
25  
2781 kaklik 26 #define CSN_SPI PIN_C2 // preassure sensor connection
2779 kaklik 27 #include "..\MPL115A1.c"
2768 kaklik 28  
2915 kaklik 29 unsigned int16 timer0_overflow_count;
3133 kaklik 30 unsigned int16 timer1_overflow_count;
31 float anemo_max;
2782 kaklik 32  
2907 kaklik 33 int1 barometer_present;
2782 kaklik 34  
3133 kaklik 35 float anemo_compute()
36 {
37 float anemo;
38 anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(((timer1_overflow_count * 0xFFFF) + get_timer1())/32768.0); // pulses per second calculation
39 anemo = anemo / 10.5; // frequency divided by anemomether constant.
40 return anemo;
41 }
42  
2782 kaklik 43 #int_TIMER1
44 void TIMER1_isr(void)
45 {
3133 kaklik 46 float anemo;
47 anemo = anemo_compute();
48 if (anemo > anemo_max) anemo_max=anemo;
2907 kaklik 49  
3133 kaklik 50 timer1_overflow_count++;
2782 kaklik 51 }
52  
3133 kaklik 53 #int_TIMER0 // anemometr pulses counting timer owerflow
2782 kaklik 54 void TIMER0_isr(void)
55 {
3133 kaklik 56 timer0_overflow_count++;
2782 kaklik 57 }
58  
2913 kaklik 59 /*#int_default
2907 kaklik 60 void default_isr()
2843 kaklik 61 {
62 printf("Unexplained interrupt\r\n");
63 }
2913 kaklik 64 */
2752 kaklik 65 void welcome(void) // Welcome message
66 {
67 char REV[50]=ID; // Buffer for concatenate of a version string
68  
69 if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0;
2780 kaklik 70 printf("\r\n\r\n# AWS01A %s (C) 2013 www.mlab.cz \r\n",VER); // Welcome message
2752 kaklik 71 printf("#%s\r\n",&REV[4]);
2913 kaklik 72 // printf("# ver seq ");
73 // printf("#temp[mK] hum_temp[mK] hum[%%] ");
74 // printf("bar_temp[mK] pressure[hPa] Anemo[m/s]check\r\n\r\n");
2752 kaklik 75 }
76  
3133 kaklik 77 void print_slow(char *output, int8 *check)
78 {
79 int8 j; // String pointer
80 j=0;
81 while(output[j]!=0)
82 {
83 delay_us(SEND_DELAY);
84 putc(output[j]);
85 *check^=output[j++];
86 }
87 }
88  
89  
2751 kaklik 90 void main()
91 {
3133 kaklik 92 unsigned int16 seq=0;
2751 kaklik 93  
2848 kakl 94 setup_oscillator(OSC_8MHZ); // pri prouziti bootloaderu neni treba nastavovat
95 setup_wdt(WDT_2304MS);
2843 kaklik 96 restart_wdt(); //---WDT
2822 kaklik 97 setup_adc_ports(NO_ANALOGS|VSS_VDD);
2751 kaklik 98 setup_adc(ADC_CLOCK_DIV_2);
2848 kakl 99 setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
2907 kaklik 100 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
2751 kaklik 101 setup_timer_2(T2_DISABLED,0,1);
102 setup_ccp1(CCP_OFF);
2823 kaklik 103 setup_comparator(NC_NC_NC_NC); // This device COMP currently not supported by the PICWizard
2779 kaklik 104 setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
105 output_high(CSN_SPI);
3133 kaklik 106 int1 repeat;
107 float anemo;
2782 kaklik 108  
2834 kaklik 109 welcome(); // welcome print and device indentification
2848 kakl 110  
2897 kakl 111 enable_interrupts(INT_TIMER1); // interrupts used for anemometer readings
112 enable_interrupts(INT_TIMER0);
2907 kaklik 113 enable_interrupts(GLOBAL);
2848 kakl 114  
115 restart_wdt(); //---WDT
116  
2915 kaklik 117 // barometer init
118 barometer_present = MPL_init(); // get correction coefficients from the sensor
119  
2848 kakl 120 sht_init();
2915 kaklik 121  
2913 kaklik 122 SHT25_soft_reset();
123  
2915 kaklik 124 // anemometer init
125 set_timer0(0);
126 set_timer1(0);
127 timer0_overflow_count=0;
128 anemo=0;
3133 kaklik 129 repeat=TRUE;
2915 kaklik 130  
2843 kaklik 131 restart_wdt(); //---WDT
3133 kaklik 132 delay_ms(1000);
2822 kaklik 133  
2751 kaklik 134 while (TRUE)
135 {
3133 kaklik 136 do
137 {
138 delay_ms(RESPONSE_DELAY);
139 //---WDT
140 restart_wdt();
141 } while (!kbhit()&&!repeat);
2768 kaklik 142  
3133 kaklik 143 //---WDT
144 restart_wdt();
2752 kaklik 145  
3133 kaklik 146 { // Retrieve command
147 char ch='k';
148  
149 if(kbhit()) ch=getc();
150  
151 switch (ch)
152 {
153 case 'i':
154 welcome(); // Information about version, etc...
155 break; // Only when dome is closed
156  
157 case 's':
158 repeat=FALSE; // Single measure mode
159 break;
160  
161 case 'r':
162 repeat=TRUE; // Repeat mode
163 break;
164  
165 case 'u':
166 reset_cpu(); // Update firmware
167 }
168 }
169  
170 char output[8]; // Output buffer
171 int8 check=0; // Checksum is calculated between '$' and '*'
172 float SHT_temp1=0,SHT_hum1=0;
173 float SHT_temp2=0,SHT_hum2=0;
174 float local_temp;
175 float barometer_temperature, barometer_pressure;
176 float anemo;
177  
178  
179 { // printf
180  
181 local_temp = ds1820_read()+27315;
182 sht_rd(SHT_temp1,SHT_hum1);
183 SHT_temp1 = (SHT_temp1 + 273.15)*100;
2779 kaklik 184  
3133 kaklik 185 SHT_temp2 = SHT25_get_temp();
186 SHT_hum2 = SHT25_get_hum();
187 SHT_temp2 = (SHT_temp2 + 273.15)*100;
188 if (barometer_present == TRUE)
189 {
190 barometer_temperature = (MPL_get_temperature() + 273.15)*100;
191 barometer_pressure = MPL_get_pressure() * 10.0; // conversion to hectopascals
192 }
193 else
194 {
195 barometer_temperature = 0;
196 barometer_pressure = 0;
197 }
2915 kaklik 198  
2752 kaklik 199 delay_us(SEND_DELAY);
200 putc('$');
201 delay_us(SEND_DELAY);
202 sprintf(output,"AWS%s \0",VER);
3133 kaklik 203 print_slow(output, &check);
2752 kaklik 204 sprintf(output,"%Lu \0", seq);
3133 kaklik 205 print_slow(output, &check);
2835 kaklik 206 sprintf(output,"%5.0f \0", local_temp );
3133 kaklik 207 print_slow(output, &check);
2907 kaklik 208 sprintf(output,"%5.0f \0", SHT_temp1);
3133 kaklik 209 print_slow(output, &check);
2907 kaklik 210 sprintf(output,"%3.1f \0", SHT_hum1);
3133 kaklik 211 print_slow(output, &check);
2907 kaklik 212 sprintf(output,"%5.0f \0", SHT_temp2);
3133 kaklik 213 print_slow(output, &check);
2907 kaklik 214 sprintf(output,"%3.1f \0", SHT_hum2);
3133 kaklik 215 print_slow(output, &check);
2837 kaklik 216 sprintf(output,"%5.0f \0", barometer_temperature);
3133 kaklik 217 print_slow(output, &check);
2779 kaklik 218 sprintf(output,"%5.1f \0", barometer_pressure);
3133 kaklik 219 print_slow(output, &check);
220  
221 //anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(((timer1_overflow_count * 0xFFFF) + get_timer1())/32768.0); // pulses per second calculation
222 //anemo = anemo / 10.5; // frequency divided by anemomether constant.
223 sprintf(output,"%3.1f \0", anemo_compute());
224  
225 timer0_overflow_count=0;
226 timer1_overflow_count=0;
227 set_timer0(0);
228 set_timer1(0);
229  
230 print_slow(output, &check);
231 //sprintf(output,"%3.1f \0", anemo_max);
232 //print_slow(output, &check);
233  
234 //anemo_max = 0;
235  
2752 kaklik 236 sprintf(output,"*%X\r\n\0", check);
3133 kaklik 237 print_slow(output, &check);
238  
2752 kaklik 239 delay_us(SEND_DELAY);
240 }
2907 kaklik 241  
2752 kaklik 242 //---WDT
243 restart_wdt();
3133 kaklik 244 seq++; // Increment the number of measurement
245 delay_ms(MEASURE_DELAY);
2751 kaklik 246 }
247 }
2828 kaklik 248