work nice now
Fork of LSM303DLHC by
Revision 7:ddd9717cdb71, committed 2013-11-17
- Comitter:
- patsteph
- Date:
- Sun Nov 17 17:58:43 2013 +0000
- Parent:
- 6:fcf7e9b8ce21
- Commit message:
- mbed working with arduino program
Changed in this revision
LSM303DLHC.cpp | Show annotated file Show diff for this revision Revisions of this file |
LSM303DLHC.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r fcf7e9b8ce21 -r ddd9717cdb71 LSM303DLHC.cpp --- a/LSM303DLHC.cpp Tue Nov 12 18:30:37 2013 +0000 +++ b/LSM303DLHC.cpp Sun Nov 17 17:58:43 2013 +0000 @@ -1,37 +1,4 @@ -/** LSM303DLHC 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 LSM303DLHC breakout by ryantm: - * - * 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 "LSM303DLHC.h" @@ -39,22 +6,6 @@ const int addr_acc = 0x32; const int addr_mag = 0x3c; -enum REG_ADDRS { - /* --- Mag --- */ - CRA_REG_M = 0x00, - CRB_REG_M = 0x01, - MR_REG_M = 0x02, - OUT_X_M = 0x03, - OUT_Y_M = 0x05, - OUT_Z_M = 0x07, - /* --- Acc --- */ - CTRL_REG1_A = 0x20, - CTRL_REG4_A = 0x23, - OUT_X_A = 0x28, - OUT_Y_A = 0x2A, - OUT_Z_A = 0x2C, -}; - bool LSM303DLHC::write_reg(int addr_i2c,int addr_reg, char v) { char data[2] = {addr_reg, v}; @@ -107,25 +58,44 @@ } -bool LSM303DLHC::read(float *ax, float *ay, float *az, float *mx, float *my, float *mz) { - char acc[6], mag[6]; +bool LSM303DLHC::readcomp(float *mx, float *my, float *mz) +{ + char mag[6]; char temp; + + if (recv(addr_mag, OUT_X_M, mag, 6)) + { + for(int i=0; i<6; i+=2) + { + temp = mag[i]; + mag[i] = mag[i+1]; //la boucle inverse les valeurs du table + mag[i+1] = temp; + } + + + *mx = *((short*)(mag));///1100.0; + *mz = *((short*)(mag+2));///980.0; //ATTENTION: le z vient avant le y, c'est normal!!!!! + *my = *((short*)(mag+4));///1100.0; + - if (recv(addr_acc, OUT_X_A, acc, 6) && recv(addr_mag, OUT_X_M, mag, 6)) { - *ax = *((short*)(acc))/8192.0; //32768/4=8192 - *ay = *((short*)(acc+2))/8192.0; - *az = *((short*)(acc+4))/8192.0; + return true; + } + + return false; +} +bool LSM303DLHC::readacc(float *ax, float *ay, float *az) +{ + char acc[6]; + + if (recv(addr_acc, OUT_X_A, acc, 6)) + { + *ax = *((short*)(acc)) >>4;///8192.0; //32768/4=8192 + *ay = *((short*)(acc+2)) >>4;///8192.0; + *az = *((short*)(acc+4)) >>4;///8192.0; //full scale magnetic readings are from -2048 to 2047 //gain is x,y =1100; z = 980 LSB/gauss - for(int i=0; i<6; i+=2) - { - temp = mag[i]; - mag[i] = mag[i+1]; - mag[i+1] = temp; - } - *mx = *((short*)(mag))/1100.0; - *my = *((short*)(mag+2))/1100.0; - *mz = *((short*)(mag+4))/980.0; + + return true; } @@ -133,7 +103,6 @@ return false; } - bool LSM303DLHC::recv(char sad, char sub, char *buf, int length) { if (length > 1) sub |= 0x80;
diff -r fcf7e9b8ce21 -r ddd9717cdb71 LSM303DLHC.h --- a/LSM303DLHC.h Tue Nov 12 18:30:37 2013 +0000 +++ b/LSM303DLHC.h Sun Nov 17 17:58:43 2013 +0000 @@ -3,7 +3,19 @@ #define __LSM303DLHC_H #include "mbed.h" - + /* --- Mag --- */ +#define CRA_REG_M 0x00 +#define CRB_REG_M 0x01 +#define MR_REG_M 0x02 +#define OUT_X_M 0x03 +#define OUT_Y_M 0x05 +#define OUT_Z_M 0x07 + /* --- Acc --- */ +#define CTRL_REG1_A 0x20 +#define CTRL_REG4_A 0x23 +#define OUT_X_A 0x28 +#define OUT_Y_A 0x2A +#define OUT_Z_A 0x2C class LSM303DLHC { public: @@ -20,8 +32,9 @@ * @param ax,ay,az is the accelerometer 3d vector, written by the function * @param mx,my,mz is the magnetometer 3d vector, written by the function */ - bool read(float *ax, float *ay, float *az, float *mx, float *my, float *mz); - + bool readcomp(float *mx, float *my, float *mz); + bool readacc(float *ax, float *ay, float *az); + bool write_reg(int addr_i2c,int addr_reg, char v); private: I2C _LSM303; @@ -30,7 +43,7 @@ float ax, ay, az; float mx, my, mz; - bool write_reg(int addr_i2c,int addr_reg, char v); + bool read_reg(int addr_i2c,int addr_reg, char *v); bool recv(char sad, char sub, char *buf, int length); };