PDA

View Full Version : help reading adxl312



cluster
- 31st July 2011, 15:17
hi,
i am having trouble reading acceleration data from adxl312, this accelerometer is similar to adxl345. below is my code, it doesn't work, no matter how much i shake it i get only 0 reading. i would really appreciate any help..
i am working with pic18f4550
thanks

include "modedefs.bas"
DEFINE OSC 48
rx var porta.3
tx var porta.4
data_in var porta.5
data_out var porte.0
CLOCK var porte.1
cs var porte.2
adxl_data_x0 var word
adxl_data_x1 var word


pause 100

high CS
SEROUT tx,T9600,["ADXL 312 Test...",10]
high CLOCK
low cs
shiftout data_out, CLOCK, 2, [%0011000100100000]
high cs

high CLOCK
low cs
shiftout data_out, CLOCK, 2, [%0010111010000000]
high cs

high CLOCK
low cs
shiftout data_out, CLOCK, 2, [%0010111100000000]
high cs

high CLOCK
low cs
shiftout data_out, CLOCK, 2, [%0010110000001010]
high cs

high CLOCK
low cs
shiftout data_out, CLOCK, 2, [%0010110100001000]
high cs

main:
pause 10

high CLOCK
low cs
shiftout data_out, CLOCK, 2, [%10110011]
shiftin data_in, CLOCK, 6, [adxl_data_x1\13]
high CS

high CLOCK
low cs
shiftout data_out, CLOCK, 2, [%10110010]
shiftin data_in, CLOCK, 6, [adxl_data_x0\13]
high CS

SEROUT tx,T9600,["START",10]
SEROUT tx,T9600,[#adxl_data_x0,10]
SEROUT tx,T9600,[#adxl_data_x1,10]
SEROUT tx,T9600,["END",10]
goto main

cncmachineguy
- 31st July 2011, 17:00
are you sure you are running at 48Mhz?

cluster
- 31st July 2011, 17:46
thanks for you reply
i am using 20Mhz crystal if i define osc 20 then i get garbage from serout so i changed it to 48
is it wrong?

cncmachineguy
- 31st July 2011, 18:25
Is the posted code all of it? Did you do any configuring? Not sure you need to, just trying to see where to start looking in the datasheet to be able to answer. In the mean time, here is a quick easy test for if the speed is correct:



main:
toggle "some output with an LED connected"
pause 500
goto main


Of course you still need all setup stuff like define osc and such, but if the speed is correct you should get a blink at 60 hz.

cluster
- 31st July 2011, 20:57
when i put the above code (@ osc 48) led remained on and never turned off, so i modified it and led blinks at 1hz


test:
high porta.0
pause 250
low porta.0
pause 250
goto test

when i changed osc to 20 i got led to blink at 2hz..

cncmachineguy
- 31st July 2011, 21:35
Ok, first my apoligies, you should have gotton 1 hz with the code I posted. NOT 60 Hz. if the LED never turned off, well that is confusing. Maybe a typo somewhere?

On to more important stuff. with your code as posted (pause 250) you should get 2 hz. Is that what you are getting? pause 250 equals 2 Hz with OSC 20? If this is so, then I think you need to have OSC 20 as your define. Heres why I know:

pause uses OSC define to generate delays in software based on the OSC value. So when OSC matches actual pic speed the pauses will be the correct amount of time. 250 mSec on then 250 mSec of is 2 Hz.

So assuming make sure your DEFINE OSC is correct. Of course that doesn't address the fact that you say you get junk from serout when OSC is 20.

cluster
- 1st August 2011, 13:22
sorry i messed up the test results, this is the result i got, tested it twice

test program
test:
high porta.0
pause 500
low porta.0
pause 500
goto test

result at osc 48
led on off duration is exactly 1 second

result at osc 20
led on off duration is twice

so i think define osc is correct.

i found this code(in document section) from sparkfun:http://www.sparkfun.com/products/9814
it is for adxl345 and uses spi but its in c and for atmega..

cncmachineguy
- 1st August 2011, 14:56
Well I am sorry it wasn't as simple as the clock being wrong, But now you know how to test it. I actually do this on all my programs before going any further just to verify the clock/osc and me are all at the same speed.

Darrel Taylor
- 2nd August 2011, 01:31
The SHIFTOUT command sends 8-bits unless otherwise specified.
To send a 16-bit constant, you'll need to specify the number of bits.

shiftout data_out, CLOCK, 2, [%0011000100100000\16]

igorkhod
- 11th August 2012, 18:25
Hon Cluster. that you have succeeded. your device to work? what were the errors?

cluster
- 12th August 2012, 09:40
Hello igorkhod,
later on i did not use adxl312 instead i used adxl345 because adxl312 was not available in small quantity.
adxl312 is similar to adxl345, if i can remember correctly adxl312 have more resolution than adxl345.

if you are working with adxl312 first try to read device ID. below code is for adxl345

i2c_out VAR BYTE 'data to sent over I2C bus

i2c_in VAR BYTE[2] 'data received over I2C bus

i2c_ack VAR BIT 'acknowledgement bit

loop:
GOSUB I2C_START 'Start Condition
i2c_out = $A6
GOSUB I2C_TX 'Send data in “i2c_out”
i2c_out = $00
gosub I2C_TX
GOSUB I2C_STOP 'Stop Condition

GOSUB I2C_START 'Start Condition
i2c_out = $A7
GOSUB I2C_TX 'Send data in “i2c_out”
GOSUB I2C_RX
GOSUB I2C_STOP 'Stop Condition
goto loop

I2C_START: 'I2C start (start communication on I2C bus)

HIGH SDA

HIGH SCL

LOW SDA

LOW SCL

RETURN



I2C_STOP: 'I2C stop (terminate communication on I2C bus)

LOW SDA

HIGH SCL

HIGH SDA

PAUSE 1

RETURN



I2C_RX: 'I2C receive -> receive data from slave

SHIFTIN SDA,SCL,0,[i2c_in[0]] 'Shift in first byte MSBpre

SHIFTOUT SDA,SCL,1,[%0\1] 'Send acknowledge (ACK) = 0

SHIFTIN SDA,SCL,0,[i2c_in[1]] 'Shift in second byte MSBpre

SHIFTOUT SDA,SCL,1,[%1\1] 'Send not acknowledge (NACK) = 1

RETURN



I2C_TX: 'I2C transmit -> send data to the slave

SHIFTOUT SDA,SCL,1,[i2c_out] 'Shift out “i2c_out” MSBfirst

SHIFTIN SDA,SCL,0,[i2c_ack\1] 'Receive ACK bit
'if i2c_ack = 1 then
'SEROUT tx,T9600,["ACK NOT RECEIVED FOR"]
'SEROUT tx,T9600,[#i2c_out]
'SEROUT tx,T9600,[":"]
'SEROUT tx,T9600,[#i2c_ack,10]
'endif

RETURN

igorkhod
- 5th October 2012, 19:30
CLUSTER , Thanks for the reply. I have one more question. I ran into a problem. How to link pic16f877a and adxl345. Adxl 345 - is 3 volts. Pic - is 5 volts. How to decide this problem? Thanks in advance ..