Port Expander Library

mikroPascal PRO for FT90x provides a library for communication with the Microchip’s Port Expander MCP23S17 via SPI interface.

  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 :
var SPExpanderRST : sbit; sfr; external; Reset line. var SPExpanderRST : sbit at GPIO_PIN32_bit;
var SPExpanderCS : sbit; sfr; external; Chip Select line. var SPExpanderCS : sbit at GPIO_PIN31_bit;

Library Routines

Expander_Init

Prototype

procedure Expander_Init(ModuleAddress : byte);

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
Returns

Nothing.

Requires

External dependencies of the library from the top of the page must be defined before using this function.

SPI module needs to be initialized. See SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// Port Expander module connections
var  SPExpanderRST           : sbit at GPIO_PIN32_bit;
var  SPExpanderCS            : sbit at GPIO_PIN31_bit;

...
SPIM1_Init();
Expander_Init(0);                      // Initialize Port Expander
Notes

None.

Expander_Init_Advanced

Prototype

procedure Expander_Init_Advanced(var rstPort : dword; rstPin : byte; haen : byte);

Description

Initializes Port Expander using SPI communication.

Parameters
  • rstPort: Port Expander's reset port
  • rstPin: Port Expander's reset pin
  • haen: Port Expander's hardware address
Returns

Nothing.

Requires

External dependencies of the library from the top of the page must be defined before using this function.

SPI module needs to be initialized. See SPIM1_Init and SPIM1_Init_Advanced routines.

Example

          
Notes

None.

Expander_Read_Byte

Prototype

function Expander_Read_Byte(ModuleAddress, RegAddress : byte) : byte;

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
Returns

Byte read.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Read a byte from Port Expander's register
var read_data : byte;
...
read_data := Expander_Read_Byte(0,1);
Notes

None.

Expander_Write_Byte

Prototype

procedure Expander_Write_Byte(ModuleAddress, RegAddress, Data : byte);

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
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Write a byte to the Port Expander's register
Expander_Write_Byte(0,1,$FF);
Notes

None.

Expander_Read_PortA

Prototype

function Expander_Read_PortA(ModuleAddress : byte) : byte;

Description

The function reads byte from Port Expander's PortA.

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

Byte read.

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
var read_data : byte;
...
Expander_Set_DirectionPortA(0,$FF);   // set expander's porta to be input
...
read_data := Expander_Read_PortA(0);
Notes

None.

Expander_Read_PortB

Prototype

function Expander_Read_PortB(ModuleAddress : byte) : byte;

Description

The function reads byte from Port Expander's PortB.

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

Byte read.

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
var read_data : byte;
...
Expander_Set_DirectionPortB(0,$FF);     // set expander's portb to be input
...
read_data := Expander_Read_PortB(0);
Notes

None.

Expander_Read_PortAB

Prototype

function Expander_Read_PortAB(ModuleAddress : byte) : word;

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
Returns

Word read.

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
var read_data : word;
...
Expander_Set_DirectionPortAB(0,$FFFF);    // set expander's porta and portb to be input
...
read_data := Expander_Read_PortAB(0);
Notes

None.

Expander_Write_PortA

Prototype

procedure Expander_Write_PortA(ModuleAddress, Data : byte);

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
Returns

Nothing.

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,$00);    // set expander's porta to be output
...
Expander_Write_PortA(0, $AA);
Notes

None.

Expander_Write_PortB

Prototype

procedure Expander_Write_PortB(ModuleAddress, Data : byte);

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
Returns

Nothing.

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,$00);   // set expander's portb to be output
...
Expander_Write_PortB(0,$55);
Notes

None.

Expander_Write_PortAB

Prototype

procedure Expander_Write_PortAB(ModuleAddress : byte; Data : word);

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
Returns

Nothing.

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, $0000);    // set expander's porta and portb to be output
...
Expander_Write_PortAB(0, $AA55);
Notes

None.

Expander_SetBit_PortA

Prototype

procedure Expander_SetBit_PortA(ModuleAddress : byte; BitMask : byte);

Description

The function sets bits designated by the BitMask parameter to Port Expander's PortA.

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • BitMask: appropriate bitmask.
Returns

Nothing.

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
// Sets lower nibble of the PortA
...
Expander_Set_DirectionPortA(0, $0000);    // set expander's PortA to be output
...
Expander_SetBit_PortA(0, 0x0F);
Notes

None.

Expander_ClrBit_PortA

Prototype

procedure Expander_ClrBit_PortA(ModuleAddress : byte; BitMask : byte);

Description

The function clears bits designated by the BitMask parameter in Port Expander's PortA.

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • BitMask: appropriate bitmask.
Returns

Nothing.

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
// Clears lower nibble of the PortA
...
Expander_Set_DirectionPortA(0, $0000);    // set expander's PortA to be output
...
Expander_ClrBit_PortA(0, 0x0F);
Notes

None.

Expander_ToggleBit_PortA

Prototype

procedure Expander_ToggleBit_PortA(ModuleAddress : byte; BitMask : byte);

Description

The function toggles bits designated by the BitMask parameter in Port Expander's PortA.

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • BitMask: appropriate bitmask.
Returns

Nothing.

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
// Toggles lower nibble of the PortA
...
Expander_Set_DirectionPortA(0, $0000);    // set expander's PortA to be output
...
Expander_ToggleBit_PortA(0, 0x0F);
Notes

None.

Expander_SetBit_PortB

Prototype

procedure Expander_SetBit_PortB(ModuleAddress : byte; BitMask : byte);

Description

