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.
Fork of LSM303DLHC by
LSM303DLHC.cpp
00001 /** LSM303DLHC Interface Library 00002 * 00003 * base on code by Michael Shimniok http://bot-thoughts.com 00004 * 00005 * and on test program by @tosihisa and 00006 * 00007 * and on Pololu sample library for LSM303DLHC breakout by ryantm: 00008 * 00009 * Copyright (c) 2011 Pololu Corporation. For more information, see 00010 * 00011 * http://www.pololu.com/ 00012 * http://forum.pololu.com/ 00013 * 00014 * FREE FALL INTERRUPT CODE ADDED BY TASOS CHARISIS 00015 * 00016 * Permission is hereby granted, free of charge, to any person 00017 * obtaining a copy of this software and associated documentation 00018 * files (the "Software"), to deal in the Software without 00019 * restriction, including without limitation the rights to use, 00020 * copy, modify, merge, publish, distribute, sublicense, and/or sell 00021 * copies of the Software, and to permit persons to whom the 00022 * Software is furnished to do so, subject to the following 00023 * conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00030 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 00032 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 00033 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00034 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00035 * OTHER DEALINGS IN THE SOFTWARE. 00036 */ 00037 #include "mbed.h" 00038 #include "LSM303DLHC.h" 00039 00040 00041 const int addr_acc = 0x32; 00042 const int addr_mag = 0x3c; 00043 00044 enum REG_ADDRS { 00045 /* --- Mag --- */ 00046 CRA_REG_M = 0x00, 00047 CRB_REG_M = 0x01, 00048 MR_REG_M = 0x02, 00049 OUT_X_M = 0x03, 00050 OUT_Y_M = 0x05, 00051 OUT_Z_M = 0x07, 00052 /* --- Acc --- */ 00053 CTRL_REG1_A = 0x20, 00054 CTRL_REG3_A = 0x22, 00055 CTRL_REG4_A = 0x23, 00056 CTRL_REG5_A = 0x24, 00057 REFERENCE_A = 0x26, 00058 INT1_CFG_A = 0x30, 00059 INT1_THS_A = 0x32, 00060 INT1_DURATION_A = 0x33, 00061 OUT_X_A = 0x28, 00062 OUT_Y_A = 0x2A, 00063 OUT_Z_A = 0x2C, 00064 }; 00065 00066 bool LSM303DLHC::write_reg(int addr_i2c,int addr_reg, char v) 00067 { 00068 char data[2] = {addr_reg, v}; 00069 return LSM303DLHC::_LSM303.write(addr_i2c, data, 2) == 0; 00070 } 00071 00072 bool LSM303DLHC::read_reg(int addr_i2c,int addr_reg, char *v) 00073 { 00074 char data = addr_reg; 00075 bool result = false; 00076 00077 __disable_irq(); 00078 if ((_LSM303.write(addr_i2c, &data, 1) == 0) && (_LSM303.read(addr_i2c, &data, 1) == 0)){ 00079 *v = data; 00080 result = true; 00081 } 00082 __enable_irq(); 00083 return result; 00084 } 00085 00086 00087 LSM303DLHC::LSM303DLHC(PinName sda, PinName scl): 00088 _LSM303(sda, scl) 00089 { 00090 char reg_v; 00091 _LSM303.frequency(200000); 00092 00093 reg_v = 0; 00094 00095 reg_v |= 0x57; /* Normal mode 100 Hz X/Y/Z axis enable. */ 00096 write_reg(addr_acc,CTRL_REG1_A,reg_v); 00097 00098 reg_v = 0; 00099 // reg_v |= 0x01 << 6; /* 1: data MSB @ lower address */ 00100 reg_v = 0x01 << 4; /* +/- 4g */ 00101 write_reg(addr_acc,CTRL_REG4_A,reg_v); 00102 00103 /* -- mag --- */ 00104 reg_v = 0; 00105 reg_v |= 0x06 << 2; /* Minimum magnetic data output rate = 75Hz */ 00106 write_reg(addr_mag,CRA_REG_M,reg_v); 00107 00108 reg_v = 0; 00109 reg_v |= 0x01 << 5; /* +-1.3Gauss */ 00110 //reg_v |= 0x07 << 5; /* +-8.1Gauss */ 00111 write_reg(addr_mag,CRB_REG_M,reg_v); 00112 00113 reg_v = 0; /* Continuous-conversion mode */ 00114 write_reg(addr_mag,MR_REG_M,reg_v); 00115 00116 /** ------------------------FREE FALL INTERRUPT SETTINGS------------------------------------------- 00117 --------------------CALIBRATION VALUES ARE g THRESHOLD AND DURATION---------------------------------- 00118 **/ 00119 00120 //Setup Free Fall INT as INT1 00121 reg_v = 0x40; /* AOI1 interrupt on INT1 */ 00122 write_reg(addr_acc,CTRL_REG3_A,reg_v); 00123 00124 reg_v = 0x64; /* 100 Counts --> 0.2g at +- 4g scale (2mg/LSB*/ 00125 write_reg(addr_acc,INT1_THS_A,reg_v); 00126 00127 reg_v = 0x5; /* 5 Counts --> 50ms duration at 100Hz sampling */ 00128 write_reg(addr_acc,INT1_DURATION_A,reg_v); 00129 00130 reg_v = 0x95; /* Enable AOI and low events not high on all axes */ 00131 write_reg(addr_acc,INT1_CFG_A,reg_v); 00132 } 00133 00134 00135 bool LSM303DLHC::read(float *ax, float *ay, float *az, float *mx, float *my, float *mz) { 00136 char acc[6], mag[6]; 00137 00138 if (recv(addr_acc, OUT_X_A, acc, 6) && recv(addr_mag, OUT_X_M, mag, 6)) { 00139 *ax = float(short(acc[1] << 8 | acc[0]))/8192; //32768/4=8192 00140 *ay = float(short(acc[3] << 8 | acc[2]))/8192; 00141 *az = float(short(acc[5] << 8 | acc[4]))/8192; 00142 //full scale magnetic readings are from -2048 to 2047 00143 //gain is x,y =1100; z = 980 LSB/gauss 00144 *mx = float(short(mag[0] << 8 | mag[1]))/1100; 00145 *mz = float(short(mag[2] << 8 | mag[3]))/980; 00146 *my = float(short(mag[4] << 8 | mag[5]))/1100; 00147 00148 return 1; 00149 } 00150 00151 return 0; 00152 } 00153 00154 00155 bool LSM303DLHC::recv(char sad, char sub, char *buf, int length) { 00156 if (length > 1) sub |= 0x80; 00157 00158 return _LSM303.write(sad, &sub, 1, true) == 0 && _LSM303.read(sad, buf, length) == 0; 00159 }
Generated on Wed Jul 13 2022 02:47:33 by
1.7.2
