<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic PCAL9714 sample code in Other NXP Products</title>
    <link>https://community.nxp.com/t5/Other-NXP-Products/PCAL9714-sample-code/m-p/1851717#M22305</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;we are trying to communicate with this IC using our Arduino UNO.&lt;/P&gt;&lt;P&gt;Can you provide a sample code/library for this IC please, we cannot make it work somehow.&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 22 Apr 2024 14:45:27 GMT</pubDate>
    <dc:creator>Thai</dc:creator>
    <dc:date>2024-04-22T14:45:27Z</dc:date>
    <item>
      <title>PCAL9714 sample code</title>
      <link>https://community.nxp.com/t5/Other-NXP-Products/PCAL9714-sample-code/m-p/1851717#M22305</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;we are trying to communicate with this IC using our Arduino UNO.&lt;/P&gt;&lt;P&gt;Can you provide a sample code/library for this IC please, we cannot make it work somehow.&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2024 14:45:27 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Other-NXP-Products/PCAL9714-sample-code/m-p/1851717#M22305</guid>
      <dc:creator>Thai</dc:creator>
      <dc:date>2024-04-22T14:45:27Z</dc:date>
    </item>
    <item>
      <title>Re: PCAL9714 sample code</title>
      <link>https://community.nxp.com/t5/Other-NXP-Products/PCAL9714-sample-code/m-p/1851850#M22311</link>
      <description>&lt;P&gt;Hello &lt;A id="link_6" class="lia-link-navigation lia-page-link lia-user-name-link" href="https://community.nxp.com/t5/user/viewprofilepage/user-id/194453" target="_self" aria-label="View Profile of Thai"&gt;&lt;SPAN class=""&gt;Thai&lt;/SPAN&gt;&lt;/A&gt;,&lt;/P&gt;
&lt;P&gt;I hope all is great with you. Thank you for using the NXP communities.&lt;/P&gt;
&lt;P&gt;Unfortunately, we do not have any sample code available for the device. Please accept my apologies.&lt;/P&gt;
&lt;P&gt;Are you able to write/read the internal registers of the device?&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;David&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2024 20:07:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Other-NXP-Products/PCAL9714-sample-code/m-p/1851850#M22311</guid>
      <dc:creator>diazmarin09</dc:creator>
      <dc:date>2024-04-22T20:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: PCAL9714 sample code</title>
      <link>https://community.nxp.com/t5/Other-NXP-Products/PCAL9714-sample-code/m-p/1854409#M22369</link>
      <description>&lt;P&gt;Hi David,&lt;BR /&gt;&lt;BR /&gt;Yes I tried to write to the register and read it back but it didnt give the same value. (I am Thai co-worker)&lt;BR /&gt;I can share with you my code so far and you can correct me, please.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;This is the ino file: pcal&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#include &amp;lt;Arduino.h&amp;gt;
#include &amp;lt;SPI.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include "pcal9714.h"

void setup() {
    Serial.begin(9600);
    uint8_t debounce_pins_port0 = 0xFF; // to enable debounce on all pins of port 0
    uint8_t debounce_pins_port1 = 0xFF; // to enable debounce on all pins of port 1
    uint16_t debounce_counter_value = 10;        // debounce time of 10 microseconds
    
    pcal9714_configure_debounce(debounce_pins_port0, debounce_pins_port1, debounce_counter_value);

    // Set all pins you want to monitor as inputs with internal pull-up
    for (uint8_t pin = 0; pin &amp;lt; 8; pin++) {
      pcal9714_set_mode(pin, INPUT_PULLUP);

    }

    // Write a test value to a register
    uint8_t testValue = 0xAA; // Example test value (170 in decimal)
    pcal9714_write(PCAL_CONF_PORT0, testValue);

    // Read the value back
    uint8_t readBackValue = pcal9714_read(PCAL_CONF_PORT0);

    // Print the read back value to the serial monitor
    Serial.print("Wrote: 0x");
    Serial.print(testValue, HEX);
    Serial.print(", Read back: 0x");
    Serial.println(readBackValue, HEX);


        // Set GPIOEXP_LIMIT_SWITCH_HOME_Y_V10 as an output
    pcal9714_set_mode(GPIOEXP_LIMIT_SWITCH_HOME_Y_V10, OUTPUT); // Set the pin as output

    // Set the pin high
    pcal9714_set_pin(GPIOEXP_LIMIT_SWITCH_HOME_Y_V10, HIGH); // Drive the pin high

    // Add a short delay to observe the pin voltage with a multimeter
    delay(1000); // Wait for 1 second

    // Set the pin low
    pcal9714_set_pin(GPIOEXP_LIMIT_SWITCH_HOME_Y_V10, LOW); // Drive the pin low

    // Add another short delay to observe the pin voltage with a multimeter
    delay(1000); // Wait for 1 second


    Serial.println("Direct pin test setup complete.");

}

