Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:00 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 port expander again

     Page 1 of 2    
Author Message
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 07:01pm 29 Nov 2023
Copy link to clipboard 
Print this post

Hi, I'm trying to use mcp23017 dual expander to work as input port for 8 button pad.
I've had lots of tips from this forum but I can't figure how to set/read a port.
I found picaxe basic and got gcbasic examples but not mmbasic example.
I am trying. My code is display ssd1306 and just prints test.
The data sheet is no help to me. the non picomite mmbasic stuff didn't work.
Any read input for this welcome and maybe others finding it difficult please.


option autorun on
OPTION EXPLICIT
OPTION DEFAULT NONE

dim portabits$

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,255 ' set direction to input
I2C WRITE MCP23017,0,2,IODIRB,0 ' set direction to output
I2C WRITE MCP23017,0,2,GPUPA,255 'set weak pull up for port
'
Font 2
'Pause 200
CLS
'
Do
'I2C read MCP23017,0,2,GPIOA,portabits$
I2C read GPIOA,0,1,portabits$ 'THIS DON'T WORK
text 0,20,portabits$
text 0,0,"test"
Pause 100
Loop
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6099
Posted: 08:20pm 29 Nov 2023
Copy link to clipboard 
Print this post

Try

dim integer portabits



 Do
   I2C write MCP23017,0,1,GPIOA
   I2C read MCP23017,0,1,portabits
   text 0,20,bin$(portabits,8)
   text 0,0,"test"
   Pause 100
 Loop


Please, don't use OPTION AUTORUN until you have your program working. It will only cause grief.

Edit: corrected error in example code.
Edited 2023-11-30 06:56 by TassyJim
VK7JH
MMedit   MMBasic Help
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 864
Posted: 08:56pm 29 Nov 2023
Copy link to clipboard 
Print this post

How recent is your chip?

I read that the newer devices lost the input capability on some ports(?)

Hearsay for me so please research.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6797
Posted: 09:20pm 29 Nov 2023
Copy link to clipboard 
Print this post

Officially the silicon hasn't changed but from documentation revision D it tells you that bit 7 of both ports is output only and that it's now approved for automotive usage. Apparently there has always been a problem with using bit 7 as an input, sometimes it's ok and sometimes it isn't.
Edited 2023-11-30 07:21 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: 10:58pm 29 Nov 2023
Copy link to clipboard 
Print this post

  PhenixRising said  How recent is your chip?

I read that the newer devices lost the input capability on some ports(?)

Hearsay for me so please research.

at least 5 year old

Do

I2C write MCP23017,0,1,GPIOA
I2C read MCP23017,0,1,portabits
text 0,20,bin$(portabits,8)

Pause 100
Loop

Thanks but doesn't read the bit grounded but sometimes shows 11111111 which if pullups work makes sense. but mind of its own. it could be chip setup for addressing ??
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1611
Posted: 03:33am 30 Nov 2023
Copy link to clipboard 
Print this post

Stan, Please forgive me if I seem a bit frustrated but:

You have been warned that bit 7 of each port may not work as an input. You have said that you struggle with data sheets. So why persevere with a chip that may not work as you want and one that you probably won't understand? The CD4021 or 74HC165 require no setup and are easy to use - they do require pullup resistors though.

Finally you said:
  Stan said  I found picaxe basic and got gcbasic examples

Why not post them here?

In the meantime I found this code written by Lizby:
' MCP23017.bas


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 the 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 GPUPB = &h0D ' Port B pull-up register

SetPin 14,i2c1sda
SetPin 15,i2c1scl

I2C2 open 100, 1000
I2C2 WRITE MCP23017,0,2,IODIRA,0 ' set direction to output
I2C2 WRITE MCP23017,0,2,IODIRB,0 ' set direction to output
'  I2C2 WRITE MCP23017,0,2,GPUPB,&h3f ' set weak pullups on all except bits 6&7

do
mcp17
loop

sub mcp17
'  I2C2 WRITE MCP23017,0,2,IODIRA,0 ' set direction to output
'  I2C2 WRITE MCP23017,0,2,IODIRB,0 ' set direction to output
print "write to mcp23017"
for i = 1 to 6
  I2C2 Write MCP23017,0,2,OLATA,&b10101010 '  
  I2C2 Write MCP23017,0,2,OLATB,&b01010101 '  
  PAUSE     1000
  I2C2 Write MCP23017,0,2,OLATA,&b01010101 '  
  I2C2 Write MCP23017,0,2,OLATB,&b10101010 '  
  PAUSE     1000
Next i
I2C2 Write MCP23017,0,2,OLATA,0 ' turn all off
I2C2 Write MCP23017,0,2,OLATB,0 ' turn all off
pause 1000
end sub

It's not exactly what you want but it's a start.

Bill
Keep safe. Live long and prosper.
 
IanT
Regular Member

Joined: 29/11/2016
Location: United Kingdom
Posts: 87
Posted: 01:18pm 30 Nov 2023
Copy link to clipboard 
Print this post

Hello Stan,

I'm very far from the most expert here but I did wonder why you couldn't just use the 'Port' function to read your pad directly?

