Page 1 of 1

PIC SPI communication questions

Posted: Fri Feb 28, 2014 7:34 am
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

Re: PIC SPI communication questions

Posted: Thu Mar 13, 2014 9:52 pm
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)

Re: PIC SPI communication questions

Posted: Thu Mar 13, 2014 9:55 pm
by headhuntergr
Thank's a lot.
I will try to set up a pic and try a few tricks.

Re: PIC SPI communication questions

Posted: Thu Mar 20, 2014 9:35 am
by hyperion007
How did you make out? Did it work for you?

/Daniel

Re: PIC SPI communication questions

Posted: Thu Mar 20, 2014 2:05 pm
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.

Re: PIC SPI communication questions

Posted: Fri Mar 21, 2014 6:33 pm
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

Re: PIC SPI communication questions

Posted: Fri Mar 21, 2014 7:28 pm
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).

Re: PIC SPI communication questions

Posted: Fri Mar 21, 2014 8:30 pm
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

Re: PIC SPI communication questions

Posted: Fri Mar 21, 2014 8:40 pm
by headhuntergr
0xFF? Isn't that 111111111?
Don't quite get the "& 0xFF" part but i'll search.

Re: PIC SPI communication questions

Posted: Sat Mar 22, 2014 7:46 am
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/

Re: PIC SPI communication questions

Posted: Sun Dec 28, 2014 12:09 am
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 :)

Re: PIC SPI communication questions

Posted: Sun Dec 28, 2014 12:59 pm
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)

Re: PIC SPI communication questions

Posted: Sun Dec 28, 2014 1:40 pm
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.