Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
wollewald authored Oct 30, 2021
1 parent 6cb6d66 commit 8f4b712
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 40 deletions.
144 changes: 105 additions & 39 deletions src/ADXL345_WE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,53 @@
/************ Constructors ************/

ADXL345_WE::ADXL345_WE(int addr){
useSPI = false;
_wire = &Wire;
i2cAddress = addr;
}

ADXL345_WE::ADXL345_WE(){
useSPI = false;
_wire = &Wire;
i2cAddress = 0x53;
}

ADXL345_WE::ADXL345_WE(TwoWire *w, int addr){
useSPI = false;
_wire = w;
i2cAddress = addr;
}

ADXL345_WE::ADXL345_WE(TwoWire *w){
useSPI = false;
_wire = w;
i2cAddress = 0x53;
}

ADXL345_WE::ADXL345_WE(SPIClass *s, int cs, bool spi){
useSPI = spi;
_spi = s;
csPin = cs;
}

ADXL345_WE::ADXL345_WE(int cs, bool spi){
useSPI = spi;
_spi = &SPI;
csPin = cs;
}

/************ Basic settings ************/

bool ADXL345_WE::init(){
writeRegister(ADXL345_POWER_CTL,0);
if(!setMeasureMode(true)){
return false;
if(useSPI){
_spi->begin();
_spi->setDataMode(SPI_MODE3);
pinMode(csPin, OUTPUT);
digitalWrite(csPin, HIGH);
}
writeRegister(ADXL345_POWER_CTL,0);
writeRegister(ADXL345_POWER_CTL, 16);
setMeasureMode(true);
corrFact.x = 1.0;
corrFact.y = 1.0;
corrFact.z = 1.0;
Expand All @@ -53,6 +74,9 @@ bool ADXL345_WE::init(){
offsetVal.z = 0.0;
writeRegister(ADXL345_DATA_FORMAT,0);
setFullRes(true);
if(!((readRegister8(ADXL345_DATA_FORMAT)) & (1<<ADXL345_FULL_RES))){
return false;
}
writeRegister(ADXL345_INT_ENABLE, 0);
writeRegister(ADXL345_INT_MAP,0);
writeRegister(ADXL345_TIME_INACT, 0);
Expand All @@ -66,7 +90,7 @@ bool ADXL345_WE::init(){
readAndClearInterrupts();
writeRegister(ADXL345_FIFO_CTL,0);
writeRegister(ADXL345_FIFO_STATUS,0);

return true;
}

Expand Down Expand Up @@ -310,17 +334,15 @@ float ADXL345_WE::getRoll(){

/************ Power, Sleep, Standby ************/

bool ADXL345_WE::setMeasureMode(boolean measure){
void ADXL345_WE::setMeasureMode(bool measure){
regVal = readRegister8(ADXL345_POWER_CTL);
if(measure){
regVal |= (1<<ADXL345_MEASURE);
}
else{
regVal &= ~(1<<ADXL345_MEASURE);
}
uint8_t ack = writeRegister(ADXL345_POWER_CTL, regVal);

return (ack == 0);
writeRegister(ADXL345_POWER_CTL, regVal);
}

void ADXL345_WE::setSleep(bool sleep, adxl345_wUpFreq freq){
Expand Down Expand Up @@ -592,21 +614,39 @@ void ADXL345_WE::resetTrigger(){
*************************************************/

uint8_t ADXL345_WE::writeRegister(uint8_t reg, uint8_t val){
_wire->beginTransmission(i2cAddress);
_wire->write(reg);
_wire->write(val);
if(!useSPI){
_wire->beginTransmission(i2cAddress);
_wire->write(reg);
_wire->write(val);

return _wire->endTransmission();
return _wire->endTransmission();
}
else{
digitalWrite(csPin, LOW);
_spi->transfer(reg);
_spi->transfer(val);
digitalWrite(csPin, HIGH);
return false; // to be amended
}
}

uint8_t ADXL345_WE::readRegister8(uint8_t reg){
uint8_t regValue = 0;
_wire->beginTransmission(i2cAddress);
_wire->write(reg);
_wire->endTransmission(false);
_wire->requestFrom(i2cAddress,1);
if(_wire->available()){
regValue = _wire->read();
if(!useSPI){
_wire->beginTransmission(i2cAddress);
_wire->write(reg);
_wire->endTransmission(false);
_wire->requestFrom(i2cAddress,1);
if(_wire->available()){
regValue = _wire->read();
}
}
else{
reg |= 0x80;
digitalWrite(csPin, LOW);
_spi->transfer(reg);
regValue = _spi->transfer(0x00);
digitalWrite(csPin, HIGH);
}
return regValue;
}
Expand All @@ -615,34 +655,60 @@ uint8_t ADXL345_WE::readRegister8(uint8_t reg){
int16_t ADXL345_WE::readRegister16(uint8_t reg){
uint8_t MSByte = 0, LSByte = 0;
int16_t regValue = 0;
_wire->beginTransmission(i2cAddress);
_wire->write(reg);
_wire->endTransmission(false);
_wire->requestFrom(i2cAddress,2);
if(_wire->available()){
LSByte = _wire->read();
MSByte = _wire->read();
}
regValue = (MSByte<<8) + LSByte;
if(!useSPI){
_wire->beginTransmission(i2cAddress);
_wire->write(reg);
_wire->endTransmission(false);
_wire->requestFrom(i2cAddress,2);
if(_wire->available()){
LSByte = _wire->read();
MSByte = _wire->read();
}
}
else{
reg = reg | 0x80;
reg = reg | 0x40;
digitalWrite(csPin, LOW);
_spi->transfer(reg);
LSByte = _spi->transfer(0x00);
MSByte = _spi->transfer(0x00);
digitalWrite(csPin, HIGH);
}
regValue = (MSByte<<8) + LSByte;
return regValue;
}

// This way of reading is needed for the FIFO
uint64_t ADXL345_WE::readRegister3x16(uint8_t reg){
uint8_t byte0 = 0, byte1 = 0, byte2 = 0, byte3 = 0, byte4 = 0, byte5 = 0;
uint64_t regValue = 0;
_wire->beginTransmission(i2cAddress);
_wire->write(reg);
_wire->endTransmission(false);
_wire->requestFrom(i2cAddress,6);
if(_wire->available()){
byte0 = _wire->read();
byte1 = _wire->read();
byte2 = _wire->read();
byte3 = _wire->read();
byte4 = _wire->read();
byte5 = _wire->read();
if(!useSPI){
_wire->beginTransmission(i2cAddress);
_wire->write(reg);
_wire->endTransmission(false);
_wire->requestFrom(i2cAddress,6);
if(_wire->available()){
byte0 = _wire->read();
byte1 = _wire->read();
byte2 = _wire->read();
byte3 = _wire->read();
byte4 = _wire->read();
byte5 = _wire->read();
}
}
else{
reg = reg | 0x80;
reg = reg | 0x40;
digitalWrite(csPin, LOW);
_spi->transfer(reg);
byte0 = _spi->transfer(0x00);
byte1 = _spi->transfer(0x00);
byte2 = _spi->transfer(0x00);
byte3 = _spi->transfer(0x00);
byte4 = _spi->transfer(0x00);
byte5 = _spi->transfer(0x00);
digitalWrite(csPin, HIGH);
}

regValue = ((uint64_t) byte1<<40) + ((uint64_t) byte0<<32) +((uint64_t) byte3<<24) +
+ ((uint64_t) byte2<<16) + ((uint64_t) byte5<<8) + (uint64_t)byte4;
return regValue;
Expand Down
8 changes: 7 additions & 1 deletion src/ADXL345_WE.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#endif

#include <Wire.h>
#include <SPI.h>

/* registers */
#define ADXL345_ADDRESS 0x53
Expand Down Expand Up @@ -163,6 +164,8 @@ class ADXL345_WE
ADXL345_WE();
ADXL345_WE(TwoWire *w, int addr);
ADXL345_WE(TwoWire *w);
ADXL345_WE(SPIClass *s, int cs, bool spi);
ADXL345_WE(int cs, bool spi);

/* Basic settings */

Expand Down Expand Up @@ -195,7 +198,7 @@ class ADXL345_WE

/* Power, Sleep, Standby */

bool setMeasureMode(bool measure);
void setMeasureMode(bool measure);
void setSleep(bool sleep, adxl345_wUpFreq freq);
void setSleep(bool sleep);
void setAutoSleep(bool autoSleep, adxl345_wUpFreq freq);
Expand Down Expand Up @@ -229,6 +232,7 @@ class ADXL345_WE

private:
TwoWire *_wire;
SPIClass *_spi;
int i2cAddress;
uint8_t regVal; // intermediate storage of register values
xyzFloat rawVal;
Expand All @@ -237,6 +241,8 @@ class ADXL345_WE
xyzFloat offsetVal;
xyzFloat angleOffsetVal;
xyzFloat corrFact;
int csPin;
bool useSPI;
int16_t rangeFactor;
uint8_t writeRegister(uint8_t reg, uint8_t val);
uint8_t readRegister8(uint8_t reg);
Expand Down

0 comments on commit 8f4b712

Please sign in to comment.