1 |
// i2c slave addresses |
- |
|
2 |
#define HMC5883L_WRT_ADDR 0x3C |
- |
|
3 |
#define HMC5883L_READ_ADDR 0x3D |
- |
|
4 |
|
- |
|
5 |
// Register addresses |
- |
|
6 |
#define HMC5883L_CFG_A_REG 0x00 |
- |
|
7 |
#define HMC5883L_CFG_B_REG 0x01 |
- |
|
8 |
#define HMC5883L_MODE_REG 0x02 |
- |
|
9 |
#define HMC5883L_X_MSB_REG 0x03 |
- |
|
10 |
|
- |
|
11 |
//------------------------------ |
1 |
//------------------------------ |
12 |
// Low level routines |
2 |
// Low level routines |
13 |
//------------------------------ |
3 |
//------------------------------ |
14 |
void hmc5883l_write_reg(int8 reg, int8 data) |
4 |
void hmc5883l_write_reg(int8 reg, int8 data) |
15 |
{ |
5 |
{ |
16 |
i2c_start(); |
6 |
i2c_start(); |
17 |
i2c_write(HMC5883L_WRT_ADDR); |
7 |
i2c_write(HMC5883L_WRT_ADDR); |
18 |
i2c_write(reg); |
8 |
i2c_write(reg); |
19 |
i2c_write(data); |
9 |
i2c_write(data); |
20 |
i2c_stop(); |
10 |
i2c_stop(); |
21 |
} |
11 |
} |
22 |
|
12 |
|
23 |
//------------------------------ |
13 |
//------------------------------ |
24 |
int8 hmc5883l_read_reg(int8 reg) |
14 |
int8 hmc5883l_read_reg(int8 reg) |
25 |
{ |
15 |
{ |
26 |
int8 retval; |
16 |
int8 retval; |
27 |
|
17 |
|
28 |
i2c_start(); |
18 |
i2c_start(); |
29 |
i2c_write(HMC5883L_WRT_ADDR); |
19 |
i2c_write(HMC5883L_WRT_ADDR); |
30 |
i2c_write(reg); |
20 |
i2c_write(reg); |
31 |
i2c_start(); |
21 |
i2c_start(); |
32 |
i2c_write(HMC5883L_READ_ADDR); |
22 |
i2c_write(HMC5883L_READ_ADDR); |
33 |
retval = i2c_read(0); |
23 |
retval = i2c_read(0); |
34 |
i2c_stop(); |
24 |
i2c_stop(); |
35 |
|
25 |
|
36 |
return(retval); |
26 |
return(retval); |
37 |
} |
27 |
} |
38 |
|
28 |
|
39 |
//------------------------------ |
29 |
//------------------------------ |
40 |
typedef struct |
30 |
typedef struct |
41 |
{ |
31 |
{ |
42 |
signed int16 x; |
32 |
signed int16 x; |
43 |
signed int16 y; |
33 |
signed int16 y; |
44 |
signed int16 z; |
34 |
signed int16 z; |
45 |
}hmc5883l_result; |
35 |
}hmc5883l_result; |
46 |
|
36 |
|
47 |
// This global structure holds the values read |
37 |
// This global structure holds the values read |
48 |
// from the HMC5883L x,y,z registers. |
38 |
// from the HMC5883L x,y,z registers. |
49 |
hmc5883l_result compass = {0,0,0}; |
39 |
hmc5883l_result compass = {0,0,0}; |
50 |
|
40 |
|
51 |
//------------------------------ |
41 |
//------------------------------ |
52 |
void hmc5883l_read_data(void) |
42 |
void hmc5883l_read_data(void) |
53 |
{ |
43 |
{ |
54 |
unsigned int8 x_lsb; |
44 |
unsigned int8 x_lsb; |
55 |
unsigned int8 x_msb; |
45 |
unsigned int8 x_msb; |
56 |
|
46 |
|
57 |
unsigned int8 y_lsb; |
47 |
unsigned int8 y_lsb; |
58 |
unsigned int8 y_msb; |
48 |
unsigned int8 y_msb; |
59 |
|
49 |
|
60 |
unsigned int8 z_lsb; |
50 |
unsigned int8 z_lsb; |
61 |
unsigned int8 z_msb; |
51 |
unsigned int8 z_msb; |
62 |
|
52 |
|
63 |
i2c_start(); |
53 |
i2c_start(); |
64 |
i2c_write(HMC5883L_WRT_ADDR); |
54 |
i2c_write(HMC5883L_WRT_ADDR); |
65 |
i2c_write(HMC5883L_X_MSB_REG); // Point to X-msb register |
55 |
i2c_write(HMC5883L_X_MSB_REG); // Point to X-msb register |
66 |
i2c_start(); |
56 |
i2c_start(); |
67 |
i2c_write(HMC5883L_READ_ADDR); |
57 |
i2c_write(HMC5883L_READ_ADDR); |
68 |
|
58 |
|
69 |
x_msb = i2c_read(); |
59 |
x_msb = i2c_read(); |
70 |
x_lsb = i2c_read(); |
60 |
x_lsb = i2c_read(); |
71 |
|
61 |
|
72 |
z_msb = i2c_read(); |
62 |
z_msb = i2c_read(); |
73 |
z_lsb = i2c_read(); |
63 |
z_lsb = i2c_read(); |
74 |
|
64 |
|
75 |
y_msb = i2c_read(); |
65 |
y_msb = i2c_read(); |
76 |
y_lsb = i2c_read(0); // do a NACK on last read |
66 |
y_lsb = i2c_read(0); // do a NACK on last read |
77 |
|
67 |
|
78 |
i2c_stop(); |
68 |
i2c_stop(); |
79 |
|
69 |
|
80 |
// Combine high and low bytes into 16-bit values. |
70 |
// Combine high and low bytes into 16-bit values. |
81 |
compass.x = make16(x_msb, x_lsb); |
71 |
compass.x = make16(x_msb, x_lsb); |
82 |
compass.y = make16(y_msb, y_lsb); |
72 |
compass.y = make16(y_msb, y_lsb); |
83 |
compass.z = make16(z_msb, z_lsb); |
73 |
compass.z = make16(z_msb, z_lsb); |
84 |
} |
74 |
} |
85 |
|
75 |
|