void loop() {
    // Read the state of a pin (for example, P0_2) and print it to the serial monitor
  int limitSwitchValue = pcal9714_read_pin(GPIOEXP_LIMIT_SWITCH_HOME_Y_V10);
  
  // Print the state to the serial monitor
  Serial.print("Limit Switch State: ");
  Serial.println(limitSwitchValue);

  // Add a delay to prevent flooding the serial monitor
  delay(500);

  static bool pinState = LOW;
  pcal9714_set_pin(GPIOEXP_LIMIT_SWITCH_HOME_Y_V10, pinState);
  pinState = !pinState; // Toggle the state
  delay(1000); // 1 second delay

       
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;BR /&gt;This is the cpp file pcal9714&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="cpp"&gt;#include &amp;lt;Arduino.h&amp;gt;
#include "pcal9714.h"
#include &amp;lt;SPI.h&amp;gt;


/**
********************************************************************************
* @file pcal9714.cpp
* @author ahmed
* @date 2024-04-22
* @brief Implementation of the pcal9714 driver.
********************************************************************************
*/

/// @brief Configure the pcal9714 device.
void pcal9714_init(void)
{

pinMode(PCAL9714_SPI_CS_PIN, OUTPUT);
digitalWrite(PCAL9714_SPI_CS_PIN, HIGH); // Deselect the device
SPI.begin(); // Initialize SPI communication

}

/// @brief Set the value of a pin on the pcal9714 device.
///  channel Channel number.
///  value 1 for high, 0 for low.
void pcal9714_set_pin(uint8_t channel, uint8_t value)
{
uint8_t port = channel / 8; // Determine port (0 or 1)
uint8_t pin = channel % 8; // Determine pin within the port

uint8_t portValue = pcal9714_read_port(port); // Read current port value

if (value) {
portValue |= (1 &amp;lt;&amp;lt; pin); // Set the pin to HIGH
} else {
portValue &amp;amp;= ~(1 &amp;lt;&amp;lt; pin); // Set the pin to LOW
}

pcal9714_write(PCAL_OUT_PORT0 + port, portValue); // Write to the output port

}

/// @brief Read the value of a pin on the pcal9714 device.
///  channel Channel number.
/// @return The value of the pin.
uint8_t pcal9714_read_pin(uint8_t channel)
{
uint8_t port = channel / 8;
uint8_t pin = channel % 8;

uint8_t portValue = pcal9714_read_port(port);
return (portValue &amp;gt;&amp;gt; pin) &amp;amp; 0x01;
}

/// @brief Set the mode of a pin on the pcal9714 device.
///  channel Channel number.
///  value 1 for high impedence input, 0 for output.
void pcal9714_set_mode(uint8_t channel, uint8_t mode) {
uint8_t port = channel / 8;
uint8_t pin = channel % 8;

// Setting the overall port configuration for push-pull if we're setting the pin as an output
if (mode == OUTPUT) {
uint8_t out_conf = pcal9714_read(PCAL_OUT_CONF_REG);
out_conf |= 0x03; // Set the ports to push-pull mode
pcal9714_write(PCAL_OUT_CONF_REG, out_conf);

// Set the individual pin configuration for pull-up if required
uint8_t out_conf_reg = (port == 0) ? PCAL_OUT_CONF_REG0 : PCAL_OUT_CONF_REG1;
uint8_t out_conf_value = pcal9714_read(out_conf_reg);
out_conf_value |= (1 &amp;lt;&amp;lt; pin); // Enable pull-up for the pin
pcal9714_write(out_conf_reg, out_conf_value);
}

// Then set the pin direction (input/output)
uint8_t config_reg = (port == 0) ? PCAL_CONF_PORT0 : PCAL_CONF_PORT1;
uint8_t current_config = pcal9714_read(config_reg);

if (mode == OUTPUT) {
current_config &amp;amp;= ~(1 &amp;lt;&amp;lt; pin); // Clear the bit to set it as output
} else {
current_config |= (1 &amp;lt;&amp;lt; pin); // Set the bit to set it as input
}

pcal9714_write(config_reg, current_config);
}


void pcal9714_configure_debounce(uint8_t debounce_pins_port0, uint8_t debounce_pins_port1, uint8_t debounce_counter_value) {
// Initialize SPI communication
pcal9714_init();

// Set up the switch debounce enable registers for port 0 and port 1
pcal9714_write(PCAL_DEBOUNCE_EN_REG0, debounce_pins_port0);
pcal9714_write(PCAL_DEBOUNCE_EN_REG1, debounce_pins_port1);

// Write the debounce counter value to the debounce count register
pcal9714_write(PCAL_DEBOUNCE_COUNT, debounce_counter_value);
}


/// @brief Write a value to a register on the pcal9714 device.
///  addr Address of the register.
///  value Value to write.
void pcal9714_write(uint8_t addr, uint8_t value)
{
digitalWrite(PCAL9714_SPI_CS_PIN, LOW); // Select the device
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));

