Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 1 month ago.
How to switch between different data rates?
#include "ADXL345_I2C.h" #include "CheckRTC.h" ADXL345_I2C accelerometer(D14, D15); //Serial pc(USBTX,USBRX); Serial pc(D8, D2); int main() { pc.baud(115200); int readings[3] = {0, 0, 0}; time_t seconds; char buf[64]; struct tm t; t.tm_year = 15 + 100; t.tm_mon = 10 - 1; t.tm_mday = 07; t.tm_hour = 14; t.tm_min = 27; t.tm_sec = 00; seconds = mktime(&t); set_time(seconds); pc.printf("Starting ADXL345 test...\n"); wait(.001); pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID()); wait(.001); // These are here to test whether any of the initialization fails. It will print the failure if (accelerometer.setPowerControl(0x00)){ pc.printf("didn't intitialize power control\n"); return 0; } //Full resolution, +/-16g, 4mg/LSB. wait(.001); if(accelerometer.setDataFormatControl(0x0B)){ pc.printf("didn't set data format\n"); return 0; } wait(.001); //3.2kHz data rate. //Measurement mode. if(accelerometer.setPowerControl(MeasurementMode)) { pc.printf("didn't set the power control to measurement\n"); return 0; } switch ( int i = 7 ) { case 1: accelerometer.setDataRate(ADXL345_3200HZ); break; case 2: accelerometer.setDataRate(ADXL345_1600HZ); break; case 3: accelerometer.setDataRate(ADXL345_800HZ); break; case 4: accelerometer.setDataRate(ADXL345_400HZ); break; case 5: accelerometer.setDataRate(ADXL345_200HZ); break; case 6: accelerometer.setDataRate(ADXL345_100HZ); break; case 7: accelerometer.setDataRate(ADXL345_50HZ); break; case 8: accelerometer.setDataRate(ADXL345_25HZ); break; case 9: accelerometer.setDataRate(ADXL345_12HZ5); break; case 10: accelerometer.setDataRate(ADXL345_6HZ25); break; } int i = 1; while (1) { seconds = time(NULL); strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); accelerometer.getOutput(readings); i++; pc.printf("\n\r %i, %i, %i %d", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2], i); pc.printf(" %s\r\n",buf); } }