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