Port Expander Library

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 :

Library Dependency Tree

Port Expander Library Dependency Tree

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

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.


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_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. See Expander_Init.

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. See Expander_Init.

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. See Expander_Init.

Port Expander's PortA should be configured as input. See Expander_Set_DirectionPortA and Expander_Set_DirectionPortAB routines.

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);

Expander_Read_PortB

Prototype

char Expander_Read_PortB(char ModuleAddress);

Returns

Byte read.

Description

The function reads byte from Port Expander's PortB.

Parameters :

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

Requires

Port Expander must be initialized. See Expander_Init.

Port Expander's PortB should be configured as input. See Expander_Set_DirectionPortB and Expander_Set_DirectionPortAB routines.

Example
// Read a byte from Port Expander's PORTB
char read_data;
...
Expander_Set_DirectionPortB(0,0xFF);        // set expander's portb to be input
...
read_data = Expander_Read_PortB(0);

Expander_Read_PortAB

Prototype

unsigned int Expander_Read_PortAB(char ModuleAddress);

Returns

Word read.

Description

The function reads word from Port Expander's ports. PortA readings are in the higher byte of the result. PortB readings are in the lower byte of the result.

Parameters :

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

Requires

Port Expander must be initialized. See Expander_Init.

Port Expander's PortA and PortB should be configured as inputs. See Expander_Set_DirectionPortA, Expander_Set_DirectionPortB and Expander_Set_DirectionPortAB routines.

Example
// Read a byte from Port Expander's PORTA and PORTB
unsigned int read_data;
...
Expander_Set_DirectionPortAB(0,0xFFFF);        // set expander's porta and portb to be input
...
read_data = Expander_Read_PortAB(0);

Expander_Write_PortA

Prototype

void Expander_Write_PortA(char ModuleAddress, char Data_);

Returns

Nothing.

Description

The function writes byte to Port Expander's PortA.

Parameters :

  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • Data_: data to be written

Requires

Port Expander must be initialized. See Expander_Init.

Port Expander's PortA should be configured as output. See Expander_Set_DirectionPortA and Expander_Set_DirectionPortAB routines.

Example
// Write a byte to Port Expander's PORTA

...
Expander_Set_DirectionPortA(0,0x00);        // set expander's porta to be output
...
Expander_Write_PortA(0, 0xAA);

Expander_Write_PortB

Prototype

void Expander_Write_PortB(char ModuleAddress, char Data_);

Returns

Nothing.

Description

The function writes byte to Port Expander's PortB.

Parameters :

  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • Data_: data to be written

Requires

Port Expander must be initialized. See Expander_Init.

Port Expander's PortB should be configured as output. See Expander_Set_DirectionPortB and Expander_Set_DirectionPortAB routines.

Example
// Write a byte to Port Expander's PORTB

...
Expander_Set_DirectionPortB(0,0x00);        // set expander's portb to be output
...
Expander_Write_PortB(0, 0x55);

Expander_Write_PortAB

Prototype

void Expander_Write_PortAB(char ModuleAddress, unsigned int Data_);

Returns

Nothing.

Description

The function writes word to Port Expander's ports.

Parameters :

  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • Data_: data to be written. Data to be written to PortA are passed in Data's higher byte. Data to be written to PortB are passed in Data's lower byte

Requires

Port Expander must be initialized. See Expander_Init.

Port Expander's PortA and PortB should be configured as outputs. See Expander_Set_DirectionPortA, Expander_Set_DirectionPortB and Expander_Set_DirectionPortAB routines.

Example
// Write a byte to Port Expander's PORTA and PORTB 

...
Expander_Set_DirectionPortAB(0,0x0000);        // set expander's porta and portb to be output
...
Expander_Write_PortAB(0, 0xAA55);

Expander_Set_DirectionPortA

Prototype

void Expander_Set_DirectionPortA(char ModuleAddress, char Data_);

Returns

Nothing.

Description

The function sets Port Expander's PortA direction.

Parameters :

  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • Data_: data to be written to the PortA direction register. Each bit corresponds to the appropriate pin of the PortA register. Set bit designates corresponding pin as input. Cleared bit designates corresponding pin as output.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Set Port Expander's PORTA to be output
Expander_Set_DirectionPortA(0,0x00);

Expander_Set_DirectionPortB

Prototype

