1 |
#include "main.h" |
1 |
#include "main.h" |
2 |
#include "HMC5883L.h" |
2 |
#include "HMC5883L.h" |
- |
|
3 |
#include <math.h> |
3 |
|
4 |
|
4 |
void main() |
5 |
void main() |
5 |
{ |
6 |
{ |
6 |
int1 last; |
7 |
float last,b,anemo_speed; |
7 |
unsigned int16 anemo_round=0; |
8 |
unsigned int16 anemo_round=0; |
8 |
unsigned int16 i; |
9 |
unsigned int16 i; |
9 |
|
10 |
|
10 |
|
11 |
|
11 |
//signed int16 X,Y,Z; |
12 |
//signed int16 X,Y,Z; |
12 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
13 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
13 |
setup_adc(ADC_CLOCK_DIV_2); |
14 |
setup_adc(ADC_CLOCK_DIV_2); |
14 |
setup_spi(SPI_SS_DISABLED); |
15 |
setup_spi(SPI_SS_DISABLED); |
15 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
16 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
16 |
setup_timer_1(T1_DISABLED); |
17 |
setup_timer_1(T1_DISABLED); |
17 |
setup_timer_2(T2_DISABLED,0,1); |
18 |
setup_timer_2(T2_DISABLED,0,1); |
18 |
setup_ccp1(CCP_OFF); |
19 |
setup_ccp1(CCP_OFF); |
19 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
20 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
20 |
|
21 |
|
21 |
// Init the HMC5883L. Set Mode register for |
22 |
// Init the HMC5883L. Set Mode register for |
22 |
// continuous measurements. |
23 |
// continuous measurements. |
23 |
hmc5883l_write_reg(HMC5883L_CFG_A_REG, 0x18); // no average, maximal update range |
24 |
hmc5883l_write_reg(HMC5883L_CFG_A_REG, 0x18); // no average, maximal update range |
24 |
hmc5883l_write_reg(HMC5883L_CFG_B_REG, 0xE0); // maximal range |
25 |
hmc5883l_write_reg(HMC5883L_CFG_B_REG, 0xE0); // maximal range |
25 |
hmc5883l_write_reg(HMC5883L_MODE_REG, 0x00); |
26 |
hmc5883l_write_reg(HMC5883L_MODE_REG, 0x00); |
26 |
|
27 |
|
27 |
// Continuously read and display the x,y,z results. |
28 |
// Continuously read and display the x,y,z results. |
28 |
// Wait at least 67 ms between reads, re the HMC5883L data sheet. |
29 |
// Wait at least 67 ms between reads, re the HMC5883L data sheet. |
29 |
|
30 |
|
30 |
|
31 |
|
31 |
printf("Magnetometr: \r\n",); |
32 |
printf("Magnetometr: \r\n",); |
32 |
printf("(c)mlab JACHO 2013: \r\n",); |
33 |
printf("(c)mlab JACHO 2013: \r\n",); |
33 |
printf("X, Y, Z \r\n",); |
34 |
printf("X, Y, Z \r\n",); |
34 |
|
35 |
|
35 |
/* while(true) |
36 |
/* while(true) |
36 |
{ |
37 |
{ |
37 |
X = mag_readX(); |
38 |
X = mag_readX(); |
38 |
Y = mag_readY(); |
39 |
Y = mag_readY(); |
39 |
Z = mag_readZ(); |
40 |
Z = mag_readZ(); |
40 |
printf("%4Ld %4Ld %4Ld \r\n", X, Y, Z); |
41 |
printf("%4Ld %4Ld %4Ld \r\n", X, Y, Z); |
41 |
Delay_ms(50); |
42 |
Delay_ms(50); |
42 |
|
43 |
|
43 |
} |
44 |
} |
44 |
*/ |
45 |
*/ |
45 |
|
46 |
|
46 |
while(TRUE) |
47 |
while(TRUE) |
47 |
{ |
48 |
{ |
48 |
|
49 |
|
49 |
for( i=0;i<=1000;i++) |
50 |
// for(i=0;i<=10;i++) |
50 |
{ |
51 |
// { |
51 |
// delay_ms(); |
- |
|
52 |
hmc5883l_read_data(); |
52 |
hmc5883l_read_data(); |
- |
|
53 |
|
- |
|
54 |
// b = atan2((float)compass.y,(float)compass.x); // vypocet azimutu z kartezskych souradnic |
- |
|
55 |
// b = (b/3.141596)*180; // prevod na stupne |
- |
|
56 |
// b += 180; |
53 |
|
57 |
|
54 |
if(compass.x < 0) |
58 |
// anemo_speed += (b-last); |
55 |
{ |
59 |
// last=b; |
56 |
if(last == 1) anemo_round++; |
- |
|
57 |
last=0; |
60 |
// delay_ms(10); |
58 |
} |
61 |
// } |
59 |
if(compass.x > 0) last=1; |
62 |
// anemo_speed=anemo_speed/10; |
60 |
} |
- |
|
61 |
|
63 |
|
62 |
printf("%6Ld %6Ld %6Ld %6Ld \n\r", compass.x, compass.y, compass.z, anemo_round); |
64 |
printf("%6Ld %6Ld %6Ld \n\r", compass.x, compass.y, compass.z); |
63 |
anemo_round=0; |
65 |
// delay_ms(100); |
64 |
} |
66 |
} |
65 |
|
67 |
|
66 |
} |
68 |
} |
67 |
|
69 |
|
68 |
|
70 |
|
69 |
|
71 |
|