Using SPI and Shiftout together


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425

    Default Using SPI and Shiftout together

    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:
    Code:
    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?

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Using SPI and Shiftout together

    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:
    Code:
    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.

  3. #3
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Using SPI and Shiftout together

    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:
    Code:
    DataToSend = (Control << 12) + (Value << 2)
    What is the layman's description?

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,598


    Did you find this post helpful? Yes | No

    Default Re: Using SPI and Shiftout together

    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

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Using SPI and Shiftout together

    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.

  6. #6
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Using SPI and Shiftout together

    Quote Originally Posted by HenrikOlsson View Post
    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.

Similar Threads

  1. Need to use SPI but I'm already using the SPI port
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 30th December 2012, 09:52
  2. Shiftout Modifiers for SPI LCDs (DOGM series)
    By jbike123 in forum PBP Wish List
    Replies: 0
    Last Post: - 9th November 2010, 00:08
  3. SPI instead of SHIFTOUT - MAX7219
    By Momboz in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 26th April 2008, 22:58
  4. Replace Shiftout with spi
    By Pedro Santos in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 7th January 2007, 16:57
  5. SPI on a pic without hardware SPI?
    By modifyit in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 26th April 2006, 13:29

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts