Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 13:31 27 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : mcp23017 i2c port expander

Author Message
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 09:59pm 23 Oct 2023
Copy link to clipboard 
Print this post

I want to make a board with 8 buttons and use mcp 23017 i2c port expander.
Even games an i2c button would be fast enough.
I forgot I had it, wired to range sensor and ssd1306.. same i2c pins. That's cool.
Would be useful for buttons but looking at the data sheet and I'm lost.
I have Lizby's code for output but don't know how to set to input and pull up settings etc.
Is there a thread about this device please? stan

'co @lizby
'OPTION EXPLICIT
'OPTION DEFAULT NONE
' MCP23017.bas I2C test

const mcp23017 = &h20 ' A2, A1, A0, R/W all connected to 0V
Const i2caddr=mcp23017
const IODIRA = &h00 ' Port A IO Direction register DEFAULT = I/P
const IODIRB = &h01 ' Port B IO Direction register DEFAULT = I/P
const IOCON = &h0A ' IO Expander config register - address &h0B accesses  same register
const GPIOA = &h12 ' Port A General purpose register
const GPIOB = &h13 ' Port B General Purpose register
const OLATA = &h14 ' Port A latch register
const OLATB = &h15 ' Port B latch register
const GPUPA = &h0C ' Port C pull-up register
const GPUPB = &h0D ' Port B pull-up register
I2C WRITE MCP23017,0,2,IODIRA,0 ' set direction to output
I2C WRITE MCP23017,0,2,IODIRB,0 ' set direction to output
do
mcp17
loop
sub mcp17
for i = 1 to 6
  I2C Write MCP23017,0,2,OLATA,&b10101010 '  
  I2C Write MCP23017,0,2,OLATB,&b01010101 '  
  PAUSE     1000
  I2C Write MCP23017,0,2,OLATA,&b01010101 '  
  I2C Write MCP23017,0,2,OLATB,&b10101010 '  
  PAUSE     1000
Next i
I2C Write MCP23017,0,2,OLATA,0 ' turn all off
I2C Write MCP23017,0,2,OLATB,0 ' turn all off
pause 1000
end sub



 
Andy-g0poy
Regular Member

Joined: 07/03/2023
Location: United Kingdom
Posts: 59
Posted: 11:18pm 23 Oct 2023
Copy link to clipboard 
Print this post

It's a fairly complex chip, but this sort of arrangement has been used for decades.

You have 16 i/o pins

Each i/o pin has a bit in a number of control registers

IODIRA = &h00 ' Port A
IODIRB = &h01 ' Port B

they call these "IO Direction register"

(We used to call then Data Direction Registers "DDR")

The &h00 and &h01 are the register addresses.

So taking port A as an example.

IODIRA bit 0 --> portA 0
IODIRA bit 1 --> portA 1
IODIRA bit 2 --> portA 2
IODIRA bit 3 --> portA 3
IODIRA bit 4 --> portA 4
IODIRA bit 5 --> portA 5
IODIRA bit 6 --> portA 6
IODIRA bit 7 --> portA 7


If you want to set a pin as an output then you need to set the corresponding bit in the IODIRA register to zero

So if you wanted port A pins 0,1 and 2 as outputs, and the rest as inputs your would write

&b11111000 (or &hF8)  to IODIRA

