Configuring sensors dynamically using serial bus from the host computer.

Dependencies:   MAG3110 mbed

Committer:
mja054
Date:
Sat Feb 15 00:46:41 2014 +0000
Revision:
2:a422a41dbea1
Parent:
0:1efeb3fc4ba6
Send the time delay

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mja054 0:1efeb3fc4ba6 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
mja054 0:1efeb3fc4ba6 2 *
mja054 0:1efeb3fc4ba6 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mja054 0:1efeb3fc4ba6 4 * and associated documentation files (the "Software"), to deal in the Software without
mja054 0:1efeb3fc4ba6 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mja054 0:1efeb3fc4ba6 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mja054 0:1efeb3fc4ba6 7 * Software is furnished to do so, subject to the following conditions:
mja054 0:1efeb3fc4ba6 8 *
mja054 0:1efeb3fc4ba6 9 * The above copyright notice and this permission notice shall be included in all copies or
mja054 0:1efeb3fc4ba6 10 * substantial portions of the Software.
mja054 0:1efeb3fc4ba6 11 *
mja054 0:1efeb3fc4ba6 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mja054 0:1efeb3fc4ba6 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mja054 0:1efeb3fc4ba6 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mja054 0:1efeb3fc4ba6 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mja054 0:1efeb3fc4ba6 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mja054 0:1efeb3fc4ba6 17 */
mja054 0:1efeb3fc4ba6 18
mja054 0:1efeb3fc4ba6 19 #ifndef MMA8451Q_H
mja054 0:1efeb3fc4ba6 20 #define MMA8451Q_H
mja054 0:1efeb3fc4ba6 21
mja054 0:1efeb3fc4ba6 22 #include "mbed.h"
mja054 0:1efeb3fc4ba6 23
mja054 0:1efeb3fc4ba6 24 /**
mja054 0:1efeb3fc4ba6 25 * MMA8451Q accelerometer example
mja054 0:1efeb3fc4ba6 26 *
mja054 0:1efeb3fc4ba6 27 * @code
mja054 0:1efeb3fc4ba6 28 * #include "mbed.h"
mja054 0:1efeb3fc4ba6 29 * #include "MMA8451Q.h"
mja054 0:1efeb3fc4ba6 30 *
mja054 0:1efeb3fc4ba6 31 * #define MMA8451_I2C_ADDRESS (0x1d<<1)
mja054 0:1efeb3fc4ba6 32 *
mja054 0:1efeb3fc4ba6 33 * int main(void) {
mja054 0:1efeb3fc4ba6 34 *
mja054 0:1efeb3fc4ba6 35 * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS);
mja054 0:1efeb3fc4ba6 36 * PwmOut rled(LED_RED);
mja054 0:1efeb3fc4ba6 37 * PwmOut gled(LED_GREEN);
mja054 0:1efeb3fc4ba6 38 * PwmOut bled(LED_BLUE);
mja054 0:1efeb3fc4ba6 39 *
mja054 0:1efeb3fc4ba6 40 * while (true) {
mja054 0:1efeb3fc4ba6 41 * rled = 1.0 - abs(acc.getAccX());
mja054 0:1efeb3fc4ba6 42 * gled = 1.0 - abs(acc.getAccY());
mja054 0:1efeb3fc4ba6 43 * bled = 1.0 - abs(acc.getAccZ());
mja054 0:1efeb3fc4ba6 44 * wait(0.1);
mja054 0:1efeb3fc4ba6 45 * }
mja054 0:1efeb3fc4ba6 46 * }
mja054 0:1efeb3fc4ba6 47 * @endcode
mja054 0:1efeb3fc4ba6 48 */
mja054 0:1efeb3fc4ba6 49 class MMA8451Q
mja054 0:1efeb3fc4ba6 50 {
mja054 0:1efeb3fc4ba6 51 public:
mja054 0:1efeb3fc4ba6 52 /**
mja054 0:1efeb3fc4ba6 53 * MMA8451Q constructor
mja054 0:1efeb3fc4ba6 54 *
mja054 0:1efeb3fc4ba6 55 * @param sda SDA pin
mja054 0:1efeb3fc4ba6 56 * @param sdl SCL pin
mja054 0:1efeb3fc4ba6 57 * @param addr addr of the I2C peripheral
mja054 0:1efeb3fc4ba6 58 */
mja054 0:1efeb3fc4ba6 59 MMA8451Q(PinName sda, PinName scl, int addr);
mja054 0:1efeb3fc4ba6 60
mja054 0:1efeb3fc4ba6 61 /**
mja054 0:1efeb3fc4ba6 62 * MMA8451Q destructor
mja054 0:1efeb3fc4ba6 63 */
mja054 0:1efeb3fc4ba6 64 ~MMA8451Q();
mja054 0:1efeb3fc4ba6 65
mja054 0:1efeb3fc4ba6 66 /**
mja054 0:1efeb3fc4ba6 67 * Get the value of the WHO_AM_I register
mja054 0:1efeb3fc4ba6 68 *
mja054 0:1efeb3fc4ba6 69 * @returns WHO_AM_I value
mja054 0:1efeb3fc4ba6 70 */
mja054 0:1efeb3fc4ba6 71 uint8_t getWhoAmI();
mja054 0:1efeb3fc4ba6 72
mja054 0:1efeb3fc4ba6 73 /**
mja054 0:1efeb3fc4ba6 74 * Get X axis acceleration
mja054 0:1efeb3fc4ba6 75 *
mja054 0:1efeb3fc4ba6 76 * @returns X axis acceleration
mja054 0:1efeb3fc4ba6 77 */
mja054 0:1efeb3fc4ba6 78 float getAccX();
mja054 0:1efeb3fc4ba6 79
mja054 0:1efeb3fc4ba6 80 /**
mja054 0:1efeb3fc4ba6 81 * Get Y axis acceleration
mja054 0:1efeb3fc4ba6 82 *
mja054 0:1efeb3fc4ba6 83 * @returns Y axis acceleration
mja054 0:1efeb3fc4ba6 84 */
mja054 0:1efeb3fc4ba6 85 float getAccY();
mja054 0:1efeb3fc4ba6 86
mja054 0:1efeb3fc4ba6 87 /**
mja054 0:1efeb3fc4ba6 88 * Get Z axis acceleration
mja054 0:1efeb3fc4ba6 89 *
mja054 0:1efeb3fc4ba6 90 * @returns Z axis acceleration
mja054 0:1efeb3fc4ba6 91 */
mja054 0:1efeb3fc4ba6 92 float getAccZ();
mja054 0:1efeb3fc4ba6 93
mja054 0:1efeb3fc4ba6 94 /**
mja054 0:1efeb3fc4ba6 95 * Get XYZ axis acceleration
mja054 0:1efeb3fc4ba6 96 *
mja054 0:1efeb3fc4ba6 97 * @param res array where acceleration data will be stored
mja054 0:1efeb3fc4ba6 98 */
mja054 0:1efeb3fc4ba6 99 void getAccAllAxis(float * res);
mja054 0:1efeb3fc4ba6 100
mja054 0:1efeb3fc4ba6 101 /** JK
mja054 0:1efeb3fc4ba6 102 * Setup Double Tap detection
mja054 0:1efeb3fc4ba6 103
mja054 0:1efeb3fc4ba6 104
mja054 0:1efeb3fc4ba6 105 Example:
mja054 0:1efeb3fc4ba6 106
mja054 0:1efeb3fc4ba6 107 #include "mbed.h"
mja054 0:1efeb3fc4ba6 108 #include "MMA8451Q.h"
mja054 0:1efeb3fc4ba6 109
mja054 0:1efeb3fc4ba6 110 #define MMA8451_I2C_ADDRESS (0x1d<<1)
mja054 0:1efeb3fc4ba6 111 #define ON 0
mja054 0:1efeb3fc4ba6 112 #define OFF !ON
mja054 0:1efeb3fc4ba6 113
mja054 0:1efeb3fc4ba6 114 //Setup the interrupts for the MMA8451Q
mja054 0:1efeb3fc4ba6 115 InterruptIn accInt1(PTA14);
mja054 0:1efeb3fc4ba6 116 InterruptIn accInt2(PTA15);//not used in this prog but this is the other int from the accelorometer
mja054 0:1efeb3fc4ba6 117
mja054 0:1efeb3fc4ba6 118 uint8_t togstat=0;//Led status
mja054 0:1efeb3fc4ba6 119 DigitalOut bled(LED_BLUE);
mja054 0:1efeb3fc4ba6 120
mja054 0:1efeb3fc4ba6 121
mja054 0:1efeb3fc4ba6 122 void tapTrue(void){//ISR
mja054 0:1efeb3fc4ba6 123 if(togstat == 0){
mja054 0:1efeb3fc4ba6 124 togstat = 1;
mja054 0:1efeb3fc4ba6 125 bled=ON;
mja054 0:1efeb3fc4ba6 126 } else {
mja054 0:1efeb3fc4ba6 127 togstat = 0;
mja054 0:1efeb3fc4ba6 128 bled=OFF;
mja054 0:1efeb3fc4ba6 129 }
mja054 0:1efeb3fc4ba6 130
mja054 0:1efeb3fc4ba6 131 }
mja054 0:1efeb3fc4ba6 132
mja054 0:1efeb3fc4ba6 133
mja054 0:1efeb3fc4ba6 134 int main(void) {
mja054 0:1efeb3fc4ba6 135
mja054 0:1efeb3fc4ba6 136 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);//accelorometer instance
mja054 0:1efeb3fc4ba6 137
mja054 0:1efeb3fc4ba6 138 acc.setDoubleTap();//Setup the MMA8451Q to look for a double Tap
mja054 0:1efeb3fc4ba6 139 accInt1.rise(&tapTrue);//call tapTrue when an interrupt is generated on PTA14
mja054 0:1efeb3fc4ba6 140
mja054 0:1efeb3fc4ba6 141 while (true) {
mja054 0:1efeb3fc4ba6 142 //Interrupt driven so nothing in main loop
mja054 0:1efeb3fc4ba6 143 }
mja054 0:1efeb3fc4ba6 144 }
mja054 0:1efeb3fc4ba6 145
mja054 0:1efeb3fc4ba6 146
mja054 0:1efeb3fc4ba6 147 */
mja054 0:1efeb3fc4ba6 148 void setDoubleTap(void);
mja054 0:1efeb3fc4ba6 149
mja054 0:1efeb3fc4ba6 150 private:
mja054 0:1efeb3fc4ba6 151 I2C m_i2c;
mja054 0:1efeb3fc4ba6 152 int m_addr;
mja054 0:1efeb3fc4ba6 153 void readRegs(int addr, uint8_t * data, int len);
mja054 0:1efeb3fc4ba6 154 void writeRegs(uint8_t * data, int len);
mja054 0:1efeb3fc4ba6 155 int16_t getAccAxis(uint8_t addr);
mja054 0:1efeb3fc4ba6 156
mja054 0:1efeb3fc4ba6 157 };
mja054 0:1efeb3fc4ba6 158
mja054 0:1efeb3fc4ba6 159 #endif