Rev Author Line No. Line
2998 kaklik 1 //------------------------------
2 // Low level routines
3 //------------------------------
4 void hmc5883l_write_reg(int8 reg, int8 data)
2980 kaklik 5 {
2998 kaklik 6 i2c_start();
7 i2c_write(HMC5883L_WRT_ADDR);
8 i2c_write(reg);
9 i2c_write(data);
10 i2c_stop();
2980 kaklik 11 }
12  
2998 kaklik 13 //------------------------------
14 int8 hmc5883l_read_reg(int8 reg)
2980 kaklik 15 {
2998 kaklik 16 int8 retval;
2980 kaklik 17  
2998 kaklik 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();
2980 kaklik 25  
2998 kaklik 26 return(retval);
2980 kaklik 27 }
28  
2998 kaklik 29 //------------------------------
30 typedef struct
2980 kaklik 31 {
2998 kaklik 32 signed int16 x;
33 signed int16 y;
34 signed int16 z;
35 }hmc5883l_result;
2980 kaklik 36  
2998 kaklik 37 // This global structure holds the values read
38 // from the HMC5883L x,y,z registers.
39 hmc5883l_result compass = {0,0,0};
2980 kaklik 40  
2998 kaklik 41 //------------------------------
42 void hmc5883l_read_data(void)
2980 kaklik 43 {
2998 kaklik 44 unsigned int8 x_lsb;
45 unsigned int8 x_msb;
2980 kaklik 46  
2998 kaklik 47 unsigned int8 y_lsb;
48 unsigned int8 y_msb;
2980 kaklik 49  
2998 kaklik 50 unsigned int8 z_lsb;
51 unsigned int8 z_msb;
2980 kaklik 52  
2998 kaklik 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);
2980 kaklik 58  
2998 kaklik 59 x_msb = i2c_read();
60 x_lsb = i2c_read();
2980 kaklik 61  
2998 kaklik 62 z_msb = i2c_read();
63 z_lsb = i2c_read();
2980 kaklik 64  
2998 kaklik 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);
2980 kaklik 74 }
75