/**** Automatic weather station 01A ****/#define VERSION "0.2"#define ID "$Id: main.c 3136 2013-07-07 21:55:22Z kaklik $"#include "main.h"#include ".\common\dbloader.h"#include <string.h>#CASE // Case sensitive compiler#define SEND_DELAY 50 // Time between two characters on RS232#define RESPONSE_DELAY 100 // Reaction time after receiving a command#define MEASURE_DELAY 1000 // Delay to a next measurementchar VER[4]=VERSION; // Buffer for concatenate of a version string#define ONE_WIRE_PIN PIN_B1 // DS18B20 sensor connection#include "..\ds1820.c"#define sht_data_pin PIN_D0 // SHT11 sensor connection#define sht_clk_pin PIN_D1#include "..\SHT.c"#use i2c(master, sda=PIN_D2, scl=PIN_D3)#include "..\SHT25.h"#define CSN_SPI PIN_C2 // preassure sensor connection#include "..\MPL115A1.c"unsigned int16 timer0_overflow_count;unsigned int16 timer1_overflow_count;unsigned int16 timer0_overflow_count_last;unsigned int16 timer0_last;unsigned int16 anemo_count_max;int1 barometer_present;#int_TIMER1void TIMER1_isr(void){// 32.768 kHz crystal, 16bit counter => every 2secs interruptunsigned int16 anemo_count;unsigned int16 timer0 = get_timer0();anemo_count = (((timer0_overflow_count - timer0_overflow_count_last) << 8) + (timer0 - timer0_last));timer0_overflow_count_last = timer0_overflow_count;timer0_last = timer0;if (anemo_count > anemo_count_max) anemo_count_max=anemo_count;timer1_overflow_count++;}#int_TIMER0 // anemometr pulses counting timer owerflowvoid TIMER0_isr(void){timer0_overflow_count++;}/*#int_defaultvoid default_isr(){printf("Unexplained interrupt\r\n");}*/void welcome(void) // Welcome message{char REV[50]=ID; // Buffer for concatenate of a version stringif (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0;printf("\r\n\r\n# AWS01A %s (C) 2013 www.mlab.cz \r\n",VER); // Welcome messageprintf("#%s\r\n",&REV[4]);// printf("# ver seq ");// printf("#temp[mK] hum_temp[mK] hum[%%] ");// printf("bar_temp[mK] pressure[hPa] Anemo[m/s]check\r\n\r\n");}void print_slow(char *output, int8 *check){int8 j; // String pointerj=0;while(output[j]!=0){delay_us(SEND_DELAY);putc(output[j]);*check^=output[j++];}}void main(){unsigned int16 seq=0;timer0_overflow_count=0;timer1_overflow_count=0;timer0_overflow_count_last=0;timer0_last=0;setup_oscillator(OSC_8MHZ); // pri prouziti bootloaderu neni treba nastavovatsetup_wdt(WDT_2304MS);restart_wdt(); //---WDTsetup_adc_ports(NO_ANALOGS|VSS_VDD);setup_adc(ADC_CLOCK_DIV_2);setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);setup_timer_2(T2_DISABLED,0,1);setup_ccp1(CCP_OFF);setup_comparator(NC_NC_NC_NC); // This device COMP currently not supported by the PICWizardsetup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);output_high(CSN_SPI);int1 repeat;welcome(); // welcome print and device indentificationenable_interrupts(INT_TIMER1); // interrupts used for anemometer readingsenable_interrupts(INT_TIMER0);enable_interrupts(GLOBAL);restart_wdt(); //---WDT// barometer initbarometer_present = MPL_init(); // get correction coefficients from the sensorsht_init();SHT25_soft_reset();// anemometer initset_timer0(0);set_timer1(0);timer0_overflow_count=0;repeat=TRUE;restart_wdt(); //---WDTdelay_ms(1000);while (TRUE){do{delay_ms(RESPONSE_DELAY);//---WDTrestart_wdt();} while (!kbhit()&&!repeat);//---WDTrestart_wdt();{ // Retrieve commandchar ch='k';if(kbhit()) ch=getc();switch (ch){case 'i':welcome(); // Information about version, etc...break; // Only when dome is closedcase 's':repeat=FALSE; // Single measure modebreak;case 'r':repeat=TRUE; // Repeat modebreak;case 'u':reset_cpu(); // Update firmware}}char output[8]; // Output bufferint8 check=0; // Checksum is calculated between '$' and '*'float SHT_temp1=0,SHT_hum1=0;float SHT_temp2=0,SHT_hum2=0;int16 local_temp;float barometer_temperature;float barometer_pressure;float anemo;{ // printflocal_temp = (int16)ds1820_read();sht_rd(SHT_temp1,SHT_hum1);//SHT_temp1 = (SHT_temp1 + 273.15)*100;SHT_temp2 = SHT25_get_temp();SHT_hum2 = SHT25_get_hum();//SHT_temp2 = (SHT_temp2 + 273.15)*100;if (barometer_present == TRUE){barometer_temperature = MPL_get_temperature();barometer_pressure = MPL_get_pressure() * 10.0; // conversion to hectopascals}else{barometer_temperature = 0;barometer_pressure = 0;}delay_us(SEND_DELAY);putc('$');delay_us(SEND_DELAY);sprintf(output,"AWS%s \0",VER);print_slow(output, &check);sprintf(output,"%Lu \0", seq);print_slow(output, &check);sprintf(output,"%Ld \0", local_temp);print_slow(output, &check);sprintf(output,"%3.1f \0", SHT_temp1);print_slow(output, &check);sprintf(output,"%3.1f \0", SHT_hum1);print_slow(output, &check);sprintf(output,"%3.1f \0", SHT_temp2);print_slow(output, &check);sprintf(output,"%3.1f \0", SHT_hum2);print_slow(output, &check);sprintf(output,"%3.1f \0", barometer_temperature);print_slow(output, &check);sprintf(output,"%5.1f \0", barometer_pressure);print_slow(output, &check);// optimization: (timer1_overflow_count << 16)/32768.0 = timer1_overflow_count << 1, so we can use int16 (and not int32)anemo = ((float)((timer0_overflow_count << 8) + get_timer0()))/((float)(timer1_overflow_count << 1) + (float)(get_timer1())/32768.0); // pulses per second calculationanemo = anemo / 10.5; // frequency divided by anemomether constant.set_timer0(0);set_timer1(0);timer0_overflow_count=0;timer1_overflow_count=0;timer0_overflow_count_last=0;timer0_last=0;sprintf(output,"%3.1f \0", anemo);print_slow(output, &check);if (anemo_count_max > 0){// anemo_max comptutation; >>1 is division by two, which comes from the 2secs interval from timer1anemo = (float)(anemo_count_max >> 1) / 10.5; // frequency divided by anemomether constant.anemo_count_max = 0;}sprintf(output,"%3.1f \0", anemo);print_slow(output, &check);sprintf(output,"*%X\r\n\0", check);print_slow(output, &check);delay_us(SEND_DELAY);}//---WDTrestart_wdt();seq++; // Increment the number of measurementdelay_ms(MEASURE_DELAY);}}