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.
H3LIS331.cpp
00001 /** 00002 * Includes 00003 */ 00004 00005 #include "H3LIS331.h" 00006 00007 H3LIS331::H3LIS331(PinName sda, PinName scl) : i2c_(sda, scl) { 00008 // set default scaling factor 00009 scaling_factor = 5140.0; 00010 00011 //set default range to zero. 00012 //current_range = 0; 00013 00014 //400kHz, fast mode. 00015 i2c_.frequency(400000); 00016 00017 00018 //Power Up Device, Set Output data rate, Enable All 3 Axis 00019 //See datasheet for details. 00020 char tx[2]; 00021 //char tx2[2]; 00022 //char rx[1]; 00023 tx[0] = CTRL_REG_1; 00024 //CTRL_REG_1 [00111111] / [0x3F] to power up, set output rate to 1000Hz, and enable all 3 axis. 00025 //CTRL_REG_1 [00101111] / [0x2F] to power up, set output rate to 100Hz, and enable all 3 axis. 00026 //CTRL_REG_1 [ABCDEFGH] ABC=PowerMode, DE=OutputDataRate, F=Zenable, G=Yenable, H=XEnable 00027 tx[1] = 0x2F; 00028 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00029 00030 00031 00032 //set default scale of 4g's 00033 //scaling_factor = 8192.0; 00034 //current_range = 24; 00035 00036 tx[0] = CTRL_REG_4; 00037 tx[1] = 0x00; 00038 00039 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00040 00041 00042 00043 } 00044 00045 char H3LIS331::getWhoAmI(void){ 00046 00047 //WhoAmI Register address. 00048 char tx = WHO_AM_I_REG_H3LIS331; 00049 char rx; 00050 00051 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00052 00053 i2c_.read((H3LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1); 00054 00055 return rx; 00056 00057 } 00058 00059 00060 00061 00062 void H3LIS331::setPowerMode(char power_mode){ 00063 // Currently sets all 3 axis to enabled. Will be set to preserve existing status in future 00064 char tx[2]; 00065 tx[0] = CTRL_REG_1; 00066 tx[1] = power_mode; 00067 00068 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00069 00070 } 00071 00072 char H3LIS331::getPowerMode(void){ 00073 00074 char tx = CTRL_REG_1; 00075 char rx; 00076 00077 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00078 00079 i2c_.read((H3LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1); 00080 00081 00082 return rx; 00083 00084 } 00085 00086 00087 00088 char H3LIS331::getInterruptConfiguration(void){ 00089 00090 char tx = CTRL_REG_3; 00091 char rx; 00092 00093 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00094 00095 i2c_.read((H3LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1); 00096 00097 return rx; 00098 00099 } 00100 00101 00102 void H3LIS331::setFullScaleRange400g(void){ // Does not preserve rest of CTRL_REG_4! 00103 scaling_factor = 81.92; 00104 current_range = 400; 00105 00106 char tx[2]; 00107 tx[0] = CTRL_REG_4; 00108 tx[1] = 0x30; 00109 00110 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00111 00112 } 00113 00114 void H3LIS331::setFullScaleRange200g(void){ // Does not preserve rest of CTRL_REG_4! 00115 scaling_factor = 163.84; 00116 current_range = 200; 00117 00118 char tx[2]; 00119 tx[0] = CTRL_REG_4; 00120 tx[1] = 0x10; 00121 00122 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00123 00124 } 00125 00126 00127 void H3LIS331::setFullScaleRange100g(void){ // Does not preserve rest of CTRL_REG_4! 00128 scaling_factor = 327.68; 00129 current_range = 100; 00130 00131 char tx[2]; 00132 tx[0] = CTRL_REG_4; 00133 tx[1] = 0x00; 00134 00135 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2); 00136 00137 } 00138 00139 00140 char H3LIS331::getAccelStatus(void){ 00141 00142 char tx = STATUS_REG; 00143 char rx; 00144 00145 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00146 00147 i2c_.read((H3LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1); 00148 00149 return rx; 00150 } 00151 00152 00153 00154 float H3LIS331::getAccelX(void){ 00155 00156 char tx = ACCEL_XOUT_H_REG; 00157 char rx[2]; 00158 00159 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00160 00161 i2c_.read((H3LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2); 00162 00163 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]); 00164 00165 return output/scaling_factor; 00166 //return output; 00167 00168 } 00169 00170 float H3LIS331::getAccelY(void){ 00171 00172 char tx = ACCEL_YOUT_H_REG; 00173 char rx[2]; 00174 00175 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00176 00177 i2c_.read((H3LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2); 00178 00179 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]); 00180 00181 return output/scaling_factor; 00182 00183 } 00184 00185 float H3LIS331::getAccelZ(void){ 00186 00187 char tx = ACCEL_ZOUT_H_REG; 00188 char rx[2]; 00189 00190 i2c_.write((H3LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1); 00191 00192 i2c_.read((H3LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2); 00193 00194 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]); 00195 00196 return output/scaling_factor; 00197 }
Generated on Fri Jul 15 2022 09:28:08 by
1.7.2