asdf
Fork of LSM303DLHC by
Embed:
(wiki syntax)
Show/hide line numbers
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 * Permission is hereby granted, free of charge, to any person 00015 * obtaining a copy of this software and associated documentation 00016 * files (the "Software"), to deal in the Software without 00017 * restriction, including without limitation the rights to use, 00018 * copy, modify, merge, publish, distribute, sublicense, and/or sell 00019 * copies of the Software, and to permit persons to whom the 00020 * Software is furnished to do so, subject to the following 00021 * conditions: 00022 * 00023 * The above copyright notice and this permission notice shall be 00024 * included in all copies or substantial portions of the Software. 00025 * 00026 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00027 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00028 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00029 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 00030 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 00031 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00032 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00033 * OTHER DEALINGS IN THE SOFTWARE. 00034 */ 00035 #include "mbed.h" 00036 #include "LSM303DLHC.h" 00037 00038 00039 const int addr_acc = 0x32; 00040 const int addr_mag = 0x3c; 00041 00042 enum REG_ADDRS { 00043 /* --- Mag --- */ 00044 CRA_REG_M = 0x00, 00045 CRB_REG_M = 0x01, 00046 MR_REG_M = 0x02, 00047 OUT_X_M = 0x03, 00048 OUT_Y_M = 0x05, 00049 OUT_Z_M = 0x07, 00050 /* --- Acc --- */ 00051 CTRL_REG1_A = 0x20, 00052 CTRL_REG4_A = 0x23, 00053 OUT_X_A = 0x28, 00054 OUT_Y_A = 0x2A, 00055 OUT_Z_A = 0x2C, 00056 }; 00057 00058 bool LSM303DLHC::write_reg(int addr_i2c,int addr_reg, char v) 00059 { 00060 char data[2] = {addr_reg, v}; 00061 return LSM303DLHC::_LSM303.write(addr_i2c, data, 2) == 0; 00062 } 00063 00064 bool LSM303DLHC::read_reg(int addr_i2c,int addr_reg, char *v) 00065 { 00066 char data = addr_reg; 00067 bool result = false; 00068 00069 __disable_irq(); 00070 if ((_LSM303.write(addr_i2c, &data, 1) == 0) && (_LSM303.read(addr_i2c, &data, 1) == 0)){ 00071 *v = data; 00072 result = true; 00073 } 00074 __enable_irq(); 00075 return result; 00076 } 00077 00078 00079 LSM303DLHC::LSM303DLHC(PinName sda, PinName scl): 00080 _LSM303(sda, scl) 00081 { 00082 char reg_v; 00083 _LSM303.frequency(100000); 00084 00085 reg_v = 0; 00086 00087 reg_v |= 0x27; /* X/Y/Z axis enable. */ 00088 write_reg(addr_acc,CTRL_REG1_A,reg_v); 00089 00090 reg_v = 0; 00091 // reg_v |= 0x01 << 6; /* 1: data MSB @ lower address */ 00092 reg_v = 0x01 << 4; /* +/- 4g */ 00093 write_reg(addr_acc,CTRL_REG4_A,reg_v); 00094 00095 /* -- mag --- */ 00096 reg_v = 0; 00097 reg_v |= 0x04 << 2; /* Minimum data output rate = 15Hz */ 00098 write_reg(addr_mag,CRA_REG_M,reg_v); 00099 00100 reg_v = 0; 00101 reg_v |= 0x01 << 5; /* +-1.3Gauss */ 00102 //reg_v |= 0x07 << 5; /* +-8.1Gauss */ 00103 write_reg(addr_mag,CRB_REG_M,reg_v); 00104 00105 reg_v = 0; /* Continuous-conversion mode */ 00106 write_reg(addr_mag,MR_REG_M,reg_v); 00107 } 00108 00109 00110 bool LSM303DLHC::read(float *ax, float *ay, float *az, float *mx, float *my, float *mz) { 00111 char acc[6], mag[6]; 00112 00113 if (recv(addr_acc, OUT_X_A, acc, 6) && recv(addr_mag, OUT_X_M, mag, 6)) { 00114 *ax = float(short(acc[1] << 8 | acc[0]))/8192; //32768/4=8192 00115 *ay = float(short(acc[3] << 8 | acc[2]))/8192; 00116 *az = float(short(acc[5] << 8 | acc[4]))/8192; 00117 //full scale magnetic readings are from -2048 to 2047 00118 //gain is x,y =1100; z = 980 LSB/gauss 00119 *mx = float(short(mag[0] << 8 | mag[1]))/1100; 00120 *mz = float(short(mag[2] << 8 | mag[3]))/980; 00121 *my = float(short(mag[4] << 8 | mag[5]))/1100; 00122 00123 return true; 00124 } 00125 00126 return false; 00127 } 00128 00129 bool LSM303DLHC::read(float *ax, float *ay, float *az) { 00130 char acc[6]; 00131 00132 if (recv(addr_acc, OUT_X_A, acc, 6)) { 00133 *ax = float(short(acc[1] << 8 | acc[0]))/8192; //32768/4=8192 00134 *ay = float(short(acc[3] << 8 | acc[2]))/8192; 00135 *az = float(short(acc[5] << 8 | acc[4]))/8192; 00136 //full scale magnetic readings are from -2048 to 2047 00137 //gain is x,y =1100; z = 980 LSB/gauss 00138 00139 return true; 00140 } 00141 00142 return false; 00143 } 00144 00145 bool LSM303DLHC::recv(char sad, char sub, char *buf, int length) { 00146 if (length > 1) sub |= 0x80; 00147 00148 return _LSM303.write(sad, &sub, 1, true) == 0 && _LSM303.read(sad, buf, length) == 0; 00149 }
Generated on Sat Jul 23 2022 14:38:58 by 1.7.2