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