PDA

View Full Version : Using SPI and Shiftout together



Christopher4187
- 1st May 2013, 16:51
I want to use the hardware SPI and I have two IC's that use SPI. I'm sure there's a way to do it but I can't get it. When I use the MCP2515, I use the SSPBUF to transfer data. When I use the MCP4911, I can't figure out how to use the SSPBUF so I have to use the Shiftout. The problem with that is, I can only get it to work if I disable the SSPEN bit and then reenable after sending information to the 4911.

My code looks like this:

SSP1CON1.5=0
LOW PORTE.6
SHIFTOUT SDO, CLK, 1, [9\4,300\12]
HIGH PORTE.6
SSP1CON1.5=1

Is there a way to use the SSPBUF to communicate with the MCP4911 opposed to disabling the MSSP and using Shiftout?

HenrikOlsson
- 1st May 2013, 20:20
Hi Christopher,
I can't see why it shouldn't work. The MCP4911 exepcts data MSB first which is what the MSSP module in the PIC does so there should be no need to move around the bits. What you do need to do though is to "merge" the 4 control bits with the 12 (10) databits into two bytes which you then send using the MSSP module.

Totally untested:

Control VAR BYTE
Value VAR WORD
DataToSend VAR WORD

Control = 9
Value = 300

DataToSend = (Control << 12) + (Value << 2)
' DataToSend now contains (in binary): 1001010010110000
' The red bits are the control bits '9', the green bits are the value '300' (10bits) and the blue bits are just padding to get the full 16 bits width.

LOW PortE.6

SSPBUF = DataToSend.HighByte
' Wait for byte to go out.
SSPBUF = DataToSend.LowByte
' Wait for byte to go out.

HIGH PortE.6

/Henrik.

Christopher4187
- 1st May 2013, 21:01
Totally untested:And totally works! Thanks.

I was doing what you posted but I was trying to send the data out of SSPBUF with all 16 bits at once. I guess you can only send 8 at a time. The only thing I don't understand is why you did this:

DataToSend = (Control << 12) + (Value << 2)What is the layman's description?

Demon
- 1st May 2013, 23:58
Control shifted 12 bits to the left, value only shifted 2.

When in doubt, read Henrik's comments. That's what I do to look as if I understand what he's doing.

:)

Robert

HenrikOlsson
- 2nd May 2013, 06:10
Yes, Robert is spot on of course.
The reason for doing it is that we need to left justify the bits in the word we're shifting out (because the device wants MSB first). It moves the four control bits from the lower four bits of Control to the top four bits of ValueOfToSend then it adds in the actual value shifted two positions to the left in order to "put it" after the first four bits.

By the way, you had the control value set to 9 in your example so I used that but looking at the datasheet that doesn't seem to be a valid command. Does it really work with a control value of 9?

/Henrik.

Christopher4187
- 2nd May 2013, 10:19
Yes, Robert is spot on of course.
The reason for doing it is that we need to left justify the bits in the word we're shifting out (because the device wants MSB first). It moves the four control bits from the lower four bits of Control to the top four bits of ValueOfToSend then it adds in the actual value shifted two positions to the left in order to "put it" after the first four bits.
/Henrik.I get it now. Thanks for the explanation.
By the way, you had the control value set to 9 in your example so I used that but looking at the datasheet that doesn't seem to be a valid command. Does it really work with a control value of 9?The control value of 9 is actually for the MCP4912. I'm testing a few things at once.