1045 |
jacho |
1 |
//Knihovna pro senzor HMC5883L |
|
|
2 |
//(c) Jan Chroust 2013 |
|
|
3 |
|
|
|
4 |
|
960 |
jacho |
5 |
void set_mag (void) //uvodni nastaveni na kontinualni mereni, rozsah ± 8.1 Ga, frekvence mereni 15HZ |
|
|
6 |
{ |
|
|
7 |
i2c_start(); //nastavení Configuration Register A |
|
|
8 |
I2C_Write(MAG_ADDR_W); |
|
|
9 |
I2C_Write(0x00); |
|
|
10 |
I2C_Write(0x70); |
|
|
11 |
i2c_stop(); |
|
|
12 |
Delay_ms(6); |
|
|
13 |
|
|
|
14 |
i2c_start(); //nastavení Configuration Register B |
|
|
15 |
I2C_Write(MAG_ADDR_W); |
|
|
16 |
I2C_Write(0x01); |
|
|
17 |
I2C_Write(MAG_ROZ810); |
|
|
18 |
i2c_stop(); |
|
|
19 |
|
|
|
20 |
Delay_ms(6); |
|
|
21 |
|
|
|
22 |
i2c_start(); //nastveni Mode Register |
|
|
23 |
I2C_Write(MAG_ADDR_W); |
|
|
24 |
I2C_Write(0x02); |
|
|
25 |
I2C_Write(0x00); |
|
|
26 |
i2c_stop(); |
|
|
27 |
Delay_ms(6); |
|
|
28 |
} |
|
|
29 |
|
|
|
30 |
void set_mag_roz (unsigned int8 h) //nastavy rozsah |
|
|
31 |
{ |
|
|
32 |
|
|
|
33 |
// |
|
|
34 |
|
|
|
35 |
|
|
|
36 |
i2c_start(); |
|
|
37 |
I2C_Write(MAG_ADDR_W); |
|
|
38 |
I2C_Write(0x01); |
|
|
39 |
I2C_Write(h); |
|
|
40 |
i2c_stop(); |
|
|
41 |
|
|
|
42 |
Delay_ms(6); |
|
|
43 |
|
|
|
44 |
|
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
|
|
|
48 |
byte mag_read(byte reg) //pro cteni reg |
|
|
49 |
{ |
|
|
50 |
|
|
|
51 |
i2c_start(); |
|
|
52 |
I2C_Write(MAG_ADDR_W); |
|
|
53 |
I2C_write(reg); |
|
|
54 |
i2c_stop(); |
|
|
55 |
i2c_start(); |
|
|
56 |
I2C_Write(MAG_ADDR_R); |
|
|
57 |
reg=i2c_read(0); |
|
|
58 |
return reg; |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
|
|
|
62 |
signed int16 mag_vypocet(unsigned int8 h, unsigned int8 l) //prepocet na 16bit cislo |
|
|
63 |
{ |
|
|
64 |
signed int16 x; |
|
|
65 |
x = (((unsigned int16) h << 8) + l ); |
|
|
66 |
return x; |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
|
|
|
70 |
signed int16 mag_readX(void) //nacteni osy x |
|
|
71 |
{ |
|
|
72 |
unsigned int8 h,l; |
|
|
73 |
signed int16 x; |
|
|
74 |
h=mag_read(0x03); |
|
|
75 |
l=mag_read(0x04); |
|
|
76 |
x=mag_vypocet(h,l); |
|
|
77 |
return x; |
|
|
78 |
|
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
|
|
|
82 |
signed int16 mag_readY(void) //nacteni osy x |
|
|
83 |
{ |
|
|
84 |
unsigned int8 h,l; |
|
|
85 |
signed int16 x; |
|
|
86 |
h=mag_read(0x07); |
|
|
87 |
l=mag_read(0x08); |
|
|
88 |
x=mag_vypocet(h,l); |
|
|
89 |
return x; |
|
|
90 |
|
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
|
|
|
94 |
signed int16 mag_readZ(void) //nacteni osy x |
|
|
95 |
{ |
|
|
96 |
unsigned int8 h,l; |
|
|
97 |
signed int16 x; |
|
|
98 |
h=mag_read(0x05); |
|
|
99 |
l=mag_read(0x06); |
|
|
100 |
x=mag_vypocet(h,l); |
|
|
101 |
return x; |
|
|
102 |
|
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
|