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.
MMA8451Q.h
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #ifndef MMA8451Q_H 00020 #define MMA8451Q_H 00021 00022 #include "mbed.h" 00023 00024 /** 00025 * MMA8451Q accelerometer example 00026 * #include "mbed.h" 00027 * #include "MMA8451Q.h" 00028 * 00029 * #define MMA8451_I2C_ADDRESS (0x1d<<1) 00030 * 00031 * int main(void) { 00032 * DigitalOut led(LED_GREEN); 00033 * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS); 00034 * printf("WHO AM I: 0x%2X\r\n", acc.getWhoAmI()); 00035 * 00036 * while (true) { 00037 * printf("-----------\r\n"); 00038 * printf("acc_x: %d\r\n", acc.getAccX()); 00039 * printf("acc_y: %d\r\n", acc.getAccY()); 00040 * printf("acc_z: %d\r\n", acc.getAccZ()); 00041 * 00042 * wait(1); 00043 * led = !led; 00044 * } 00045 * } 00046 */ 00047 class MMA8451Q 00048 { 00049 public: 00050 /** 00051 * MMA8451Q constructor 00052 * 00053 * @param sda SDA pin 00054 * @param sdl SCL pin 00055 * @param addr addr of the I2C peripheral 00056 */ 00057 MMA8451Q(PinName sda, PinName scl, int addr); 00058 00059 /** 00060 * MMA8451Q destructor 00061 */ 00062 ~MMA8451Q(); 00063 00064 /** 00065 * Get the value of the WHO_AM_I register 00066 * 00067 * @returns WHO_AM_I value 00068 */ 00069 uint8_t getWhoAmI(); 00070 00071 /** 00072 * Get X axis acceleration 00073 * 00074 * @returns X axis acceleration 00075 */ 00076 int16_t getAccX(); 00077 00078 /** 00079 * Get Y axis acceleration 00080 * 00081 * @returns Y axis acceleration 00082 */ 00083 int16_t getAccY(); 00084 00085 /** 00086 * Get Z axis acceleration 00087 * 00088 * @returns Z axis acceleration 00089 */ 00090 int16_t getAccZ(); 00091 00092 /** 00093 * Get XYZ axis acceleration 00094 * 00095 * @param res array where acceleration data will be stored 00096 */ 00097 void getAccAllAxis(int16_t * res); 00098 00099 private: 00100 I2C m_i2c; 00101 int m_addr; 00102 void readRegs(int addr, uint8_t * data, int len); 00103 void writeRegs(uint8_t * data, int len); 00104 int16_t getAccAxis(uint8_t addr); 00105 00106 }; 00107 00108 #endif
Generated on Tue Jul 12 2022 18:18:44 by
