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 KingdomPosts: 2129 |
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: AustraliaPosts: 6099 |
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 KingdomPosts: 864 |
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 KingdomPosts: 6798 |
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 KingdomPosts: 2129 |
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: AustraliaPosts: 1611 |
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: 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 KingdomPosts: 87 |
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 KingdomPosts: 6798 |
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 StatesPosts: 3150 |
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 KingdomPosts: 6798 |
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 KingdomPosts: 2129 |
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 KingdomPosts: 6798 |
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 KingdomPosts: 2129 |
|
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2129 |
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 KingdomPosts: 2129 |
https://www.aliexpress.com/item/1005005533904456.html? Edited 2023-12-01 06:55 by stanleyella |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2129 |
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 KingdomPosts: 6798 |
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 KingdomPosts: 2129 |
Mick.. rpizero as expander.. interesting idea for versatile in/out, |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2129 |
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 KingdomPosts: 2129 |
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 |