SPI.transfer(addr); // Send the address
SPI.transfer(value); // Send the value

SPI.endTransaction();
digitalWrite(PCAL9714_SPI_CS_PIN, HIGH); // Deselect the device

}

/// @brief Read a value from a port on the pcal9714 device.
///  port port addr
/// @return value of the port
uint8_t pcal9714_read_port(uint8_t port)
{
uint8_t regAddr = (port == 0) ? PCAL_INP_PORT0 : PCAL_INP_PORT1;
return pcal9714_read(regAddr);
}

/// @brief Read a value from a register on the pcal9714 device.
///  addr Address of the register.
/// @return Value read from the register.
uint8_t pcal9714_read(uint8_t addr)
{
uint8_t read_value = 0;

digitalWrite(PCAL9714_SPI_CS_PIN, LOW); // Select the device
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));

SPI.transfer(addr | 0x80); // Send the address ORed with the read command
read_value = SPI.transfer(0xFF); // Send dummy data to read the value

SPI.endTransaction();
digitalWrite(PCAL9714_SPI_CS_PIN, HIGH); // Deselect the device

return read_value;


}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;BR /&gt;This is the header file: pcal9714.h&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#ifndef _PCAL9714_H_
#define _PCAL9714_H_

#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;stdbool.h&amp;gt;


/**
********************************************************************************
* @file pcal9714.h
* @author ahmed
* @date 2024-04-22
* @brief Header file for the pcal9714 driver.
********************************************************************************
*/


