PIC SPI communication questions

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
headhuntergr
Posts: 50
Joined: Fri Feb 28, 2014 7:25 am
Has thanked: 34 times
Been thanked: 5 times

PIC SPI communication questions

Post by headhuntergr »

Hello! Noob questions.
I would like to begin using flowcode and i have a few questions.
I require to program a few registers (15bit) they are usually a number like 500 up to 1800 but i have to send the whole 15 sequence.
SO i would like to read my variable (let's say 1400 which is 10101111000 11 bits ) and then send the whole 15bits like 000010101111000.
How can i do this?
I also would like to send binary data directly from a constant (15 bits) is that possible?

Thank you

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times

Re: PIC SPI communication questions

Post by hyperion007 »

I don't have a lot of experience with the SPI component in FC but I usually bit bang SPI and then you could send one bit at the time for as many bits as you need by doing:
SCLK pin=1
SDO=Variable AND 0x80
SDO pin=SDO
SCLK pin=0
SCLK pin=1
SDO=variable AND 0x40
SDO pin=SDO
SCLK pin=0


and so on.

(0x80 is for the MSB in an 8bit variable for the MSB of a 15bit variable it would start at 0x4000, then 0x2000 etc)

headhuntergr
Posts: 50
Joined: Fri Feb 28, 2014 7:25 am
Has thanked: 34 times
Been thanked: 5 times

Re: PIC SPI communication questions

Post by headhuntergr »

Thank's a lot.
I will try to set up a pic and try a few tricks.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times

Re: PIC SPI communication questions

Post by hyperion007 »

How did you make out? Did it work for you?

/Daniel

headhuntergr
Posts: 50
Joined: Fri Feb 28, 2014 7:25 am
Has thanked: 34 times
Been thanked: 5 times

Re: PIC SPI communication questions

Post by headhuntergr »

Hey i am still setting my circuit a bit.
I want to send a range of numbers (variable) from 500 to 1800 so for example the number 1800 is 11100001000 which is 11bits. So i will send first the "0000" part and then the number to complete my 15bit word.


edit: btw that is probably not the correct way since the lower numbers are expressed with less bits.

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: PIC SPI communication questions

Post by Spanish_dude »

The size of your variable is not dependent on the value it is storing.
If you use a UINT variable (depending on the compiler) you have a 2 byte wide storage space, so 16 bits wide, which can store a value going from 0 up to ~65000.
In binary, you would see the UINT variable as 0000.0000.0000.0000 going up to 1111.1111.1111.1111. No matter what value you store, you'll always have 16 bits.

Make two byte variables for your MSbyte and LSbyte and split the UINT in two bytes.
Send the MSB first and then the LSB.

- Nicolas

headhuntergr
Posts: 50
Joined: Fri Feb 28, 2014 7:25 am
Has thanked: 34 times
Been thanked: 5 times

Re: PIC SPI communication questions

Post by headhuntergr »

Thank you for your reply Nicolas seems to be extremely helpful.

I am using Uint, can you explain how to split the variable? (excuse me on this i'm a complete noob).

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: PIC SPI communication questions

Post by Spanish_dude »

Make two byte variables, lets call them MSB and LSB.
Add a calculation box somewhere between you getting the value you want to send and the actual sending of the value through SPI.

In the calculation box you'll do the following:

Code: Select all

LSB = Value_To_Send & 0xFF
MSB = (Value_To_Send >> 8) & 0xFF
Medelec made a tutorial on this somewhere, if I remember correctly.

- Nicolas

headhuntergr
Posts: 50
Joined: Fri Feb 28, 2014 7:25 am
Has thanked: 34 times
Been thanked: 5 times

Re: PIC SPI communication questions

Post by headhuntergr »

0xFF? Isn't that 111111111?
Don't quite get the "& 0xFF" part but i'll search.

headhuntergr
Posts: 50
Joined: Fri Feb 28, 2014 7:25 am
Has thanked: 34 times
Been thanked: 5 times

Re: PIC SPI communication questions

Post by headhuntergr »

Oh yes! Just got it now after reading this http://www.matrixmultimedia.com/article.php?a=366
I am still setting up my circuit hopefully i'll be able to try this today or tomorrow.

edit:
btw the datasheet of what i'm trying to control https://drive.google.com/file/d/0BxL-fH ... FBQWRDbGs/

headhuntergr
Posts: 50
Joined: Fri Feb 28, 2014 7:25 am
Has thanked: 34 times
Been thanked: 5 times

Re: PIC SPI communication questions

Post by headhuntergr »

Hello again!
I'm back with a similar problem
This
Image
Is the word i have to send. My problem is the X bytes in the middle and at the end, they are don't care bytes.
How can i send the whole 24 bit word with the data bits changing each time?
Any ideas?

Thank you for your replies :)

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times

Re: PIC SPI communication questions

Post by kersing »

Using the SPI macros send three bytes. Start by setting chip select low, next send the first byte with the command bits and four zeros (or ones the device does not care what you set those bits to), next a byte with the first 8 bits, if you use an UINT to store the 12 bits (bit 11 to bit 0 is 12 bits) you can get the first and second byte using the following in a calculation:

Code: Select all

sendByte1 = (storeUint >> 4) & 0xff
sendByte2 = (storeInt & 0xff) << 4
Next send the two bytes and set chip select to high. (Use an output icon with one bit for chip select)
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

headhuntergr
Posts: 50
Joined: Fri Feb 28, 2014 7:25 am
Has thanked: 34 times
Been thanked: 5 times

Re: PIC SPI communication questions

Post by headhuntergr »

Thank you kersing.

This is what i did, i thought about sending the command first followed by the two bytes (as some dac examples on the forum) but i got a bit confused by the don't care bits.
I was making a mistake on shifting.

Now it seems to be OK, i'll test this afternoon.
Attachments
spitest.fcfx
(5.51 KiB) Downloaded 277 times

Post Reply