Expanding the number of I/O lines using Microchip MCP23008 using pic microcontoller

A microcontroller comes with a limited number of general purpose input and output (GPIO) ports. However, some applications may require more ports than are available on the microcontroller. In such a case, GPIO expanders can be used to increase the I/O capability of the microcontroller. MCP23008 is one such device (manufactured by Microchip Technology) which provides an easy I/O expansion using 2-wire serial interface. This tutorial illustrates how to add an extra 8-bit I/O port to PIC12683 microcontroller (which has only 6 I/O pins) using MCP23008. A seven segment LED display and a tact switch will be connected to the extended port. The PIC12F683 microcontroller will count the switch presses and display the counter value on the seven segment LED  module.
Expanding the number of I O lines using Microchip MCP23008.jpgTheory

The MCP23008 an I2C slave device that provides 8-bit, general purpose, bi-directional I/O expansion for I2C bus. If you are not familiarized with I2C protocol, read my article on Inter-Integrated Circuit communication. MCP23008 supports 7-bit slave addressing, with the read/write bit filling out the control byte. The four most significant bits of the slave address are fixed (0100 for MCP23008) and the remaining three bits are user-defined hardware address bits (pins A2, A1 and A0). This allows us to connect up to eight MCP23008 devices on a common I2C bus. The control byte format for MCP23008 is shown below.

Control and configuration registers

The individual bits of the 8-bit GPIO port can be configured as input or output. The port pins can also be enabled for internal pull up resistor and interrupt-on-change. These operations are controlled through a set of configuration and control registers. The table below shows the list of these registers, their addresses and the power-on reset values.

These registers are described in the datasheet of MCP23008. I am summarizing them here. We will talk about configuring these registers for our specific purpose in the software section.

I/O Direction (IODIR): It controls the direction of the data I/O. When a bit is set, the corresponding pin becomes an input. When a bit is clear, the corresponding pin becomes an output.

Input Polarity (IPOL): It allows the user to configure the polarity on the corresponding GPIO port bits. If a bit is set, the corresponding GPIO register bit will store the inverted value on the pin.

Interrupt-on-change Control Register (GPINTEN): It controls the interrupt-on- change feature for each pin. If a bit is set, the corresponding pin is enabled for interrupt-on-change. The DEFVAL and INTCON registers must also be configured if any pins are enabled for interrupt-on-change.

Expanding the number of I O lines using Microchip MCP23008.jpgInterrupt Control (INTCON): It controls how the associated pin value is compared for the interrupt-on-change feature. If a bit is set, the corresponding I/O pin is compared against the associated bit in the DEFVAL register (described in next paragraph). If a bit value is clear, the corresponding I/O pin is compared against the previous value.

Default Comparison Value (DEFVAL): The default comparison value is configured in the DEFVAL register. If enabled (via GPINTEN and INTCON) to compare against the DEFVAL register, an opposite value on the associated pin will cause an interrupt to occur. For example, if GPINT6 (in GPINTEN), IOC6 (in INTCON) and DEF6 (in DEFVAL) bits are all set, then an interrupt will be generated when the GP6 I/O pin is pulled low. Of course, the GP6 pin direction should be defined as input.

For more detail: Expanding the number of I/O lines using Microchip MCP23008

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter