o: ActiveSupport::Cache::Entry :@compressedF: @value"¹]"³]
HMC5883L.c
1
//Knihovna pro senzor HMC5883L
2
//(c) Jan Chroust 2013
3

  
4

  
5
void set_mag (void) //uvodni nastaveni na kontinualni mereni, rozsah ? 8.1 Ga, frekvence mereni 15HZ
1
//------------------------------
2
// Low level routines
3
//------------------------------
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();
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);     
24
  I2C_Write(0x02);   
25
  I2C_Write(0x00);
26
  i2c_stop();
27
  Delay_ms(6);  
6
i2c_start();
7
i2c_write(HMC5883L_WRT_ADDR);
8
i2c_write(reg);
9
i2c_write(data);
10
i2c_stop();
28 11
}
29 12

  
30
void set_mag_roz (unsigned int8 h) //nastavy rozsah
13
//------------------------------
14
int8 hmc5883l_read_reg(int8 reg)
31 15
{
16
int8 retval;
32 17

  
33
//
34
  
35
   
36
  i2c_start();       
37
  I2C_Write(MAG_ADDR_W);     
38
  I2C_Write(0x01);   
39
  I2C_Write(h);
40
  i2c_stop();
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();
41 25

  
42
  Delay_ms(6);
43

  
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();
52
   I2C_Write(MAG_ADDR_W);
53
   I2C_write(reg);
54
   i2c_stop();
55
   i2c_start();
56
   I2C_Write(MAG_ADDR_R);
57
   reg=i2c_read(0);
58
  return reg;
59
}
32
signed int16 x;
33
signed int16 y;
34
signed int16 z;
35
}hmc5883l_result;
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

  
62
signed int16 mag_vypocet(unsigned int8 h, unsigned int8 l) //prepocet na 16bit cislo
41
//------------------------------
42
void hmc5883l_read_data(void)
63 43
{
64
signed int16 x;
65
x = (((unsigned int16) h << 8) + l );
66
return x;   
67
}
44
unsigned int8 x_lsb;
45
unsigned int8 x_msb;
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;
73
signed int16 x;
74
h=mag_read(0x03);
75
l=mag_read(0x04);
76
x=mag_vypocet(h,l);
77
return x;
78
   
79
}
50
unsigned int8 z_lsb;
51
unsigned int8 z_msb;
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);
87
l=mag_read(0x08);
88
x=mag_vypocet(h,l);
89
return x;
90
   
91
}
59
x_msb = i2c_read();
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);
99
l=mag_read(0x06);
100
x=mag_vypocet(h,l);
101
return x;
102
   
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);
103 74
}
104 75

  
105

  
:@created_atf1442367213.9772401‹:@expires_in0