SCA3000 triple axis digital interface accelerometer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SCA3000.cpp Source File

SCA3000.cpp

00001 /**
00002  * @author Aaron Berk
00003  *
00004  * @section LICENSE
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"), to deal
00008  * in the Software without restriction, including without limitation the rights
00009  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  * copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  * THE SOFTWARE.
00023  *
00024  * @section DESCRIPTION
00025  *
00026  * SCA3000, triple axis, digital interface, accelerometer.
00027  *
00028  * Datasheet:
00029  *
00030  * http://www.sparkfun.com/datasheets/Sensors/Accelerometer/SCA3000-D01.pdf
00031  */
00032 
00033 /**
00034  * Includes
00035  */
00036 #include "SCA3000.h"
00037 
00038 SCA3000::SCA3000(PinName mosi,
00039                  PinName miso,
00040                  PinName sck,
00041                  PinName cs,
00042                  PinName nr) : spi_(mosi, miso, sck), nCS_(cs), nR_(nr) {
00043 
00044     //1MHz rate.
00045     spi_.frequency(1000000);
00046     //8-bit frame, POL = 0, PHA = 0.
00047     spi_.format(8,0);
00048     //Chip select is active low [so set it high initially].
00049     nCS_ = 1;
00050     //Pulse reset.
00051     nR_ = 0;
00052     wait(0.1);
00053     nR_ = 1;
00054 
00055     int status = 0;
00056     //Check whether the device successfully reset by reading
00057     //the CSME bit of the STATUS register.
00058     //0 => no error
00059     //1 => error
00060     status = oneByteRead(SCA3000_STATUS_REG);
00061     //CSME bit = 1... uh oh...
00062     if (status & 0x2) {
00063     
00064     }
00065 
00066 }
00067 
00068 int SCA3000::getRevId(void) {
00069 
00070     return oneByteRead(SCA3000_REVID_REG);
00071 
00072 }
00073 
00074 float SCA3000::getAcceleration(int axis) {
00075 
00076     return getCounts(axis) * 0.75;
00077 
00078 }
00079 
00080 int SCA3000::getCounts(int axis){
00081 
00082     int acceleration = 0;
00083     int axis_lsb     = 0;
00084     int axis_msb     = 0;
00085 
00086     switch (axis) {
00087 
00088         case SCA3000_X_AXIS:
00089 
00090             axis_lsb = oneByteRead(SCA3000_X_LSB);
00091             axis_msb = oneByteRead(SCA3000_X_MSB);
00092             
00093             break;
00094 
00095         case SCA3000_Y_AXIS:
00096 
00097             axis_lsb = oneByteRead(SCA3000_Y_LSB);
00098             axis_msb = oneByteRead(SCA3000_Y_MSB);
00099             
00100             break;
00101 
00102         case SCA3000_Z_AXIS:
00103 
00104             axis_lsb = oneByteRead(SCA3000_Z_LSB);
00105             axis_msb = oneByteRead(SCA3000_Z_MSB);
00106             
00107             break;
00108 
00109             //An invalid axis value has been passed.
00110         default:
00111         
00112             break;
00113 
00114     }
00115 
00116     int counts = ( axis_msb << 8 | axis_lsb );
00117     
00118     //If this looks like nonsense it's because
00119     //the datasheet is completely wrong.
00120     if(counts & 0x8000){
00121         counts = (counts >> 3) & 0x1FFF;
00122         counts = (~counts & 0x1FFF) + 1;
00123         acceleration = counts * -1;
00124     }
00125     else{
00126         counts = (counts >> 3) & 0xFFF;
00127         acceleration = counts;
00128     }
00129     
00130     return acceleration;
00131     
00132 }
00133 
00134 int SCA3000::oneByteRead(int address) {
00135 
00136     //Register address sits in the first 6 bits of the transmission.
00137     int tx = ((address << 2) | SCA3000_SPI_READ);
00138     int rx = 0;
00139 
00140     nCS_ = 0;
00141     //Send address to read from.
00142     spi_.write(tx);
00143     //Read back contents of address.
00144     rx = spi_.write(0x00);
00145     nCS_ = 1;
00146 
00147     return rx;
00148 
00149 }
00150 
00151 void SCA3000::oneByteWrite(int address, char data) {
00152 
00153     //Register address sits in the first 6 bits of the transmission.
00154     int tx = ((address << 2) | SCA3000_SPI_WRITE);
00155 
00156     nCS_ = 0;
00157     //Send address to write to.
00158     spi_.write(tx);
00159     //Send data to be written.
00160     spi_.write(data);
00161     nCS_ = 1;
00162 
00163 }