SPI - 16bit out, how?

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
skartalov
Posts: 37
Joined: Fri Apr 09, 2010 10:50 am
Location: BULGARIA

SPI - 16bit out, how?

Post by skartalov » Wed Feb 27, 2013 8:10 am

Hi,
how I can access all 16bits is WORD variable, so i can send then serially over SPI line?

I am trying to address a AD5930 signal generator IC, which only accept 16 bits word on SPI. I will write the serial routine by myself, just need to clarify how to access the bits inside the WORD.

Thanks!

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Wed Feb 27, 2013 9:30 am

You can just treat the 16-bit value as two 8-bit transfers and use the built-in SPI routines if you like.

There are a number of different ways to access the bytes in a word, but one way is

Code: Select all

dim w as word

FSYNC = 0                 // assert chip enable low
SPI.WriteByte(w.byte1)    // send msb
SPI.WriteByte(w.byte0)    // send lsb
FSYNC = 1                 // latch in the 16-bit transfer

skartalov
Posts: 37
Joined: Fri Apr 09, 2010 10:50 am
Location: BULGARIA

Post by skartalov » Wed Feb 27, 2013 10:10 am

OK, now it gets a little tricky!
As you can see the datasheet of AD5930:

http://www.analog.com/static/imported-f ... AD5930.pdf

On page 17 is says that the frequency is send twice as 12 bit variables.

Now I have variable "F" which is LONGWORD with the desired frequency.
It is between 0 and 999 999.
So how to convert it and send to the AD5930 in the appropriate format?

Generally, all I want to do is initialize the IC to output squre waves only, in continious mode, and to be able to set the frequency, which is hold in my "F" LongWord variable.

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Wed Feb 27, 2013 2:04 pm

From what I can tell, you always send 16-bits.

The upper 4 bits (D15-D12) contain the register address, so you have to use some instructions to merge these with the data you're sending

Code: Select all

// register address bits (D15-D12)
const UPPER_FSTART = %11010000
const LOWER_FSTART = %11000000

dim fstart as longword,
	dw as longword,
	w as word

// get upper 12-bits
dw = fstart << 4		// need part of upper and part of lower longword
w = dw.word1 and $0fff
spi.writebyte(w.byte1 or UPPER_FSTART)
spi.writebyte(w.byte0)

// get lower 12-bits
w = fstart.word0 and $0fff
spi.writebyte(w.byte1 or LOWER_FSTART)
spi.writebyte(w.byte0)
Completely untested, and bound to have a few mistakes, but hopefully you get the picture

Post Reply