Rev 2926 Rev 2985
-   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 //------------------------------
-   12 // Low level routines
-   13 //------------------------------
1 void set_mag (void) //uvodni nastaveni na kontinualni mereni, rozsah ± 8.1 Ga, frekvence mereni 15HZ 14 void hmc5883l_write_reg(int8 reg, int8 data)
2 { 15 {
3 i2c_start(); //nastavení Configuration Register A -  
4 I2C_Write(MAG_ADDR_W); -  
5 I2C_Write(0x00); -  
6 I2C_Write(0x70); -  
7 i2c_stop(); -  
8 Delay_ms(6); -  
9 -  
10 i2c_start(); //nastavení Configuration Register B -  
11 I2C_Write(MAG_ADDR_W); -  
12 I2C_Write(0x01); -  
13 I2C_Write(MAG_ROZ810); -  
14 i2c_stop(); -  
15   -  
16 Delay_ms(6); -  
17   -  
18 i2c_start(); //nastveni Mode Register -  
19 I2C_Write(MAG_ADDR_W); -  
20 I2C_Write(0x02); -  
21 I2C_Write(0x00); -  
22 i2c_stop(); -  
23 Delay_ms(6); -  
24 } -  
25   -  
26 void set_mag_roz (unsigned int8 h) //nastavy rozsah -  
27 { -  
28   -  
29 // -  
30 -  
31 -  
32 i2c_start(); 16 i2c_start();
33 I2C_Write(MAG_ADDR_W); 17 i2c_write(HMC5883L_WRT_ADDR);
34 I2C_Write(0x01); 18 i2c_write(reg);
35 I2C_Write(h); 19 i2c_write(data);
36 i2c_stop(); 20 i2c_stop();
37   -  
38 Delay_ms(6); -  
39   -  
40 -  
41 } 21 }
42   22  
43   -  
-   23 //------------------------------
44 byte mag_read(byte reg) //pro cteni reg 24 int8 hmc5883l_read_reg(int8 reg)
45 { 25 {
-   26 int8 retval;
46 27  
47 i2c_start(); 28 i2c_start();
48 I2C_Write(MAG_ADDR_W); 29 i2c_write(HMC5883L_WRT_ADDR);
49 I2C_write(reg); 30 i2c_write(reg);
50 i2c_stop(); -  
51 i2c_start(); 31 i2c_start();
52 I2C_Write(MAG_ADDR_R); 32 i2c_write(HMC5883L_READ_ADDR);
53 reg=i2c_read(0); 33 retval = i2c_read(0);
54 return reg; 34 i2c_stop();
55 } -  
56   35  
-   36 return(retval);
-   37 }
57   38  
58 signed int16 mag_vypocet(unsigned int8 h, unsigned int8 l) //prepocet na 16bit cislo 39 //------------------------------
-   40 typedef struct
59 { 41 {
60 signed int16 x; 42 signed int16 x;
61 x = (((unsigned int16) h << 8) + l ); 43 signed int16 y;
62 return x; 44 signed int16 z;
63 } 45 }hmc5883l_result;
64   46  
-   47 // This global structure holds the values read
-   48 // from the HMC5883L x,y,z registers.
-   49 hmc5883l_result compass = {0,0,0};
65   50  
-   51 //------------------------------
66 signed int16 mag_readX(void) //nacteni osy x 52 void hmc5883l_read_data(void)
67 { 53 {
68 unsigned int8 h,l; 54 unsigned int8 x_lsb;
69 signed int16 x; 55 unsigned int8 x_msb;
70 h=mag_read(0x03); -  
71 l=mag_read(0x04); -  
72 x=mag_vypocet(h,l); -  
73 return x; -  
74 56  
75 } -  
-   57 unsigned int8 y_lsb;
-   58 unsigned int8 y_msb;
76   59  
-   60 unsigned int8 z_lsb;
-   61 unsigned int8 z_msb;
77   62  
78 signed int16 mag_readY(void) //nacteni osy x -  
79 { -  
80 unsigned int8 h,l; -  
81 signed int16 x; 63 i2c_start();
82 h=mag_read(0x07); 64 i2c_write(HMC5883L_WRT_ADDR);
83 l=mag_read(0x08); 65 i2c_write(HMC5883L_X_MSB_REG); // Point to X-msb register
84 x=mag_vypocet(h,l); 66 i2c_start();
85 return x; 67 i2c_write(HMC5883L_READ_ADDR);
86 68  
-   69 x_msb = i2c_read();
87 } 70 x_lsb = i2c_read();
88   71  
-   72 z_msb = i2c_read();
-   73 z_lsb = i2c_read();
89   74  
90 signed int16 mag_readZ(void) //nacteni osy x -  
91 { -  
92 unsigned int8 h,l; -  
93 signed int16 x; -  
94 h=mag_read(0x05); 75 y_msb = i2c_read();
95 l=mag_read(0x06); 76 y_lsb = i2c_read(0); // do a NACK on last read
96 x=mag_vypocet(h,l); -  
97 return x; -  
98 77  
99 } 78 i2c_stop();
100   79
-   80 // Combine high and low bytes into 16-bit values.
-   81 compass.x = make16(x_msb, x_lsb);
-   82 compass.y = make16(y_msb, y_lsb);
-   83 compass.z = make16(z_msb, z_lsb);
-   84 }
101   85