typedef enum gpio_expander_channels_v10_t {
GPIOEXP_NONE0_V10,
GPIOEXP_NONE1_V10,
GPIOEXP_LIMIT_SWITCH_HOME_Y_V10,
GPIOEXP_SEL_WG_CD_V10,
GPIOEXP_SEL_FIBER_V10,
GPIOEXP_SEL_WG_AB_V10,
GPIOEXP_NONE2_V10,
GPIOEXP_NONE3_V10,
GPIOEXP_LIMIT_SWITCH_HOME_X_V10,
GPIOEXP_LIMIT_SWITCH_END_X_V10,
GPIOEXP_LIMIT_SWITCH_END_Y_V10,
GPIOEXP_CCB_DETECT_V10,
GPIOEXP_CC_DETECT_V10,
GPIOEXP_MOTOR_ENABLE_V10,
NUMBER_OF_GPIOEXP_PINS_V10
}gpio_expander_channels_v10_t;

#define PCAL_INP_PORT0 0x00
#define PCAL_INP_PORT1 0x01
#define PCAL_OUT_PORT0 0x02
#define PCAL_OUT_PORT1 0x03
#define PCAL_POL_INV_PORT0 0x04
#define PCAL_POL_INV_PORT1 0x05
#define PCAL_CONF_PORT0 0x06
#define PCAL_CONF_PORT1 0x07
#define PCAL_OUT_STR_PORT0A 0x40
#define PCAL_OUT_STR_PORT0B 0x41
#define PCAL_OUT_STR_PORT1A 0x42
#define PCAL_OUT_STR_PORT1B 0x43
#define PCAL_INP_LATCH_REG0 0x44
#define PCAL_INP_LATCH_REG1 0x45
#define PCAL_PULL_EN_PORT0 0x46
#define PCAL_PULL_EN_PORT1 0x47
#define PCAL_PULL_SEL_PORT0 0x48
#define PCAL_PULL_SEL_PORT1 0x49
#define PCAL_INT_MASK_PORT0 0x4A
#define PCAL_INT_MASK_PORT1 0x4B
#define PCAL_INT_STAT_PORT0 0x4C
#define PCAL_INT_STAT_PORT1 0x4D
#define PCAL_OUT_CONF_REG 0x4F
#define PCAL_INT_EDGE_REG0A 0x50
#define PCAL_INT_EDGE_REG0B 0x51
#define PCAL_INT_EDGE_REG1A 0x52
#define PCAL_INT_EDGE_REG1B 0x53
#define PCAL_INT_CLR_REG0 0x54
#define PCAL_INT_CLR_REG1 0x55
#define PCAL_INP_PORT_READ_WO_INT_CLR0 0x56
#define PCAL_INP_PORT_READ_WO_INT_CLR1 0x57
#define PCAL_OUT_CONF_REG0 0x58
#define PCAL_OUT_CONF_REG1 0x59
#define PCAL_DEBOUNCE_EN_REG0 0x5A
#define PCAL_DEBOUNCE_EN_REG1 0x5B
#define PCAL_DEBOUNCE_COUNT 0x5C


// Define the SPI CS pin
#define PCAL9714_SPI_CS_PIN 9 // CS pin for the PCAL9714 is connected to Arduino pin 9


void pcal9714_init(void);
void pcal9714_set_pin(uint8_t channel, uint8_t value);
uint8_t pcal9714_read_pin(uint8_t channel);
void pcal9714_set_mode(uint8_t channel, uint8_t value);
void pcal9714_write(uint8_t addr, uint8_t value);
uint8_t pcal9714_read_port(uint8_t port);
uint8_t pcal9714_read(uint8_t addr);
void pcal9714_configure_debounce(uint8_t debounce_pins_port0, uint8_t debounce_pins_port1, uint8_t debounce_counter_value);




#endif /* _PCAL9714_H_ */&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 06:52:03 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Other-NXP-Products/PCAL9714-sample-code/m-p/1854409#M22369</guid>
      <dc:creator>ahmedd</dc:creator>
      <dc:date>2024-04-25T06:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: PCAL9714 sample code</title>
      <link>https://community.nxp.com/t5/Other-NXP-Products/PCAL9714-sample-code/m-p/1854425#M22370</link>
      <description>&lt;P&gt;Hi David,&lt;BR /&gt;&lt;BR /&gt;I have tried to write and read back to the register but it didnt give the same value, (I am Thai co-worker)&lt;BR /&gt;&lt;BR /&gt;I will share what I have done so far, and you can correct me.&lt;BR /&gt;&lt;BR /&gt;Here is my ino file&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#include &amp;lt;Arduino.h&amp;gt;
