more i2c and mcp23017


+ Reply to Thread
Results 1 to 27 of 27

Hybrid View

  1. #1
    Join Date
    Aug 2011
    Posts
    453


    Did you find this post helpful? Yes | No

    Default Re: more i2c and mcp23017

    For the MCP23017 28-pin DIP package make sure you have connections to:
    RESETn (pin 18)
    A2, A1, A0 (pins 17, 16, 15) - these should be pulled low (GND) for address $40... they should not be left floating

    There should also be pullups to VDD on SCL (pin 12) and SDA (pin 13)

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,159


    Did you find this post helpful? Yes | No

    Default Re: more i2c and mcp23017

    Quote Originally Posted by tumbleweed View Post
    For the MCP23017 28-pin DIP package make sure you have connections to:
    RESETn (pin 18)
    A2, A1, A0 (pins 17, 16, 15) - these should be pulled low (GND) for address $40... they should not be left floating

    There should also be pullups to VDD on SCL (pin 12) and SDA (pin 13)
    Yup, reset, a0, a1 and a2 pulled down.

    I had sda and scl pulled up as in the datasheet, even tried 4K7 like is commonly used, nothing worked
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,159


    Did you find this post helpful? Yes | No

    Default Re: more i2c and mcp23017

    Streamlined code even more:

    Code:
    #CONFIG
        __config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
        __config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
        __config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
        __config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
        __config _CONFIG5, _CP_OFF & _CPD_OFF
    #ENDCONFIG
    
    DEFINE OSC 32
    
    define  CCP1_REG     0              ' Must clear unused CCP pins or else unpredictable results
    DEFINE  CCP1_BIT     0
    define  CCP2_REG     0
    DEFINE  CCP2_BIT     0
    DEFINE  CCP3_REG    PORTB
    DEFINE  CCP3_BIT     5
    define  CCP4_REG     0
    DEFINE  CCP4_BIT     0
    define  CCP5_REG     0
    DEFINE  CCP5_BIT     0
    
    ANSELA = %00000000
    ANSELB = %00000000
    ANSELC = %00000000
    ANSELD = %00000000
    ANSELE = %00000000
    
    TRISA = %00000000
    TRISB = %00000000
    TRISC = %00000000
    TRISD = %00000000
    TRISE = %00001000
    
    PinSCL2     VAR PortB.1
    PinSDA2     VAR PortB.2
    
        HPWM 3, 8, 1953
    
    addr        var byte : addr      = %01000000        ' $40
    
    IOCONAreg    var byte
    IOCONBreg    var byte
    IODIRAreg    var byte
    IODIRBreg    var byte
    OLATAreg     var byte
    OLATBreg     var byte
    
    IOCONdat     var byte
    IODIRdat     var byte
    OLATdat      var byte
    
    IOCONdat  = %00110000
    '   bit 7   0 = The registers associated with each port are separated into different banks.
    '   bit 6   0 = The INT pins are not connected. INTA is associated with PORTA and INTB is associated with PORTB
    '   bit 5   1 = Sequential operation disabled, address pointer does not increment.
    '   bit 4   1 = Slew rate disabled
    '   bit 3   0 = Disables the MCP23S17 address pins.
    '   bit 2   0 = Active driver output (INTPOL bit sets the polarity.)
    '   bit 1   0 = Active-low
    '   bit 0   Unimplemented: Read as ‘0’
    
        if IOCONdat.7 = 0 then      ' Bank=0
            IOCONAreg  = $0A
            IOCONBreg  = $0B
            IODIRAreg  = $00
            IODIRBreg  = $01
            OLATAreg   = $14
            OLATBreg   = $15
        else                        ' Bank=1
            IOCONAreg  = $05        
            IOCONBreg  = $15
            IODIRAreg  = $00 
            IODIRBreg  = $10
            OLATAreg   = $0A
            OLATBreg   = $1A
        endif
    
    IODIRdat = %00000000                                   'set GPIO all output
    
        I2CWRITE PinSDA2,PinSCL2,addr,IOCONAreg,[IOCONdat]
        I2CWRITE PinSDA2,PinSCL2,addr,IODIRAreg,[IODIRdat]
    
        I2CWRITE PinSDA2,PinSCL2,addr,IOCONBreg,[IOCONdat]
        I2CWRITE PinSDA2,PinSCL2,addr,IODIRBreg,[IODIRdat]
    
    LOOOP:
        OLATdat    = %00000000                             ' LEDs ON 
        I2CWRITE PinSDA2,PinSCL2,addr,OLATAreg,[OLATdat]
    
        I2CWRITE PinSDA2,PinSCL2,addr,OLATBreg,[OLATdat]
    
        pause 200
    
        OLATdat    = %11111111                             ' LEDs OFF 
        I2CWRITE PinSDA2,PinSCL2,addr,OLATAreg,[OLATdat]
    
        I2CWRITE PinSDA2,PinSCL2,addr,OLATBreg,[OLATdat]
    
        pause 200
    
        GOTO LOOOP
    end
    No longer need to mess with comments to change BANK setting. I just flip bit in IOCONdat and IF takes care of the rest.

    Quadruple checked everything, still no blinky blink.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,159


    Did you find this post helpful? Yes | No

    Default Re: more i2c and mcp23017

    Changed to GPIO instead of LAT as per Dave's suggestion here:

    https://www.picbasic.co.uk/forum/sho...6693#post96693

    Also played with the syntax (register now part of the list of data).

    Code:
    #CONFIG
        __config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
        __config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
        __config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
        __config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
        __config _CONFIG5, _CP_OFF & _CPD_OFF
    #ENDCONFIG
    
    DEFINE OSC 32
    
    define  CCP1_REG     0              ' Must clear unused CCP pins or else unpredictable results
    DEFINE  CCP1_BIT     0
    define  CCP2_REG     0
    DEFINE  CCP2_BIT     0
    DEFINE  CCP3_REG    PORTB
    DEFINE  CCP3_BIT     5
    define  CCP4_REG     0
    DEFINE  CCP4_BIT     0
    define  CCP5_REG     0
    DEFINE  CCP5_BIT     0
    
    ANSELA = %00000000
    ANSELB = %00000000
    ANSELC = %00000000
    ANSELD = %00000000
    ANSELE = %00000000
    
    TRISA = %00000000
    TRISB = %00000000
    TRISC = %00000000
    TRISD = %00000000
    TRISE = %00001000
    
    PinSCL2     VAR PortB.1
    PinSDA2     VAR PortB.2
    
        HPWM 3, 8, 1953
    
    addr        var byte : addr      = %01000000        ' $40
    
    IOCONAreg    var byte
    IOCONBreg    var byte
    IODIRAreg    var byte
    IODIRBreg    var byte
    GPIOAreg     var byte
    GPIOBreg     var byte
    
    IOCONdat     var byte
    IODIRdat     var byte
    GPIOdat      var byte
    
    IOCONdat  = %10110000
    '   bit 7   0 = The registers associated with each port are separated into different banks.
    '   bit 6   0 = The INT pins are not connected. INTA is associated with PORTA and INTB is associated with PORTB
    '   bit 5   1 = Sequential operation disabled, address pointer does not increment.
    '   bit 4   1 = Slew rate disabled
    '   bit 3   0 = Disables the MCP23S17 address pins.
    '   bit 2   0 = Active driver output (INTPOL bit sets the polarity.)
    '   bit 1   0 = Active-low
    '   bit 0   Unimplemented: Read as ‘0’
    
        if IOCONdat.7 = 0 then      ' Bank=0
            IOCONAreg  = $0A
            IOCONBreg  = $0B
            IODIRAreg  = $00
            IODIRBreg  = $01
            GPIOAreg   = $12
            GPIOBreg   = $13
        else                        ' Bank=1
            IOCONAreg  = $05        
            IOCONBreg  = $15
            IODIRAreg  = $00 
            IODIRBreg  = $10
            GPIOAreg   = $09
            GPIOBreg   = $19
        endif
    
    IODIRdat = %00000000                                   'set GPIO all output
    
        I2CWRITE PinSDA2,PinSCL2,addr,[IOCONAreg,IOCONdat]
        I2CWRITE PinSDA2,PinSCL2,addr,[IODIRAreg,IODIRdat]
    
        I2CWRITE PinSDA2,PinSCL2,addr,[IOCONBreg,IOCONdat]
        I2CWRITE PinSDA2,PinSCL2,addr,[IODIRBreg,IODIRdat]
    
    LOOOP:
        GPIOdat    = %00000000                             ' LEDs ON 
        I2CWRITE PinSDA2,PinSCL2,addr,[GPIOAreg,GPIOdat]
    
        I2CWRITE PinSDA2,PinSCL2,addr,[GPIOBreg,GPIOdat]
    
        pause 200
    
        GPIOdat    = %11111111                             ' LEDs OFF 
        I2CWRITE PinSDA2,PinSCL2,addr,[GPIOAreg,GPIOdat]
    
        I2CWRITE PinSDA2,PinSCL2,addr,[GPIOBreg,GPIOdat]
    
        pause 200
    
        GOTO LOOOP
    end

    Tried with both Bank settings, still no good.

    I'm quickly running out of crazy ideas.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,159


    Did you find this post helpful? Yes | No

    Default Re: more i2c and mcp23017

    Logic Probe readings of SCL/SDA lines.

    Code:
    #CONFIG
        __config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
        __config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
        __config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
        __config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
        __config _CONFIG5, _CP_OFF & _CPD_OFF
    #ENDCONFIG
    
    DEFINE OSC 32
    
    define  CCP1_REG     0              ' Must clear unused CCP pins or else unpredictable results
    DEFINE  CCP1_BIT     0
    define  CCP2_REG     0
    DEFINE  CCP2_BIT     0
    DEFINE  CCP3_REG    PORTB
    DEFINE  CCP3_BIT     5
    define  CCP4_REG     0
    DEFINE  CCP4_BIT     0
    define  CCP5_REG     0
    DEFINE  CCP5_BIT     0
    
    ANSELA = %00000000
    ANSELB = %00000000
    ANSELC = %00000000
    ANSELD = %00000000
    ANSELE = %00000000
    
    TRISA = %00000000
    TRISB = %00000000
    TRISC = %00000000
    TRISD = %00000000
    TRISE = %00001000
    
    PinSCL2     VAR PortB.1
    PinSDA2     VAR PortB.2
    
        HPWM 3, 8, 1953
    
    addr        var byte : addr      = %01000000        ' $40
    
    IOCONAreg    var byte
    IOCONBreg    var byte
    IODIRAreg    var byte
    IODIRBreg    var byte
    GPIOAreg     var byte
    GPIOBreg     var byte
    
    IOCONdat     var byte
    IODIRdat     var byte
    GPIOdat      var byte
    
    IOCONdat  = %10110000
    '   bit 7   0 = The registers associated with each port are separated into different banks.
    '   bit 6   0 = The INT pins are not connected. INTA is associated with PORTA and INTB is associated with PORTB
    '   bit 5   1 = Sequential operation disabled, address pointer does not increment.
    '   bit 4   1 = Slew rate disabled
    '   bit 3   0 = Disables the MCP23S17 address pins.
    '   bit 2   0 = Active driver output (INTPOL bit sets the polarity.)
    '   bit 1   0 = Active-low
    '   bit 0   Unimplemented: Read as ‘0’
    
        if IOCONdat.7 = 0 then      ' Bank=0
            IOCONAreg  = $0A
            IOCONBreg  = $0B
            IODIRAreg  = $00
            IODIRBreg  = $01
            GPIOAreg   = $12
            GPIOBreg   = $13
        else                        ' Bank=1
            IOCONAreg  = $05        
            IOCONBreg  = $15
            IODIRAreg  = $00 
            IODIRBreg  = $10
            GPIOAreg   = $09
            GPIOBreg   = $19
        endif
    
    IODIRdat = %00000000                                   'set GPIO all output
                           ' Image 1
        I2CWRITE PinSDA2,PinSCL2,addr,[IOCONAreg,IOCONdat]
        I2CWRITE PinSDA2,PinSCL2,addr,[IODIRAreg,IODIRdat]
    
        pause 50
                           ' Image 2
        I2CWRITE PinSDA2,PinSCL2,addr,[IOCONBreg,IOCONdat]
        I2CWRITE PinSDA2,PinSCL2,addr,[IODIRBreg,IODIRdat]
    
        pause 100
    
    LOOOP:                 ' Image 3
        GPIOdat    = %00000000                             ' LEDs OFF 
        I2CWRITE PinSDA2,PinSCL2,addr,[GPIOAreg,GPIOdat]
        I2CWRITE PinSDA2,PinSCL2,addr,[GPIOBreg,GPIOdat]
    
        pause 200
                           ' Image 4
        GPIOdat    = %11111111                             ' LEDs ON 
        I2CWRITE PinSDA2,PinSCL2,addr,[GPIOAreg,GPIOdat]
        I2CWRITE PinSDA2,PinSCL2,addr,[GPIOBreg,GPIOdat]
    
        pause 400
    
        GOTO LOOOP
    end

    Image 1:

    Name:  I2C IOCONa.png
