Shop OBEX P1 Docs P2 Docs Learn Events
Help with I/O expanders — Parallax Forums

Help with I/O expanders

andrew777andrew777 Posts: 25
edited 2005-09-16 12:06 in Robotics

Hello, does anybody know about pcf8574 remote 8-bit I/O expanders for I²c-bus. I want to use it with a bs2px. I'm running out of I/O ports.

I want to use two of them for six pushbuttons (input) and six leds (output) I want to push one pushbutton at a time and light one corresponding led. I don't know how to get data in and out and I don't know if I can use them·in this way.···I'm going to use it for motor speed control.· The pushbutton activate the next higher speed.

Thanks,

Andrew

Post Edited (andrew777) : 8/24/2005 3:35:37 AM GMT

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-24 03:42
    The PCF8574 isn't too tough to use -- just keep the following in mind:

    1) Inputs are active-low; these pins need to be pulled up to Vdd
    2) Writes to the device are buffered, so by sending the data twice you can force the outputs to be updated
    3) When using mixed I/O (inputs and outputs), you need to write a 1 bit to input pins

    The attached program should get you started.· And, final thought, the MCP23016 from Microchip is as easy to use and has better output capability.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • andrew777andrew777 Posts: 25
    edited 2005-08-25 04:35
    Dear Jon:

    I am going to take your suggestion and purchase the MCP23016 from Microchip.

    If the chip is easier to use I'm all for that. Could you give me some pointers on how to use the chip for the purpose I indicated.

    Your help is always appreciated.

    Thank you,

    Andrew
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-25 13:07
    The MCP23016 is a general-purpose I/O port expander.· Just configure the pins and inputs or outputs, then read from them or write to them as you need; it's really pretty easy.· I've attached a demo, and here's a link to an LCD interface I created with it:

    http://www.parallax.com/dl/docs/cols/nv/vol5/col/nv109.pdf

    Be sure to download and study the latest datasheet from Microchip.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • andrew777andrew777 Posts: 25
    edited 2005-09-02 22:53

    Dear Jon:
    ·
    I need additional help with the MCP23016. I didn't want to contact you before I read the datasheet's and forum archives.· I looked at the setup schematic and I've set up the RC circuit that runs the clock.· I've put the resistors on the SDA and SCL lines.· I want to use p4 and P5 on the BS2PX for the SDA and SCL lines.· Will this alter anything other than I/O definition.· LED via 470 is this active high?· Active low pushbutton has the ground active?· Could you send me a schematic of a active low pushbutton connected to·the mcp23016.·
    ·
    I need six pushbuttons and six leds, ·I push one pushbutton and one led lights, I push two pushbuttons and two leds light etc.
    ·
    ·
    I2COUT SDA, Wr23016, IODIR0, [noparse][[/noparse]%00000000]····· ' GP0 pins are outputs
    · I2COUT SDA, Wr23016, IODIR1, [noparse][[/noparse]%00001111]····· ' GP1.0 - GP1.3 are inputs
    · I2COUT SDA, Wr23016, IPOL1, [noparse][[/noparse]%00001111]······ ' invert inputs
    ·
    Could you explain this statement.
    dataGP1 = dataGP1 << 4··················· ' move inputs to outputs
    ·
    I hate to so ignorant about the implementation of MCP23016.· I read the datasheet but I still unsure about some things to do what I'm wanted to do.
    ·
    ·
    ·
    ' =========================================================================
    '
    '·· File....... MCP23016.BSP
    '·· Purpose.... Microchip MCP23016 Demo
    '·· Author..... Parallax
    '·· E-mail..... support@parallax.com
    '·· Started....
    '·· Updated.... 05 SEP 2003
    '
    '·· {$STAMP BS2p}
    '·· {$PBASIC 2.5}
    '
    ' =========================================================================
    ·
    ·
    '
    [noparse][[/noparse] Program Description ]

    '
    ' This program demonstrates simple interfacing to the Microchip MCP23016
    ' I/O expander.· The advantage of this device over the PCF8574 series is
    ' its sink/source ability of 25 mA per pin.
    '
    ' Note: The MCP23016 uses the same device type as the PCF8574P -- do not
    '······ connect on the same buss (okay to use PCF8574AP on same buss).
    '
    ' Connections:
    '
    '·· GP0.0 -> GP0.7····· LED (via 470)
    '·· GP1.4 -> GP1.7····· LED (via 470)
    '·· GP1.0 -> GP1.3····· active-low pushbutton
    '·· Clock·············· 3.3K to Vdd; 30 pF to Vss
    ·
    ·
    '
    [noparse][[/noparse] Revision History ]

    ·
    ·
    '
    [noparse][[/noparse] I/O Definitions ]

    ·
    SDA············ PIN···· 0······ ················' I2C data pin
    SCL············ PIN···· 1······················ ' I2C clock pin
    ·
    ·
    '
    [noparse][[/noparse] Constants ]

    ·
    DevType········ CON···· %0100 << 4············· ' Device type
    DevAddr········ CON ····%000 << 1·············· ' address = %000 -> %111
    Wr23016········ CON···· DevType | DevAddr······ ' write to MCP23016
    Rd23016········ CON···· Wr23016 | 1············ ' read from MCP23016
    ·
    GP0············ CON···· $00···················· ' register addresses
    GP1············ CON···· $01
    OLAT0·········· CON···· $02
    OLAT1·········· CON···· $03
    IPOL0·········· CON···· $04
    IPOL1·········· CON···· $05
    IODIR0········· CON···· $06
    IODIR1········· CON···· $07
    INTCAP0········ CON···· $08
    INTCAP1········ CON···· $09
    IOCON0········· CON···· $0A
    IOCON1········· CON···· $0B
    ·
    ·
    #DEFINE _P40 = 0······························· ' 0 for BS2p24/pe24
    ·
    ·
    '
    [noparse][[/noparse] Variables ]

    ·
    dataGP0········ VAR···· Byte··················· ' device I/O bank 0
    dataGP1········ VAR···· Byte··················· ' -- bank 1
    idx············ VAR···· Nib···················· ' loop counter
    ·
    ·
    attachment.php?attachmentid=73930'
    [noparse][[/noparse] EEPROM Data ]

    ·
    ·
    '
    [noparse][[/noparse] Initialization ]

    ·
    Setup:
    · PAUSE 100···································· ' allow device POR
    ·
    · #IF _P40 #THEN AUXIO #ENDIF·················· ' use aux pins on BS2p40
    · I2COUT SDA, Wr23016, IODIR0, [noparse][[/noparse]%00000000]····· ' GP0 pins are outputs
    · I2COUT SDA, Wr23016, IODIR1, [noparse][[/noparse]%00001111]····· ' GP1.0 - GP1.3 are inputs
    · I2COUT SDA, Wr23016, IPOL1, [noparse][[/noparse]%00001111]······ ' invert inputs
    ·
    ·
    '
    [noparse][[/noparse] Program Code ]

    ·
    Main:
    · DO
    ··· I2COUT SDA, Wr23016, GP0, [noparse][[/noparse]dataGP0]········ ' output counter
    ··· FOR idx = 1 TO 10
    ····· I2CIN SDA, Rd23016, GP1, [noparse][[/noparse]dataGP1]······· ' read buttons
    ····· dataGP1 = dataGP1 << 4··················· ' move inputs to outputs
    ····· I2COUT SDA, Wr23016, GP1, [noparse][[/noparse]dataGP1]······ ' display button scan
    ····· DEBUG HOME,······························ ' report
    ··········· "LEDs···· ", BIN8 dataGP0, CR,
    ··········· "Buttons· ", BIN4 dataGP1.NIB1
    ··· NEXT
    ··· dataGP0 = dataGP0 + 1······················ ' increment count
    · LOOP
    ·
    · END
    ·
    ·
    '
    [noparse][[/noparse] Subroutines ]

    ·
    74 x 231 - 192B
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-02 23:04
    With the manual I2C code you can use any pins for SDA and SCL (on the BS2p family, I2CIN and I2COUT have pin group limitations).· I've attached a schematic for an active-low button circuit, but that you had to ask makes me a little nervous.· You may want to work through WAM (our "What's A Microcontroller?" book) if you don't have a lot of experience connect circuits to the BASIC Stamp and other micros.

    The << operator shifts bits to the left.· In the demo program I'm using it to move the input bits to the positions of of the LEDs connected to the outputs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 9/2/2005 11:09:39 PM GMT
    370 x 257 - 8K
  • andrew777andrew777 Posts: 25
    edited 2005-09-03 18:27
    Dear Jon:

    Thank you for your help. I have already hooked up a motor, motor controller, H-bridge and BS2px to their various circuits. I need to hook up some sensors and a encoder. I go about it methodically always being aware of ground, power supply, high and low voltage logic and short circuits. I am a inventor with PC programming experience. I really don't have electronic experience but I read. I learn fast I get help when needed. So far I have been successful. Thanks for your concern. As long as I get my specific questions answered I'll be all right.

    Thanks again,

    Andrew
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-03 19:36
    Run (quickly) to your local bookstore and get Forrest M. Mim's book: Getting Started in Electronics -- it's a no-nonsense way to learn about basic electronic components and will serve you very well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • andrew777andrew777 Posts: 25
    edited 2005-09-04 01:23
    Dear Jon:

    Thank you for the book recommendation.
    I will order this book from Amazon today.
    I have gotten helpful and timely advice from this forum.

    Best regards,

    Andrew
  • andrew777andrew777 Posts: 25
    edited 2005-09-16 11:53

    Dear Jon:

    I have gotten my circuit completed using the BS2px, mcp23016 the clock on the mcp23016 is set with a RC circuit the pushbuttons are active low. I don't know if the LEDs have to be configured active low or high. The SDA and SCL have been pulled up with 4.7 ohm resistors and have been designated to p04 and p05 on the BS2px respectively. I'm want all GP0's to be output's. I want all GP1's to be input's. I want each input to have a one-to-one correspondence to the output. Press button one and LED one lights. Press button one and three LED one and three, lights. I am using your demo program I have tried to alter it slightly but I need some help for it to work. Could you look over the program and tell me what is needed for it to work for my application. I have read related data sheets but my understanding is limited.

    I2COUT SDA, Wr23016, IODIR0, [noparse][[/noparse]%00000000] ' GP0 pins are outputs

    I2COUT SDA, Wr23016, IODIR1, [noparse][[/noparse]%11111111] ' GP1.0 - GP1.7 are inputs

    I2COUT SDA, Wr23016, IPOL1, [noparse][[/noparse]%11111111] ' invert inputs

    I have attached your slightly altered demo program for your review.

    I think the inputs and outputs should be configured like this?

    Your help is always appreciated.

    Andrew

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-16 12:06
    The MCP23016 can sink or source 25 mA per pin (this is in the docs for it) -- so you can use active-high or active-low LEDs.

    I2CIN and I2COUT won't work on pins P4/P5 -- this is in the BASIC Stamp manual. You must use P0/P1 or P8/P9.

    Time to crack open some manuals....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.