Port Expander Library
The mikroC PRO for ARM provides a library for communication with the Microchip’s Port Expander MCP23S17 via SPI interface.

- The library uses the SPI module for communication. User must initialize the appropriate SPI module before using the Port Expander Library.
- For MCUs with multiple SPI modules it is possible to initialize all of them and then switch by using the
SPI_Set_Active()
function. See the SPI Library functions. - Library does not use Port Expander interrupts.
Library Dependency Tree

External dependencies of Port Expander Library
Stellaris
The following variables must be defined in all projects using Port Expander Library: | Description : | Example : |
---|---|---|
extern sfr sbit SPExpanderRST; |
Reset line. | sbit SPExpanderRST at GPIO_PORTA_DATA0_bit; |
extern sfr sbit SPExpanderCS; |
Chip Select line. | sbit SPExpanderCS at GPIO_PORTA_DATA1_bit; |
extern sfr sbit SPExpanderRST_Direction; |
Direction of the Reset pin. | sbit SPExpanderRST_Direction at GPIO_PORTA_DIR0_bit; |
extern sfr sbit SPExpanderCS_Direction; |
Direction of the Chip Select pin. | sbit SPExpanderCS_Direction at GPIO_PORTA_DIR1_bit; |
MSP432
The following variables must be defined in all projects using Port Expander Library: | Description : | Example : |
---|---|---|
extern sfr sbit SPExpanderRST; |
Reset line. | sbit SPExpanderRST at DIO_P6OUT.B0; |
extern sfr sbit SPExpanderCS; |
Chip Select line. | sbit SPExpanderCS at DIO_P6OUT.B1; |
extern sfr sbit SPExpanderRST_Direction; |
Direction of the Reset pin. | sbit SPExpanderRST_Direction at DIO_P6DIR.B0; |
extern sfr sbit SPExpanderCS_Direction; |
Direction of the Chip Select pin. | sbit SPExpanderCS_Direction at DIO_P6DIR.B1; |
STM32
The following variables must be defined in all projects using Port Expander Library: | Description : | Example : |
---|---|---|
extern sfr sbit SPExpanderRST; |
Reset line. | sbit SPExpanderRST at GPIOB_ODR.B0; |
extern sfr sbit SPExpanderCS; |
Chip Select line. | sbit SPExpanderCS at GPIOB_ODR.B1; |
CEC1x02
The following variables must be defined in all projects using Port Expander Library: | Description : | Example : |
---|---|---|
extern sfr sbit SPExpanderRST; |
Reset line. | sbit SPExpanderRST at GPIO_OUTPUT_PIN_027_bit; |
extern sfr sbit SPExpanderCS; |
Chip Select line. | sbit SPExpanderCS at GPIO_OUTPUT_PIN_146_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_SetBit_PortA
- Expander_ClrBit_PortA
- Expander_ToggleBit_PortA
- Expander_SetBit_PortB
- Expander_ClrBit_PortB
- Expander_ToggleBit_PortB
- Expander_Set_DirectionPortA
- Expander_Set_DirectionPortB
- Expander_Set_DirectionPortAB
- Expander_Set_InputDirPortA
- Expander_Set_OutputDirPortA
- Expander_Set_InputDirPortB
- Expander_Set_OutputDirPortB
- Expander_Set_PullUpsPortA
- Expander_Set_PullUpsPortB
- Expander_Set_PullUpsPortAB
- Expander_SetBits
- Expander_ClrBits
- Expander_ToggleBits
Expander_Init
Prototype |
void Expander_Init(char ModuleAddress); |
---|---|
Description |
Initializes Port Expander using SPI communication. Port Expander module settings :
|
Parameters |
|
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 SPIx_Init and SPIx_Init_Advanced routines. |
Example |
Stellaris// Port Expander module connections sbit SPExpanderRST at GPIO_PORTA_DATA0_bit; sbit SPExpanderCS at GPIO_PORTA_DATA1_bit; sbit SPExpanderRST_Direction at GPIO_PORTA_DIR0_bit; sbit SPExpanderCS_Direction at GPIO_PORTA_DIR1_bit; // End Port Expander module connections ... SPI0_Init(); // Initialize SPI module used with PortExpander Expander_Init(0); // Initialize Port Expander MSP432// Port Expander module connections sbit SPExpanderRST at DIO_P6OUT.B0; sbit SPExpanderCS at DIO_P6OUT.B1; sbit SPExpanderRST_Direction at DIO_P6DIR.B0; sbit SPExpanderCS_Direction at DIO_P6DIR.B1; // End Port Expander module connections ... SPI1_Init(); // Initialize SPI module used with PortExpander Expander_Init(0); // Initialize Port Expander STM32// Port Expander module connections sbit SPExpanderRST at GPIOB_ODR.B0; sbit SPExpanderCS at GPIOB_ODR.B1; // End Port Expander module connections ... // Initialize SPI module used with PortExpander SPI1_Init_Advanced(_SPI_FPCLK_DIV4, _SPI_MASTER | _SPI_8_BIT | _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION | _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1, &_GPIO_MODULE_SPI1_PB345); Expander_Init(0); // Initialize Port Expander CEC1x02// Port Expander module connections sbit SPExpanderRST at GPIO_OUTPUT_PIN_027_bit; sbit SPExpanderCS at GPIO_OUTPUT_PIN_146_bit; // End Port Expander module connections ... // Initialize SPI module used with PortExpander SPI0_Init_Advanced(1000000,0,0); Expander_Init(0); // Initialize Port Expander |
Notes |
None. |
Expander_Init_Advanced
Prototype |
void Expander_Init_Advanced(unsigned long *rstPort, char rstPin, char haen); |
---|---|
Description |
Initializes Port Expander using SPI communication. |
Parameters |
|
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 SPIx_Init and SPIx_Init_Advanced routines. |
Example |
Stellaris// Port Expander module connections sbit SPExpanderRST at GPIO_PORTA_DATA0_bit; sbit SPExpanderCS at GPIO_PORTA_DATA1_bit; sbit SPExpanderRST_Direction at GPIO_PORTA_DIR0_bit; sbit SPExpanderCS_Direction at GPIO_PORTA_DIR1_bit; // End Port Expander module connections ... // If Port Expander Library uses SPI module SPI1_Init(); // Initialize SPI1 module used with PortExpander Expander_Init_Advanced(&GPIO_PORTB, 0, 0); // Initialize Port Expander MSP432// Port Expander module connections sbit SPExpanderRST at DIO_P6OUT.B0; sbit SPExpanderCS at DIO_P6OUT.B1; sbit SPExpanderRST_Direction at DIO_P6DIR.B0; sbit SPExpanderCS_Direction at DIO_P6DIR.B1; // End Port Expander module connections ... // If Port Expander Library uses SPI module SPI1_Init(); // Initialize SPI1 module used with PortExpander Expander_Init_Advanced(&GPIO_PORTB, 0, 0); // Initialize Port Expander STM32// Port Expander module connections sbit SPExpanderRST at GPIOB_ODR.B0; sbit SPExpanderCS at GPIOB_ODR.B1; // End Port Expander module connections ... // Initialize SPI module used with PortExpander SPI1_Init_Advanced(_SPI_FPCLK_DIV4, _SPI_MASTER | _SPI_8_BIT | _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION | _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1, &_GPIO_MODULE_SPI1_PB345); Expander_Init_Advanced(&GPIOB_BASE, 0, 0); // Initialize Port Expander CEC1x02// Port Expander module connections sbit SPExpanderRST at GPIO_OUTPUT_PIN_027_bit; sbit SPExpanderCS at GPIO_OUTPUT_PIN_146_bit; // End Port Expander module connections ... // Initialize SPI module used with PortExpander SPI0_Init_Advanced(1000000,0,0); Expander_Init_Advanced(&_GPIO_PORT_020_027, 0, 0); // Initialize Port Expander |
Notes |
None. |
Expander_Read_Byte
Prototype |
char Expander_Read_Byte(char ModuleAddress, char RegAddress); |
---|---|
Description |
The function reads byte from Port Expander. |
Parameters |
|
Returns |
Byte read. |
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); |
Notes |
None. |
Expander_Write_Byte
Prototype |
void Expander_Write_Byte(char ModuleAddress, char RegAddress, char data_); |
---|---|
Description |
Routine writes a byte to Port Expander. |
Parameters |
|
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,0xFF); |
Notes |
None. |
Expander_Read_PortA
Prototype |
char Expander_Read_PortA(char ModuleAddress); |
---|---|
Description |
The function reads byte from Port Expander's PortA. |
Parameters |
|
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 char read_data; ... Expander_Set_DirectionPortA(0,0xFF); // set expander's porta to be input ... read_data = Expander_Read_PortA(0); |
Notes |
None. |
Expander_Read_PortB
Prototype |
char Expander_Read_PortB(char ModuleAddress); |
---|---|
Description |
The function reads byte from Port Expander's PortB. |
Parameters |
|
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 char read_data; ... Expander_Set_DirectionPortB(0,0xFF); // set expander's portb to be input ... read_data = Expander_Read_PortB(0); |
Notes |
None. |
Expander_Read_PortAB
Prototype |
unsigned int Expander_Read_PortAB(char ModuleAddress); |
---|---|
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 |
|
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 unsigned int read_data; ... Expander_Set_DirectionPortAB(0,0xFFFF); // set expander's porta and portb to be input ... read_data = Expander_Read_PortAB(0); |
Notes |
None. |
Expander_Write_PortA
Prototype |
void Expander_Write_PortA(char ModuleAddress, char Data_); |
---|---|
Description |
The function writes byte to Port Expander's PortA. |
Parameters |
|
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,0x00); // set expander's porta to be output ... Expander_Write_PortA(0, 0xAA); |
Notes |
None. |
Expander_Write_PortB
Prototype |
void Expander_Write_PortB(char ModuleAddress, char Data_); |
---|---|
Description |
The function writes byte to Port Expander's PortB. |
Parameters |
|
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,0x00); // set expander's portb to be output ... Expander_Write_PortB(0, 0x55); |
Notes |
None. |
Expander_Write_PortAB
Prototype |
void Expander_Write_PortAB(char ModuleAddress, unsigned int Data_); |
---|---|
Description |
The function writes word to Port Expander's ports. |
Parameters |
|
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,0x0000); // set expander's porta and portb to be output ... Expander_Write_PortAB(0, 0xAA55); |
Notes |
None. |
Expander_SetBit_PortA
Prototype |
void Expander_SetBit_PortA(char ModuleAddress, BitMask : word); |
---|---|
Description |
The function sets bits designated by the |
Parameters |
|
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 |
void Expander_ClrBit_PortA(char ModuleAddress, BitMask : word); |
---|---|
Description |
The function clears bits designated by the |
Parameters |
|
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 |
void Expander_ToggleBit_PortA(char ModuleAddress, BitMask : word); |
---|---|
Description |
The function toggles bits designated by the |
Parameters |
|
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 |
void Expander_SetBit_PortB(char ModuleAddress, BitMask : word); |
---|---|
Description |
The function sets bits designated by the |
Parameters |
|
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 |
void Expander_ClrBit_PortB(char ModuleAddress, BitMask : word); |
---|---|
Description |
The function clears bits designated by the |
Parameters |
|
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 |
void Expander_ToggleBit_PortB(char ModuleAddress, BitMask : word); |
---|---|
Description |
The function toggles bits designated by the |
Parameters |
|
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 |
void Expander_Set_DirectionPortA(char ModuleAddress, char Data_); |
---|---|
Description |
The function sets Port Expander's PortA direction. |
Parameters |
|
Returns |
Nothing. |
Requires |
Port Expander must be initialized. See Expander_Init. |
Example |
// Set Port Expander's PORTA to be output Expander_Set_DirectionPortA(0,0x00); |
Notes |
None. |
Expander_Set_DirectionPortB
Prototype |
void Expander_Set_DirectionPortB(char ModuleAddress, char Data_); |
---|---|
Description |
The function sets Port Expander's PortB direction. |
Parameters |
|
Returns |
Nothing. |
Requires |
Port Expander must be initialized. See Expander_Init. |
Example |
// Set Port Expander's PORTB to be input Expander_Set_DirectionPortB(0,0xFF); |
Notes |
None. |
Expander_Set_DirectionPortAB
Prototype |
void Expander_Set_DirectionPortAB(char ModuleAddress, unsigned int Direction); |
---|---|
Description |
The function sets Port Expander's PortA and PortB direction. |
Parameters |
|
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,0x00FF); |
Notes |
None. |
Expander_Set_InputDirPortA
Prototype |
void Expander_Set_InputDirPortA(ModuleAddress, char BitMask); |
---|---|
Description |
The function sets the desired Port Expander's PortA pins as input according to the |
Parameters |
|
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 |
void Expander_Set_OutputDirPortA(ModuleAddress, char BitMask); |
---|---|
Description |
The function sets the desired Port Expander's PortB pins as input according to the |
Parameters |
|
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 |
void Expander_Set_InputDirPortB(ModuleAddress, char BitMask); |
---|---|
Description |
The function sets the desired Port Expander's PortB pins as input according to the |
Parameters |
|
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 |
void Expander_Set_OutputDirPortB(ModuleAddress, char BitMask); |
---|---|
Description |
The function sets the desired Port Expander's PortB pins as input according to the |
Parameters |
|
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 |
void Expander_Set_PullUpsPortA(char ModuleAddress, char Data_); |
---|---|
Description |
The function sets Port Expander's PortA pull up/down resistors. |
Parameters |
|
Returns |
Nothing. |
Requires |
Port Expander must be initialized. See Expander_Init. |
Example |
// Set Port Expander's PORTA pull-up resistors Expander_Set_PullUpsPortA(0, 0xFF); |
Notes |
None. |
Expander_Set_PullUpsPortB
Prototype |
void Expander_Set_PullUpsPortB(char ModuleAddress, char Data_); |
---|---|
Description |
The function sets Port Expander's PortB pull up/down resistors. |
Parameters |
|
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 |
void Expander_Set_PullUpsPortAB(char ModuleAddress, unsigned int PullUps); |
---|---|
Description |
The function sets Port Expander's PortA and PortB pull up/down resistors. |
Parameters |
|
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, 0xFFFF); |
Notes |
None. |
Expander_SetBits
Prototype |
void Expander_SetBits(char ModuleAddress, char RegAddress, char BitMask); |
---|---|
Description |
The function sets bits designated by the |
Parameters |
|
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 |
void Expander_ClrBits(char ModuleAddress, char RegAddress, char BitMask); |
---|---|
Description |
The function clears bits designated by the |
Parameters |
|
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 |
void Expander_ToggleBits(char ModuleAddress, char RegAddress, char BitMask); |
---|---|
Description |
The function toggles bits designated by the |
Parameters |
|
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. |
What do you think about this topic ? Send us feedback!