The function sets bits designated by the BitMask parameter to Port Expander's PortB.

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • BitMask: appropriate bitmask.
Returns

Nothing.

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
// Sets lower nibble of the PortB
...
Expander_Set_DirectionPortB(0, $0000);    // set expander's PortB to be output
...
Expander_SetBit_PortB(0, 0x0F);
Notes

None.

Expander_ClrBit_PortB

Prototype

procedure Expander_ClrBit_PortB(ModuleAddress : byte; BitMask : byte);

Description

The function clears bits designated by the BitMask parameter in Port Expander's PortB.

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • BitMask: appropriate bitmask.
Returns

Nothing.

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
// Clears lower nibble of the PortB
...
Expander_Set_DirectionPortB(0, $0000);    // set expander's PortB to be output
...
Expander_ClrBit_PortB(0, 0x0F);
Notes

None.

Expander_ToggleBit_PortB

Prototype

procedure Expander_ToggleBit_PortB(ModuleAddress : byte; BitMask : byte);

Description

The function toggles bits designated by the BitMask parameter in Port Expander's PortB.

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • BitMask: appropriate bitmask.
Returns

Nothing.

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
// Toggles lower nibble of the PortB
...
Expander_Set_DirectionPortB(0, $0000);    // set expander's PortB to be output
...
Expander_ToggleBit_PortB(0, 0x0F);
Notes

None.

Expander_Set_DirectionPortA

Prototype

procedure Expander_Set_DirectionPortA(ModuleAddress, Data : byte);

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.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

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

None.

Expander_Set_DirectionPortB

Prototype

procedure Expander_Set_DirectionPortB(ModuleAddress, Data : byte);

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.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

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

None.

Expander_Set_DirectionPortAB

Prototype

procedure Expander_Set_DirectionPortAB(ModuleAddress, Direction : word);

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.
Returns

Nothing.

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,$00FF);
Notes

None.

Expander_Set_InputDirPortA

Prototype

procedure Expander_Set_InputDirPortA(ModuleAddress, BitMask : byte);

Description

The function sets the desired Port Expander's PortA pins as input according to the BitMask parameter.

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • BitMask: appropriate bitmask.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Set Port Expander's PORTA lower four pins as input :
Expander_Set_InputDirPortA(0, 0x0F);
Notes

None.

Expander_Set_OutputDirPortA

Prototype

procedure Expander_Set_OutputDirPortA(ModuleAddress, BitMask : byte);

Description

The function sets the desired Port Expander's PortB pins as input according to the BitMask parameter.

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • BitMask: appropriate bitmask.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Set Port Expander's PORTA lower four pins as output :
Expander_Set_OutputDirPortA(0, 0x0F);
Notes

None.

Expander_Set_InputDirPortB

Prototype

procedure Expander_Set_InputDirPortB(ModuleAddress, BitMask : byte);

Description

The function sets the desired Port Expander's PortB pins as input according to the BitMask parameter.

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • BitMask: appropriate bitmask.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Set Port Expander's PORTB lower four pins as input :
Expander_Set_InputDirPortB(0, 0x0F);
Notes

None.

Expander_Set_OutputDirPortB

Prototype

procedure Expander_Set_OutputDirPortB(ModuleAddress, BitMask : byte);

Description

The function sets the desired Port Expander's PortB pins as input according to the BitMask parameter.

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • BitMask: appropriate bitmask.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Set Port Expander's PORTB lower four pins as output :
Expander_Set_OutputDirPortB(0, 0x0F);
Notes

None.

Expander_Set_PullUpsPortA

Prototype

procedure Expander_Set_PullUpsPortA(ModuleAddress, Data : byte);

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.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

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

None.

Expander_Set_PullUpsPortB

Prototype

procedure Expander_Set_PullUpsPortB(ModuleAddress, Data : byte);

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.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

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

None.

Expander_Set_PullUpsPortAB

Prototype

procedure Expander_Set_PullUpsPortAB(ModuleAddress : byte; PullUps : word);

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.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

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

None.

Expander_SetBits

Prototype

procedure Expander_SetBits(ModuleAddress : byte; RegAddress : byte; BitMask : byte);

Description

The function sets bits designated by the BitMask parameter to Port Expander's internal register (RegAddress parameter).

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • RegAddress: Port Expander's internal register address
  • BitMask: appropriate bitmask.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Sets lower nibble at the register located on the address 1 of the Port Expander
Expander_SetBits(0, 1, 0x0F);
Notes

None.

Expander_ClrBits

Prototype

procedure Expander_ClrBits(ModuleAddress : byte; RegAddress : byte; BitMask : byte);

Description

The function clears bits designated by the BitMask parameter in the Port Expander's internal register (RegAddress parameter).

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • RegAddress: Port Expander's internal register address
  • BitMask: appropriate bitmask.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Clears lower nibble in the register located on the address 1 of the Port Expander
Expander_ClrBits(0, 1, 0x0F);
Notes

None.

Expander_ToggleBits

Prototype

procedure Expander_ToggleBits(ModuleAddress : byte; RegAddress : byte; BitMask : byte);

Description

The function toggles bits designated by the BitMask parameter in the Port Expander's internal register (RegAddress parameter).

Parameters
  • ModuleAddress: Port Expander hardware address, see schematic at the bottom of this page
  • RegAddress: Port Expander's internal register address
  • BitMask: appropriate bitmask.
Returns

Nothing.

Requires

Port Expander must be initialized. See Expander_Init.

Example
// Toggles lower nibble in the register located on the address 1 of the Port Expander
Expander_ToggleBits(0, 1, 0x0F);
Notes

None.

Copyright (c) 2002-2015 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