Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of KX022 by
KXTJ3.cpp
00001 /* Copyright (c) 2015 ARM Ltd., MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 * 00018 * 00019 * KXTJ3 Accelerometer library 00020 * 00021 * @author Toyomasa Watarai 00022 * @version 1.0 00023 * @date 30-December-2015 00024 * 00025 * Library for "KXTJ3 Accelerometer library" from Kionix a Rohm group 00026 * http://www.kionix.com/product/KXTJ3-1020 00027 * 00028 */ 00029 00030 #include "mbed.h" 00031 #include "KXTJ3.h" 00032 00033 //KXTJ3::KXTJ3(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) 00034 //{ 00035 // initialize(); 00036 //} 00037 00038 KXTJ3::KXTJ3(I2C* i2c_obj, int addr) : m_i2c(i2c_obj), m_addr(addr) 00039 { 00040 initialize(); 00041 } 00042 00043 KXTJ3::~KXTJ3() 00044 { 00045 } 00046 00047 void KXTJ3::initialize() 00048 { 00049 // unsigned char buf; 00050 unsigned char reg[2]; 00051 /* 00052 DEBUG_PRINT("KXTJ3 init started\n\r"); 00053 readRegs(KXTJ3_WHO_AM_I, &buf, 1); 00054 if (buf != KXTJ3_WAI_VAL) { 00055 DEBUG_PRINT("KXTJ3 initialization error. (WAI %d, not %d)\n\r", buf, KXTJ3_WAI_VAL); 00056 DEBUG_PRINT("Trying to config anyway, in case there is some compatible sensor connected.\n\r"); 00057 } 00058 */ 00059 00060 readRegs(KXTJ3_CNTL1, reg, 1); 00061 if(reg[0]!=0xC0){ 00062 00063 reg[0] = KXTJ3_CNTL1; 00064 reg[1] = 0x40; 00065 writeRegs(reg, 2); 00066 00067 reg[0] = KXTJ3_ODCNTL; 00068 reg[1] = 0x01; 00069 writeRegs(reg, 2); 00070 00071 reg[0] = KXTJ3_CNTL1; 00072 reg[1] = 0xC0; 00073 writeRegs(reg, 2);} 00074 } 00075 00076 float KXTJ3::getAccX() 00077 { 00078 return (float(getAccAxis(KXTJ3_XOUT_L))/16384); 00079 } 00080 00081 float KXTJ3::getAccY() 00082 { 00083 return (float(getAccAxis(KXTJ3_YOUT_L))/16384); 00084 } 00085 00086 float KXTJ3::getAccZ() 00087 { 00088 return (float(getAccAxis(KXTJ3_ZOUT_L))/16384); 00089 } 00090 00091 void KXTJ3::getAccAllAxis(float * res) 00092 { 00093 res[0] = getAccX(); 00094 res[1] = getAccY(); 00095 res[2] = getAccZ(); 00096 } 00097 00098 int16_t KXTJ3::getAccAxis(uint8_t addr) 00099 { 00100 int16_t acc; 00101 uint8_t res[2]; 00102 00103 readRegs(addr, res, 2); 00104 acc = ((res[1] << 8) | res[0]); 00105 return acc; 00106 } 00107 00108 void KXTJ3::readRegs(int addr, uint8_t * data, int len) 00109 { 00110 int read_nok; 00111 char t[1] = {addr}; 00112 00113 m_i2c->write(m_addr, t, 1,true); 00114 read_nok = m_i2c->read(m_addr, (char *)data, len); 00115 if (read_nok){ 00116 DEBUG_PRINT("Read fail\n\r"); 00117 } 00118 } 00119 00120 void KXTJ3::writeRegs(uint8_t * data, int len) 00121 { 00122 m_i2c->write(m_addr, (char *)data, len); 00123 }
Generated on Thu Jul 14 2022 00:27:58 by
1.7.2
