PCA6416 - 16-bit I2C-bus I/O expander

Description

Library for interfacing NXP PCA6416A 16-bit I2C-bus I/O expander with interrupt output

The PCA6416A is a 16-bit general purpose I/O expander that provides remote I/O expansion for most microcontroller families via the I²C-bus interface. -) I2C interface with communication speed up to 400 kHz -) Two user selectable addresses (chosen by chip pin) -) Built-in level shiffter -) Interrupt output for indicating that an input state has changed

OVERVIEW

PCA6416_Abstract:
This library can be used to handle NXP PCA6416A type 16-bit general purpose I/O expander.

Interfaces:
  • 1x I2C
  • 1x IRQ (optional)

  • The PCA6416A is a 16-bit general purpose I/O expander that provides remote I/O expansion for most microcontroller families via the I²C-bus interface.
  • I2C interface with communication speed up to 400 kHz
  • Two user selectable addresses (chosen by chip pin)
  • Built-in level shifter
  • Interrupt output for indicating that an input state has changed

  • For more information, see the NXP website
    PCA6416_How_to_use:
    /* Hardware setup */ const { PORT_I2C = 0, // The first I2C interface should be used. CHIP_ADDR = PCA6416_I2C_ADR0 , // I2C Address of the chip // (depends on the logic level at the chip's ADDR pin) } /* Handles for available sensors */ new hPCA6416[TPCA6416_Handle]; // Handle to manage the PCA6416 main(){ // Inits the I2C interface catch(rM2M_I2cInit(PORT_I2C, PCA6416_CLOCK, 0)); // Inits PCA6416 (P1_0=Input, all other IOs configured as output with state LOW) catch(PCA6416_Init(hPCA6416, PORT_I2C, CHIP_ADDR, PCA6416_BITMASK(1,0), 0x0000)); // Sets P1_1=high catch(PCA6416_Write(hPCA6416, PCA6416_BITMASK(1,1))); // Reads inputs new input; catch(PCA6416_Read(hPCA6416, input)); }

    BASIC

    PCA6416_CLOCK -

    typical I2C clock speed to be used

    PCA6416_I2C_ADRx:

    Available PCA6416 I2C addresses

    PCA6416_I2C_ADR0 - Must be used if ADDR Pin is "Low" (logic 0)
    PCA6416_I2C_ADR1 - Must be used if ADDR Pin is "High" (logic 1)
    PCA6416_Init(handle[TPCA6416_Handle], i2c, addr, input, output)

    Initializes i2c communication with the PCA6416, sets the direction of the I/O pins and sets the outgoing logic levels of the pins defined as outputs

    handle : TPCA6416_Handle - Empty device handle for a PCA6416
    i2c : s32 - I2C interface where PCA6416 is connected to
    The I2C interface must be initialisation before using rM2M_I2cInit().
    addr : s32 - I2C Address of the chip (depends on the logic level at the chip's ADDR pin)
    Use only the addresses specified under PCA6416_I2C_ADRx
    input : s32 - Direction of the I/O pins (Bitmask, Bit0 = P0.0, ..., Bit7 = P0.7, Bit8 = P1.0, ..., Bit15 = P1.7)
    0 - Output
    1 - Input
    output : s32 - Outgoing logic levels of the pins defined as outputs (Bitmask, Bit0 = P0.0, ..., Bit7 = P0.7, Bit8 = P1.0, ..., Bit15 = P1.7)
    0 - "Low"
    1 - "High"
    Bit values have no effect on pins defined as inputs.
    returns : s32
    OK - If successful
    ERROR - If an error occurs
  • The selected I2C interface must be enabled (rM2M_I2cInit) before calling PCA6416_Init.
  • PCA6416 is specified for I2C Fast-Mode (400kHz, use PCA6416_CLOCK).
  • Power-On Default for PCA6416: all IOs configured as high-impedance inputs.
  • I2C Address is selected through ADDR pin on PCA6416.
  • PCA6416_BITMASK(port, pin)

    Helper macro to convert port and pin to a bitmask

    port : s32 - Port number 0-1
    pin : s32 - Pin number 0-7
    returns : s32 - 16-bit bitmask
    PCA6416_Read(handle[TPCA6416_Handle], &val)

    Read PCA6416 Inputs

    handle : TPCA6416_Handle - Device handle of a specific PCA6416 (Initialized by PCA6416_Init() )
    val - Buffer for storing the read input values (Bitmask, Bit0 = P0.0, ..., Bit7 = P0.7, Bit8 = P1.0, ..., Bit15 = P1.7)
    1 - "High"
    0 - "Low"
    The Polarity inversion registers allow polarity inversion of pins defined as inputs. Polarity inversion is not enabled by this library. However, you can use the PCA6416_Com() function to enable polarity inversion.
    returns : s32
    OK - If successful
    ERROR - If an error occurs
    PCA6416_Write(handle[TPCA6416_Handle], value)

    Writes the logic levels to be set for the outputs pins to the output port register of the PCA6416

    handle : TPCA6416_Handle - Device handle of a specific PCA6416 (Initialized by PCA6416_Init() )
    value - Logic levels to be set (Bitmask, Bit0 = P0.0, ..., Bit7 = P0.7, Bit8 = P1.0, ..., Bit15 = P1.7)
    0 - "Low"
    1 - "High"
    Bit values have no effect on pins defined as inputs.
    returns : s32
    OK - If successful
    ERROR - If an error occurs
    PCA6416_IrqInit(irq, callback)

    Enable IRQ handling

    irq : s32 - Number of the interruptible pin (ranging from 0 for the first IRQ pin of the device up to WATCH_COUNT-1)
    callback : TonWatch - Function that should be executed in the event of an interrupt

    Type of function: #callback func(pinstate);

    The interrupt is generated (falling edge on the PCA6416 INT pin) if any of the inputs changes. It is recommended to use PCA6416_Read within the callback function to get the current input state.
    PCA6416_IrqClose(irq)

    Disable IRQ handling

    irq : s32 - Number of the interruptible pin (ranging from 0 for the first IRQ pin of the device up to WATCH_COUNT-1)

    EXPERT

    PCA6416_CLOCK_MAX -

    absolute maximum I2C clock speed

    PCA6416_CMD_x:

    Available commands/registers (Command | R/W | Default | Description)

    PCA6416_CMD_INPUT_PORT_0 - 0x00 | r | undefined | Input port 0
    PCA6416_CMD_INPUT_PORT_1 - 0x01 | r | undefined | Input port 1
    PCA6416_CMD_OUTPUT_PORT_0 - 0x02 | r/w | 0xFF | Output port 0
    PCA6416_CMD_OUTPUT_PORT_1 - 0x03 | r/w | 0xFF | Output port 1
    PCA6416_CMD_POLARITY_INV_0 - 0x04 | r/w | 0x00 | Polarity Inversion port 0 (Only affects the input register)
    PCA6416_CMD_POLARITY_INV_1 - 0x05 | r/w | 0x00 | Polarity Inversion port 1 (Only affects the input register)
    PCA6416_CMD_CONFIG_PORT_0 - 0x06 | r/w | 0xFF | Configuration port 0
    PCA6416_CMD_CONFIG_PORT_1 - 0x07 | r/w | 0xFF | Configuration port 1
    PCA6416_Com(handle[TPCA6416_Handle], aData{}, writelen, readlen)

    Performs raw PCA6416 read/write access. Data is first of all sent and data is then received.

    handle : TPCA6416_Handle - Device handle of a specific PCA6416 (Initialized by PCA6416_Init() )
    aData : {} - Array in which the data to be sent must initially be saved. Once the data has been sent, the array is used as a memory for the data to be received.
    writelen : s32 - Number of bytes to be sent
    readlen : s32 - Number of bytes to be received
    returns : s32
    OK - if successful
    ERROR - if one of the following errors occurs
  • Number of bytes to be sent >255
  • Number of bytes to be received >255
  • Another error occurs
  • Access to PCA6416 is always started by sending one byte command code (see PCA6416_CMD_x definitions) followed by one byte read/write data. Please see datasheet for more information.

    DEBUG

    PCA6416_Debug_Config:
    To configure debug mode add the following macro to the main.dde file.
    /** * Use this macro to configure the debug mode: * * 0: No debugging * >0: Issues the following information via the console: * -) I2C address used to access the PCA6416 * -) When initializing: direction of the I/O pins and Outgoing logic * levels of the pins defined as outputs * -) The content of the input port registers is output when calling PCA6416_Read * -) The logic levels to be set for the outputs are output when calling PCA6416_Write * */ #define PCA6416_DEBUG 0

    On this page

    PCA6416 - 16-bit I2C-bus I/O expander