Interfacing the MCP23008 GPIO Expander on the Curiosity Nano Explorer development board using a PIC18F56Q24 Curiosity Nano board
This example demonstrates the setup and configuration of the MCP23008 GPIO expander using the PIC18F56Q24 microcontroller. The Curiosity Nano Explorer Board features several onboard peripherals that are connected directly to the MCP23008 GPIO expander, such as a joystick control and multiple touch buttons. In this example, the PIC18F56Q24 is configured to interface with the GPIO expander using the I2C protocol. The MCP23008 GPIO expander provides 8-bit, general-purpose, parallel I/O expansion via the I2C protocol.
- MPLAB® X IDE 6.20.0 or newer (MPLAB® X IDE 6.20)
- MPLAB® XC8 3.00.0 or newer compiler (MPLAB® XC8 3.00.0)
- MPLAB® Code Configurator (MCC) 5.5.1 or newer (mplab-code-configurator)
- Microchip PIC18F-Q Series DFP (1.24.430) or newer (packs.download.microchip.com/)
- PIC18F56Q24 Curiosity Nano Evaluation Kit
- Curiosity Nano Explorer
- USB type C cable
The userguide for the Curiosity Explorer Nano board provides detailed information about the pin mapping between the Curiosity Explorer board and the PIC18F56Q24 Curiosity Nano board being used in this example.
Based on the pin mapping details shown in the diagram above, the I2CSDA and I2CSCL lines will be used to communicate with the MCP23008 I2C GPIO expander, utilizing the PIC18F56Q24 Curiosity Nano board connected to the Explorer board.
The detailed step by step instructions required to recreate this example are listed below:
Begin by setting up UART to enable data display on the terminal.
The UART configuration should appear as shown below.
- With MCC open, add the UART driver and select UART2 in the dependency selector.
- Ensure that "Redirect Printf to UART" is enabled.
- The UART configuration should appear as shown below.
- No changes are required for UART2PLIB
- Add the I2C driver and select I2C1 as the I2C Host PLIB Selector.
- Set the requested speed to 100 kHz and ensure that the I2C interrupt is enabled.
- The I2C configuration should appear as shown below.
- Based on the diagram below, select the appropriate UART and I2C pins.
- The MCC configuration for the pins is shown below.
- Click on "Generate" to complete the configuration in MCC.
- Add the MCP23008.c and MCP23008.h driver code to the project.
- The joystick supports five actions (Up, Left, Down, Right, Push), which are connected to GP0, GP1, GP2, GP3, and GP4 on the MCP23008, respectively.
- These actions are used as inputs to the MCU, so configure the GPx pins as inputs through the MCP23008 GPIO expander.
- MCP23008 datasheet provides all necessary information for configuring the GPx pins as inputs.
- The MCP23008.c source code includes the MCP23008_init() and MCP23008_JOY() functions, which handle initialization and reading joystick inputs, respectively.
- After adding the MCP23008 source and header files, include the header in the main code and call the MCP23008_init() and MCP23008_JOY() functions in main.c as shown below. Ensure that INTERRUPT_GlobalInterruptEnable() is called.
Depending on the value returned by MCP23008_JOY(), the input direction is printed to the terminal using UART.
After programming the device, the direction of the joystick is displayed on the COM port as it is moved, as shown below.
The demo demonstrates how to interface the MCP23008 I2C GPIO expander on the Curiosity Nano Explorer board using a PIC18F56Q24 microcontroller.
In this demonstration, the joystick connected to the MCP23008 is used, and its input is displayed on the terminal as the joystick is moved.