tried out the sensor board by mbed
Dependencies: MAG3110 MMA8451Q MMA8491Q MPL3115A2 TSI TextLCD mbed
Revision 0:3add2fd1bf66, committed 2013-10-10
- Comitter:
- jppang
- Date:
- Thu Oct 10 02:23:00 2013 +0000
- Child:
- 1:561b57bbbdcc
- Commit message:
- Tried out by modifying MMA8451Q library
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MAG3110.lib Thu Oct 10 02:23:00 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/SomeRandomBloke/code/MAG3110/#9644c7d596a1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA8451Q.lib Thu Oct 10 02:23:00 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/JoKer/code/MMA8451Q/#2d14600116fc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA8491Q.cpp Thu Oct 10 02:23:00 2013 +0000 @@ -0,0 +1,155 @@ +/* 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 "MMA8491Q.h" + +#define INT_SOURCE 0x0C +#define REG_WHO_AM_I 0x0D +#define HP_FILTER_CUTOFF 0x0F +#define PULSE_CFG 0x21 +#define PULSE_SRC 0x22 +#define PULSE_THSX 0x23 +#define PULSE_THSY 0x24 +#define PULSE_THSZ 0x25 +#define PULSE_TMLT 0x26 +#define PULSE_LTCY 0x27 +#define PULSE_WIND 0x28 +#define REG_CTRL_REG_1 0x2A +#define CTRL_REG2 0x2B +#define CTRL_REG4 0x2D +#define CTRL_REG5 0x2E +#define REG_OUT_X_MSB 0x01 +#define REG_OUT_Y_MSB 0x03 +#define REG_OUT_Z_MSB 0x05 + +#define UINT14_MAX 16383 + +MMA8491Q::MMA8491Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { + // activate the peripheral + uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; + writeRegs(data, 2); +} + +MMA8491Q::~MMA8491Q() { } + +uint8_t MMA8491Q::getWhoAmI() { + uint8_t who_am_i = 0; + readRegs(REG_WHO_AM_I, &who_am_i, 1); + return who_am_i; +} + +float MMA8491Q::getAccX() { +//divide by 4096 b/c MMA output is 4096 counts per g so this f outputs accelorometer value formatted to g (gravity) + return (float(getAccAxis(REG_OUT_X_MSB))*4.0/4096.0); +} + +float MMA8491Q::getAccY() { + return (float(getAccAxis(REG_OUT_Y_MSB))*4.0/4096.0); +} + +float MMA8491Q::getAccZ() { + return (float(getAccAxis(REG_OUT_Z_MSB))*4.0/4096.0); +} + +void MMA8491Q::getAccAllAxis(float * res) { + res[0] = getAccX(); + res[1] = getAccY(); + res[2] = getAccZ(); +} + +int16_t MMA8491Q::getAccAxis(uint8_t addr) { + int16_t acc; + uint8_t res[2]; + readRegs(addr, res, 2); + + acc = (res[0] << 6) | (res[1] >> 2); + if (acc > UINT14_MAX/2) + acc -= UINT14_MAX; + + return acc; +} + +void MMA8491Q::setDoubleTap(void){ +//Implemented directly from Freescale's AN4072 +//Added to MMA8491Q lib + + uint8_t CTRL_REG1_Data; +// int adds; + uint8_t data[2] = {REG_CTRL_REG_1, 0x08}; + + //400 Hz, Standby Mode + writeRegs(data,2); + + //Enable X, Y and Z Double Pulse with DPA = 0 no double pulse abort + data[0]=PULSE_CFG;data[1]=0x2A; + writeRegs(data,2); + + //SetThreshold 3g on X and Y and 5g on Z + //Note: Every step is 0.063g + //3 g/0.063g = 48 counts + //5g/0.063g = 79 counts + data[0]=PULSE_THSX;data[1]=0x30; + writeRegs(data,2);//Set X Threshold to 3g + data[0]=PULSE_THSY;data[1]=0x30; + writeRegs(data,2);//Set Y Threshold to 3g + data[0]=PULSE_THSZ;data[1]=0x4F; + writeRegs(data,2);//Set Z Threshold to 5g + + //Set Time Limit for Tap Detection to 60 ms LP Mode + //Note: 400 Hz ODR, Time step is 1.25 ms per step + //60 ms/1.25 ms = 48 counts + data[0]=PULSE_TMLT;data[1]=0x30; + writeRegs(data,2);//60 ms + + //Set Latency Time to 200 ms + //Note: 400 Hz ODR LPMode, Time step is 2.5 ms per step 00 ms/2.5 ms = 80 counts + data[0]=PULSE_LTCY;data[1]=0x50; + writeRegs(data,2);//200 ms + + //Set Time Window for second tap to 300 ms + //Note: 400 Hz ODR LP Mode, Time step is 2.5 ms per step + //300 ms/2.5 ms = 120 counts + data[0]=PULSE_WIND;data[1]=0x78; + writeRegs(data,2);//300 ms + + //Route INT1 to System Interrupt + data[0]=CTRL_REG4;data[1]=0x08; + writeRegs(data,2);//Enable Pulse Interrupt in System CTRL_REG4 + data[0]=CTRL_REG5;data[1]=0x08; + writeRegs(data,2);//Route Pulse Interrupt to INT1 hardware Pin CTRL_REG5 + + //Set the device to Active Mode + readRegs(0x2A,&CTRL_REG1_Data,1);//Read out the contents of the register + CTRL_REG1_Data |= 0x01; //Change the value in the register to Active Mode. + data[0]=REG_CTRL_REG_1; + data[1]=CTRL_REG1_Data; + writeRegs(data,2);//Write in the updated value to put the device in Active Mode +} + + +void MMA8491Q::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 MMA8491Q::writeRegs(uint8_t * data, int len) { + m_i2c.write(m_addr, (char *)data, len); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA8491Q.h Thu Oct 10 02:23:00 2013 +0000 @@ -0,0 +1,159 @@ +/* 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. +*/ + +#ifndef MMA8491Q_H +#define MMA8491Q_H + +#include "mbed.h" + +/** +* MMA8491Q accelerometer example +* +* @code +* #include "mbed.h" +* #include "MMA8491Q.h" +* +* #define MMA8451_I2C_ADDRESS (0x1d<<1) +* +* int main(void) { +* +* MMA8491Q acc(P_E25, P_E24, MMA8491_I2C_ADDRESS); +* PwmOut rled(LED_RED); +* PwmOut gled(LED_GREEN); +* PwmOut bled(LED_BLUE); +* +* while (true) { +* rled = 1.0 - abs(acc.getAccX()); +* gled = 1.0 - abs(acc.getAccY()); +* bled = 1.0 - abs(acc.getAccZ()); +* wait(0.1); +* } +* } +* @endcode +*/ +class MMA8491Q +{ +public: + /** + * MMA8491Q constructor + * + * @param sda SDA pin + * @param sdl SCL pin + * @param addr addr of the I2C peripheral + */ + MMA8491Q(PinName sda, PinName scl, int addr); + + /** + * MMA8491Q destructor + */ + ~MMA8491Q(); + + /** + * Get the value of the WHO_AM_I register + * + * @returns WHO_AM_I value + */ + uint8_t getWhoAmI(); + + /** + * Get X axis acceleration + * + * @returns X axis acceleration + */ + float getAccX(); + + /** + * Get Y axis acceleration + * + * @returns Y axis acceleration + */ + float getAccY(); + + /** + * Get Z axis acceleration + * + * @returns Z axis acceleration + */ + float getAccZ(); + + /** + * Get XYZ axis acceleration + * + * @param res array where acceleration data will be stored + */ + void getAccAllAxis(float * res); + + /** JK + * Setup Double Tap detection + + +Example: + +#include "mbed.h" +#include "MMA8491Q.h" + +#define MMA8451_I2C_ADDRESS (0x1d<<1) +#define ON 0 +#define OFF !ON + +//Setup the interrupts for the MMA8491Q +InterruptIn accInt1(PTA14); +InterruptIn accInt2(PTA15);//not used in this prog but this is the other int from the accelorometer + +uint8_t togstat=0;//Led status +DigitalOut bled(LED_BLUE); + + +void tapTrue(void){//ISR + if(togstat == 0){ + togstat = 1; + bled=ON; + } else { + togstat = 0; + bled=OFF; + } + +} + + +int main(void) { + + MMA8491Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);//accelorometer instance + + acc.setDoubleTap();//Setup the MMA8491Q to look for a double Tap + accInt1.rise(&tapTrue);//call tapTrue when an interrupt is generated on PTA14 + + while (true) { + //Interrupt driven so nothing in main loop + } +} + + + */ + void setDoubleTap(void); + +private: + I2C m_i2c; + int m_addr; + void readRegs(int addr, uint8_t * data, int len); + void writeRegs(uint8_t * data, int len); + int16_t getAccAxis(uint8_t addr); + +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPL3115A2.lib Thu Oct 10 02:23:00 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/clemente/code/MPL3115A2/#98b64b8fe817
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TSI.lib Thu Oct 10 02:23:00 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/vsluiter/code/TSI/#4dc2f5a3a731
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Oct 10 02:23:00 2013 +0000 @@ -0,0 +1,74 @@ + +#include "mbed.h" +#include "TSISensor.h" +#include "MMA8451Q.h" +#include "MMA8491Q.h" +#include "MAG3110.h" +#include "MPL3115A2.h" +#include <cstdlib> +#include <iostream> + +//#define MMA8451_I2C_ADDRESS (0x1d<<1) +//#define MMA8491_I2C_ADDRESS (0x55<<1) +//#define MPL3115A2_I2C_ADDRESS (0x60<<1) +//#define MAG3110_I2C_ADDRESS (0x0E<<1) + +Serial pc(USBTX, USBRX); + +TSISensor tsi; +MMA8451Q acc51(PTE25, PTE24, 0x1D<<1); +MMA8491Q acc91(PTE0, PTE1, 0x55<<1); +MAG3110 mag(PTE0, PTE1, 0x0E<<1); +MPL3115A2 mpl(PTE0, PTE1, 0x60<<1); + +PwmOut rled(LED_RED); +PwmOut gled(LED_GREEN); +PwmOut bled(LED_BLUE); + +DigitalOut enMMA8491(PTA13); + +InterruptIn intMPL3115(PTD3); +InterruptIn intMAG3110(PTD1); + +DigitalIn acc91X(PTD2); +DigitalIn acc91Y(PTD0); +DigitalIn acc91Z(PTD5); + + +int main() +{ + using namespace std; + + float onTime = 1.0; + float offTime = 0.0; + float holdTime = 1.0; + bool on = true; + bool off = false; + int magX = 0, magY = 0, magZ = 0; + mpl.Altimeter_Mode(); + + while(true) { + enMMA8491 = on; + rled = onTime - abs(acc51.getAccX()); + gled = onTime - abs(acc51.getAccY()); + bled = onTime - abs(acc51.getAccZ()); + mag.getValues(&magX, &magY, &magZ); + + cout << "MMA8451: " << acc51.getAccX() << "\t" << acc51.getAccY() << "\t" << acc51.getAccZ() << "\n\r" << endl; + cout << "MMA8491: " << acc91X << "\t" << acc91Y << "\t" << acc91Z << "\n\r" << endl; + cout << "MMA8491: " << acc91.getAccX() << "\t" << acc91.getAccY() << "\t" << acc91.getAccZ() << "\n\r" << endl; + cout << "MAG3110: " << magX << "\t" << magY << "\t" << magZ << "\n\r" << endl; + wait(holdTime); + + enMMA8491 = off; + cout << mpl.getStatus() << "\n\r" << endl; + rled = onTime - tsi.readPercentage(); + gled = onTime - tsi.readPercentage(); + bled = onTime - tsi.readPercentage(); + + cout << "Touch: " << tsi.readPercentage() << "\n\r" << endl; + cout << "MPL3115: " << mpl.getAltimeter() << "\t" << mpl.getPressure() << "\t" << mpl.getTemperature() << "\n\r" << endl; + wait(holdTime); + } + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.lib Thu Oct 10 02:23:00 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/#a9913a65894f