News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

MCP23017 16 bit expander

Started by chris_cb_uk, Nov 03, 2021, 05:57 PM

Previous topic - Next topic

chris_cb_uk

Hi All, I struggle sometimes reading I2C datasheets correctly.  I have the 23017 working somewhat outputting, however it is not as I expected in terms of referring to a GPIO v's latch output.  Additionally I'm having issues reading in a value.

Details thus far for the outputting to the expander and it just flashes all outputs on and off alternative from A to B using the latch functions. However when I tried to address using the actual GPIOA/B using I2COut SDApin,SCLpin, expanderwrite,$09,[%11111111] the outputs do not change.
  Device = 18F4550
    Declare Xtal = 16   
    Declare LCD_DTPin = PORTB.4
    Declare LCD_RSPin = PORTB.2
    Declare LCD_ENPin = PORTB.3
    Declare LCD_Type 0
    Declare LCD_Interface 4
    Declare LCD_Lines 2
    Declare I2C_Bus_SCL On
   
   
Symbol SDApin = PORTB.0 ' Alias the SDA (Data) line
Symbol SCLpin = PORTB.1 ' Alias the SSL (Clock) line
Symbol expanderwrite = %01000000
Symbol expanderread = %01000001

I2COut SDApin,SCLpin, expanderwrite,$05,[%10111000] 'Set configuration a
I2COut SDApin,SCLpin, expanderwrite,$15,[%10111000]  'Set configuration b
I2COut SDApin,SCLpin, expanderwrite,$00, [%00000000] 'makes all gpio port A output
I2COut SDApin,SCLpin, expanderwrite,$01, [%00000000] 'makes all gpio port B output


start:

I2COut SDApin,SCLpin, expanderwrite,$0A,[%11111111]' sets all gpio A high
I2COut SDApin,SCLpin, expanderwrite,$1A,[%00000000]   'sets all gpio B Low
DelayMS 800

I2COut SDApin,SCLpin, expanderwrite,$0A,[%00000000]  'sets all gpio A Low
I2COut SDApin,SCLpin, expanderwrite,$1A,[%11111111] ' sets all gpio B high
DelayMS 900
GoTo start

Additionally when I am trying to read the port values into the pic to display simply as a binary figure on the LCD nothing happens.

       Device = 18F4550
    Declare Xtal = 16
    Declare LCD_DTPin = PORTB.4
    Declare LCD_RSPin = PORTB.2
    Declare LCD_ENPin = PORTB.3
    Declare LCD_Type 0
    Declare LCD_Interface 4
    Declare LCD_Lines 2
    Declare I2C_Bus_SCL On
   
Symbol SDApin = PORTB.0 ' Alias the SDA (Data) line
Symbol SCLpin = PORTB.1 ' Alias the SSL (Clock) line
Symbol expanderwrite = %01000000
Symbol expanderread = %01000001
Dim ouporta As Word
Dim ouportb As Word
I2COut SDApin,SCLpin, expanderwrite,$05,[%10111000] 'Set configuration a
I2COut SDApin,SCLpin, expanderwrite,$15,[%10111000] 'Set configuration b
I2COut SDApin,SCLpin, expanderwrite,$06,[%11111111] 'resistor pullup port a
I2COut SDApin,SCLpin, expanderwrite,$16,[%11111111] 'resistor pullup port b

I2COut SDApin,SCLpin, expanderwrite,$00, [%11111111] 'makes all gpio port A output
I2COut SDApin,SCLpin, expanderwrite,$01, [%11111111] 'makes all gpio port B output
 DelayMS 100
start:

I2CIn SDApin,SCLpin, expanderread,$09,[ouporta]
DelayMS 10
I2CIn SDApin,SCLpin, expanderread,$1A,[ouportb]
Print At 1,1, Bin8 ouporta
Print At 2,1, Bin8 ouportb
DelayMS 300
GoTo start

I'm aware it's probably something very minor but I've been having issues for a few hours going back and fourth checking the datasheet.

Any pointers would be well received.

Chris

tumbleweed

It looks like you're writing to the wrong registers.

The 23017 powers up with IOCON.BANK = 0, so that means the default register map is the one shown in tables 3-3 and 3-5

chris_cb_uk

Thanks Tumbleweed, I thought I had set the iocon register $05 and $15 set in binary for my ease of understanding with reference to table reference 3-6 (page21)?

chris_cb_uk

Oh am I right in reading table 3-5 that as the device boots into IOCON. Bank=0 I need to address the IOCON at $0A and $0B?

tumbleweed

Quote from: chris_cb_uk on Nov 04, 2021, 07:43 AMOh am I right in reading table 3-5 that as the device boots into IOCON. Bank=0 I need to address the IOCON at $0A and $0B?
Right. Since it powers up with IOCON.BANK=0 initially the register offsets in table 3-5 apply.
You only need to program IOCON once, so pick either $0A or $0B.

If you set IOCON.BANK=1 then the other register map will apply.


chris_cb_uk

Works perfectly now and running as 8 in and 8 output expander.  A very nice simple solution to port expansion in an I2C line.