#include &amp;lt;SPI.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include "pcal9714.h"

void setup() {
    Serial.begin(9600);
    uint8_t debounce_pins_port0 = 0xFF; // to enable debounce on all pins of port 0
    uint8_t debounce_pins_port1 = 0xFF; // to enable debounce on all pins of port 1
    uint16_t debounce_counter_value = 10;        // debounce time of 10 microseconds
    
    pcal9714_configure_debounce(debounce_pins_port0, debounce_pins_port1, debounce_counter_value);

    // Set all pins you want to monitor as inputs with internal pull-up
    for (uint8_t pin = 0; pin &amp;lt; 8; pin++) {
      pcal9714_set_mode(pin, INPUT_PULLUP);

    }

    // Write a test value to a register
    uint8_t testValue = 0xAA; // Example test value (170 in decimal)
    pcal9714_write(PCAL_CONF_PORT0, testValue);

    // Read the value back
    uint8_t readBackValue = pcal9714_read(PCAL_CONF_PORT0);

    // Print the read back value to the serial monitor
    Serial.print("Wrote: 0x");
    Serial.print(testValue, HEX);
    Serial.print(", Read back: 0x");
    Serial.println(readBackValue, HEX);


        // Set GPIOEXP_LIMIT_SWITCH_HOME_Y_V10 as an output
    pcal9714_set_mode(GPIOEXP_LIMIT_SWITCH_HOME_Y_V10, OUTPUT); // Set the pin as output

    // Set the pin high
    pcal9714_set_pin(GPIOEXP_LIMIT_SWITCH_HOME_Y_V10, HIGH); // Drive the pin high

    // Add a short delay to observe the pin voltage with a multimeter
    delay(1000); // Wait for 1 second

    // Set the pin low
    pcal9714_set_pin(GPIOEXP_LIMIT_SWITCH_HOME_Y_V10, LOW); // Drive the pin low

    // Add another short delay to observe the pin voltage with a multimeter
    delay(1000); // Wait for 1 second


    Serial.println("Direct pin test setup complete.");

}

void loop() {
    // Read the state of a pin (for example, P0_2) and print it to the serial monitor
  int limitSwitchValue = pcal9714_read_pin(GPIOEXP_LIMIT_SWITCH_HOME_Y_V10);
  
  // Print the state to the serial monitor
  Serial.print("Limit Switch State: ");
  Serial.println(limitSwitchValue);

  // Add a delay to prevent flooding the serial monitor
  delay(500);

  static bool pinState = LOW;
  pcal9714_set_pin(GPIOEXP_LIMIT_SWITCH_HOME_Y_V10, pinState);
  pinState = !pinState; // Toggle the state
  delay(1000); // 1 second delay

       
}&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;cpp file&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#include &amp;lt;Arduino.h&amp;gt;
#include "pcal9714.h"
#include &amp;lt;SPI.h&amp;gt;


/**
 ********************************************************************************
 * @file    pcal9714.cpp
 * @author  ahmed
 * @date    2024-04-22
 * @brief   Implementation of the pcal9714 driver.
 ********************************************************************************
 */

/// @brief Configure the pcal9714 device.
void pcal9714_init(void)
{
    
    pinMode(PCAL9714_SPI_CS_PIN, OUTPUT);
    digitalWrite(PCAL9714_SPI_CS_PIN, HIGH); // Deselect the device
    SPI.begin();  // Initialize SPI communication

}

