PDA

View Full Version : ADXL345 Accelerometer using I2C



Terry
- 4th August 2015, 00:58
Hi,
I am currently very unsuccessful at communicating via I2C to an ADXL345 Accelerometer. The code and circuit appear to both very simple, but the response I get back makes no sense. Here are the details:
The ADXL345 CS is set high (3.3v) to put the device into I2C mode.
The ADXL345 Alt Address is set low (0v)
I have pull-up resistors on the Clock and SDA lines.

The chip is a PIC18F26K22 running at +3.3v

Below is the I2C code:


I2Cread ADXL345_SDA,ADXL345_SCL, $A7, $00, [X0] ' Read Device ID
serout2 RS232_TX, 6, ["ID: ",bin8 X0,13,10] ' The result should be 11100101 but is 00010000 like all other read results.

I2CWRITE ADXL345_SDA,ADXL345_SCL, $A6, $2D, [8] ' Put into Run Mode
pauseus 5

Main_I2C:
I2Cread ADXL345_SDA,ADXL345_SCL, $A7, $32, [X0,X1,Y0,Y1,Z0,Z1] ' Read accelerometer data
serout2 RS232_TX, 6, ["X: ",dec X0,",",dec X1]
serout2 RS232_TX, 6, [" Y: ",dec Y0,",",dec Y1]
serout2 RS232_TX, 6, [" Z: ",dec Z0,",",dec Z1,13,10]
pause 100
Goto Main_I2C


When reading back any byte the answer is always 16 or 00010000, which implies that I have something fundamentally configured wrong.

Any suggestions is greatly appreciated.

Terry

richard
- 4th August 2015, 02:27
try $a6 for the i2cread address as well as the write address , pbp will set bit.0 of the address as required

LinkMTech
- 5th August 2015, 01:43
It looks good to me. You need to check your connections and scope it and see if you're actually talking to it.
See post# 8 (http://www.picbasic.co.uk/forum/showthread.php?t=18292) where the I2CREAD was used same as yours, just different address.

alec
- 5th August 2015, 12:19
There was a recent thread that explained that you have to shift the seven bit address left, and let PBP insert bit 0 for the I2Cread or I2C write command. Using both a barometer chip and a humidity chip I made the address a constant, then made a variable that shifted the constant left one bit, and used that variable as the address.

HTU21D_i2c_address con $40
HTU21D_i2c_address1 var byte
HTU21D_i2c_address1 = HTU21D_i2c_address <<1
i2cwrite SDA, SCL, HTU21D_i2c_address1, [trig_temp]
pause 10
i2cread SDA, SCL, HTU21D_i2c_address1, [temp]
pause 10
i2cwrite SDA, SCL, HTU21D_i2c_address1, [trig_RH]
pause 10
i2cread SDA, SCL, HTU21D_i2c_address1, [RH]

try something like this using the $1D address as the constant, shift it once, and let PBP add bit 0

Terry
- 6th August 2015, 18:23
Thanks for everyone's input.
The main issue was Louie's point, the ADXL345 is a QFN part and I had a bad solder joint.
From the ADXL345 data sheet, the read/write addresses are given as $A6 and $A7 which do take into account the read/write bit and it is this value used in the I2C PBP code. I added a label to the I2C commands which allows a jump if there is an I2C communication error talking to the device. I added this label for all commands sent whether read or write, this error occurred for every command which directed me to investigating the hardware. Adding the label was a very valuable diagnostic tool.

Below is my working code for the ADXL345, much more work is still needed to get the device configured for my application and get the raw counts into meaningful values. Overall the PBP I2C commands work great and are simple to use.



X0 var byte
X1 var byte
Y0 var byte
Y1 var byte
Z0 var byte
Z1 var byte
ID var byte
Mode var byte

high ADXL345_CS ' Set high to put unit into I2C mode
pause 1 ' Added to insure no R/W issue
low ADXL345_SDO ' Set low to put unit addresses to $A6, $A7
pause 1 ' Added to insure no R/W issue

serout2 RS232_TX, 6, [13,13,10,"Start of Test Program. "]

'Initialize ADXL345
I2Cread ADXL345_SDA,ADXL345_SCL, $A7,$00, [ID], I2C_error 'read Device ID
serout2 RS232_TX, 6, ["Device ID: $", hex2 ID,13,10] ' Value should be $E5

I2CWRITE ADXL345_SDA,ADXL345_SCL, $A6, $31, [$0B], I2C_error '+/-16g, 13-bit mode
pauseus 5
I2CWRITE ADXL345_SDA,ADXL345_SCL, $A6, $2D, [$08], I2C_error 'Start Measurement or put into Run Mode
pauseus 5

I2Cread ADXL345_SDA,ADXL345_SCL, $A7, $2D, [Mode],I2C_error ' Confirm mode setting
serout2 RS232_TX, 6, ["Mode: ",hex Mode,13,10] ' value should be 8
pauseus 5

Main_I2C: ' Main Loop
I2Cread ADXL345_SDA,ADXL345_SCL, $A7, $32, [X0,X1,Y0,Y1,Z0,Z1], I2C_error
serout2 RS232_TX, 6, ["X,Y,Z, ", Sdec 256*X1+X0,", ",Sdec 256*Y1+Y0,", ",Sdec 256*Z1+Z0,13,10]
pause 100
Goto Main_I2C

I2C_Error:
serout2 RS232_TX, 6, ["I2C Comm Error",13,10]
stop