Shop OBEX P1 Docs P2 Docs Learn Events
basic stamp communicating via SPI protocal to an accelerometer not working — Parallax Forums

basic stamp communicating via SPI protocal to an accelerometer not working

BillDerBillDer Posts: 33
edited 2008-11-18 02:13 in BASIC Stamp
Hello, I'm new of course and have been trying to "talk" to my accelerometer. It is an analog devices ADIS16204 that uses SPI (serial peripheral interface). I have written some very basic code to just get them talking. However, I just get the same numbers back regardless of what I send, except when I drive the reset pin low, I get the as expected nothing. I know I must be doing something wrong, I have hooked up the oscilloscope and can see the clock pulses I generate and the signals returned by the accelerometer. But they never change. I am trying to read the x-axis register where the x-direction real time acceleration is stored. It is in register 5, so I send a five, and I get back a non-sensical number 24xxx something acceleration. Crazy.

Here is my code, but first the data sheet says that the chip reads MSB first, the first data is at the pin when chip select goes low and post pulse after. Any suggestions would be greatly appreciated. I am also attaching the chips datasheet.
Thanks,
Bill

' {$STAMP BS2e}
'{$pbasic 2.5}
'ShiftinA2DExample1.bse

'----Declarations

adcBits VAR Byte
v VAR Byte
r VAR Byte
reg1 VAR Bit(8)
reg2 VAR Bit(8)
transmitdata VAR Word
receivedata VAR Word
testword VAR Word
volts VAR Word

' ---- Initialization
reset PIN 0
sclk PIN 1
CS PIN 2
dataoutput PIN 3
Datainput PIN 4


DEBUG CLS
DEBUG HOME

'
Main Routine

HIGH reset
LOW reset
HIGH reset
PAUSE 300

DO
HIGH cs
transmitdata = 0
transmitdata.HIGHBYTE = 5
DEBUG CR, BIN16 transmitdata

receivedata = 0
GOSUB adc_data
GOSUB display

LOOP


ADC_data:


LOW cs
SHIFTOUT datainput, sclk, MSBFIRST, [noparse][[/noparse]transmitdata\16]
HIGH cs
'PAUSE 5

LOW cs
SHIFTIN DATAoutput, sclk, MSBPRE, [noparse][[/noparse]receivedata\16]
HIGH cs
RETURN

Display:
DEBUG HOME
DEBUG "16-bit transmitted binary value: ", BIN16 transmitdata
DEBUG CR, "decimal value transmit: ", DEC transmitdata.HIGHBYTE

testword = receivedata & %0011111111111111
DEBUG CR, "16-bit binary value receive: ", SBIN13 receivedata
DEBUG CR, CR, "decimal value receive: ", SDEC receivedata

RETURN

Comments

  • FranklinFranklin Posts: 4,747
    edited 2008-11-18 02:13
    You need to refrain from double posting. Go to the sandbox, open your post and hit the red X in the upper right corner.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
Sign In or Register to comment.