PCA6408 - 8-bit I2C-bus I/O expander

Description

Library for interfacing NXP PCA6408A 8-bit I2C-bus I/O expander with interrupt output

The PCA6408A is a 8-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

PCA6408_Abstract:
This library can be used to handle NXP PCA6408A type 8-bit general purpose I/O expander.

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

  • The PCA6408A is an 8-bit general purpose I/O expander that provides remote I/O expansion for most microcontroller families via the I2C-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
    PCA6408_How_to_use:
    /* Hardware setup */ const { PORT_I2C = 0, // The first I2C interface should be used. CHIP_ADDR = PCA6408_I2C_ADR1, // I2C Address of the chip // (depends on the logic level at the chip's ADDR pin) } /* Handles for available sensors */ new hPCA6408[TPCA6408_Handle]; // Handle to manage the PCA6408 /** * Function that is called up when the device has been prepared for the start of the application by the * "salve" function. I.e. the device is ready for proceeding with the application. */ #callback Basic_Test() { // Inits the I2C interface catch(rM2M_I2cInit(PORT_I2C, PCA6408_CLOCK, 0)); // Inits PCA6408 (P0=Input, all other IOs configured as output with state LOW) catch(PCA6408_Init(hPCA6408, PORT_I2C, CHIP_ADDR, PCA6408_BITMASK(0), 0x00)); // Sets P1=high catch(PCA6408_Write(hPCA6408, 0x02)); // Reads and outputs inputs new iReadValue; catch(PCA6408_Read(hPCA6408, iReadValue)); #log("Inputs: %02X", iReadValue); // Closes the I2C interface catch(rM2M_I2cClose(PORT_I2C)); } /* Application entry point */ main(){ salve(Basic_Test); }

    BASIC

    PCA6408_CLOCK -

    typical I2C clock speed to be used

    PCA6408_I2C_ADRx:

    Available PCA6408 I2C addresses

    PCA6408_I2C_ADR0 - Must be used if ADDR Pin is "Low" (logic 0)
    PCA6408_I2C_ADR1 - Must be used if ADDR Pin is "High" (logic 1)
    PCA6408_Init(handle[TPCA6408_Handle], i2c, addr, input, output)

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

    handle : TPCA6408_Handle - Empty device handle for a PCA6408
    i2c : s32 - I2C interface where PCA6408 is connected to
    The I2C interface must be initialized 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 PCA6408_I2C_ADRx
    input : s32 - Direction of the I/O pins (Bit0 = P0, ..., Bit7 = P7)
    0 - Output
    1 - Input
    output : s32 - Outgoing logic levels of the pins defined as outputs (Bit0 = P0, ..., Bit7 = P7)
    0 - "Low"
    1 - "High"
    Bit values have no effect on pins defined as inputs.
    returns : s32
    OK - If successful
    < OK - if an error occurs
  • The selected I2C interface must be enabled (rM2M_I2cInit) before calling PCA6408_Init.
  • PCA6408 is specified for I2C Fast-Mode (400kHz, use PCA6408_CLOCK).
  • Power-On Default for PCA6408: all IOs configured as high-impedance inputs.
  • I2C Address is selected through ADDR pin on PCA6408
  • PCA6408_BITMASK(pin)

    Helper macro to convert pin to a bitmask

    pin : s32 - Pin number 0-7
    returns : s32 - 8-bit bitmask
    PCA6408_Read(handle[TPCA6408_Handle], &val, iRegister=PCA6408_CMD_INPUT_PORT)

    Read PCA6408 register


    The function reads from the specified register and returns the read value.

    handle : TPCA6408_Handle - Device handle of a specific PCA6408 (Initialized by PCA6408_Init() )
    val : s32 - Buffer for storing the read value

  • PCA6408_CMD_INPUT_PORT: Incoming logic levels of the pins, regardless of whether the pin is defined as an input or an output (Bit0 = P0, ..., Bit7 = P7)
    1 - "High"
    0 - "Low"
    Polarity inversion registers allow polarity inversion of pins defined as inputs. Polarity inversion is not enabled by this library. However, you can use the PCA6408_Com() function to enable polarity inversion.

  • PCA6408_CMD_OUTPUT_PORT: Outgoing logic levels of the pins defined as outputs (Bit0 = P0, ..., Bit7 = P7)
    0 - "Low"
    1 - "High"

  • PCA6408_CMD_POLARITY: Allows polarity inversion of pins defined as inputs (Bit0 = P0, ..., Bit7 = P7)
    1 - pin’s polarity is inverted
    0 - pin’s original polarity is retained

  • CA6408_CMD_CONFIG: Direction of the I/O pins (Bit0 = P0, ..., Bit7 = P7)
    0 - Output
    1 - Input

  • iRegister : s32 - Register from which the data should be read
    Use only the registers specified under PCA6408_CMD_x
    returns : s32
    OK - If successful
    ERROR_PARAM - Register address is invalid
    < OK - if an error occurs
    PCA6408_Write(handle[TPCA6408_Handle], value)

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

    handle : TPCA6408_Handle - Device handle of a specific PCA6408 (Initialized by PCA6408Init() )
    value - Logic levels to be set (Bit0 = P0, ..., Bit7 = P7)
    0 - "Low"
    1 - "High"
    Bit values have no effect on pins defined as inputs.
    returns : s32
    OK - If successful
    < OK - if an error occurs
    PCA6408_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 PCA6408 INT pin) if any of the inputs changes. It is recommended to use PCA6408_Read within the callback function to get the current input state.
    PCA6408_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

    PCA6408_CLOCK_MAX -

    absolute maximum I2C clock speed

    PCA6408_CMD_x:

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

    PCA6408_CMD_INPUT_PORT - | r | undefined | Input port register
    PCA6408_CMD_OUTPUT_PORT - | r/w | 0xFF | Output port register
    PCA6408_CMD_POLARITY - | r/w | 0x00 | Polarity inversion register
    PCA6408_CMD_CONFIG - | r/w | 0xFF | Configuration register
    PCA6408_Com(handle[TPCA6408_Handle], aData{}, writelen, readlen)

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

    handle : TPCA6408_Handle - Device handle of a specific PCA6408 (Initialized by PCA6408_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
    < OK - if an error occurs
    Access to PCA6408 is always started by sending one byte command code (see PCA6408_CMD_x definitions) followed by one byte read/write data. Please see datasheet for more information

    DEBUG

    PCA6408_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 PCA6408 * -) When initializing: direction of the I/O pins and Outgoing logic * levels of the pins defined as outputs * -) The content of the register is output when calling PCA6408_Read * -) The logic levels to be set for the outputs are output when calling PCA6408_Write * */ #define PCA6408_DEBUG 0

    On this page

    PCA6408 - 8-bit I2C-bus I/O expander