Views: 808
Size:  23.3 KB


    Image 2:

    Name:  I2C IOCONb.png
Views: 770
Size:  18.3 KB

    (next 2 images in next post)

    Weird how 2 statements generate 3 groups of pulses (I expected 1 to 1 ratio).
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,159


    Did you find this post helpful? Yes | No

    Default Re: more i2c and mcp23017

    Image 3:

    Name:  I2C LEDoff.png
Views: 819
Size:  14.6 KB


    Image 4:

    Name:  I2C LEDon.png
Views: 783
Size:  15.5 KB
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  7. #7
    Join Date
    Aug 2011
    Posts
    453


    Did you find this post helpful? Yes | No

    Default Re: more i2c and mcp23017

    Yup, reset, a0, a1 and a2 pulled down.
    Typo? RESETn is a low-active input so it wants a pullup, not pulldown...

    The traces show you're getting a NAK to the first byte (chip address), so that needs to get fixed before anything else.

Similar Threads

  1. need help with PBP, I2C and MCP23017
    By queenidog in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 20th May 2018, 00:01
  2. I2C two master on one I2C display
    By Kmt in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 22nd September 2014, 20:44
  3. I2C question w/ MCP23017 Port Expanders
    By dsicon in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th December 2012, 05:47
  4. PIC 18F4550 and MCP23017
    By DaveC3 in forum Code Examples
    Replies: 12
    Last Post: - 4th December 2010, 14:01
  5. I2C, little help please
    By gluphus in forum Serial
    Replies: 2
    Last Post: - 15th March 2006, 04:32

Members who have read this thread : 11

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