Extend I/O Ports using PIC Microcontroller

Extend IO Ports schematic
The mikroC PRO for PIC provides a library for communication with the Microchip’s Port Expander MCP23S17 via SPI interface. Connections of the PIC compliant MCU and MCP23S17 is given on the schematic at the bottom of this page.

  Important :

  • The library uses the SPI module for communication. User must initialize the appropriate SPI module before using the Port Expander Library.
  • Library does not use Port Expander interrupts.Extend IO Ports

    External dependencies of Port Expander Library

    The following variables must be defined in all projects using Port Expander Library: Description : Example :
    extern sfr sbit SPExpanderRST; Reset line. sbit SPExpanderRST at RC0_bit;
    extern sfr sbit SPExpanderCS; Chip Select line. sbit SPExpanderCS at RC1_bit;
    extern sfr sbit SPExpanderRST_Direction; Direction of the Reset pin. sbit SPExpanderRST_Direction at TRISC0_bit;
    extern sfr sbit SPExpanderCS_Direction; Direction of the Chip Select pin. sbit SPExpanderCS_Direction at TRISC1_bit;

    Library Routines

    • Expander_Init
    • Expander_Init_Advanced
    • Expander_Read_Byte
    • Expander_Write_Byte
    • Expander_Read_PortA
    • Expander_Read_PortB
    • Expander_Read_PortAB
    • Expander_Write_PortA
    • Expander_Write_PortB
    • Expander_Write_PortAB
    • Expander_Set_DirectionPortA
    • Expander_Set_DirectionPortB
    • Expander_Set_DirectionPortAB
    • Expander_Set_PullUpsPortA
    • Expander_Set_PullUpsPortB
    • Expander_Set_PullUpsPortAB

    Expander_Init

    Prototype void Expander_Init(char ModuleAddress);
    Returns Nothing.
    Description Initializes Port Expander using SPI communication.
    Port Expander module settings : 

    • hardware addressing enabled
    • automatic address pointer incrementing disabled (byte mode)
    • BANK_0 register adressing
    • slew rate enabled

    Parameters :

    • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
    Requires Global variables : 

    • SPExpanderCS: Chip Select line
    • SPExpanderRST: Reset line
    • SPExpanderCS_Direction: Direction of the Chip Select pin
    • SPExpanderRST_Direction: Direction of the Reset pin

    must be defined before using this function.
    SPI module needs to be initialized. See SPIx_Init and SPIx_Init_Advanced routines.

    Example
    // Port Expander module connections
    sbit  SPExpanderRST at RC0_bit;
    sbit  SPExpanderCS  at RC1_bit;
    sbit  SPExpanderRST_Direction at TRISC0_bit;
    sbit  SPExpanderCS_Direction  at TRISC1_bit;
    // End Port Expander module connections
    
    ...
    
    ANSEL  = 0;               // Configure AN pins as digital I/O
    ANSELH = 0;
    
    // If Port Expander Library uses SPI module
    SPI1_Init();               // Initialize SPI module used with PortExpander
    Expander_Init(0);         // Initialize Port Expander
    

    Expander_Init_Advanced

    Prototype void Expander_Init_Advanced(char *rstPort, char rstPin, char haen);
    Returns Nothing.
    Description Initializes Port Expander using SPI communication.
    Parameters : 

    • rstPort: Port Expander’s reset port
    • rstPin: Port Expander’s reset pin
    • haen: Enable hardware addressing. Valid values :
      • 0 – hardware addressing disabled
      • 1 – hardware addressing enabled
    Requires
    • SPExpanderCS: Chip Select line
    • SPExpanderRST: Reset line
    • SPExpanderCS_Direction: Direction of the Chip Select pin
    • SPExpanderRST_Direction: Direction of the Reset pin

    must be defined before using this function.

    Example
    // Port Expander module connections
    sbit  SPExpanderRST at RC0_bit;
    sbit  SPExpanderCS  at RC1_bit;
    sbit  SPExpanderRST_Direction at TRISC0_bit;
    sbit  SPExpanderCS_Direction  at TRISC1_bit;
    // End Port Expander module connections
    
    ...
    
    ANSEL  = 0;               // Configure AN pins as digital I/O
    ANSELH = 0;
    
    // If Port Expander Library uses SPI module
    SPI1_Init();              // Initialize SPI module used with PortExpander
    Expander_Init_Advanced(&PORTB, 0, 0);         // Initialize Port Expander
    

     

    Expander_Read_Byte

    Prototype char Expander_Read_Byte(char ModuleAddress, char RegAddress);
    Returns Byte read.
    Description The function reads byte from Port Expander.
    Parameters : 

    • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
    • RegAddress: Port Expander’s internal register address
    Requires Port Expander must be initialized.
    Example
    // Read a byte from Port Expander's register
    char read_data;
    ...
    read_data = Expander_Read_Byte(0,1);
    

    Expander_Write_Byte

    Prototype void Expander_Write_Byte(char ModuleAddress, char RegAddress, char Data);
    Returns Nothing.
    Description Routine writes a byte to Port Expander.
    Parameters : 

    • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
    • RegAddress: Port Expander’s internal register address
    • Data_: data to be written
    Requires Port Expander must be initialized.
    Example
    // Write a byte to the Port Expander's register
    Expander_Write_Byte(0,1,0xFF);
    

    Expander_Read_PortA

    Prototype char Expander_Read_PortA(char ModuleAddress);
    Returns Byte read.
    Description The function reads byte from Port Expander’s PortA.
    Parameters : 

    • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
    Requires Port Expander must be initialized. 

    Port Expander’s PortA should be configured as input.

    Example
    // Read a byte from Port Expander's PORTA
    char read_data;
    ...
    Expander_Set_DirectionPortA(0,0xFF);        // set expander's porta to be input
    ...
    read_data = Expander_Read_PortA(0);
    

    Extend IO Ports schematic

    Read more: Extend I/O Ports using PIC 

Leave a Comment

Your email address will not be published. Required fields are marked *