/// @brief Set the value of a pin on the pcal9714 device.
/// &lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/197964"&gt;@Param&lt;/a&gt; channel Channel number.
/// &lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/197964"&gt;@Param&lt;/a&gt; value 1 for high, 0 for low.
void pcal9714_set_pin(uint8_t channel, uint8_t value)
{
    uint8_t port = channel / 8;  // Determine port (0 or 1)
    uint8_t pin = channel % 8;   // Determine pin within the port

    uint8_t portValue = pcal9714_read_port(port); // Read current port value

    if (value) {
        portValue |= (1 &amp;lt;&amp;lt; pin); // Set the pin to HIGH
    } else {
        portValue &amp;amp;= ~(1 &amp;lt;&amp;lt; pin); // Set the pin to LOW
    }

    pcal9714_write(PCAL_OUT_PORT0 + port, portValue); // Write to the output port

}

/// @brief Read the value of a pin on the pcal9714 device.
/// &lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/197964"&gt;@Param&lt;/a&gt; channel Channel number.
/// @return The value of the pin.
uint8_t pcal9714_read_pin(uint8_t channel)
{
    uint8_t port = channel / 8;
    uint8_t pin = channel % 8;

    uint8_t portValue = pcal9714_read_port(port);
    return (portValue &amp;gt;&amp;gt; pin) &amp;amp; 0x01; 
}

/// @brief Set the mode of a pin on the pcal9714 device.
/// &lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/197964"&gt;@Param&lt;/a&gt; channel Channel number.
/// &lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/197964"&gt;@Param&lt;/a&gt; value 1 for high impedence input, 0 for output.
void pcal9714_set_mode(uint8_t channel, uint8_t mode) {
    uint8_t port = channel / 8;
    uint8_t pin = channel % 8;

    // Setting the overall port configuration for push-pull if we're setting the pin as an output
    if (mode == OUTPUT) {
        uint8_t out_conf = pcal9714_read(PCAL_OUT_CONF_REG);
        out_conf |= 0x03; // Set the ports to push-pull mode
        pcal9714_write(PCAL_OUT_CONF_REG, out_conf);

        // Set the individual pin configuration for pull-up if required
        uint8_t out_conf_reg = (port == 0) ? PCAL_OUT_CONF_REG0 : PCAL_OUT_CONF_REG1;
        uint8_t out_conf_value = pcal9714_read(out_conf_reg);
        out_conf_value |= (1 &amp;lt;&amp;lt; pin); // Enable pull-up for the pin
        pcal9714_write(out_conf_reg, out_conf_value);
    }

    // Then set the pin direction (input/output)
    uint8_t config_reg = (port == 0) ? PCAL_CONF_PORT0 : PCAL_CONF_PORT1;
    uint8_t current_config = pcal9714_read(config_reg);

    if (mode == OUTPUT) {
        current_config &amp;amp;= ~(1 &amp;lt;&amp;lt; pin); // Clear the bit to set it as output
    } else {
        current_config |= (1 &amp;lt;&amp;lt; pin); // Set the bit to set it as input
    }

    pcal9714_write(config_reg, current_config);
}


void pcal9714_configure_debounce(uint8_t debounce_pins_port0, uint8_t debounce_pins_port1, uint8_t debounce_counter_value) {
    // Initialize SPI communication
    pcal9714_init();

    // Set up the switch debounce enable registers for port 0 and port 1
    pcal9714_write(PCAL_DEBOUNCE_EN_REG0, debounce_pins_port0);
    pcal9714_write(PCAL_DEBOUNCE_EN_REG1, debounce_pins_port1);

    // Write the debounce counter value to the debounce count register
    pcal9714_write(PCAL_DEBOUNCE_COUNT, debounce_counter_value);
}


/// @brief Write a value to a register on the pcal9714 device.
/// &lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/197964"&gt;@Param&lt;/a&gt; addr Address of the register.
/// &lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/197964"&gt;@Param&lt;/a&gt; value Value to write.
void pcal9714_write(uint8_t addr, uint8_t value)
{
    digitalWrite(PCAL9714_SPI_CS_PIN, LOW); // Select the device
    SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
    
    SPI.transfer(addr);  // Send the address
    SPI.transfer(value); // Send the value

    SPI.endTransaction();
    digitalWrite(PCAL9714_SPI_CS_PIN, HIGH); // Deselect the device

}

