Port alias in Port Expanders (MCP23S17)


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: Port alias in Port Expanders (MCP23S17)

    Henrik-
    I may have been too hasty in my description ( I am back on my home project) as I am stumped. I had used the I2C version of this part a while back and have it working. Now I am trying to use the SPI version (MCP23S17) for its speed and I can't seem to figure out how to get from a byte indicating which bit to turn on for the MCP23S17 part.
    Lets say I receive a var and it's value is $E. This var needs to tell the MCP23S17 that the 15th output should turn on. Now that is GPB-7 on the MCP23S17.
    I have a routine that converts HEX to DECIMAL (thanks to this forum years ago) and that works fine, how to I get a single decimal to go to the correct port on the MCP23S17?
    My code works fine to send a binary string (easier to visualize for me) to the MCP23S17 but I can't seem to get the finesse of going from 1 hex or 1 dec char to the right bit output.
    I have looked through the forum and I have seen yours and others pointing to OUT.0[var] which gets to the VAR-position of the OUT variable but as I have 2 eight bit outputs I am stumped......

    From the MCP23S17 datasheet it looks like it may be addressed as one 16 bit output, is that accurate? Any suggestions?
    Some code below:
    Code:
    OutA var byte
    
    OUTtest:'------------ TEST
                          OutA=$1                                                  'Simulate number from serial comm, example '1'
                          K=OutA                                                   'Sets up the conversion from hex to decimal
                          gosub H2D                                               'Performs the conversion
                          debug "After H2D K=",dec1 K,13               'Double-check what the sub did
                          k=k-8                                                        'try this for a pointer
                          OutA_DataOut=OutA.0[k]                        'Try to set the correct bit out of 8 to turn on and pass to OutA_DataOut which is passed to the MCB23S17
                            OutA_MCPReg = OLATA                             'Sets up PORTA output
    '                        OutA_DataOut=%11111110                    'This works by setting individual bits, but not good for what I want to do
                            gosub SEND_OutA_2317                         'Send to the expander
                            pause 10                                               'wait a bit
                            OutA_MCPReg = OLATB                             'Sets up PORTB output
                            OutA_DataOut=%01111111
                            gosub SEND_OutA_2317
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  2. #2
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: Port alias in Port Expanders (MCP23S17)

    Henrik-
    Also forgot to mention that in the older I2C design I had 8 bits of inputs and 8 bits of outputs on one 2317.
    This new design is all outputs - I thought it would be easier!
    Not so much......
    Regards,
    Steve
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Port alias in Port Expanders (MCP23S17)

    Sounds like what you're looking for is the DCD operator:
    DCD returns the decoded value of a bit number. It changes a bit number (0 - 31) into a binary number with only that bit set to 1. All other bits are set to 0.
    B0 = DCD 2 ' Sets B0 to %00000100
    So, perhaps something:
    Code:
    OutBits VAR WORD
      GPA VAR OutBits.BYTE0
      GPB VAR OutBits.BYTE1
    
    Outbits = DCD $E
    Henrik.

  4. #4
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: Port alias in Port Expanders (MCP23S17)

    Thanks I will try that!

    I have made some progress but I still need a way to get from 1 byte which will tell me which of the 16 outputs to turn on.

    Current test code:
    Code:
                          OutA=$FFFE                                        'Initial value for word var                      
                          OutA_hi=OutA.highbyte                             'Splits into hibyte
                          OutA_lo=OutA.lowbyte                              'And into low byte of the word var
                          OutA_lo.3=0                                       'Bit mod
                          OutA_hi.5=0                                       'Another bit mod
                            OutA_MCPReg = OLATA                             'Sets up PORTA output
                            OutA_DataOut=OutA_lo                            'Sets up data to be pushed to the MCP23S17 as the low byte
                            gosub SEND_OutA_2317                            'Send it
                            pause 10                                        'Wait a bit
                            OutA_MCPReg = OLATB                             'Sets up PORTB output
                            OutA_DataOut=OutA_hi                            'Sets up data to be pushed to the MCP23S17 as the hi byte
                            gosub SEND_OutA_2317                            'Send it
    This gives me what I want - EXCEPT - for that I have to resolve 1 byte into a word and figure out how to get the pointer right........
    starting with a byte var which has a value from 0-15 to a specific bit on the MCP23S17 still evades me I am afraid.
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  5. #5
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: Port alias in Port Expanders (MCP23S17)

    Henrik-
    Thanks for the assist, but I don't think I can use that idea. There are some outputs which will be already active which another serial command comes in and I will need to add to the already ON LEDs with whatever new ones are called for.
    There is a serial comm line coming into the project and on that serial comm is a byte which will let me know which output(s) to turn on. Some may or may not be on.
    So, I will have to logically AND them to get the correct outputs on when they need to be.
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  6. #6
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default Re: Port alias in Port Expanders (MCP23S17)

    Why does this not work?
    OutA.12=0 doesn't gen an error but it nor the other (OutA.15=0) turns ON an LED?????

    Code:
                          OutA=$FFFE                                        'Initial value for word var                      
                          OutA_hi=OutA.highbyte                             'Splits into hibyte
                          OutA_lo=OutA.lowbyte                              'And into low byte of the word var
    '                      OutA_lo.3=0                                       'Bit mod - WORKS
    '                      OutA_hi.5=0                                       'Another bit mod - WORKS
                          OutA.12=0      'Why doesn't this work?
                          OutA.15=0      'Or this?
                            OutA_MCPReg = OLATA                             'Sets up PORTA output
                            OutA_DataOut=OutA_lo                            'Sets up data to be pushed to the MCP23S17 as the low byte
                            gosub SEND_OutA_2317                            'Send it
                            pause 10                                        'Wait a bit
                            OutA_MCPReg = OLATB                             'Sets up PORTB output
                            OutA_DataOut=OutA_hi                            'Sets up data to be pushed to the MCP23S17 as the hi byte
                            gosub SEND_OutA_2317                            'Send it
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: Port alias in Port Expanders (MCP23S17)

    OutA_hi=OutA.highbyte 'Splits into hibyte
    OutA_lo=OutA.lowbyte 'And into low byte of the word var
    is a simple assignment operation not an alias

    OutA=$FFFE 'Initial value for word var
    OutA.12=0
    OutA.15=0
    OutA_hi=OutA.highbyte 'Splits into hibyte
    OutA_lo=OutA.lowbyte 'And into low byte of the word var
    would fix it (get the order correct)

Similar Threads

  1. I2C question w/ MCP23017 Port Expanders
    By dsicon in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th December 2012, 05:47
  2. 18F2520 - problem configuring fuse for port B as digital port.
    By hwhisperer in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th October 2010, 11:41
  3. LCD R/S works on Port A but not Port B
    By TDonBass in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 10th February 2009, 12:41
  4. RB port sequencer
    By freelancebee in forum mel PIC BASIC
    Replies: 0
    Last Post: - 17th August 2005, 19:58
  5. Duplicating port input to port output
    By lwindridge in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 26th April 2004, 21:43

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