ADXL345 I2C with v29 beta library

10 May 2011

Hiya All, Having trouble with the ADXL345 accelerometer communicating via I2C with the new beta library. I am using the ADXL345 library available on the site. With the old mbed library, the device worked perfectly. With the new library all the received communications are incorrect, although nothing else has changed in the main program.

I have seen that a timeout was added to the I2C class, however I'm not sure what effect that would have had.

I have to use the beta library as I'm also using the new DSP algorithms.

Code is as follows:

#include "mbed.h"
#include "ADXL345.h"

int main() {

    //set up i2c
    ADXL345 acc(p9, p10);
    Serial pc(USBTX, USBRX);
    
    pc.baud(115200);
    DigitalOut led(LED1);

    
    signed short readings[3];
    bool loop = true;
    
    
    //attempt to get the device ID
    pc.printf("Device ID (should be 0xE5 or 229d) : %d\r\n", acc.getDevId());
    
    //attempt to get z axis
    pc.printf("Z Axis: %d\r\n", acc.getZAxis()); 
    
    //get register statuses
    char regs[29];
    acc.multiByteRead(0x1d, regs, 29);
    for (int i=0; i<29; i++) {
        pc.printf("Reg %d: %X\r\n", i+29, regs[i]);
    }
        
    acc.setPowerControl(8);
    
    while(loop) {
        acc.getOutput(readings);
        pc.printf("X: %4d\tY: %4d\tZ: %4d\r", readings[0], readings[1], readings[2]);
        if (pc.readable()) {
            if (pc.getc() == 'q') {
                loop = false;
                pc.printf("Exited Program\r\n");
            }
        }
        wait_ms(10);
    }
   
}

Any help would be hugely appreciated! Are there any other good I2C libraries hanging around?

Cheers, Rob

12 May 2011

Have written a hacked library as a drop in replacement offering I2C1 functionality using the v29 beta mbed library. Haven't had time to actually try and find the original issue, although I'm sure it will be sorted for the proper release

Import libraryI2CR

A hacked library to offer I2C functionality similar to the standard mbed library for I2C1 only. Is seemingly required when using the v29 beta mbed library as of 12.05.2011, as the I2C does not seem to work otherwise. Has been developed and tested with the ADXL345 accelerometer.

Cheers, Rob

31 Jul 2012

what pins should be attached when using I2C1 library?