Need to flip order of bits MSB to LSB re: MCP23S17


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Aug 2011
    Posts
    457


    Did you find this post helpful? Yes | No

    Default Re: Need to flip order of bits MSB to LSB re: MCP23S17

    Here's a few tips

    First off, don't REV anything. That's only going to confuse the issue.

    The MCP23S17 expects the bytes to be sent MSB first, that is the first bit clocked out appears at GPx7 and the last bit appears at GPx0.
    That's what you should get if you specify MSBFIRST in the SHIFTOUT instruction
    Code:
    SHIFTOUT SDO, SCLK, 1, [databyte]
    If you need to reverse the bits at the output port, then specify LSBFIRST in the SHIFTOUT instruction
    Code:
    SHIFTOUT SDO, SCLK, 0, [databyte]

    Now, on to the ordering of bytes. When you have a word declaration like
    Code:
     Voltage VAR WORD
     VoltageFst VAR Voltage.BYTE0 'this is the lower byte of the word
     VoltageSec VAR Voltage.BYTE1 'this is the upper byte of the word
    
     Voltage = $8001
    That would give you VoltageFst = $01, and VoltageSec = $80


    So, if you used your send routine with those values
    Code:
    nCSA = 0                'Enable MCP23S17-A
        SHIFTOUT SDO, SCLK, 1, [MCP23A,OLATA]   ' i.e. SHIFTOUT SDO, SCLK, MSB,[MCP23A,OLATA]    
        SHIFTOUT SDO, SCLK, 0, [VoltageFst]     ' i.e. SHIFTOUT SDO, SCLK, LSB,[VoltageFst]    
        nCSA = 1                'Disable the MCP23S17
        pause 1
        nCSA = 0                'Enable MCP23S17-A
        SHIFTOUT SDO, SCLK, 1, [MCP23A,OLATB]   ' i.e. SHIFTOUT SDO, SCLK, MSB,[MCP23A,OLATB]
        SHIFTOUT SDO, SCLK, 0, [VoltageSec]     ' i.e. SHIFTOUT SDO, SCLK, LSB,[VoltageSec]         
        nCSA = 1                'Disable the MCP23S17
    RETURN
    You end up with:
    OLATA7:OLATA0 = $80 ' VoltageFst is $01, but was sent out LSB first so it swapped order
    OLATB7:OLATB0 = $01 ' VoltageSec is $80, but was sent out LSB first so it swapped order

    If that's not what you need then you'll either have to send VoltageFst to OLATB/VoltageSec to OLATA to swap the bytes,
    and/or use MSB in the SHIFTOUT instruction, but that depends on exactly how the output ports are connected to the fiber interface.

    In your case I wouldn't bother with trying to using 16-bit mode with the MCP23S17, at least not right now. That'll change all of the
    register mapping addresses and likely confuse things even more. 16-bit mode can be faster, but since you're using SHIFTOUT instead
    of the hardware MSSP it's already slow.

  2. #2
    Join Date
    Sep 2006
    Location
    Indiana, USA
    Posts
    72


    Did you find this post helpful? Yes | No

    Default Re: Need to flip order of bits MSB to LSB re: MCP23S17

    Awesome stuff guys!

    Yes, when I got in and tried the thought that I had of just clocking in the set up register stuff first:

    Code:
    nCSA = 0                'Enable MCP23S17-A
        SHIFTOUT SDO, SCLK, 1, [MCP23A,OLATA]   ' i.e. SHIFTOUT SDO, SCLK, MSB,[MCP23A,OLATA]    
        SHIFTOUT SDO, SCLK, 0, [VoltageFst]     ' i.e. SHIFTOUT SDO, SCLK, LSB,[VoltageFst]    
        nCSA = 1                'Disable the MCP23S17
    It worked just like Tabsoft and Tumbleweed both surmised (or tested!) and yes, the byte order was wrong as well, so I just swapped the two alias bytes:

    Code:
        nCSA = 0                'Enable MCP23S17-A
        SHIFTOUT SDO, SCLK, 1, [MCP23A,OLATA]
        SHIFTOUT SDO, SCLK, 0, [VoltageSec]
        nCSA = 1                'Disable the MCP23S17
        pause 1
        nCSA = 0                'Enable MCP23S17-A
        SHIFTOUT SDO, SCLK, 1, [MCP23A,OLATB]
        SHIFTOUT SDO, SCLK, 0, [VoltageFst]        
        nCSA = 1                'Disable the MCP23S17
    And that gave me the full picture!

    So yay for my tired little brain at least showing some spark of life there with my hunch! Sometimes it's just good to put stuff up here to get the right thought patterns going.

    And thank all of you for helping to fill in the blanks! You guys rock!

    Now to get to tackling all the other stuff this thing needs to do.

    Like I posted above, the PIC is handling a setup byte and then a 16 bit value that ends up setting a 16 bit DAC at the other end of a fiber optic link. The card my circuit is talking to is an OEM ADC/DAC card that uses two pulse widths to represent a one or zero. The widths are tight and the entire pulse train is pretty narrow, so the PIC couldn't begin to help with that. So its a mess of shift registers and one-shot chips that do the critical timing, the PIC presents the full 24 bits to a string of parallel shift registers via the MCP23S17's and sends an enable pulse to transmit when everything is set.

    I'm working on another card that does the RX side by turning the pulse widths back into parallel data, and then I'll read them with a MCP23S17 again. Just 16 bits on the return.

    I'll likely end up redoing the board for the send side since it's got some "blue wires" going on, and the fact it requires some trickery in code to map everything just right. But at least I can do some more debugging with it now.

    My major mistake was the fact that I breadboarded the digital logic circuitry to figure that all out and never bothered to do the PIC stuff on there as well!!!

    I've learned my lesson!

    Thanks again guys! You are a wealth of knowledge and way more help than anyone could hope for!

Similar Threads

  1. shiftin with \bits - What order do they come?
    By tbarania in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd April 2015, 04:39
  2. MSB Compile error
    By RFEFX in forum PBP3
    Replies: 2
    Last Post: - 26th January 2015, 06:32
  3. Replies: 2
    Last Post: - 23rd April 2013, 16:34
  4. Toggled Pic flip flop Help Please
    By g7jiq in forum General
    Replies: 2
    Last Post: - 22nd March 2009, 10:09
  5. Flip Flop
    By tazntex in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 13th November 2008, 20:53

Members who have read this thread : 0

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