PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
ADXL345_Demo.cpp

Print the current acceleration to the screen in a constant loop while doing a simple averaging filter.

cmake_minimum_required(VERSION 3.12)
find_package(PropWare REQUIRED)
project(ADXL345_Demo)
create_simple_executable(${PROJECT_NAME} ADXL345_Demo.cpp)
static const Port::Mask SCLK = Port::P0;
static const Port::Mask MOSI = Port::P1;
static const Port::Mask MISO = Port::P2;
static const Port::Mask CS = Port::P3;
static const unsigned int AVERAGING_BUFFER_LENGTH = 8;
static const int PRINT_LOOP_FREQUENCY = 40;
// Reading at the maximum frequency gives us enough samples in a short enough period of time that we can filter while
// still printing up-to-date values
static const ADXL345::DataRate DATA_RATE = ADXL345::DataRate::_3200_HZ;
static const ADXL345::Range RANGE = ADXL345::_2G;
void initialize (const ADXL345 &accelerometer);
int main () {
const ADXL345 accelerometer(CS);
initialize(accelerometer);
auto timer = CNT + SECOND / PRINT_LOOP_FREQUENCY;
while (1) {
int16_t buffer[AVERAGING_BUFFER_LENGTH][ADXL345::AXES];
int32_t totals[ADXL345::AXES] = {0, 0, 0};
float fValues[ADXL345::AXES];
// Read the most recent entries
for (unsigned int i = 0; i < AVERAGING_BUFFER_LENGTH; ++i)
accelerometer.read(buffer[i]);
// Create an average for each axis
for (unsigned int i = 0; i < AVERAGING_BUFFER_LENGTH; ++i)
for (unsigned int axis = 0; axis < ADXL345::AXES; ++axis)
totals[axis] += buffer[i][axis];
for (unsigned int axis = 0; axis < ADXL345::AXES; ++axis)
fValues[axis] = ADXL345::scale(totals[axis], RANGE) / AVERAGING_BUFFER_LENGTH;
pwOut.printf("X: %5.2f, Y: %5.2f, Z: %5.2f\n", fValues[ADXL345::X], fValues[ADXL345::Y], fValues[ADXL345::Z]);
timer = waitcnt2(timer, SECOND / PRINT_LOOP_FREQUENCY);
}
}
void initialize (const ADXL345 &accelerometer) {
SPI::get_instance().set_mosi(MOSI);
SPI::get_instance().set_miso(MISO);
SPI::get_instance().set_sclk(SCLK);
ADXL345::RateAndPowerMode rateAndPowerMode;
rateAndPowerMode.fields.dataRate = DATA_RATE;
accelerometer.write(ADXL345::Register::RATE_AND_POWER_MODE, rateAndPowerMode.raw);
ADXL345::DataFormat dataFormat;
dataFormat.fields.range = RANGE;
accelerometer.write(ADXL345::Register::DATA_FORMAT, dataFormat.raw);
ADXL345::FIFOControl fifoControl;
fifoControl.fields.fifoMode = ADXL345::FIFOMode::STREAM;
accelerometer.write(ADXL345::Register::FIFO_CONTROL, fifoControl.raw);
accelerometer.start();
}
PropWare::Printer::printf
void printf(const char fmt[], const T first, const Targs... remaining) const
Similar in functionality to the C-standard function printf.
Definition: printer.h:497
PropWare::SPI
SPI serial communications library; Core functionality comes from a dedicated assembly cog.
Definition: spi.h:43
waitcnt2
#define waitcnt2(a, b)
Wait until system counter reaches a value.
Definition: propeller.h:183
PropWare::Port
Flexible port that can have any pin enabled or disabled. Pins are independent of each other.
Definition: port.h:38
main
int main(void)
Definition: GraphicsTest.c:20
adxl345.h
PropWare.h
PropWare::ADXL345
Definition: adxl345.h:32
pwOut
PropWare::Printer pwOut
Most common use of printing in PropWare applications (not thread safe; see PropWare::pwSyncOut for mu...
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33