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