Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Cosmic_Pi_STM32_i2c
LSM303D.cpp
- Committer:
- pingu_98
- Date:
- 2019-01-06
- Revision:
- 3:9e595dd5d97e
- Parent:
- 2:8cd0c3c11b48
File content as of revision 3:9e595dd5d97e:
/** LSM303D Interface Library
*
* base on code by Michael Shimniok http://bot-thoughts.com
*
* and on test program by @tosihisa and
*
* and on Pololu sample library for LSM303D breakout by ryantm:
*
* ported by Brian Claus
*
* Copyright (c) 2011 Pololu Corporation. For more information, see
*
* http://www.pololu.com/
* http://forum.pololu.com/
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "mbed.h"
#include "LSM303D.h"
const int addr_acc_mag = 0x3A;
enum REG_ADDRS {
/* --- Mag --- */
OUT_X_M = 0x08,
OUT_Y_M = 0x0A,
OUT_Z_M = 0x0C,
/* --- Acc --- */
OUT_X_A = 0x28,
OUT_Y_A = 0x2A,
OUT_Z_A = 0x2C,
//
CTRL0 = 0x1F,
CTRL1 = 0x20,
CTRL2 = 0x21,
CTRL3 = 0x22,
CTRL4 = 0x23,
CTRL5 = 0x24,
CTRL6 = 0x25,
CTRL7 = 0x26
};
LSM303D::LSM303D(I2C *i2c):
i2c_(i2c)
//_LSM303(sda, scl)
{
char reg_v;
//_LSM303.frequency(200000);
reg_v = 0;
//acc
reg_v |= 0x57; /* 50 Hz X/Y/Z axis enable. */
write_reg(addr_acc_mag,CTRL1,reg_v);
/* -- mag --- */
reg_v = 0;
reg_v |= 0x78; //M_RES = 11 (high resolution mode); M_ODR = 100 (50 Hz ODR)
write_reg(addr_acc_mag,CTRL5,reg_v);
reg_v = 0;
reg_v |= 0x00; //continuous mag
write_reg(addr_acc_mag,CTRL7,reg_v);
}
bool LSM303D::write_reg(int addr_i2c,int addr_reg, char v)
{
char data[2] = {addr_reg, v};
i2c_->write(addr_i2c, data, 2);
return 0;
}
bool LSM303D::read_reg(int addr_i2c,int addr_reg, char *v)
{
char data = addr_reg;
bool result = false;
__disable_irq();
if ((i2c_->write(addr_i2c, &data, 1) == 0) && (i2c_->write(addr_i2c, &data, 1) == 0)){
*v = data;
result = true;
}
__enable_irq();
return result;
}
bool LSM303D::read(float *ax, float *ay, float *az, float *mx, float *my, float *mz) {
char acc[6], mag[6];
if (recv(addr_acc_mag, OUT_X_A, acc, 6) && recv(addr_acc_mag, OUT_X_M, mag, 6)) {
*ax = float(short(acc[1] << 8 | acc[0]))*0.00061f; //32768/4=8192
*ay = float(short(acc[3] << 8 | acc[2]))*0.00061f;
*az = float(short(acc[5] << 8 | acc[4]))*0.00061f;
//+-4gauss
*mx = float(short(mag[0] << 8 | mag[1]))*0.16f;
*mz = float(short(mag[2] << 8 | mag[3]))*0.16f;
*my = float(short(mag[4] << 8 | mag[5]))*0.16f;
return 0;
}
return false;
}
bool LSM303D::recv(char sad, char sub, char *buf, int length) {
if (length > 1) sub |= 0x80;
return i2c_->write(sad, &sub, 1, true) == 0 && i2c_->read(sad, buf, length) == 0;
}