ADXL362 Accelerometer - Why does this SPI code work?

nfk

Senior Member
Hi,

I'm a bit confused. I wrote the following code to get an Analog Devices ADXL362 triple axis accelerometer working. It uses SPI to communicate with the outside world. The weird thing is that I have to have the SPI mode set differently for send and receive to make it work - as per the code below. Can anyone tell me why? It's not a problem - the setup works fine for read and write - it just seems rather odd. (The code puts a very basic display on the screen showing the readings from the X, Y and Z axes.)

As an example of what I mean, in the section which says:

Code:
	shiftout 1,2,1, (11,45)
	shiftin 1,3,0, (b0)
...the mode settings of 1 and 0 are different and the setup doesn't work if you make them both either 1 or 0.

Here's the whole program:

Code:
'The setup uses:
'- Analog Devices ADXL362 3-Axis Accelerometer
'- PICAXE 28X1 IC
'- TextStar display

init:
	symbol lcd=0		'LCD output port
	symbol cs=b.7		'Chip Select line

	high cs			'Set the chip select high (off)
	high 0			'Set the LCD line high (because it idles high)

	pause 100
	
'Put a title on the display
	serout lcd,2400,(12)
	pause 2000	'Wait for LCD to initialise
	serout lcd,2400,("    ADXL362     ")
	serout lcd,2400,(" Accelerometer  ")
	pause 1000	'Wait for 1 second
	serout lcd,2400,(12)
	
'Set up the power register
	low cs
	shiftout 1,2,1, (10,45,2) 'Bits 0 and 1 set to a value of decimal '2' sets the device to make measurements
	high cs

	pause 250	

'Read the power register and display it
	low cs
	shiftout 1,2,1, (11,45)
	shiftin 1,3,0, (b0)
	high cs
	serout lcd,2400,(254,"H")
	serout lcd,2400,("Power Byte = ",#b0,"    ")
	pause 1000
	serout lcd,2400,(12)			'Clear the screen
		
main:

x:
	low cs					'Chip select on
	shiftout 1,2,1, (11,8)			'Send 11 (read) and 8 (X-axis register address) 1=Clock Pin 2=Output Pin 1=Mode
	shiftin 1,3,0, (b0)			'Shift the result into b0
	high cs					'Chip select off
	serout lcd,2400,(254,"H")		'Move the LCD's cursor to the home position
	serout lcd,2400,("X",254,"b",7,b0)	'Display a bar graph on the LCD

y:
	low cs
	shiftout 1,2,1, (11,9)
	shiftin 1,3,0, (b0)
	high cs
	serout lcd,2400,("Y",254,"b",7,b0)
	
z:
	low cs
	shiftout 1,2,1, (11,10)
	shiftin 1,3,0, (b0)
	high cs
	serout lcd,2400,("Z",254,"b",7,b0)

goto main
 

hippy

Ex-Staff (retired)
For SHIFTOUT -

0 LSBFirst_L (LSB first, idles low)
1 MSBFirst_L (MSB first, idles low)

For SHIFTIN -

0 MSBPre_L (MSB first, sample before clock, idles low)
1 LSBPre_L (LSB first, sample before clock, idles low)

For the chip you have SHIFTOUT and SHIFTIN both require MSB first but the values for specifying MSB first are different. If you use predefined names it doesn't matter what the values used are. Likewise using binary and hexadecimal numbers can make it clearer, such that you are reading register $2D ...

Code:
shiftout 1, 2, MSBFirst_L, ( %00001011, $2D )
shiftin  1, 3, MSBPre_L,   ( b0 )
 

nfk

Senior Member
Thanks Hippy. I didn't occur to me that the mode values would be different.

Anyway, mystery solved!
 
Top