/// @brief Read a value from a port on the pcal9714 device.
/// &lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/197964"&gt;@Param&lt;/a&gt; port port addr
/// @return value of the port
uint8_t pcal9714_read_port(uint8_t port)
{
    uint8_t regAddr = (port == 0) ? PCAL_INP_PORT0 : PCAL_INP_PORT1; 
    return pcal9714_read(regAddr);
}

/// @brief Read a value from a register on the pcal9714 device.
/// &lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/197964"&gt;@Param&lt;/a&gt; addr Address of the register.
/// @return Value read from the register.
uint8_t pcal9714_read(uint8_t addr)
{
    uint8_t read_value = 0;

    digitalWrite(PCAL9714_SPI_CS_PIN, LOW); // Select the device
    SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));

    SPI.transfer(addr | 0x80); // Send the address ORed with the read command
    read_value = SPI.transfer(0xFF); // Send dummy data to read the value

    SPI.endTransaction();
    digitalWrite(PCAL9714_SPI_CS_PIN, HIGH); // Deselect the device

    return read_value;


}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the header file&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#ifndef _PCAL9714_H_
#define _PCAL9714_H_

#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;stdbool.h&amp;gt;


/**
 ********************************************************************************
 * @file    pcal9714.h
 * @author  ahmed
 * @date    2024-04-22
 * @brief   Header file for the pcal9714 driver.
 ********************************************************************************
 */


typedef enum gpio_expander_channels_v10_t {
    GPIOEXP_NONE0_V10,
    GPIOEXP_NONE1_V10,
    GPIOEXP_LIMIT_SWITCH_HOME_Y_V10,
    GPIOEXP_SEL_WG_CD_V10,
    GPIOEXP_SEL_FIBER_V10,
    GPIOEXP_SEL_WG_AB_V10,
    GPIOEXP_NONE2_V10,
    GPIOEXP_NONE3_V10,
    GPIOEXP_LIMIT_SWITCH_HOME_X_V10,
    GPIOEXP_LIMIT_SWITCH_END_X_V10,
    GPIOEXP_LIMIT_SWITCH_END_Y_V10,
    GPIOEXP_CCB_DETECT_V10,
    GPIOEXP_CC_DETECT_V10,
    GPIOEXP_MOTOR_ENABLE_V10,
    NUMBER_OF_GPIOEXP_PINS_V10
}gpio_expander_channels_v10_t;

#define PCAL_INP_PORT0 0x00
#define PCAL_INP_PORT1 0x01
#define PCAL_OUT_PORT0 0x02
#define PCAL_OUT_PORT1 0x03
#define PCAL_POL_INV_PORT0 0x04
#define PCAL_POL_INV_PORT1 0x05
#define PCAL_CONF_PORT0 0x06
#define PCAL_CONF_PORT1 0x07
#define PCAL_OUT_STR_PORT0A 0x40
#define PCAL_OUT_STR_PORT0B 0x41
#define PCAL_OUT_STR_PORT1A 0x42
#define PCAL_OUT_STR_PORT1B 0x43
#define PCAL_INP_LATCH_REG0 0x44
#define PCAL_INP_LATCH_REG1 0x45
#define PCAL_PULL_EN_PORT0 0x46
#define PCAL_PULL_EN_PORT1 0x47
#define PCAL_PULL_SEL_PORT0 0x48
#define PCAL_PULL_SEL_PORT1 0x49
#define PCAL_INT_MASK_PORT0 0x4A
#define PCAL_INT_MASK_PORT1 0x4B
#define PCAL_INT_STAT_PORT0 0x4C
#define PCAL_INT_STAT_PORT1 0x4D
#define PCAL_OUT_CONF_REG 0x4F
#define PCAL_INT_EDGE_REG0A 0x50
#define PCAL_INT_EDGE_REG0B 0x51
#define PCAL_INT_EDGE_REG1A 0x52
#define PCAL_INT_EDGE_REG1B 0x53
#define PCAL_INT_CLR_REG0 0x54
#define PCAL_INT_CLR_REG1 0x55
#define PCAL_INP_PORT_READ_WO_INT_CLR0 0x56
#define PCAL_INP_PORT_READ_WO_INT_CLR1 0x57
#define PCAL_OUT_CONF_REG0 0x58
#define PCAL_OUT_CONF_REG1 0x59
#define PCAL_DEBOUNCE_EN_REG0 0x5A
#define PCAL_DEBOUNCE_EN_REG1 0x5B
#define PCAL_DEBOUNCE_COUNT 0x5C


