Rev Author Line No. Line
2980 kaklik 1 #define VERSION "0.1"
2 #define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $"
3  
4 #include <main.h>
5 #include <math.h>
6 #include <string.h>
7  
8 #include "SHT25.h"
9 #include "MPL3115.h"
10 #include "HMC5883L.h"
11  
2996 kaklik 12 unsigned int16 timer0_overflow_count;
2980 kaklik 13 float anemo=0;
2996 kaklik 14 unsigned int8 rain;
2980 kaklik 15  
16 #int_TIMER1
17 void TIMER1_isr(void)
18 {
19 // 32.768 kHz krystal pro timer1 oscilátor
20 anemo = ((timer0_overflow_count * 0xFF) + get_timer0())/(32768.0/0xFFFF); // pocet pulzu za 1s
21  
22 timer0_overflow_count=0; //nulovani
23 set_timer0(0);
24 set_timer1(0);
2996 kaklik 25 output_toggle(PIN_E0);
2980 kaklik 26 }
27  
28 #int_TIMER0 //pro preteceni èítaèe pùlzù od anemometru (RA4)
29 void TIMER0_isr(void)
30 {
2996 kaklik 31 timer0_overflow_count++;
32 }
2984 kaklik 33  
2996 kaklik 34 #INT_EXT
35 void EXT_isr() //interrup from rain sensor clip.
2984 kaklik 36 {
2996 kaklik 37 rain++;
38 if (input(PIN_B0)) ext_int_edge( H_TO_L );
39 if (!input(PIN_B0)) ext_int_edge( L_TO_H );
2980 kaklik 40 }
41  
2984 kaklik 42 float wind_direction(void) //vypocet azimutu smeru vetru
2980 kaklik 43 {
44 signed int16 X,Y,Z;
2984 kaklik 45 float b;
46 X = mag_readX();
47 Y = mag_readY();
48  
2980 kaklik 49  
2984 kaklik 50 b = atan2((float)Y,(float)X); // vypocet azimutu z kartezskych souradnic
51 b = (b/3.141596)*180; // prevod na stupne
52 b += 180;
53 return b;
2980 kaklik 54 }
55  
56  
2984 kaklik 57 void welcome(void) // uvodni zprava
2980 kaklik 58 {
59 printf("\r\n\r\n# Meteorologicka stanice %s (C) 2013 www.mlab.cz \r\n",VERSION);
60 printf("\r\n %s \r\n",ID);// Welcome message
61 printf("# ver poradi ");
2984 kaklik 62 printf("altimet_t[C] altimet_a[m] altimet_p[Pa] ");
2996 kaklik 63 printf("sht_t[C] sht_h[%%] sht_config Wind_direction Anemo[pls/s]check\r\n\r\n");
2980 kaklik 64 }
65  
66 void main()
67 {
68 setup_adc_ports(NO_ANALOGS|VSS_VDD);
69 setup_adc(ADC_CLOCK_DIV_2);
70 setup_spi(SPI_SS_DISABLED);
71 setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
72 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
73 setup_timer_2(T2_DISABLED,0,1);
74 setup_ccp1(CCP_OFF);
75 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
76  
77  
78 float altimet_t; //teplota z MPL3115
79 float altimet_p; //tlak z MPL3115
80 float altimet_a; //vyska z MPL3115
81 float sht25_t; //teplota z SHT25
82 float sht25_h; //relativni vlhkost z SHT25
83 float smer_v; //smer vetru
84  
85 unsigned int8 sht_config;
86  
87 //nastaveni SHT25
88 SHT25_soft_reset();
89 sht_config = SHT25_RH12_T14 | SHT25_HEATER_OFF; //vypnuti topeni v SHT25
90 SHT25_setup(sht_config);
91  
92 //nastavení pøerušení pro anemometr
2996 kaklik 93 enable_interrupts(INT_TIMER1);
2980 kaklik 94 enable_interrupts(INT_TIMER0);
2996 kaklik 95 enable_interrupts(INT_EXT);
2980 kaklik 96 enable_interrupts(GLOBAL);
97 // vynulovani promenych pro anemometr
98 set_timer0(0);
99 set_timer1(0);
100 timer0_overflow_count=0;
2996 kaklik 101 rain=0;
2980 kaklik 102  
2996 kaklik 103  
2984 kaklik 104 welcome();
105 set_mag(); //nastaveni magnetometru pro smer vetru
106  
2980 kaklik 107 while(TRUE)
108 {
2990 kaklik 109 unsigned int32 i=0;
2980 kaklik 110  
111 mpl3115_setP(); //nastaveni pro tlak a teplotu
112 delay_ms (500);
113 altimet_t=mpl3115_T();
114 altimet_p=mpl3115_P();
115  
116 mpl3115_setA(); //nastaveni pro vysku a teplotu
117 delay_ms (500);
2984 kaklik 118 altimet_a = mpl3115_A();
2990 kaklik 119  
120 if (i<10000) sht_config = SHT25_RH12_T14 | SHT25_HEATER_OFF; // loop alters on chip heater on and off to check correct function
121 else
122 {
123 sht_config = SHT25_RH12_T14 | SHT25_HEATER_ON;
124 if (i > 10050) i = 0;
125 }
126  
2980 kaklik 127  
2984 kaklik 128 sht25_t = SHT25_get_temp(); //mereni hodnot z SHT25
129 sht25_h = SHT25_get_hum();
2990 kaklik 130 SHT25_setup(sht_config);
2980 kaklik 131  
2984 kaklik 132 smer_v = wind_direction(); //vrati azimut aktualniho smeru vetru
133  
2996 kaklik 134 printf(" %6.2f %7.2f %9.2f %6.2f %6.2f %X %5.2f %6.1f %u \n\r", altimet_t, altimet_a, altimet_p, sht25_t, sht25_h, sht_config, smer_v, anemo, rain);
2980 kaklik 135  
136 delay_ms(1000);
2996 kaklik 137 rain=0;
2990 kaklik 138 i++;
2980 kaklik 139 }
140 }
141