SPI usage for 24 bit

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

SPI usage for 24 bit

Post by Francis » Sun Jan 25, 2009 10:47 am

I'd be grateful if someone could just clear up a query I have with SPI reading.

I'm using an 18F2520 with an AD7763 from Analog devices and trying to read the registers.

Some of the numbers I get back from it are fine.
But a couple seem a little odd, and these are from 24 bit registers.

Writing and reading are in whole bytes, so I decided to use SPI instead of Shift.

I couldn't find anything similar on-Forum or in-Help.
This is a simple routine I've been using to read 24 bits and put it into a LongWord.
Does it look roughly OK?

Code: Select all

Function Read24Bit(pAddress As Byte) As LongWord
    Dim WBuffer As LongWord
    wbuffer=0
    CSR=0
    SPI.WriteByte(pAddress)         ' Note MSBit = 0 = a READ for AD7763
    DelayUS(1)
    Wbuffer.byte2 = SPI.ReadByte()  ' MSByte first.
    WBuffer.byte1 = SPI.ReadByte()
    WBuffer.byte0 = SPI.ReadByte()
    result = wbuffer
    CSR=1
End Function
I haven't connected the board to the mains as I was practising at low voltage.
It's just that with the device's V & I diff amps shorted its still giving large numbers back.
I was expecting very low numbers heading towards zero.

Have I got the wrong 'end of the stick' concerning >1byte reads on SPI?
(Similar method but using WBuffer as Word for 16 bit seems OK).

Regards,
Francis.

Note1: The 'Delay' above was merely experimental as there were comments about small delays mentioned in Data Sheet. It makes no apparent difference with or without.

Note2: I have double-checked PCB continuity on my board.

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Mon Jan 26, 2009 11:12 am

I did a version using SHIFT and got the same results, so I guess I've answered my own question.
All is happy. I just need to find some rubber gloves now :wink:

Post Reply