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