MCP23S17 init all outputs and to a logic 1


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151

    Default MCP23S17 init all outputs and to a logic 1

    Hello to All-
    I have been trying to figure out the best and/or fastest way to initialize a MCP23S17 (SPI) to all outputs with an active low.
    I init the devices by calling this routing (Thanks to the Forum)
    Code:
    INIT_OutA_2317:'MCP2317 used for A Outputs:
                 OutA_MCPReg = IOCON          'Configures the MCP23S17
                 OutA_DataOut = $20
                 GOSUB SEND_OutA_2317
                 OutA_MCPReg = IODIRA         'Configures PortA(1) All OUTPUTS
                 OutA_DataOut = $00
                 GOSUB SEND_OutA_2317
                 OutA_MCPReg = IODIRB         'Configures PortB(2) All OUTPUTs
                 OutA_DataOut = $00
                 GOSUB SEND_OutA_2317      
                 RETURN
    And the Send Out Sub:
    Code:
    SEND_OutA_2317:
                 CSA = 0                 'Enable the MCP23S17
                 SSP1BUF = MCPWRT         'Command to Write to the MCP23S17
                 while !PIR1.3 : wend
                 PIR1.3=0                                                                               'Reset the flag
                 SSP1CON1.7=0                                                                           'Clear collision bit
                 SSP1BUF = OutA_MCPReg         'Register to Write the Data
                 while !PIR1.3 : wend
                 PIR1.3=0                                                                               'Reset the flag
                 SSP1CON1.7=0                                                                           'Clear collision bit
                 SSP1BUF = OutA_DataOut        'Data to be sent
                 while !PIR1.3 : wend
                 PIR1.3 = 0                                                                             'Reset the flag
                 SSP1CON1.7=0                                                                           'Clear collision bit
                 CSA = 1                 'Disable the MCP23S17
                 RETURN
    And the set port sub:
    Code:
    SetPort_A:
                 OutA_hi=OutA.highbyte                             'Splits into hibyte
                 OutA_lo=OutA.lowbyte                              'And into low byte of the word var
                 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 3                                        '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
                 return
    And then do a for next loop:
    Code:
    for i=0 to 15 : OutA.0[i]=1 : gosub SetPort_A : next i
    Even working with a 18F6527 at 40 MHz, the thing comes up with all outputs active (active low is what I am looking to do) for a small time but fast enough to trigger all 16 outputs.
    I will try cutting some of the pauses but is there a better/faster way to do this?
    Any pointers would be helpful - as is always this forum......
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: MCP23S17 init all outputs and to a logic 1

    from data sheet


    Serial Data to Output Valid— — 450 ns
    CS Setup Time 50 nS


    a 3mS pause is way to long


    SetPort_A:
    OutA_hi=OutA.highbyte 'Splits into hibyte
    OutA_lo=OutA.lowbyte 'And into low byte of the word var
    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 3 'Wait a bit cannot see that this is necessary or even adds any value
    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
    return

    if you want speed why set only 1 bit at a time when they all are going to be set anyway

    for i=0 to 15 : OutA.0[i]=1 : gosub SetPort_A : next i
    Code:
    OutA=$ffff
    gosub SetPort_A
    would be 15 times faster

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


    Did you find this post helpful? Yes | No

    Default Re: MCP23S17 init all outputs and to a logic 1

    Thanks Richard!
    I get caught up thinking in terms of 8 bits that I forget that I can actually speak in words! Hah!
    I did miss the PAUSE statement and have changed it to PAUSEUS 10 and it seems happy.
    I used the OutA=$FFFF : gosub SetPort_A and it seems that it too fast for the outputs to actually toggle which is what I was driving for.
    Thanks again, good eye and quite helpful.
    Steve
    "If we knew what we were doing, it wouldn't be called research"
    - Albert Einstein

Similar Threads

  1. Need to flip order of bits MSB to LSB re: MCP23S17
    By Ryan7777 in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 30th May 2015, 01:42
  2. LCD init code ...safety
    By jorge in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 24th February 2011, 13:53
  3. init PORTD FOR 16F877A
    By coss_cat in forum General
    Replies: 3
    Last Post: - 19th June 2010, 17:16
  4. Init
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 21st April 2007, 01:43
  5. do i need to init the CG or DD in 4 bit lcd?
    By EDWARD in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th May 2005, 02:18

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