Interface for an LIS302 accelerometer, using the SPI interface

Dependents:   LIS302_HelloWorld mbed_line_camera

Committer:
simon
Date:
Tue Jun 08 13:34:48 2010 +0000
Revision:
1:fe7ccd3480e6

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:fe7ccd3480e6 1 /* mbed LIS302 Accelerometer Library
simon 1:fe7ccd3480e6 2 * Copyright (c) 2008-2010, sford, cstyles, wreynolds
simon 1:fe7ccd3480e6 3 *
simon 1:fe7ccd3480e6 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 1:fe7ccd3480e6 5 * of this software and associated documentation files (the "Software"), to deal
simon 1:fe7ccd3480e6 6 * in the Software without restriction, including without limitation the rights
simon 1:fe7ccd3480e6 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 1:fe7ccd3480e6 8 * copies of the Software, and to permit persons to whom the Software is
simon 1:fe7ccd3480e6 9 * furnished to do so, subject to the following conditions:
simon 1:fe7ccd3480e6 10 *
simon 1:fe7ccd3480e6 11 * The above copyright notice and this permission notice shall be included in
simon 1:fe7ccd3480e6 12 * all copies or substantial portions of the Software.
simon 1:fe7ccd3480e6 13 *
simon 1:fe7ccd3480e6 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 1:fe7ccd3480e6 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 1:fe7ccd3480e6 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 1:fe7ccd3480e6 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 1:fe7ccd3480e6 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 1:fe7ccd3480e6 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 1:fe7ccd3480e6 20 * THE SOFTWARE.
simon 1:fe7ccd3480e6 21 */
simon 1:fe7ccd3480e6 22
simon 1:fe7ccd3480e6 23 #ifndef MBED_LIS302_H
simon 1:fe7ccd3480e6 24 #define MBED_LIS302_H
simon 1:fe7ccd3480e6 25
simon 1:fe7ccd3480e6 26 #include "mbed.h"
simon 1:fe7ccd3480e6 27
simon 1:fe7ccd3480e6 28 /** An interface for the LIS302 triple axis SPI accelerometer
simon 1:fe7ccd3480e6 29 *
simon 1:fe7ccd3480e6 30 * @code
simon 1:fe7ccd3480e6 31 * // Print out the Z axis acceleration
simon 1:fe7ccd3480e6 32 * #include "mbed.h"
simon 1:fe7ccd3480e6 33 * #include "LIS302.h"
simon 1:fe7ccd3480e6 34 *
simon 1:fe7ccd3480e6 35 * LIS302 acc(p5, p6, p7, p8); // mosi, miso, clk, ncs
simon 1:fe7ccd3480e6 36 *
simon 1:fe7ccd3480e6 37 * int main() {
simon 1:fe7ccd3480e6 38 * while(1) {
simon 1:fe7ccd3480e6 39 * printf("Z axis acceleration = %.2f\n", acc.z());
simon 1:fe7ccd3480e6 40 * wait(0.1);
simon 1:fe7ccd3480e6 41 * }
simon 1:fe7ccd3480e6 42 * }
simon 1:fe7ccd3480e6 43 * @endcode
simon 1:fe7ccd3480e6 44 */
simon 1:fe7ccd3480e6 45 class LIS302 {
simon 1:fe7ccd3480e6 46 public:
simon 1:fe7ccd3480e6 47
simon 1:fe7ccd3480e6 48 /** Create an LIS302 interface, connected to the specified pins
simon 1:fe7ccd3480e6 49 *
simon 1:fe7ccd3480e6 50 * @param mosi SPI data out
simon 1:fe7ccd3480e6 51 * @param miso SPI data in
simon 1:fe7ccd3480e6 52 * @param clk SPI clock
simon 1:fe7ccd3480e6 53 * @param ncs Active low chip select (DigitalOut)
simon 1:fe7ccd3480e6 54 */
simon 1:fe7ccd3480e6 55 LIS302(PinName mosi, PinName miso, PinName clk, PinName ncs);
simon 1:fe7ccd3480e6 56
simon 1:fe7ccd3480e6 57 /** Read the X axis acceleration
simon 1:fe7ccd3480e6 58 *
simon 1:fe7ccd3480e6 59 * @return A floating-point value representing acceleration in g
simon 1:fe7ccd3480e6 60 */
simon 1:fe7ccd3480e6 61 float x();
simon 1:fe7ccd3480e6 62
simon 1:fe7ccd3480e6 63 /** Read the Y axis acceleration
simon 1:fe7ccd3480e6 64 *
simon 1:fe7ccd3480e6 65 * @return A floating-point value representing acceleration in g
simon 1:fe7ccd3480e6 66 */
simon 1:fe7ccd3480e6 67 float y();
simon 1:fe7ccd3480e6 68
simon 1:fe7ccd3480e6 69 /** Read the Z axis acceleration
simon 1:fe7ccd3480e6 70 *
simon 1:fe7ccd3480e6 71 * @return - A floating-point value representing acceleration in g
simon 1:fe7ccd3480e6 72 */
simon 1:fe7ccd3480e6 73 float z();
simon 1:fe7ccd3480e6 74
simon 1:fe7ccd3480e6 75 /** Select the range of the accelerometer
simon 1:fe7ccd3480e6 76 *
simon 1:fe7ccd3480e6 77 * @param range 0 = 2g, 1 = 8g
simon 1:fe7ccd3480e6 78 */
simon 1:fe7ccd3480e6 79 void range(int g);
simon 1:fe7ccd3480e6 80
simon 1:fe7ccd3480e6 81 /** Configure the minima and maxima for the axes to linearise the readings
simon 1:fe7ccd3480e6 82 *
simon 1:fe7ccd3480e6 83 * @param maxx float defining the maximum X value
simon 1:fe7ccd3480e6 84 * @param minx float defining the minimum X value
simon 1:fe7ccd3480e6 85 * @param maxy float defining the maximum Y value
simon 1:fe7ccd3480e6 86 * @param miny float defining the minimum Y value
simon 1:fe7ccd3480e6 87 * @param maxz float defining the maximum Z value
simon 1:fe7ccd3480e6 88 * @param minz float defining the minimum Z value
simon 1:fe7ccd3480e6 89 */
simon 1:fe7ccd3480e6 90 void calibrate(float maxx = 1, float minx = -1, float maxy = 1, float miny = -1, float maxz = 1, float minz = -1);
simon 1:fe7ccd3480e6 91
simon 1:fe7ccd3480e6 92 private:
simon 1:fe7ccd3480e6 93 SPI _spi;
simon 1:fe7ccd3480e6 94 DigitalOut _ncs;
simon 1:fe7ccd3480e6 95
simon 1:fe7ccd3480e6 96 int whoami();
simon 1:fe7ccd3480e6 97 int status();
simon 1:fe7ccd3480e6 98
simon 1:fe7ccd3480e6 99 float _factor;
simon 1:fe7ccd3480e6 100 float _maxx, _minx;
simon 1:fe7ccd3480e6 101 float _maxy, _miny;
simon 1:fe7ccd3480e6 102 float _maxz, _minz;
simon 1:fe7ccd3480e6 103 };
simon 1:fe7ccd3480e6 104
simon 1:fe7ccd3480e6 105 #endif