void Expander_Set_DirectionPortB(char ModuleAddress, char Data_);

Returns

Nothing.

Description

The function sets Port Expander's PortB direction.

Parameters :

  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • Data_: data to be written to the PortB direction register. Each bit corresponds to the appropriate pin of the PortB register. Set bit designates corresponding pin as input. Cleared bit designates corresponding pin as output.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Set Port Expander's PORTB to be input
Expander_Set_DirectionPortB(0,0xFF);

Expander_Set_DirectionPortAB

Prototype

void Expander_Set_DirectionPortAB(char ModuleAddress, unsigned int Direction);

Returns

Nothing.

Description

The function sets Port Expander's PortA and PortB direction.

Parameters :

  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • Direction: data to be written to direction registers. Data to be written to the PortA direction register are passed in Direction's higher byte. Data to be written to the PortB direction register are passed in Direction's lower byte. Each bit corresponds to the appropriate pin of the PortA/PortB register. Set bit designates corresponding pin as input. Cleared bit designates corresponding pin as output.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Set Port Expander's PORTA to be output and PORTB to be input
Expander_Set_DirectionPortAB(0,0x00FF);

Expander_Set_PullUpsPortA

Prototype

void Expander_Set_PullUpsPortA(char ModuleAddress, char Data_);

Returns

Nothing.

Description

The function sets Port Expander's PortA pull up/down resistors.

Parameters :

  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • Data_: data for choosing pull up/down resistors configuration. Each bit corresponds to the appropriate pin of the PortA register. Set bit enables pull-up for corresponding pin.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Set Port Expander's PORTA pull-up resistors
Expander_Set_PullUpsPortA(0, 0xFF);

Expander_Set_PullUpsPortB

Prototype

void Expander_Set_PullUpsPortB(char ModuleAddress, char Data_);

Returns

Nothing.

Description

The function sets Port Expander's PortB pull up/down resistors.

Parameters :

  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • Data_: data for choosing pull up/down resistors configuration. Each bit corresponds to the appropriate pin of the PortB register. Set bit enables pull-up for corresponding pin.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Set Port Expander's PORTB pull-up resistors
Expander_Set_PullUpsPortB(0, 0xFF);

Expander_Set_PullUpsPortAB

Prototype

void Expander_Set_PullUpsPortAB(char ModuleAddress, unsigned int PullUps);

Returns

Nothing.

Description

The function sets Port Expander's PortA and PortB pull up/down resistors.

Parameters :

  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • PullUps: data for choosing pull up/down resistors configuration. PortA pull up/down resistors configuration is passed in PullUps's higher byte. PortB pull up/down resistors configuration is passed in PullUps's lower byte. Each bit corresponds to the appropriate pin of the PortA/PortB register. Set bit enables pull-up for corresponding pin.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Set Port Expander's PORTA and PORTB pull-up resistors
Expander_Set_PullUpsPortAB(0, 0xFFFF);

Library Example

The example demonstrates how to communicate with Port Expander MCP23S17.

Note that Port Expander pins A2 A1 A0 are connected to GND so Port Expander Hardware Address is 0.

Copy Code To ClipboardCopy Code To Clipboard
// 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

unsigned char i = 0;

void main() {
  ANSEL  = 0;                            // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                          // Disable comparators
  C2ON_bit = 0;
  
  TRISB = 0;                             // Set PORTB as output
  PORTB  = 0xFF;

  // If Port Expander Library uses SPI1 module
  SPI1_Init();                                   // Initialize SPI module used with PortExpander

//  If Port Expander Library uses SPI2 module
//  SPI2_Init();                                   // Initialize SPI module used with PortExpander

  Expander_Init(0);                      // Initialize Port Expander

  Expander_Set_DirectionPortA(0, 0x00);  // Set Expander's PORTA to be output

  Expander_Set_DirectionPortB(0,0xFF);   // Set Expander's PORTB to be input
  Expander_Set_PullUpsPortB(0,0xFF);     // Set pull-ups to all of the Expander's PORTB pins

  while(1) {                             // Endless loop
    Expander_Write_PortA(0, i++);        // Write i to expander's PORTA
    PORTB = Expander_Read_PortB(0);      // Read expander's PORTB and write it to LEDs
    Delay_ms(100);
  }
  
}

HW Connection

Port Expander HW connection

Port Expander HW connection

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code