libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
lsm303dlhc.h
Go to the documentation of this file.
1 #ifndef LIBPROPELLER_LSM303DLHC_H_
2 #define LIBPROPELLER_LSM303DLHC_H_
3 
4 #ifndef UNIT_TEST
5 #include "libpropeller/i2c/i2c.h"
6 #else
8 #endif
9 
19 class LSM303DLHC {
20 public:
21 
25  bus_ = NULL;
26  status_ = false;
27  }
28 
63  bool Init(I2C * i2c_bus) {
64  bus_ = i2c_bus;
65 
66  //Check to make sure the LSM303DLHC is actually there.
67  status_ = bus_->Ping(kDeviceMagnAddress);
68  if (status_ == false) {
69  return false;
70  }
71 
72  status_ = bus_->Ping(kDeviceAcclAddress);
73  if (status_ == false) {
74  return false;
75  }
76 
77 
78  //Initialize Magn
79  bus_->Put(kDeviceMagnAddress, kCRA_REG_M, 0b10011100);
80  bus_->Put(kDeviceMagnAddress, kCRB_REG_M, kGain_1_3);
81  bus_->Put(kDeviceMagnAddress, kMR_REG_M, 0b00000000);
82 
83  //Initialize Accl
84  bus_->Put(kDeviceAcclAddress, kCTRL_REG1_A, 0b10010111);
85  bus_->Put(kDeviceAcclAddress, kCTRL_REG4_A, 0b00111000);
86 
87  return true;
88  }
89 
99  bool ReadAccl(int& x, int& y, int& z) {
100  char data[6];
101 
102  if (status_ == false) {
103  x = y = z = 0;
104  return false;
105  }
106 
107  if (bus_->Get(kDeviceAcclAddress, kOUT_X_L_A, data, 6) == false) {
108  x = y = z = 0;
109  return false;
110  }
111 
112  //16 + 4 = 20, or the 12 bit data given by the LSM303DLHC.
113  //We are keeping the last 4 bits, and can divide them out later
114  //(SRLM thinks the accl is only 12 bits, but the datasheet doesn't seem to
115  //specify.
116  x = ((data[0] | (data[1] << 8)) << 16) >> 16;
117  y = ((data[2] | (data[3] << 8)) << 16) >> 16;
118  z = ((data[4] | (data[5] << 8)) << 16) >> 16;
119 
120  return true;
121  }
122 
132  bool ReadMagn(int& x, int& y, int& z) {
133  char data[6];
134 
135  if (status_ == false) {
136  x = y = z = 0;
137  return false;
138  }
139 
140  if (bus_->Get(kDeviceMagnAddress, kOUT_X_H_M, data, 6) == false) {
141  x = y = z = 0;
142  return false;
143  }
144 
145  //Warning: the LSM303DLHC datasheet lists the magnetometer high registers
146  //first, instead of the low (backwards compared to the accel and L3GD20).
147  //Also note that the order of the parameters is XZY!
148  x = ((data[1] | (data[0] << 8)) << 16) >> 16;
149  z = ((data[3] | (data[2] << 8)) << 16) >> 16;
150  y = ((data[5] | (data[4] << 8)) << 16) >> 16;
151 
152  return true;
153  }
154 
155 private:
156  I2C * bus_;
157 
158  bool status_;
159 
160  const static unsigned char kDeviceAcclAddress = 0b00110010;
161  const static unsigned char kDeviceMagnAddress = 0b00111100;
162 
163  //For the LSM303DLH Accl
164  const static unsigned char kCTRL_REG1_A = 0x20;
165  const static unsigned char kCTRL_REG4_A = 0x23;
166  const static unsigned char kOUT_X_L_A = 0x28 | 0x80; //(turn on auto increment)
167 
168  //For the LSM303DLH Magn
169  const static unsigned char kCRA_REG_M = 0x00;
170  const static unsigned char kCRB_REG_M = 0x01;
171  const static unsigned char kMR_REG_M = 0x02;
172  const static unsigned char kOUT_X_H_M = 0x03 | 0x80; //(turn on auto increment)
173 
174  const static unsigned char kGain_1_9 = 0b01000000;
175  const static unsigned char kGain_1_3 = 0b00100000;
176 };
177 
178 #endif // LIBPROPELLER_LSM303DLHC_H_