MCP23017 i2c IO expander data

westaust55

Moderator
MCP23017 i2c IO expander data

Having used the MCP23017 IO expanders only as outputs to date, it was back to the 48 page datasheet to gather more data for input and interrupt purposes.
I took the opportunity to create (yet another :D) table

Attached is a one-page table listing all the MCP23017 registers, with notes on the functions and details on the each bit in control registers.

hope this proves to be a helpful chart for others as well.

EDIT: PLEASE SEE POST 7 FOR AN UPDATED VERSION
 

Attachments

Last edited:

alphamike27

New Member
You are a legend Westy.

I have just received some freebies from Microchip. These IC's are fantastic.

I am still coming to grips with the input interrupts, will post some results on this thread after I wade through it.
 

andrew_qld

Senior Member
I have used them as inputs before- I especially like the pull up resistors which means you don't need current limiting on the inputs like you do with a raw picaxe chip.

I have also used the surface mount versions with great success. I but them direct from microchip - they arrive in Australia in a few days from Thailand on Fedex.

I'm sure i have code somewhere, im in the process of moving but I'll see if i can find it...
 

westaust55

Moderator
MCP23017 - Demo using interrupt signals to read inputs

@alphamike27,

Here is some code that I have put together as a demo for a trial on monitoring for an interrupt signal with the PICAXE and then collecting the data (from portA) indicating which input was activated and flashing an output (on portB) to indicate the signal was handled.

Simulation looks okay but yet to wire up a circuit.
Will let you know how it all goes and then add a final working version to the code snippet area.

Code:
;======================================================================
; Demo Program to test MCP23017 inputs with interrupt
;======================================================================
;
#PICAXE 28X1 ; or 40X1 or an X2 device with HI2C.... commands for 18X change to i2cslave, i2c write and i2cread commands
;
; MCP23017 Device Address constant
SYMBOL Expand0 = %01000000  ; $0100 A2, A1, A0, R/W
;
; MCP23017 register address constants
SYMBOL IODIRA       = $00 ; Port A IO Direction register
SYMBOL IODIRB       = $01 ; Port B IO Direction register
SYMBOL IPOLA        = $02 ; Port A Input Polarity Register
SYMBOL IPOLB        = $03 ; Port B Input Polarity Register  - not used in this code example
SYMBOL GPINTENA = $04 ; Port A GPIO Interrupt on-change pins 
SYMBOL GPINTENB = $05 ; Port B GPIO Interrupt on-change pins - not used in this code example
SYMBOL DEFVALA   = $06 ; Port A Interrupt register default value
SYMBOL DEFVALB   = $07 ; Port A Interrupt register default value - not used in this code example
SYMBOL INTCONA   = $08 ; Port A Interrupt Control Register
SYMBOL INTCONB   = $09 ; Port B Interrupt Control Register - not used in this code example
SYMBOL IOCON       = $0A ; IO Expander config register  - address $0B accesses the same register
SYMBOL GPPUA      = $0C ; Port A GPIO Pull-up resistor register
SYMBOL GPPUB      = $0D ; Port B GPIO Pull-up resistor register - not used in this code example
SYMBOL INTFA        = $0E ; Port A Interrupt Flag Register
SYMBOL INTFB        = $0F ; Port B Interrupt Flag Register - not used in this code example
SYMBOL INTCAPA   = $10 ; Port A Interrupt Capture Register
SYMBOL INTACPB   =  $11 ; Port B Interrupt Capture Register - not used in this code example
SYMBOL GPIOA       = $12 ; Port A General purpose register
SYMBOL GPIOB      = $13 ; Port B General Purpose register
SYMBOL OLATA      = $14 ; Port A Output Latch register
SYMBOL OLATB      = $15 ; Port B Output Latch register
;
;
; Intialise
HI2CSETUP i2cmaster, Expand0, i2cfast, i2cbyte
;
; Control register setup:
; seq reg addressing, INT pins separate, Seq Op for addr auto inc, --, --, Active Int drive, Int Active low
HI2COUT  IOCON, ($00)
;
HI2COUT IODIRA, ($FF, $00)             ; set all port A pins as inputs (1) and all port B pins as outputs (0)
HI2COUT IPOLA, ($00)                      ; port A register bits have same logic state as the signal on physical pin 
HI2COUT GPPUA, ($FF)                   ; enable all port A 100 kOhm pull-up resistors
HI2COUT GPINTENA, ($FF)              ; enable all Port A pins for interrupt on change
HI2COUT DEFVALA, ($FF)               ; normal state is all port A pins is high -  a low causes an interrupt
;
; set up for Interrupts from MCP23017 on PICACE input 1
SETINT %00000010, %00000010      ; to interrupt on input1 high only
;  SETINT %00000000, %00000010  ; to interrupt on input1 low only
;
; Main program loop - displays a continual "Knight Rider" style shifting LED across the 8 outputs on port B
Main: 
           DO
               b3  =  1
               DO    ; LED dot pattern to move left
                  HI2COUT GPIOB, (b3)
                  b3 = b3 << 1 ; shift left 1 bit
                  PAUSE 500
           LOOP UNTIL b3 = 0

           b3  =  64
           DO  ; LED dot pattern to move right
              HI2COUT GPIOB, (b3)
              b3 = b3 >> 1 ; shift right 1 bit
              PAUSE 500
           LOOP UNTIL b3 = 1

          LOOP ;  loop forever waiting for an interrupt
