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++; |
2997 |
kaklik |
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 ); |
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; |
2997 |
kaklik |
46 |
X = mag_readX(); |
|
|
47 |
delay_ms(10); |
2984 |
kaklik |
48 |
Y = mag_readY(); |
|
|
49 |
|
2980 |
kaklik |
50 |
|
2984 |
kaklik |
51 |
b = atan2((float)Y,(float)X); // vypocet azimutu z kartezskych souradnic |
|
|
52 |
b = (b/3.141596)*180; // prevod na stupne |
|
|
53 |
b += 180; |
|
|
54 |
return b; |
2980 |
kaklik |
55 |
} |
|
|
56 |
|
|
|
57 |
|
2984 |
kaklik |
58 |
void welcome(void) // uvodni zprava |
2980 |
kaklik |
59 |
{ |
|
|
60 |
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 |
|
|
62 |
printf("# ver poradi "); |
2984 |
kaklik |
63 |
printf("altimet_t[C] altimet_a[m] altimet_p[Pa] "); |
2996 |
kaklik |
64 |
printf("sht_t[C] sht_h[%%] sht_config Wind_direction Anemo[pls/s]check\r\n\r\n"); |
2980 |
kaklik |
65 |
} |
|
|
66 |
|
|
|
67 |
void main() |
|
|
68 |
{ |
|
|
69 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
|
|
70 |
setup_adc(ADC_CLOCK_DIV_2); |
|
|
71 |
setup_spi(SPI_SS_DISABLED); |
|
|
72 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
|
|
73 |
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT); |
|
|
74 |
setup_timer_2(T2_DISABLED,0,1); |
|
|
75 |
setup_ccp1(CCP_OFF); |
|
|
76 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
|
|
77 |
|
|
|
78 |
|
|
|
79 |
float altimet_t; //teplota z MPL3115 |
|
|
80 |
float altimet_p; //tlak z MPL3115 |
|
|
81 |
float altimet_a; //vyska z MPL3115 |
|
|
82 |
float sht25_t; //teplota z SHT25 |
|
|
83 |
float sht25_h; //relativni vlhkost z SHT25 |
|
|
84 |
float smer_v; //smer vetru |
|
|
85 |
|
|
|
86 |
unsigned int8 sht_config; |
|
|
87 |
|
|
|
88 |
//nastavení pøeruení pro anemometr |
2996 |
kaklik |
89 |
enable_interrupts(INT_TIMER1); |
2980 |
kaklik |
90 |
enable_interrupts(INT_TIMER0); |
2996 |
kaklik |
91 |
enable_interrupts(INT_EXT); |
2980 |
kaklik |
92 |
enable_interrupts(GLOBAL); |
|
|
93 |
// vynulovani promenych pro anemometr |
|
|
94 |
set_timer0(0); |
|
|
95 |
set_timer1(0); |
|
|
96 |
timer0_overflow_count=0; |
2996 |
kaklik |
97 |
rain=0; |
2980 |
kaklik |
98 |
|
2984 |
kaklik |
99 |
welcome(); |
|
|
100 |
set_mag(); //nastaveni magnetometru pro smer vetru |
|
|
101 |
|
2997 |
kaklik |
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 |
|
2980 |
kaklik |
108 |
while(TRUE) |
|
|
109 |
{ |
2990 |
kaklik |
110 |
unsigned int32 i=0; |
2980 |
kaklik |
111 |
|
|
|
112 |
mpl3115_setP(); //nastaveni pro tlak a teplotu |
|
|
113 |
delay_ms (500); |
|
|
114 |
altimet_t=mpl3115_T(); |
|
|
115 |
altimet_p=mpl3115_P(); |
|
|
116 |
|
|
|
117 |
mpl3115_setA(); //nastaveni pro vysku a teplotu |
|
|
118 |
delay_ms (500); |
2984 |
kaklik |
119 |
altimet_a = mpl3115_A(); |
2990 |
kaklik |
120 |
|
|
|
121 |
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 |
|
|
123 |
{ |
|
|
124 |
sht_config = SHT25_RH12_T14 | SHT25_HEATER_ON; |
|
|
125 |
if (i > 10050) i = 0; |
|
|
126 |
} |
|
|
127 |
|
2980 |
kaklik |
128 |
|
2984 |
kaklik |
129 |
sht25_t = SHT25_get_temp(); //mereni hodnot z SHT25 |
|
|
130 |
sht25_h = SHT25_get_hum(); |
2990 |
kaklik |
131 |
SHT25_setup(sht_config); |
2980 |
kaklik |
132 |
|
2984 |
kaklik |
133 |
smer_v = wind_direction(); //vrati azimut aktualniho smeru vetru |
|
|
134 |
|
2996 |
kaklik |
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); |
2980 |
kaklik |
136 |
|
|
|
137 |
delay_ms(1000); |
2996 |
kaklik |
138 |
rain=0; |
2990 |
kaklik |
139 |
i++; |
2980 |
kaklik |
140 |
} |
|
|
141 |
} |
|
|
142 |
|