// Define the SPI CS pin
#define PCAL9714_SPI_CS_PIN   9  // CS pin for the PCAL9714 is connected to Arduino pin 9


void pcal9714_init(void);
void pcal9714_set_pin(uint8_t channel, uint8_t value);
uint8_t pcal9714_read_pin(uint8_t channel);
void pcal9714_set_mode(uint8_t channel, uint8_t value);
void pcal9714_write(uint8_t addr, uint8_t value);
uint8_t pcal9714_read_port(uint8_t port);
uint8_t pcal9714_read(uint8_t addr);
void pcal9714_configure_debounce(uint8_t debounce_pins_port0, uint8_t debounce_pins_port1, uint8_t debounce_counter_value);




#endif /* _PCAL9714_H_ */&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;BR /&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 07:04:25 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Other-NXP-Products/PCAL9714-sample-code/m-p/1854425#M22370</guid>
      <dc:creator>ahmedd</dc:creator>
      <dc:date>2024-04-25T07:04:25Z</dc:date>
    </item>
    <item>
      <title>Re: PCAL9714 sample code</title>
      <link>https://community.nxp.com/t5/Other-NXP-Products/PCAL9714-sample-code/m-p/1857762#M22464</link>
      <description>&lt;P&gt;Hello &lt;SPAN&gt;ahmedd&lt;/SPAN&gt;,&lt;/P&gt;
&lt;P&gt;Please accept my apologies for the delayed response.&lt;/P&gt;
&lt;P&gt;I would like to focus on your &lt;EM&gt;pcal9714_write&lt;/EM&gt; and &lt;EM&gt;pcal9714_read&lt;/EM&gt; functions.&lt;/P&gt;
&lt;P&gt;The bus master must send the target slave address followed by a read or write operation. The slave address of the PCAL9714 is shown below:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="diazmarin09_0-1714503166606.png" style="width: 234px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/276728iEA15BA0A89CA6F1F/image-dimensions/234x91?v=v2" width="234" height="91" role="button" title="diazmarin09_0-1714503166606.png" alt="diazmarin09_0-1714503166606.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you considering such an address before trying to write or read an internal register?&lt;/P&gt;
&lt;P&gt;Following the first byte of the slave address, the bus master sends a command byte, which is write-only and stored in the pointer register in the PCAL9714. Finally, the data.&lt;/P&gt;
&lt;P&gt;In total, 24 bits of data are transferred before deserting CS.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="diazmarin09_1-1714503172393.png" style="width: 467px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/276729iC30D831572E1D1C6/image-dimensions/467x118?v=v2" width="467" height="118" role="button" title="diazmarin09_1-1714503172393.png" alt="diazmarin09_1-1714503172393.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please share an oscilloscope of your data transfer?&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;David&lt;/P&gt;</description>
      <pubDate>Tue, 30 Apr 2024 18:53:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Other-NXP-Products/PCAL9714-sample-code/m-p/1857762#M22464</guid>
      <dc:creator>diazmarin09</dc:creator>
      <dc:date>2024-04-30T18:53:06Z</dc:date>
    </item>
  </channel>
</rss>