;
; Interrupt Subroutine – called when MCP23017 input on Port A is pulled low
; Stops the "Knight Rider" effect and flashes the LED corresponding to the input pin on Port A that has been switched low.
Interrrupt:
           HI2CIN INTFA, (b0)                         ; fetch the IO Expander Flag register – set bit indicates pin causing interrupt
           HI2CIN INTCAPA, (b1)                    ; fetch the status for all port A pins at time of the interrupt
           FOR b2 = 1 to 5                             ; flash LED corresponding to the input causing interrupt 5 times  
              HI2COUT GPIOB, (b0)                 ; set LED ON
              PAUSE 300
              HI2COUT GPIOB, ($00)               ; all LEDs  OFF
              PAUSE 300
           NEXT b2
           HI2COUT GPIOB, (b3)                   ;  Show the original LED again
           SETINT %00000010, %00000010   ; turn the interrupt function back on
           RETURN
 

westaust55

Moderator
MCP23017 i2c IO exapander data - rev 1

AlphaMike27 picked up a few small typo’s in my MCP23017 Control Map table.

Attached is an updated version which also has the device address given at the top for quick reference
 

Attachments

zsotya

New Member
example

Hello!

I tried read my MCP2317 ports. (I2C Address is 000)
I checked the datasheet but i cant do it.
Can you write me an example, and explain the lines.

I was send the follow on the line:
I2C_START 400A00 I2C_STOP
I2C_START 400B00 I2C_STOP
I2C_START 400000 I2C_STOP
I2C_START 400200 I2C_STOP
I2C_START 4012 I2C_START 41 I2C_STOP

It is how i tried read the A port.
What i am doing wrong?

Please help!

Thanks
Zsolt




AlphaMike27 picked up a few small typo’s in my MCP23017 Control Map table.

Attached is an updated version which also has the device address given at the top for quick reference
 

westaust55

Moderator
@Zsolt,

Welcome to the PICAXE forum.

In looking at the lines you provide,
I was send the follow on the line:
I2C_START 400A00 I2C_STOP
I2C_START 400B00 I2C_STOP
I2C_START 400000 I2C_STOP
I2C_START 400200 I2C_STOP
I2C_START 4012 I2C_START 41 I2C_STOP
My first question becomes, is your program for a PICAXE micro-controller?

It appears that it is for a C Compiler such as that from Keil Software.

If it is intended for a PICAXE chip then please upload your entire program so that we (not just myself but all on this PICAXE forum ) can better understand what you are doing and help you.

If you are working with (for example) an ARM or 8051 processor, then I suggest you may do better to post on a forum that supports those chips.
eg: http://www.keil.com/forum/
 

craigcurtin

Senior Member
Thanks for the great info Westaus

I am looking for an IO port expansion option for a project i have. basically i need to drive 16 solid state opto isolated relays from a 40x1. These will need to be drive with upto 4 active at anyone time

Is this chip able to do this i.e. parallel output - from what i can see it appears that it can ?

The SSRs i am looking at will be driven by between 3-32v DV (and hence are OK for direct picaxe driving) have you had success driving multiple outputs at once with this ?

regards

Craig
 

westaust55

Moderator
MCP23017 IO expanders

@Craig,

Yes, you can set as many of the 16 IO pins on or off as you desire. They are controlled with two bytes and if you set the MCP23017 up the right way, those two registers are consecutive so one writei2c /hi2cout command and have both bytes of data in the one command.

I have used the MCP23017 to directly drive LED&#8217;s with one LED per IO pin and via transistors for up to 8 LED&#8217;s per pin.

You will need to consider the current rating of each MCP23017 IO pin and the total chip current rating.
Personally, I would still go for the transistor interface between the MCP23017 and relay coils.

Don&#8217;t forget the free wheeling/fly-back diodes across each relay coil.
 
Last edited:

craigcurtin

Senior Member
thanks Westy

