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