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