Would i still need the external protection if i am using SSRs that are opto isolated ? I would have thought that would keep me pretty safe.

I only intend to drive a max of 4 of the relays at any one point in time so the current rating should be good.

thanks for all your valuable help

regards

Craig
 

westaust55

Moderator
SSR should be okay in terms of not requiring free-wheeling doides.
You still need to look at the datasheet to ascertain current required out of the MCP23017 to "drive" the SSR at 5Vdc.
 

westaust55

Moderator
yes, keeping in mid that the MCP23017 current rating you quote are the absolute maximums, there is still ample "head room" when using a max of 4 SSR's.

With respect to the SSR's the datasheets states:
Load can be resitance type.
:
:
Load could not be DC type. Load could not be inductive type.
:
:
without heatsink, the maximum continuous load current could never be over 3 Amps.
:
:
If the load is inductive type the rated current should be multipled by 0.3 to 0.7 this factor equal to loads power factor.
So your AC load rating capability for those SSR's could be as low as 1 Amp.

Trust that all fits in with your plans.
 

SAborn

Senior Member
This is the sort of thing (the chart) that should be in a data section on the forum that is easy to find, and not lost in the depths of the forum.

Yes i know we can run a search..........."IF"... we know what we are looking for.

I know this has come up recently about having a new section and it got squashed, but there should be a reserved section for approved information.
 

craigcurtin

Senior Member
Yep i think i am good there

yes, keeping in mid that the MCP23017 current rating you quote are the absolute maximums, there is still ample "head room" when using a max of 4 SSR's.

With respect to the SSR's the datasheets states:


So your AC load rating capability for those SSR's could be as low as 1 Amp.

Trust that all fits in with your plans.
Each of the relays will driving a SMPS with Active PFC - apparently 97% or (converted server based power supplies) - they have bigger versions (upto 80amps) in their SSR range so may move to those as these SMPS can pull 5 amps (1200watts)

DO you have any basic setup code for using the MCP23017 - i know you have good coding standards - so may as well start with something tidy before i butcher ?

regards

Craig
 

westaust55

Moderator
DO you have any basic setup code for using the MCP23017 - i know you have good coding standards - so may as well start with something tidy before i butcher ?

regards

Craig
Yes, and I have posted many examples including post 6 in this thread.

Time for you to do an advanced forum search for MCP23017 with myself and even BCJKiwi as the Authors. Use the Search buitton in orange bar at top of this page

I have previously posted projects using MCP23017's which include:
- 8x8 RGB LED matrix drive using 2 MCP23017
- Compass indicator with 16 LEDs and 32 steps​
 

craigcurtin

Senior Member
Thanks Westy

Yep I found the one on the 8x8 very early this morning.

I will peruse the code and see how i go

thanks for all the help

Craig
 

zsotya

New Member
Hello,

I dont mean exact programming language, i just need let say "Algorithm for this".
What i have to do to set the A ports to input, and after read the status of the ports?

Thanks

Zsotya

@Zsolt,

Welcome to the PICAXE forum.

In looking at the lines you provide,


My first question becomes, is your program for a PICAXE micro-controller?

It appears that it is for a C Compiler such as that from Keil Software.

If it is intended for a PICAXE chip then please upload your entire program so that we (not just myself but all on this PICAXE forum ) can better understand what you are doing and help you.

If you are working with (for example) an ARM or 8051 processor, then I suggest you may do better to post on a forum that supports those chips.
eg: http://www.keil.com/forum/
 

bpowell

Senior Member
Question: Can the 14M2 be used with the MCP23027?

Question for you: Can the 14M2 support I2C communication?

the MCP23027 uses I2C communication...it doesn't care who (or what) is talking to it...as long as it's talking the correct language.
 

CDRIVE

Senior Member
Well according to the Basic Commands PDF all M2 & X2 parts will support I2C protocol. I guess I was asking if there were any known caveats.

Chris
 

bpowell

Senior Member
Nope...the MCP23017 is an I/O chip that stands on its own...you just need to tell it what to do...any chip that can talk I2C can can do it. I use a 08M2 to talk to 2 of these for my binary clock...works a treat.
 

darb1972

Senior Member
This is the sort of thing (the chart) that should be in a data section on the forum that is easy to find, and not lost in the depths of the forum.

Yes i know we can run a search..........."IF"... we know what we are looking for.

I know this has come up recently about having a new section and it got squashed, but there should be a reserved section for approved information.
I know. Old thread. Not sure if this ever happened (can't recall seeing it), but a huge plus 1 for the request from SAborn.

Great idea. Definitely ensures that golden nuggets like this one don't end up buried in a sea of endless posts.
 
Top