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