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