electroSome

Expanding IO Ports of PIC Microcontroller using MCP23S17

Usually a microcontroller comes with a limited number of IO ports called General Purpose Input Output (GPIO) ports. But some applications require more IO ports than that available on a microcontroller. In these cases we can use IO Port Expanders to increase the IO capability of a microcontroller. MCP23017 and MCP23S17 are two such 16 bit IO expander with Serial Interface  manufactured by Microchip. MCP23017 uses high speed I2C interface while MCP23S17 used high speed SPI interface.

MCP23S17

In this tutorial we will see how to expand IO ports of a PIC Microcontroller using MCP23S17. MikroC Pro for PIC Microcontroller provides built in libraries to communicate with MCP23S17 via SPI interface.  We can connect up to eight MCP23S17 on a SPI Bus by using the three address pins present on it.

MCP23S17 Pin Diagram

In this example project we use PIC 16F877A to demonstrate the working of MCP23S17. PORTA and PORTB of MCP23S17 is configured as Input and Output respectively. SPST switches are connected to PORTA along with the internal PULL UP of MCP23S17 and LEDs are connected to PORTB via current limiting resistors. Input to PORTA is the output to PORTB, so LEDs connected to PORTB will GLOW according the the status of SPST switches connected to PORTA.

Circuit Diagram – IO Port Expanding PIC Microcontroller

 

Expanding IO Ports of a PIC Microcontroller using MCP23S17

 Note: VDD , VSS of the PIC Microcontroller and MCP23S17 are not shown in the circuit diagram. VDD should be connected to +5V and VSS to OV.

When a switch is ON corresponding LED will be OFF and when a switch is OFF corresponding LED will be ON. Since all the three address pin of MCP23S17 are grounded its address is 0.

MikroC Library Functions

Note : Port Expander Module connections should be declared before the main functions as shown in the following example. SPI Module and Port Expander should be initialized before using other functions.

MikroC Code

// Port Expander module connections
sbit  SPExpanderRST at RC1_bit;
sbit  SPExpanderCS  at RC0_bit;
sbit  SPExpanderRST_Direction at TRISC1_bit;
sbit  SPExpanderCS_Direction  at TRISC0_bit;
// End Port Expander module connections

void main()
{
  int a;
  SPI1_Init();               // Initialize SPI module used with PortExpander
  Expander_Init(0);          // Initialize Port Expander at address 0

  Expander_Set_DirectionPortA(0, 0xFF); //All pins on PORTA as Inputs
  Expander_Set_DirectionPortB(0, 0x00); //All pins on PORTB as Outputs

  Expander_Set_PullUpsPortA(0, 0xFF);  //Setting Internal Pull Ups for PORTA

  do
  {
     a = Expander_Read_PortA(0);  //Reading Data from PORTA
     Expander_Write_PortB(0, a);  //Writing Data to PORTB
     Delay_ms(100);
  }while(1);
}

You can download the hex file, MikroC source code, Proteus files etc here…

Expanding IO Ports of a PIC Microcontroller