mcauser / micropython-mcp23008

WIP MicroPython driver for MCP23008 8-bit I/O Expander
0 stars 0 forks source link

Example #1

Open cpu012 opened 1 year ago

cpu012 commented 1 year ago

I made an example since there is no documentation

from machine import Pin, I2C
import mcp23008
import time

# connect scl to GPIO4 and sda to GPIO5 or redefine below
i2c = I2C(scl=Pin(4), sda=Pin(5))
mcp = mcp23008.MCP23008(i2c, 0x20)

last_trig = time.ticks_ms() # last time the interrupt was triggered - used for debouncing

debounce = 500 # debounce time - in milliseconds

# method interface
mcp.pin(0, mode=1, pullup=False, interrupt_enable=1)
mcp.pin(1, mode=0, value=1)

mcp.config(interrupt_polarity=1, sequential_operation=0)

# isr
def callback(p):
    # debounce
    global last_trig
    t = time.ticks_ms()
    if (t - last_trig >= debounce):
        mcp.pin(1, value=not mcp.pin(1)) # toggle the pin

        last_trig = t
    mcp.interrupt_captured # reset interrupt

# set up the interrupts
intP = Pin(0, Pin.IN) 
intP.irq(trigger=Pin.IRQ_RISING, handler=callback) 

Example

cpu012 commented 1 year ago

Example_bb