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