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