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