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