(I always prefer to use the binary setup as it's easier to see what pins are in and out)

The port B I/O is controlled by IODIRB in the same way.

Likewise GPUPA and GPUGB control the pull up resistor.

If you set bit 2 in GPUPA to 1 then the pull up will be enabled on port A 2

Reading the port is simply done by reading GPIOA and GPIOB

Setting an output pin to 1 or 0 is a little more obscure. If you write to the correct bit in GPIOA or GPIOB then this sets the corresponding bit in latch OLATA or OLATB

When you write the OLATA or OLATB then the value is transferred to the I/O pins.

This allows you to set a number of pins all at once, rather that set each bit one at a time.

There is a lot more you can do with this such as interrupts and so on, but I hope this is enough to get you started.

Andy
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 01:10am 24 Oct 2023
Copy link to clipboard 
Print this post

  stanleyella said  don't know how to set to input and pull up settings etc.


Same port definitions as for writing. Then, at some point (for port B)

I2C WRITE MCP23017,0,2,GPUPB,&h3f ' set weak pullups on all except bits 6&7

then when you read:

   I2C WRITE MCP23017,0,1,GPIOB ' set the register to read
   I2C READ MCP23017,0,1,inputs ' read the B port

the integer "inputs" will contain the 8 bits of values.

I don't know whether there's a thread where this is done--you'd have to search on MCP23017.

This wasn't done on a Pico, and it was over 4 years ago, so I don't remember anything about it--I'm just copying code here and can't even affirm that it was even working code.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 07:03am 24 Oct 2023
Copy link to clipboard 
Print this post

The Wii uses I2C and that's plenty fast enough. :)

I've experimented with using a PicoMite as a I2C expander, giving both digital and analogue values. That was interesting. I've not used the MCP23xx or MCP23xxx chips yet though. MMBasic's support for I2C is very good.
Edited 2023-10-24 17:04 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 01:41pm 24 Oct 2023
Copy link to clipboard 
Print this post

Thanks Andy and all for replying. I got enough to work with and sorry I don't understand data sheets but a code example explains everything.
I found another 23017 board from mega 328 and gcbasic code. Used with uln2003 dual stepper motor driver but output only. I used 2 pullup resistors but only needed for i2c leads longer than a foot.
I2c is nice, like I'm using 3 devices on the same 2 pins.
A strip board with a 16 port expander and buttons that I could connect with 4 wire ribbon cable when needed would have been handy so I'm making one.
If I can get weak pull ups to work then is switch from expander pin to ground safe please? stan

 
Andy-g0poy
Regular Member

Joined: 07/03/2023
Location: United Kingdom
Posts: 59
Posted: 05:18pm 24 Oct 2023
Copy link to clipboard 
Print this post

Switch to ground is fine. That's really what the pull up is intended to work with.

Weak pull-ups are a bit of a new thing (only appeared 20 ish years or so ago)   Intended to be able to be overcome if needed. If you have the option you can use no pull ups on the chip settings and use real resistors to provide the necessary pull up on the board.

The weak pull-ups usually work fine, and save a bit of work.

Do persevere with the data sheets, that one is pretty good as such things go. once you get used to the format things become a lot clearer. Often far better that trying to understand someone elses code! :-)

One thing that it REALLY useful is to make up a simple 8 LED board that you can plug into a breadboard - nothing like switching on a few LEDS to get the hang of a new I/O chip.


Andy
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 05:40pm 24 Oct 2023
Copy link to clipboard 
Print this post

Thank you Andy, saves using pullup resistors. Microchip, ili lcd,v53l0x data sheets ,I know it's all there but I'm too thick :(
leds no. My 3 soldered board got displays and a terminal could show the whole port as bin I hope. I'll use the i2c bread board to test and display. stan
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 06:58pm 24 Oct 2023
Copy link to clipboard 
Print this post

If you would like one of my PicX experimenter's PCBs, Stan, send me a PM and we can work something out. Details are in the link in my sig. You might find one useful. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 08:00pm 24 Oct 2023
Copy link to clipboard 
Print this post

  stanleyella said  A strip board with a 16 port expander and buttons that I could connect with 4 wire ribbon cable when needed would have been handy so I'm making one.


These 23017 modules are handy and cheap (no buttons, though):


PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 02:31pm 26 Oct 2023
Copy link to clipboard 
Print this post

Thanks lizby. Turns out I bought 5, I found the tube with 2 in. The other is on breadboard, the other in a robot and the other on stripboard ready to replace the one in the robot with dual uln2003 not the boards with leds that come with the motors.
I am rediscovering the devices from only using them with 8bit.
stan
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 04:01pm 26 Oct 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  If you would like one of my PicX experimenter's PCBs, Stan, send me a PM and we can work something out. Details are in the link in my sig. You might find one useful. :)

That's kind of you to offer Mick but I'm a "vero" guy. stripboard is easy to change and  as a hobbyist it's ephemeral. I don't make permanent circuits
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 06:03pm 26 Oct 2023
Copy link to clipboard 
Print this post

PicX isn't suitable for permanent circuits. It's strictly for experimenting. It's simply a collection of common hardware bits that you can jumper together to configure it to be what you need at the time - a glorified breadboard. The two little breadboards let you add extra bits like an audio DAC or PWM filter. They don't need to be big as most of the stuff you would normally add as breakout boards is already taken care of, including power rails, a couple of buttons and LEDs.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024