Interface for an LIS302 accelerometer, using the SPI interface
Dependents: LIS302_HelloWorld mbed_line_camera
LIS302.cpp
00001 /* mbed LIS302 Accelerometer Library 00002 * Copyright (c) 2008-2010, sford, cstyles, wreynolds 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 * THE SOFTWARE. 00021 */ 00022 00023 #include "LIS302.h" 00024 #include "mbed.h" 00025 00026 #define LIS302_WHOAMI 0x0F 00027 #define LIS302_CTRL_REG1 0x20 00028 #define LIS302_CTRL_REG2 0x21 00029 #define LIS302_CTRL_REG3 0x22 00030 #define LIS302_HP_FILTER_RST 0x23 00031 #define LIS302_STATUS_REG 0x27 00032 #define LIS302_OUTX 0x29 00033 #define LIS302_OUTY 0x2B 00034 #define LIS302_OUTZ 0x2D 00035 #define LIS302_FF_WU_CFG1 0x30 00036 #define LIS302_FF_WU_SRC_1 0x31 00037 #define LIS302_FF_WU_THS_1 0x32 00038 #define LIS302_FF_WU_DURATION_1 0x33 00039 #define LIS302_FF_WU_CFG_2 0x34 00040 #define LIS302_FF_WU_SRC_2 0x35 00041 #define LIS302_FF_WU_THS_2 0x36 00042 #define LIS302_FF_WU_DURATION_2 0x37 00043 #define LIS302_CLICK_CFG 0x38 00044 #define LIS302_CLICK_SRC 0x39 00045 #define LIS302_CLICK_THSY_X 0x3B 00046 #define LIS302_CLICK_THSZ 0x3C 00047 #define LIS302_READ 0x80 00048 #define LIS302_WRITE 0x00 00049 00050 #define LIS302_STATUS_X_AVAILABLE 0x1 00051 #define LIS302_STATUS_Y_AVAILABLE 0x2 00052 #define LIS302_STATUS_Z_AVAILABLE 0x4 00053 00054 #define FACTOR_2g 55.6 00055 #define FACTOR_8g 13.9 00056 00057 LIS302::LIS302(PinName mosi, PinName miso, PinName clk, PinName ncs) 00058 : _spi(mosi, miso, clk), _ncs(ncs) { 00059 00060 // Make sure CS is high 00061 _ncs = 1; 00062 00063 // Set up the spi interface 00064 _spi.format(8, 3); 00065 _spi.frequency(1000000); 00066 00067 // Write to CTRL_REG1 00068 _ncs = 0; 00069 _spi.write(LIS302_WRITE | LIS302_CTRL_REG1); 00070 _spi.write (0x47); 00071 _ncs = 1; 00072 00073 // Write to CTRL_REG2 00074 _ncs = 0; 00075 _spi.write(LIS302_WRITE | LIS302_CTRL_REG2); 00076 _spi.write (0x0); // This is default anyway 00077 _ncs = 1; 00078 00079 // Write to CTRL_REG3 00080 _ncs = 0; 00081 _spi.write(LIS302_WRITE | LIS302_CTRL_REG3); 00082 _spi.write (0x0); // This is default anyway 00083 _ncs = 1; 00084 00085 range(0); 00086 calibrate(); 00087 } 00088 00089 float LIS302::x() { 00090 // wait for a new sample 00091 while(!(status() & LIS302_STATUS_X_AVAILABLE)); 00092 00093 _ncs = 0; 00094 _spi.write(LIS302_READ | LIS302_OUTX); 00095 signed char raw = _spi.write(0x00); 00096 _ncs = 1; 00097 00098 float gradient = (2.0/(_maxx-_minx)); 00099 return (gradient*(float)(raw)/_factor)+((-gradient*_maxx)+1); 00100 } 00101 00102 float LIS302::y() { 00103 // wait for a new sample 00104 while(!(status() & LIS302_STATUS_Y_AVAILABLE)); 00105 00106 _ncs = 0; 00107 _spi.write(LIS302_READ | LIS302_OUTY); 00108 signed char raw = _spi.write(0x00); 00109 _ncs = 1; 00110 00111 float gradient = (2.0/(_maxy-_miny)); 00112 return (gradient*(float)(raw)/_factor)+((-gradient*_maxy)+1); 00113 } 00114 00115 float LIS302::z() { 00116 // wait for a new sample 00117 while(!(status() & LIS302_STATUS_Z_AVAILABLE)); 00118 00119 _ncs = 0; 00120 _spi.write(LIS302_READ | LIS302_OUTZ); 00121 signed char raw = _spi.write(0x00); 00122 _ncs = 1; 00123 00124 float gradient = (2.0/(_maxz-_minz)); 00125 return (gradient*(float)(raw)/_factor)+((-gradient*_maxz)+1); 00126 } 00127 00128 void LIS302::range(int g) { 00129 00130 // fetch the current CRTL_REG1 00131 _ncs = 0; 00132 _spi.write(LIS302_READ | LIS302_CTRL_REG1); 00133 int value = _spi.write(0x00); 00134 _ncs = 1; 00135 00136 // set the range bit, and the calculation factor 00137 if(g) { 00138 value |= 0x20; // 8g 00139 _factor = FACTOR_8g; 00140 } else { 00141 value &= ~0x20; // 2g 00142 _factor = FACTOR_2g; 00143 } 00144 00145 _ncs = 0; 00146 _spi.write(LIS302_WRITE | LIS302_CTRL_REG1); 00147 _spi.write(value); 00148 _ncs = 1; 00149 } 00150 00151 void LIS302::calibrate(float maxx, float minx, float maxy, float miny, float maxz, float minz) { 00152 _maxx = maxx; 00153 _minx = minx; 00154 _maxy = maxy; 00155 _miny = miny; 00156 _maxz = maxz; 00157 _minz = minz; 00158 } 00159 00160 int LIS302::whoami() { 00161 _ncs = 0; 00162 _spi.write(LIS302_READ | LIS302_WHOAMI); 00163 int value = _spi.write(0x00); 00164 _ncs = 1; 00165 return value; 00166 } 00167 00168 int LIS302::status() { 00169 _ncs = 0; 00170 _spi.write(0xA7); 00171 int value = _spi.write(LIS302_READ | LIS302_STATUS_REG); 00172 _ncs = 1; 00173 return value; 00174 }
Generated on Tue Jul 12 2022 11:48:13 by 1.7.2