Port Expander Library
The mikroPascal PRO for 8051 provides a library for communication with the Microchip’s Port Expander MCP23S17 via SPI interface. Connections of the 8051 compliant MCU and MCP23S17 is given on the schematic at the bottom of this page.
Notes:
- Library uses the SPI module for communication. The user must initialize SPI module before using the Port Expander Library.
- Library does not use Port Expander interrupts.
External dependencies of Port Expander Library
The following variables must be defined in all projects using Port Expander Library: | Description : | Example : |
---|---|---|
var SPExpanderCS : sbit; bdata; sfr; external; |
Chip Select line. | var SPExpanderCS : sbit at P1_1_bit; |
var SPExpanderRST : sbit; bdata; sfr; external; |
Reset line. | var SPExpanderRST : sbit at P1_0_bit; |
Library Routines
- Expander_Init
- 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 |
procedure Expander_Init(ModuleAddress : byte); |
---|---|
Returns |
Nothing. |
Description |
Initializes Port Expander using SPI communication. Port Expander module settings :
Parameters :
|
Requires |
SPI module needs to be initialized. See SPIx_Init and SPIx_Init_Advanced routines. |
Example |
// port expander pinout definition var SPExpanderCS : sbit at P1_1_bit; SPExpanderRST : sbit at P1_0_bit; ... SPI1_Init(); // initialize SPI module Expander_Init(0); // initialize port expander |
Expander_Read_Byte
Prototype |
function Expander_Read_Byte(ModuleAddress : byte; RegAddress : byte) : byte; |
---|---|
Returns |
Byte read. |
Description |
The function reads byte from Port Expander. Parameters :
|
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); |
Expander_Write_Byte
Prototype |
procedure Expander_Write_Byte(ModuleAddress: byte; RegAddress: byte; Data_: byte); |
---|---|
Returns |
Nothing. |
Description |
Routine writes a byte to Port Expander. Parameters :
|
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 |
function Expander_Read_PortA(ModuleAddress: byte): byte; |
---|---|
Returns |
Byte read. |
Description |
The function reads byte from Port Expander's PortA. Parameters :
|
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,0xFF); // set expander's porta to be input ... read_data := Expander_Read_PortA(0); |
Expander_Read_PortB
Prototype |
function Expander_Read_PortB(ModuleAddress: byte): byte; |
---|---|
Returns |
Byte read. |
Description |
The function reads byte from Port Expander's PortB. Parameters :
|
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,0xFF); // set expander's portb to be input ... read_data := Expander_Read_PortB(0); |
Expander_Read_PortAB
Prototype |
function Expander_Read_PortAB(ModuleAddress: byte): word; |
---|---|
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 :
|
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,0xFFFF); // set expander's porta and portb to be input ... read_data := Expander_Read_PortAB(0); |
Expander_Write_PortA
Prototype |
procedure Expander_Write_PortA(ModuleAddress: byte; Data_: byte); |
---|---|
Returns |
Nothing. |
Description |
The function writes byte to Port Expander's PortA. Parameters :
|
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 |
procedure Expander_Write_PortB(ModuleAddress: byte; Data_: byte); |
---|---|
Returns |
Nothing. |
Description |
The function writes byte to Port Expander's PortB. Parameters :
|
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 |
procedure Expander_Write_PortAB(ModuleAddress: byte; Data_: word); |
---|---|
Returns |
Nothing. |
Description |
The function writes word to Port Expander's ports. Parameters :
|
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 |
procedure Expander_Set_DirectionPortA(ModuleAddress: byte; Data_: byte); |
---|---|
Returns |
Nothing. |
Description |
The function sets Port Expander's PortA direction. Parameters :
|
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 |
procedure Expander_Set_DirectionPortB(ModuleAddress: byte; Data_: byte); |
---|---|
Returns |
Nothing. |
Description |
The function sets Port Expander's PortB direction. Parameters :
|
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 |
procedure Expander_Set_DirectionPortAB(ModuleAddress: byte; Direction: word); |
---|---|
Returns |
Nothing. |
Description |
The function sets Port Expander's PortA and PortB direction. Parameters :
|
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 |
procedure Expander_Set_PullUpsPortA(ModuleAddress: byte; Data_: byte); |
---|---|
Returns |
Nothing. |
Description |
The function sets Port Expander's PortA pull up/down resistors. Parameters :
|
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 |
procedure Expander_Set_PullUpsPortB(ModuleAddress: byte; Data_: byte); |
---|---|
Returns |
Nothing. |
Description |
The function sets Port Expander's PortB pull up/down resistors. Parameters :
|
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 |
procedure Expander_Set_PullUpsPortAB(ModuleAddress: byte; PullUps: word); |
---|---|
Returns |
Nothing. |
Description |
The function sets Port Expander's PortA and PortB pull up/down resistors. Parameters :
|
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
.
program Soft_SPI; var i, received_data : byte; // Port Expander module connections var SPExpanderRST : sbit at P0_6_bit; var SPExpanderCS : sbit at P0_7_bit; // End Port Expander module connections var SoftSpi_CLK : sbit at P0_2_bit; var SoftSpi_SDI : sbit at P0_3_bit; var SoftSpi_SDO : sbit at P0_4_bit; begin i := 0; WDTCN := 0xDE; // Sequence for WDTCN := 0xAD; // disabling the watchdog timer OSCICN := 0x83; // Enable internal oscillator (24.5MHz divided by 1) P0MDOUT := P0MDOUT or %11010101; // Configure P0.7(CS), P0.6(RST), P0.4(MOSI), P0.2(SCK) // and P0.0(TX) pin as push-pull UART1_Init(4800); // Initialize UART module at 4800 bps Delay_ms(100); // Wait for UART module to stabilize UART1_Write_Text('Start'); SPI_Rd_Ptr := @Soft_SPI_Read; // Link Port Expander Library to Software SPI library 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 TRUE do // Endless loop begin Expander_Write_PortA(0, i); // Write i to expander's PORTA Inc(i); received_data := Expander_Read_PortB(0); // Read expander's PORTB UART1_Write(received_data); // and send it to UART Delay_ms(100); end; end.
HW Connection
Port Expander HW connection
What do you think about this topic ? Send us feedback!