If you don't have enough spare pins on your Pico, then perhaps you could look at using a Mite to act as an I2C slave - as described by Geoff in 'Getting Started on the Micromite'. You could then programme everything in MMB and not worry so much about chip data sheets. I'm thinking of doing this (just for fun really) as I have some 28 DIL (PIC32s) Mites sitting in a box that don't get looked at so much now I use Picomites. They have all the Mite I/O functionality available, including of course I2C. You can in effect, build a custom I/O device with a Mite.

Hopefully what I'm suggesting makes good sense (he says nervously) and I haven't overlooked some basic flaw in my suggestion. In my case, I like that sample code is already available to work with, as a leg-up (to get going on new things) is very useful.

Regards,


IanT
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6797
Posted: 01:47pm 30 Nov 2023
Copy link to clipboard 
Print this post

I've been using a RP2040-Zero as an I2C expander for a controller (I'm just doing a hardware prototype at the moment). It has a lot of advantages. I can program a cheap analogue joystick module as either analogue X-Y or as digital by using the PicoMite as comparators. I can also change the I2C address under software control! :) Pretty cheap from AE too.
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: 02:20pm 30 Nov 2023
Copy link to clipboard 
Print this post

  Turbo46 said  In the meantime I found this code written by Lizby:


Which I had already provided to Stan back in October: read MCP23017
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6797
Posted: 02:33pm 30 Nov 2023
Copy link to clipboard 
Print this post

Note: The MCP23017 isn't one 16-bit port anyway. It's two distinct ports and you have to switch between them then send the read or write control commands. It's like having a 2-position switch inside to select port A or port B. The MCP23008 is a single "clean" 8-bit port, which is probably of more use for up to 8 inputs.
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: 05:21pm 30 Nov 2023
Copy link to clipboard 
Print this post

I am grateful for help from Mick,lizby,andy,phil and anyone not mentioned.
I have a robot using 28BYJ-48 Stepper Motors with dual ULN2003 Driver running from mcp23017 porta,4 bits each motor no prob using output.
My prob is reading porta or b as input.. any pin.
It was discussed on picaxe forum but I haven't used it in years.
https://picaxeforum.co.uk/threads/mcp23017-i2c-io-expander-data.12531/
My robot is in gcbasic and output only. It's not a lib function in gcbasic though.
For 5 buttons I'd use 5 gpio but I like i2c.
The 8 button pad was inspired by others pcb but I could get by without it. The challenge until my attention span wains is sorting this expander.
Anyone used PCF8574AN?
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6797
Posted: 05:49pm 30 Nov 2023
Copy link to clipboard 
Print this post

I haven't got a MCP23017 so I can't experiment with one. Lizby's code should be fine though.

Driving all 8 bits of either port is fine, and is fully supported.
Receiving uncorrupted I2C data from bit 7 has been a problem for years. The SPI version of the chip is OK and works as expected.
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: 06:17pm 30 Nov 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  I haven't got a MCP23017 so I can't experiment with one.
You get a free brain ache :)
Lizby's code should be fine though.
Did I miss input?
Driving all 8 bits of either port is fine, and is fully supported.
Yes.. output
Receiving uncorrupted I2C data from bit 7 has been a problem for years. The SPI version of the chip is OK and works as expected.
Searched for but no-one else seems to mention it?
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 06:34pm 30 Nov 2023
Copy link to clipboard 
Print this post

I'm using the mcp23017 and v53lox laser range finder and ssd1306 on same i2c pins same time no prob and it's tidy wiring.
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 08:52pm 30 Nov 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  I've been using a RP2040-Zero as an I2C expander for a controller (I'm just doing a hardware prototype at the moment). It has a lot of advantages. I can program a cheap analogue joystick module as either analogue X-Y or as digital by using the PicoMite as comparators. I can also change the I2C address under software control! :) Pretty cheap from AE too.

https://www.aliexpress.com/item/1005005533904456.html?


Edited 2023-12-01 06:55 by stanleyella
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 10:16pm 30 Nov 2023
Copy link to clipboard 
Print this post

Reading the port is simply done by reading GPIOA and GPIOB..andy
Simply?? Thanks for your reply but SIMPLY?? I guess it's Simple when you understand.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6797
Posted: 10:17pm 30 Nov 2023
Copy link to clipboard 
Print this post

This style Stan.
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: 10:36pm 30 Nov 2023
Copy link to clipboard 
Print this post

Mick.. rpizero as expander.. interesting idea for versatile in/out,
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 10:48pm 30 Nov 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  This style Stan.


can it be progged with mmbasic and any info? ae argh login.. ebay, I'll check.
will it be more difficult than mcp23017.. if that's possible?
I like the idea of i2c expanders and at a good price a rpizero...what ever that is.
If you think an i2c expander useful and rpizero for £1.50 then useful for others..
lib type commands like it's in the manual. If it's spi then complicates using ili lcd but i2c better.
Edited 2023-12-01 10:16 by stanleyella
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 12:35am 01 Dec 2023
Copy link to clipboard 
Print this post

Rpizero as expander, price ok but someone got to sort it as an expander, don't that mean code it. herder than 23017 :)
 
     Page 1 of 2    
Print this page
© JAQ Software 2024