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