PDA

View Full Version : Trouble interfacing ADXL345/375 accelerometer



achilles03
- 2nd September 2015, 02:05
I'm having trouble interfacing an ADXL375 accelerometer (almost the same as the more widely used ADXL345 from what I can tell). I would appreciate any help!

The datasheet for the ADXL375 is here:
http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL375.pdf

I have it connected to a 16F676 with 1k resistors connecting the ADXL375's DQI (input) and DQO (output) pins to the pic. CLK and CS are direct connection to the pic with the ADXL375's two interrupt pins left floating. I'm using a 3.3v regulator.

I'm trying to interface with it in SPI mode (which is the default if I've read the datasheet correctly). However, all I get are "0-0-0" lines in the terminal. Any suggestions?

Thanks in advance for any help!



@ device pic16F676, hs_osc, wdt_off, pwrt_off, mclr_off, protect_off
cmcon=7
define osc 8
ANSEL = %00000000 'AN0 through AN7 are digital

AX1 VAR WORD 'X-dir accel
AY1 VAR WORD 'Y-dir accel
AZ1 VAR WORD 'Z-dir accel
wconfig var byte 'ADXL FIFO address write byte
rconfig var byte 'ADXL FIFO address read byte
'temporary shiftout bytes
ACDATA1 var byte
ACDATA2 var byte
ACDATA3 var byte
ACDATA4 var byte
ACDATA5 var byte
ACDATA6 var byte

CS var PORTa.2 ' CHIP SELECT
SDO var PORTc.0 ' DATA OUT FOR ADXL375
SDI var PORTc.1 ' DATA IN FOR ADXL375
CLK var PORTc.2 ' CLOCK PIN
LED var PORTc.4 ' LED PIN

rconfig=50 'first FIFO address register where acceleration values are stored
rconfig.7=1 'change bit7 to 1 to indicate a read operation
rconfig.6=1 'change bit6 to 1 to indicate multiple bytes will be read

'write address for the power_ctl byte with bit7=0 (write operation) and bit6=0 (1 byte operation)
wconfig=45

'blink to verify program is running
high LED
pause 100
low LED
pause 100
high LED

high CS
pause 1

loop:
low CS
pause 1
shiftout SDI,CLK,1,[wconfig\8,%00001000\8] 'write 1 to bit3 of power_ctl register to put ADXL375 in measurement mode
high CS
pause 1
low CS
shiftout SDI,CLK,1,[rconfig\8] 'shiftout read command and FIFO address
shiftin SDO,CLK,1,[ACDATA1,ACDATA2,ACDATA3,ACDATA4,ACDATA5,ACDATA6] 'shiftin acceleration values
high CS
AX1.highbyte=ACDATA1
AX1.lowbyte=ACDATA2
AY1.highbyte=ACDATA3
AY1.lowbyte=ACDATA4
AZ1.highbyte=ACDATA5
AZ1.lowbyte=ACDATA6
serout2 portc.5,16468,[DEC AX1,"-",DEC AY1,"-",DEC AZ1,10,13]

pause 1000

goto loop

end

AvionicsMaster1
- 3rd September 2015, 13:30
Try to set define OSC 8. The OSC needs to be in caps.

achilles03
- 3rd September 2015, 22:38
It was communicating with the terminal program at 9600 baud without a problem... wouldn't it have spouted gibberish if it didn't use 8Mhz? I'll use uppercase to be safe.

But I think I figured out the problem... I was communicating with the clock pin idling low... it should have been idling high. Therefore the pic was not shifting the right data or the right register address to the ADXL375, and so it never got into measurement mode. After I changed it to idle high, I started getting values that looked meaningful (but very noisy!), although I think that's just because I don't have any filtering on the power supply or at the IC pins (just wanted to get it working). I'll try that next.

Dave

AvionicsMaster1
- 5th September 2015, 05:00
Are you using internal oscillator?

achilles03
- 6th September 2015, 20:13
It's using an external crystal with the "HS" oscillator setting.

After adding some caps, the output is much better and it's clearly exporting acceleration data correctly. Flipping it around on all axes show the expected +/-1g change. The offsets are rather large (+3g in some axis), but that can be adjusted/calibrated.