MCP23S17 - Three different approaches - ShiftIn/ShiftOut / MSSP+PBP / MSSP+ASM


Closed Thread
Results 1 to 19 of 19

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: MCP23S17 - Three different approaches - ShiftIn/ShiftOut / MSSP+PBP / MSSP+ASM

    Im having trouble getting this to work. my circuit is the same as Gadelhas has in his schematic, except im not grabbing any inputs with my circuit and i'm using a different PIC . The way I have it coded is so that I can use it with a 16F648A w/ 4Mhz internal OSC and I think it should light each led on the MCP23S17s PORTA one at a time before reseting at the end of the string and repeating, but its not working in that the leds never light. I have an LED on PORTB.4 as kind of a watch dog blinky and its behaving as I would expect. Any ideas on whats wrong with my version of Gadelhas' code or something else I'm missing?

    Code:
    '                              CONFIGS
    ' ================================================================
    @ DEVICE PIC16F648A, INTRC_OSC_NOCLKOUT, WDT_OFF, PWRT_OFF, MCLR_OFF, PROTECT_OFF, BOD_OFF, LVP_OFF
    
    '                          DEFINES & INCLUDES
    ' ================================================================
     DEFINE OSC 4
     include "modedefs.bas"
    '                REGISTERS AND PINOUT ( 1 = IN; 0 = OUT )
    ' ================================================================
               '76543210          
    CMCON = 7                              ' Turn off un-needed PIC hardware
    VRCON = 0                              '   ''             ''
    CCP1CON = 0                            '   ''             ''
    INTCON = %00000000                     '   ''             ''
    
     
    '                         ALIAS & MODIFIERS
    ' ================================================================
      nCS VAR PORTA.0 
     nRST VAR PORTB.5
     SCLK VAR PORTB.0
      SDO VAR PORTB.3
      
    '                        VARIABLES & COSTANTS
    ' ================================================================
     MCPWRT CON $40    'Command o write in MCP23S17 Address $00
     MCPRED CON $41    'Command o read from MCP23S17 Address $00
     IOCON  CON $0A    'Register address to MCP23S17 Config
     IODIRA CON $00    'Register address to MCP23S17 PortA(1)
     IODIRB CON $01    'Register address to MCP23S17 PortB(2)
     GPIOA  CON $12    'Register address to MCP23S17 GPIOA(1)
     GPIOB  CON $13    'Register address to MCP23S17 GPIOB(2)
     OLATA  CON $14    'Register address to MCP23S17 LATA(1)
     OLATB  CON $15    'Register address to MCP23S17 LATB(2)
      
     DataOut VAR BYTE  'Variable for Data Byte to Send to the MCP23S17
     DataIn  VAR BYTE  'Variable for Data Byte to Receive from the MCP23S17
     MCPReg  VAR BYTE  'Variable for the Register to Send to the MCP23S17
     
    '                           PROGRAM INIT
    ' ================================================================
    clear
    pause 100
    nrst = 1
    ncs = 1
    sclk = 0
    sdo = 0
    pause 100
        GOSUB INIT_MCP23S17    'Init the MCP23S17
    '                            MAIN LOOP
    ' ================================================================
    MAIN:
        MCPReg=OLATA             'This light up the leds, connected in
        DataOut=DataOut+1        'PortA(1) of the MCP23S17
        if dataout >=8 then
        dataout=0
        GOSUB SEND_MCP23S17
        gosub INIT_MCP23S17:
        endif
        GOSUB SEND_MCP23S17
        
    '    MCPReg = GPIOB           'This read the PortB(1) of the MCP23S17
    '   GOSUB RECEIVE_MCP23S17   'and show the result on the PORTB of the
    '   PORTB = DataIn           'PIC
        
        PAUSE 500
        if dataout = 0 then
        high PORTB.4
        pause 250
        low PORTB.4
        pause 250
        endif 
        high PORTB.4
        pause 500
        low PORTB.4
    GOTO MAIN
    '                              SUB - ROTINES
    ' ================================================================    
    INIT_MCP23S17:
        nRST = 0                'Reset the port expander
        PAUSE 1
        nRST = 1
        MCPReg = IOCON          'Configures the MCP23S17
        DataOut = $20
        GOSUB SEND_MCP23S17
        MCPReg = IODIRA         'Configures PortA(1) All Output
        DataOut = $00
        GOSUB SEND_MCP23S17
        
        MCPReg = IODIRB         'Configures PortB(2) All Input
        DataOut = $FF
        GOSUB SEND_MCP23S17      
    RETURN
    SEND_MCP23S17:
        nCS = 0                'Enable the MCP23S17
        SHIFTOUT SDO, SCLK, MSBFIRST, [MCPWRT,MCPReg,DataOut]        
        nCS = 1                'Disable the MCP23S17
    RETURN
    'RECEIVE_MCP23S17:
    '    nCS = 0                'Enable the MCP23S17
    '    SHIFTOUT SDO, SCLK, MSBFIRST, [MCPRED,MCPReg]        
    '   SHIFTIN SDI, SCLK, MSBPRE, [DataIn]    
    '    nCS = 1                'Disable the MCP23S17
    'RETURN
    END

  2. #2
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: MCP23S17 - Three different approaches - ShiftIn/ShiftOut / MSSP+PBP / MSSP+ASM

    Hi Ryan7777;

    Try to add this 2 lines after the INTCON configuration and see if it works.

    TRISA = %00000000
    TRISB = %00000000
    Thanks and Regards;
    Gadelhas

  3. #3
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: MCP23S17 - Three different approaches - ShiftIn/ShiftOut / MSSP+PBP / MSSP+ASM

    Hi Ryan7777;

    Have you allready tried my sugestion?

    I've tried and it worked, so you must add those to lines that i mention.
    Also remember that you are working with binary numbers, so this;

    Code:
        DataOut=DataOut+1        
        if dataout >=8 then
    will not light up all the leds.
    Thanks and Regards;
    Gadelhas

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


    Did you find this post helpful? Yes | No

    Default Re: MCP23S17 - Three different approaches - ShiftIn/ShiftOut / MSSP+PBP / MSSP+ASM

    Oh hey, Thanks! You're exactly right the set up of my ports was the snag. I was also confused with the dataOut= and the pins on the port expander being addressed as a binary number (1,2,4,8,16,32,64,128). My first go after after setting up TRIS, the port expander was counting up in binary and of course stopping because of the if dataOut >=8 then bit of code. I fixed all of that and tossed together some quick, sloppy code using a couple of FOR NEXT loops and SELECT CASE structure to get it to count up and down in proper fashion to scan the LEDs one at a time up PORTA and back down. So if I wanted to play "night rider", now I all I'd need is a black firebird Now I feel like a have a good grasp on how to talk to the MCP23S17 for the project I had originally intended it for. Thank you for the fast replies and for posting your tutorial on this part! I'll post my "blinky" code when I get back to my real PC.

    Thanks again!

    Ryan

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