![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
mbed RPC_Serial for LabVIEW interface: http://mbed.org/cookbook/Interfacing-with-LabVIEW http://mbed.org/cookbook/Interfacing-Using-RPC Steps: *Compile RPC_Serial main *Copy bit file to mbed flash and reset *Run examples in LabVIEW
Dependencies: C12832 MMA7660 RPCInterface mbed
Fork of RPC_Serial by
MMA7660.h@1:e245b0b4d96c, 2015-05-13 (annotated)
- Committer:
- JonathanCaes
- Date:
- Wed May 13 07:42:37 2015 +0000
- Revision:
- 1:e245b0b4d96c
RPC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JonathanCaes | 1:e245b0b4d96c | 1 | /* Copyright (c) <year> <copyright holders>, MIT License |
JonathanCaes | 1:e245b0b4d96c | 2 | * |
JonathanCaes | 1:e245b0b4d96c | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
JonathanCaes | 1:e245b0b4d96c | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
JonathanCaes | 1:e245b0b4d96c | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
JonathanCaes | 1:e245b0b4d96c | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
JonathanCaes | 1:e245b0b4d96c | 7 | * furnished to do so, subject to the following conditions: |
JonathanCaes | 1:e245b0b4d96c | 8 | * |
JonathanCaes | 1:e245b0b4d96c | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
JonathanCaes | 1:e245b0b4d96c | 10 | * substantial portions of the Software. |
JonathanCaes | 1:e245b0b4d96c | 11 | * |
JonathanCaes | 1:e245b0b4d96c | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
JonathanCaes | 1:e245b0b4d96c | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
JonathanCaes | 1:e245b0b4d96c | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
JonathanCaes | 1:e245b0b4d96c | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
JonathanCaes | 1:e245b0b4d96c | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
JonathanCaes | 1:e245b0b4d96c | 17 | */ |
JonathanCaes | 1:e245b0b4d96c | 18 | |
JonathanCaes | 1:e245b0b4d96c | 19 | #include "mbed.h" |
JonathanCaes | 1:e245b0b4d96c | 20 | |
JonathanCaes | 1:e245b0b4d96c | 21 | |
JonathanCaes | 1:e245b0b4d96c | 22 | #ifndef MMA7660_H |
JonathanCaes | 1:e245b0b4d96c | 23 | #define MMA7660_H |
JonathanCaes | 1:e245b0b4d96c | 24 | |
JonathanCaes | 1:e245b0b4d96c | 25 | #define MMA7660_ADDRESS 0x98 |
JonathanCaes | 1:e245b0b4d96c | 26 | #define MMA7660_SENSITIVITY 21.33 |
JonathanCaes | 1:e245b0b4d96c | 27 | |
JonathanCaes | 1:e245b0b4d96c | 28 | #define MMA7660_XOUT_R 0x00 |
JonathanCaes | 1:e245b0b4d96c | 29 | #define MMA7660_YOUT_R 0x01 |
JonathanCaes | 1:e245b0b4d96c | 30 | #define MMA7660_ZOUT_R 0x02 |
JonathanCaes | 1:e245b0b4d96c | 31 | #define MMA7660_TILT_R 0x03 |
JonathanCaes | 1:e245b0b4d96c | 32 | #define MMA7660_INT_R 0x06 |
JonathanCaes | 1:e245b0b4d96c | 33 | #define MMA7660_MODE_R 0x07 |
JonathanCaes | 1:e245b0b4d96c | 34 | #define MMA7660_SR_R 0x08 |
JonathanCaes | 1:e245b0b4d96c | 35 | |
JonathanCaes | 1:e245b0b4d96c | 36 | |
JonathanCaes | 1:e245b0b4d96c | 37 | /** An interface for the MMA7660 triple axis accelerometer |
JonathanCaes | 1:e245b0b4d96c | 38 | * |
JonathanCaes | 1:e245b0b4d96c | 39 | * @code |
JonathanCaes | 1:e245b0b4d96c | 40 | * //Uses the measured z-acceleration to drive leds 2 and 3 of the mbed |
JonathanCaes | 1:e245b0b4d96c | 41 | * |
JonathanCaes | 1:e245b0b4d96c | 42 | * #include "mbed.h" |
JonathanCaes | 1:e245b0b4d96c | 43 | * #include "MMA7660.h" |
JonathanCaes | 1:e245b0b4d96c | 44 | * |
JonathanCaes | 1:e245b0b4d96c | 45 | * MMA7660 MMA(p28, p27); |
JonathanCaes | 1:e245b0b4d96c | 46 | * |
JonathanCaes | 1:e245b0b4d96c | 47 | * DigitalOut connectionLed(LED1); |
JonathanCaes | 1:e245b0b4d96c | 48 | * PwmOut Zaxis_p(LED2); |
JonathanCaes | 1:e245b0b4d96c | 49 | * PwmOut Zaxis_n(LED3); |
JonathanCaes | 1:e245b0b4d96c | 50 | * |
JonathanCaes | 1:e245b0b4d96c | 51 | * int main() { |
JonathanCaes | 1:e245b0b4d96c | 52 | * if (MMA.testConnection()) |
JonathanCaes | 1:e245b0b4d96c | 53 | * connectionLed = 1; |
JonathanCaes | 1:e245b0b4d96c | 54 | * |
JonathanCaes | 1:e245b0b4d96c | 55 | * while(1) { |
JonathanCaes | 1:e245b0b4d96c | 56 | * Zaxis_p = MMA.z(); |
JonathanCaes | 1:e245b0b4d96c | 57 | * Zaxis_n = -MMA.z(); |
JonathanCaes | 1:e245b0b4d96c | 58 | * } |
JonathanCaes | 1:e245b0b4d96c | 59 | * |
JonathanCaes | 1:e245b0b4d96c | 60 | * } |
JonathanCaes | 1:e245b0b4d96c | 61 | * @endcode |
JonathanCaes | 1:e245b0b4d96c | 62 | */ |
JonathanCaes | 1:e245b0b4d96c | 63 | class MMA7660 |
JonathanCaes | 1:e245b0b4d96c | 64 | { |
JonathanCaes | 1:e245b0b4d96c | 65 | public: |
JonathanCaes | 1:e245b0b4d96c | 66 | /** |
JonathanCaes | 1:e245b0b4d96c | 67 | * The 6 different orientations and unknown |
JonathanCaes | 1:e245b0b4d96c | 68 | * |
JonathanCaes | 1:e245b0b4d96c | 69 | * Up & Down = X-axis |
JonathanCaes | 1:e245b0b4d96c | 70 | * Right & Left = Y-axis |
JonathanCaes | 1:e245b0b4d96c | 71 | * Back & Front = Z-axis |
JonathanCaes | 1:e245b0b4d96c | 72 | * |
JonathanCaes | 1:e245b0b4d96c | 73 | */ |
JonathanCaes | 1:e245b0b4d96c | 74 | enum Orientation {Up, Down, |
JonathanCaes | 1:e245b0b4d96c | 75 | Right, Left, |
JonathanCaes | 1:e245b0b4d96c | 76 | Back, Front, |
JonathanCaes | 1:e245b0b4d96c | 77 | Unknown |
JonathanCaes | 1:e245b0b4d96c | 78 | }; |
JonathanCaes | 1:e245b0b4d96c | 79 | |
JonathanCaes | 1:e245b0b4d96c | 80 | /** |
JonathanCaes | 1:e245b0b4d96c | 81 | * Creates a new MMA7660 object |
JonathanCaes | 1:e245b0b4d96c | 82 | * |
JonathanCaes | 1:e245b0b4d96c | 83 | * @param sda - I2C data pin |
JonathanCaes | 1:e245b0b4d96c | 84 | * @param scl - I2C clock pin |
JonathanCaes | 1:e245b0b4d96c | 85 | * @param active - true (default) to enable the device, false to keep it standby |
JonathanCaes | 1:e245b0b4d96c | 86 | */ |
JonathanCaes | 1:e245b0b4d96c | 87 | MMA7660(PinName sda, PinName scl, bool active = true); |
JonathanCaes | 1:e245b0b4d96c | 88 | |
JonathanCaes | 1:e245b0b4d96c | 89 | /** |
JonathanCaes | 1:e245b0b4d96c | 90 | * Tests if communication is possible with the MMA7660 |
JonathanCaes | 1:e245b0b4d96c | 91 | * |
JonathanCaes | 1:e245b0b4d96c | 92 | * Because the MMA7660 lacks a WHO_AM_I register, this function can only check |
JonathanCaes | 1:e245b0b4d96c | 93 | * if there is an I2C device that responds to the MMA7660 address |
JonathanCaes | 1:e245b0b4d96c | 94 | * |
JonathanCaes | 1:e245b0b4d96c | 95 | * @param return - true for successfull connection, false for no connection |
JonathanCaes | 1:e245b0b4d96c | 96 | */ |
JonathanCaes | 1:e245b0b4d96c | 97 | bool testConnection( void ); |
JonathanCaes | 1:e245b0b4d96c | 98 | |
JonathanCaes | 1:e245b0b4d96c | 99 | /** |
JonathanCaes | 1:e245b0b4d96c | 100 | * Sets the active state of the MMA7660 |
JonathanCaes | 1:e245b0b4d96c | 101 | * |
JonathanCaes | 1:e245b0b4d96c | 102 | * Note: This is unrelated to awake/sleep mode |
JonathanCaes | 1:e245b0b4d96c | 103 | * |
JonathanCaes | 1:e245b0b4d96c | 104 | * @param state - true for active, false for standby |
JonathanCaes | 1:e245b0b4d96c | 105 | */ |
JonathanCaes | 1:e245b0b4d96c | 106 | void setActive( bool state); |
JonathanCaes | 1:e245b0b4d96c | 107 | |
JonathanCaes | 1:e245b0b4d96c | 108 | /** |
JonathanCaes | 1:e245b0b4d96c | 109 | * Reads acceleration data from the sensor |
JonathanCaes | 1:e245b0b4d96c | 110 | * |
JonathanCaes | 1:e245b0b4d96c | 111 | * When the parameter is a pointer to an integer array it will be the raw data. |
JonathanCaes | 1:e245b0b4d96c | 112 | * When it is a pointer to a float array it will be the acceleration in g's |
JonathanCaes | 1:e245b0b4d96c | 113 | * |
JonathanCaes | 1:e245b0b4d96c | 114 | * @param data - pointer to array with length 3 where the acceleration data will be stored, X-Y-Z |
JonathanCaes | 1:e245b0b4d96c | 115 | */ |
JonathanCaes | 1:e245b0b4d96c | 116 | void readData( int *data); |
JonathanCaes | 1:e245b0b4d96c | 117 | void readData( float *data); |
JonathanCaes | 1:e245b0b4d96c | 118 | |
JonathanCaes | 1:e245b0b4d96c | 119 | /** |
JonathanCaes | 1:e245b0b4d96c | 120 | * Get X-data |
JonathanCaes | 1:e245b0b4d96c | 121 | * |
JonathanCaes | 1:e245b0b4d96c | 122 | * @param return - X-acceleration in g's |
JonathanCaes | 1:e245b0b4d96c | 123 | */ |
JonathanCaes | 1:e245b0b4d96c | 124 | float x( void ); |
JonathanCaes | 1:e245b0b4d96c | 125 | |
JonathanCaes | 1:e245b0b4d96c | 126 | /** |
JonathanCaes | 1:e245b0b4d96c | 127 | * Get Y-data |
JonathanCaes | 1:e245b0b4d96c | 128 | * |
JonathanCaes | 1:e245b0b4d96c | 129 | * @param return - Y-acceleration in g's |
JonathanCaes | 1:e245b0b4d96c | 130 | */ |
JonathanCaes | 1:e245b0b4d96c | 131 | float y( void ); |
JonathanCaes | 1:e245b0b4d96c | 132 | |
JonathanCaes | 1:e245b0b4d96c | 133 | /** |
JonathanCaes | 1:e245b0b4d96c | 134 | * Get Z-data |
JonathanCaes | 1:e245b0b4d96c | 135 | * |
JonathanCaes | 1:e245b0b4d96c | 136 | * @param return - Z-acceleration in g's |
JonathanCaes | 1:e245b0b4d96c | 137 | */ |
JonathanCaes | 1:e245b0b4d96c | 138 | float z( void ); |
JonathanCaes | 1:e245b0b4d96c | 139 | |
JonathanCaes | 1:e245b0b4d96c | 140 | /** |
JonathanCaes | 1:e245b0b4d96c | 141 | * Sets the active samplerate |
JonathanCaes | 1:e245b0b4d96c | 142 | * |
JonathanCaes | 1:e245b0b4d96c | 143 | * The entered samplerate will be rounded to nearest supported samplerate. |
JonathanCaes | 1:e245b0b4d96c | 144 | * Supported samplerates are: 120 - 64 - 32 - 16 - 8 - 4 - 2 - 1 samples/second. |
JonathanCaes | 1:e245b0b4d96c | 145 | * |
JonathanCaes | 1:e245b0b4d96c | 146 | * @param samplerate - the samplerate that will be set |
JonathanCaes | 1:e245b0b4d96c | 147 | */ |
JonathanCaes | 1:e245b0b4d96c | 148 | void setSampleRate(int samplerate); |
JonathanCaes | 1:e245b0b4d96c | 149 | |
JonathanCaes | 1:e245b0b4d96c | 150 | /** |
JonathanCaes | 1:e245b0b4d96c | 151 | * Returns if it is on its front, back, or unknown side |
JonathanCaes | 1:e245b0b4d96c | 152 | * |
JonathanCaes | 1:e245b0b4d96c | 153 | * This is read from MMA7760s registers, page 12 of datasheet |
JonathanCaes | 1:e245b0b4d96c | 154 | * |
JonathanCaes | 1:e245b0b4d96c | 155 | * @param return - Front, Back or Unknown orientation |
JonathanCaes | 1:e245b0b4d96c | 156 | */ |
JonathanCaes | 1:e245b0b4d96c | 157 | Orientation getSide( void ); |
JonathanCaes | 1:e245b0b4d96c | 158 | |
JonathanCaes | 1:e245b0b4d96c | 159 | /** |
JonathanCaes | 1:e245b0b4d96c | 160 | * Returns if it is on it left, right, down or up side |
JonathanCaes | 1:e245b0b4d96c | 161 | * |
JonathanCaes | 1:e245b0b4d96c | 162 | * This is read from MMA7760s registers, page 12 of datasheet |
JonathanCaes | 1:e245b0b4d96c | 163 | * |
JonathanCaes | 1:e245b0b4d96c | 164 | * @param return - Left, Right, Down, Up or Unknown orientation |
JonathanCaes | 1:e245b0b4d96c | 165 | */ |
JonathanCaes | 1:e245b0b4d96c | 166 | Orientation getOrientation ( void ); |
JonathanCaes | 1:e245b0b4d96c | 167 | |
JonathanCaes | 1:e245b0b4d96c | 168 | |
JonathanCaes | 1:e245b0b4d96c | 169 | private: |
JonathanCaes | 1:e245b0b4d96c | 170 | |
JonathanCaes | 1:e245b0b4d96c | 171 | /** |
JonathanCaes | 1:e245b0b4d96c | 172 | * Writes data to the device |
JonathanCaes | 1:e245b0b4d96c | 173 | * |
JonathanCaes | 1:e245b0b4d96c | 174 | * @param adress - register address to write to |
JonathanCaes | 1:e245b0b4d96c | 175 | * @param data - data to write |
JonathanCaes | 1:e245b0b4d96c | 176 | */ |
JonathanCaes | 1:e245b0b4d96c | 177 | void write( char address, char data); |
JonathanCaes | 1:e245b0b4d96c | 178 | |
JonathanCaes | 1:e245b0b4d96c | 179 | /** |
JonathanCaes | 1:e245b0b4d96c | 180 | * Read data from the device |
JonathanCaes | 1:e245b0b4d96c | 181 | * |
JonathanCaes | 1:e245b0b4d96c | 182 | * @param adress - register address to write to |
JonathanCaes | 1:e245b0b4d96c | 183 | * @return - data from the register specified by RA |
JonathanCaes | 1:e245b0b4d96c | 184 | */ |
JonathanCaes | 1:e245b0b4d96c | 185 | char read( char adress); |
JonathanCaes | 1:e245b0b4d96c | 186 | |
JonathanCaes | 1:e245b0b4d96c | 187 | /** |
JonathanCaes | 1:e245b0b4d96c | 188 | * Read multiple regigsters from the device, more efficient than using multiple normal reads. |
JonathanCaes | 1:e245b0b4d96c | 189 | * |
JonathanCaes | 1:e245b0b4d96c | 190 | * @param adress - register address to write to |
JonathanCaes | 1:e245b0b4d96c | 191 | * @param length - number of bytes to read |
JonathanCaes | 1:e245b0b4d96c | 192 | * @param data - pointer where the data needs to be written to |
JonathanCaes | 1:e245b0b4d96c | 193 | */ |
JonathanCaes | 1:e245b0b4d96c | 194 | void read( char adress, char *data, int length); |
JonathanCaes | 1:e245b0b4d96c | 195 | |
JonathanCaes | 1:e245b0b4d96c | 196 | /** |
JonathanCaes | 1:e245b0b4d96c | 197 | * Reads single axis |
JonathanCaes | 1:e245b0b4d96c | 198 | */ |
JonathanCaes | 1:e245b0b4d96c | 199 | float getSingle(int number); |
JonathanCaes | 1:e245b0b4d96c | 200 | |
JonathanCaes | 1:e245b0b4d96c | 201 | I2C _i2c; |
JonathanCaes | 1:e245b0b4d96c | 202 | bool active; |
JonathanCaes | 1:e245b0b4d96c | 203 | float samplerate; |
JonathanCaes | 1:e245b0b4d96c | 204 | }; |
JonathanCaes | 1:e245b0b4d96c | 205 | |
JonathanCaes | 1:e245b0b4d96c | 206 | |
JonathanCaes | 1:e245b0b4d96c | 207 | #endif |