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.
Dependencies: mbed CC3000_Hostdriver TEMT6200 TSI Wi-Go_eCompass_Lib_V3 WiGo_BattCharger
Fork of CC3000_Simple_Socket by
Diff: Drivers/MMA8451Q/MMA8451Q.cpp
- Revision:
- 14:af04ddf7c59a
- Child:
- 15:cc204c19f888
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Drivers/MMA8451Q/MMA8451Q.cpp Mon Nov 18 17:43:55 2013 +0000
@@ -0,0 +1,77 @@
+/* Copyright (c) 2010-2011 mbed.org, MIT License
+*
+* 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 "MMA8451Q.h"
+
+#define REG_STATUS 0x00
+#define REG_CTRL_REG_1 0x2A
+#define REG_OUT_X_MSB 0x01
+#define REG_OUT_Y_MSB 0x03
+#define REG_OUT_Z_MSB 0x05
+
+MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
+ // activate the peripheral
+ unsigned char t;
+ readRegs(REG_CTRL_REG_1, &t, 1);
+ unsigned char data[2] = {REG_CTRL_REG_1, t|0x01};
+ writeRegs(data, 2);
+}
+
+MMA8451Q::~MMA8451Q() { }
+
+int16_t MMA8451Q::getAccAxis(uint8_t addr) {
+ uint16_t acc;
+ uint8_t res[2];
+ readRegs(addr, res, 2);
+ acc = (res[0] << 6) | (res[1] >> 2);
+ return acc;
+}
+
+int16_t MMA8451Q::getAccRawX(void)
+{
+ return getAccAxis(REG_OUT_X_MSB);
+}
+
+int16_t MMA8451Q::getAccRawY(void)
+{
+ return getAccAxis(REG_OUT_Y_MSB);
+}
+
+int16_t MMA8451Q::getAccRawZ(void)
+{
+ return getAccAxis(REG_OUT_Z_MSB);
+}
+
+uint8_t MMA8451Q::isDataAvailable(void)
+{
+ uint8_t status;
+ readRegs( REG_STATUS, &status, 1);
+ return (status & 0xf);
+}
+
+void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
+ char t[1] = {addr};
+ m_i2c.write(m_addr, t, 1, true);
+ m_i2c.read(m_addr, (char *)data, len);
+}
+
+void MMA8451Q::writeRegs(uint8_t * data, int len) {
+ m_i2c.write(m_addr, (char *)data, len);
+}
+
+
