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