Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: relaekort_til_motorstyring Nucleo_MCP23S17_Test BSM02 POT_V_1_1
MCP23S17 Class Reference
Interface to the MCP23S17 16-Bit I/O Expander with Serial Interface (SPI) More...
#include <MCP23S17.h>
Public Member Functions | |
MCP23S17 (int hardwareaddress, SPI &spi, PinName nCs, PinName nReset) | |
Create an instance of the MCP23S17 connected via specfied SPI instance, with the specified address. | |
MCP23S17 (int hardwareaddress, SPI &spi, PinName nCs) | |
Create an instance of the MCP23S17 connected via specfied SPI instance, with the specified address, and Global reset only. | |
char | read (char reg_address) |
Read an Register address. | |
void | write (char reg_address, char data) |
Write to Register address. | |
void | bit (char reg_address, int bitnumber, bool high_low) |
Write to Bit in a register. | |
void | reset () |
Resetting the MCP23S17. | |
char | iodira () |
Read IODIRA. | |
void | iodira (char data) |
Write to IODIRA. | |
char | iodirb () |
Read IODIRB. | |
void | iodirb (char data) |
Write to IODIRB. | |
char | ipola () |
Read IPOLA. | |
void | ipola (char data) |
Write to IPOLA. | |
char | ipolb () |
Read IPOLB. | |
void | ipolb (char data) |
Write to IPOLB. | |
char | gpintena () |
Read GPINTENA. | |
void | gpintena (char data) |
Write to GPINTENA. | |
char | gpintenb () |
Read GPINTENB. | |
void | gpintenb (char data) |
Write to GPINTENB. | |
char | defvala () |
Read DEFVALA. | |
void | defvala (char data) |
Write to DEFVALA. | |
char | defvalb () |
Read DEFVALB. | |
void | defvalb (char data) |
Write to DEFVALB. | |
char | intcona () |
Read INTCONA. | |
void | intcona (char data) |
Write to INTCONA. | |
char | intconb () |
Read INTCONB. | |
void | intconb (char data) |
Write to INTCONB. | |
char | iocon () |
Read IOCON. | |
void | iocon (char data) |
Write to IOCON. | |
char | gppua () |
Read GPPUA. | |
void | gppua (char data) |
Write to GPPUA. | |
char | gppub () |
Read GPPUB. | |
void | gppub (char data) |
Write to GPPUB. | |
char | intfa () |
Read INTFA. | |
char | intfb () |
Read INTFB. | |
char | intcapa () |
Read INTCAPA. | |
char | intcapb () |
Read INTCAPB. | |
char | gpioa () |
Read GPIOA. | |
void | gpioa (char data) |
Write to GPIOA. | |
char | gpiob () |
Read GPIOB. | |
void | gpiob (char data) |
Write to GPIOB. | |
char | olata () |
Read OLATA. | |
void | olata (char data) |
Write to OLATA. | |
char | olatb () |
Read OLATB. | |
void | olatb (char data) |
Write to OLATB. | |
void | intmirror (bool mirror) |
Write to IOCON.MIRROR. | |
void | intpol (bool polarity) |
Write to IOCON.INTPOL. |
Detailed Description
Interface to the MCP23S17 16-Bit I/O Expander with Serial Interface (SPI)
Using the driver:
- remenber to setup SPI in main routine.
- remenber to setup interrupt pin or pins in main routine (if you are using interrupts).
Defaults in this driver:
- as default is hardware adressing "On" and if disable use "0" in hardwareaddress when creating the instance.
- as default is interrupt pins "Active High".
- as default is INTA is associated with PortA and INTB is associated with PortB.
Limitations of using this driver:
- can't use Open-Drain output.
- can't use Sequential Operation mode bit.
- can't use BANK 1 addressing.
Definition at line 83 of file MCP23S17.h.
Constructor & Destructor Documentation
MCP23S17 | ( | int | hardwareaddress, |
SPI & | spi, | ||
PinName | nCs, | ||
PinName | nReset | ||
) |
Create an instance of the MCP23S17 connected via specfied SPI instance, with the specified address.
- Parameters:
-
hardwareaddress The SPI hardware address 0-7 for this MCP23S17. spi The mbed SPI instance (make in main routine) nCs The SPI chip select pin. nReset The Hardware reset pin.
Definition at line 30 of file MCP23S17.cpp.
MCP23S17 | ( | int | hardwareaddress, |
SPI & | spi, | ||
PinName | nCs | ||
) |
Create an instance of the MCP23S17 connected via specfied SPI instance, with the specified address, and Global reset only.
- Parameters:
-
hardwareaddress The SPI hardware address 0-7 for this MCP23S17. spi The mbed SPI instance (make in main routine) nCs The SPI chip select pin.
Definition at line 36 of file MCP23S17.cpp.
Member Function Documentation
void bit | ( | char | reg_address, |
int | bitnumber, | ||
bool | high_low | ||
) |
Write to Bit in a register.
- Parameters:
-
reg_adress The selected register to write to. bit The bit with to write in, values from 1 to 8 high_low The value to write the bit True = '1' and False = '0'.
Definition at line 52 of file MCP23S17.cpp.
char defvala | ( | ) |
Read DEFVALA.
DEFAULT COMPARE REGISTER FOR INTERRUPT-ON-CHANGE The default comparison value is configured in the DEFVAL register, If enabled (via GPINTEN and INTCON).
- Returns:
- The 8 bits read.
Definition at line 141 of file MCP23S17.cpp.
void defvala | ( | char | data ) |
Write to DEFVALA.
DEFAULT COMPARE REGISTER FOR INTERRUPT-ON-CHANGE The default comparison value is configured in the DEFVAL register, If enabled (via GPINTEN and INTCON).
- Parameters:
-
data The 8 bits to write to DEVALA register.
Definition at line 146 of file MCP23S17.cpp.
char defvalb | ( | ) |
Read DEFVALB.
DEFAULT COMPARE REGISTER FOR INTERRUPT-ON-CHANGE The default comparison value is configured in the DEFVAL register, If enabled (via GPINTEN and INTCON).
- Returns:
- The 8 bits read.
Definition at line 151 of file MCP23S17.cpp.
void defvalb | ( | char | data ) |
Write to DEFVALB.
DEFAULT COMPARE REGISTER FOR INTERRUPT-ON-CHANGE The default comparison value is configured in the DEFVAL register, If enabled (via GPINTEN and INTCON).
- Parameters:
-
data The 8 bits to write to DEVALB register.
Definition at line 156 of file MCP23S17.cpp.
char gpintena | ( | ) |
Read GPINTENA.
INTERRUPT-ON-CHANGE CONTROL REGISTER The GPINTEN register controls the interrupt-onchange feature for each pin.
- Returns:
- The 8 bits read.
Definition at line 121 of file MCP23S17.cpp.
void gpintena | ( | char | data ) |
Write to GPINTENA.
INTERRUPT-ON-CHANGE CONTROL REGISTER The GPINTEN register controls the interrupt-onchange feature for each pin.
- Parameters:
-
data The 8 bits to write to GPINTENA register.
Definition at line 126 of file MCP23S17.cpp.
void gpintenb | ( | char | data ) |
Write to GPINTENB.
INTERRUPT-ON-CHANGE CONTROL REGISTER The GPINTEN register controls the interrupt-onchange feature for each pin.
- Parameters:
-
data The 8 bits to write to GPINTENB register.
Definition at line 136 of file MCP23S17.cpp.
char gpintenb | ( | ) |
Read GPINTENB.
INTERRUPT-ON-CHANGE CONTROL REGISTER The GPINTEN register controls the interrupt-onchange feature for each pin.
- Returns:
- The 8 bits read.
Definition at line 131 of file MCP23S17.cpp.
char gpioa | ( | ) |
Read GPIOA.
PORT REGISTER The GPIO register reflects the value on the port. Reading from this register reads the port. Writing to this register modifies the Output Latch (OLAT) register.
- Returns:
- The 8 bits read.
Definition at line 231 of file MCP23S17.cpp.
void gpioa | ( | char | data ) |
Write to GPIOA.
PORT REGISTER The GPIO register reflects the value on the port. Reading from this register reads the port. Writing to this register modifies the Output Latch (OLAT) register.
- Parameters:
-
data The 8 bits to write to GPIOA register.
Definition at line 236 of file MCP23S17.cpp.
char gpiob | ( | ) |
Read GPIOB.
PORT REGISTER The GPIO register reflects the value on the port. Reading from this register reads the port. Writing to this register modifies the Output Latch (OLAT) register.
- Returns:
- The 8 bits read.
Definition at line 241 of file MCP23S17.cpp.
void gpiob | ( | char | data ) |
Write to GPIOB.
PORT REGISTER The GPIO register reflects the value on the port. Reading from this register reads the port. Writing to this register modifies the Output Latch (OLAT) register.
- Parameters:
-
data The 8 bits to write to GPIOB register.
Definition at line 246 of file MCP23S17.cpp.
char gppua | ( | ) |
Read GPPUA.
PULL-UP RESISTOR CONFIGURATION REGISTER The GPPU register controls the pull-up resistors for the port pins.
- Returns:
- The 8 bits read.
Definition at line 191 of file MCP23S17.cpp.
void gppua | ( | char | data ) |
Write to GPPUA.
PULL-UP RESISTOR CONFIGURATION REGISTER The GPPU register controls the pull-up resistors for the port pins.
- Parameters:
-
data The 8 bits to write to GPPUA register.
Definition at line 196 of file MCP23S17.cpp.
char gppub | ( | ) |
Read GPPUB.
PULL-UP RESISTOR CONFIGURATION REGISTER The GPPU register controls the pull-up resistors for the port pins.
- Returns:
- The 8 bits read.
Definition at line 201 of file MCP23S17.cpp.
void gppub | ( | char | data ) |
Write to GPPUB.
PULL-UP RESISTOR CONFIGURATION REGISTER The GPPU register controls the pull-up resistors for the port pins.
- Parameters:
-
data The 8 bits to write to GPPUB register.
Definition at line 206 of file MCP23S17.cpp.
char intcapa | ( | ) |
Read INTCAPA.
INTERRUPT CAPTURE REGISTER - READ ONLY The INTCAP register captures the GPIO port value at the time the interrupt occurred. The register is ‘read only’ and is updated only when an interrupt occurs.
- Returns:
- The 8 bits read.
Definition at line 221 of file MCP23S17.cpp.
char intcapb | ( | ) |
Read INTCAPB.
INTERRUPT CAPTURE REGISTER - READ ONLY The INTCAP register captures the GPIO port value at the time the interrupt occurred. The register is ‘read only’ and is updated only when an interrupt occurs.
- Returns:
- The 8 bits read.
Definition at line 226 of file MCP23S17.cpp.
char intcona | ( | ) |
Read INTCONA.
INTERRUPT CONTROL REGISTER The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature.
- Returns:
- The 8 bits read.
Definition at line 161 of file MCP23S17.cpp.
void intcona | ( | char | data ) |
Write to INTCONA.
INTERRUPT CONTROL REGISTER The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature.
- Parameters:
-
data The 8 bits to write to INTCONA register.
Definition at line 166 of file MCP23S17.cpp.
char intconb | ( | ) |
Read INTCONB.
INTERRUPT CONTROL REGISTER The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature.
- Returns:
- The 8 bits read.
Definition at line 171 of file MCP23S17.cpp.
void intconb | ( | char | data ) |
Write to INTCONB.
INTERRUPT CONTROL REGISTER The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature.
- Parameters:
-
data The 8 bits to write to INTCONB register.
Definition at line 176 of file MCP23S17.cpp.
char intfa | ( | ) |
Read INTFA.
INTERRUPT FLAG REGISTER - READ ONLY The INTF register reflects the interrupt condition on the port pins of any pin that is enabled for interrupts via the GPINTEN register.
- Returns:
- The 8 bits read.
Definition at line 211 of file MCP23S17.cpp.
char intfb | ( | ) |
Read INTFB.
INTERRUPT FLAG REGISTER - READ ONLY The INTF register reflects the interrupt condition on the port pins of any pin that is enabled for interrupts via the GPINTEN register.
- Returns:
- The 8 bits read.
Definition at line 216 of file MCP23S17.cpp.
void intmirror | ( | bool | mirror ) |
Write to IOCON.MIRROR.
IOCON REGISTER - INTERRUPT MIRROR BIT 1 = The INT pins are internally connected 0 = The INT pins are not connected. INTA is associated with PortA and INTB is associated with PortB
- Parameters:
-
mirror write true ('1') or false ('0').
Definition at line 271 of file MCP23S17.cpp.
void intpol | ( | bool | polarity ) |
Write to IOCON.INTPOL.
IOCON REGISTER - INTERRUPT POLARITY BIT This bit sets the polarity of the INT output pin. 1 = Active-high. 0 = Active-low.
- Parameters:
-
polarity write true ('1') or false ('0').
Definition at line 285 of file MCP23S17.cpp.
char iocon | ( | ) |
Read IOCON.
CONFIGURATION REGISTER The IOCON register contains several bits for configuring the device.
- Returns:
- The 8 bits read.
Definition at line 181 of file MCP23S17.cpp.
void iocon | ( | char | data ) |
Write to IOCON.
CONFIGURATION REGISTER The IOCON register contains several bits for configuring the device.
- Parameters:
-
data The 8 bits to write to IOCON register.
Definition at line 186 of file MCP23S17.cpp.
char iodira | ( | ) |
Read IODIRA.
I/O DIRECTION REGISTER Controls the direction of the data I/O.
- Returns:
- The 8 bits read.
Definition at line 81 of file MCP23S17.cpp.
void iodira | ( | char | data ) |
Write to IODIRA.
I/O DIRECTION REGISTER Controls the direction of the data I/O.
- Parameters:
-
data The 8 bits to write to IODIRA register.
Definition at line 86 of file MCP23S17.cpp.
void iodirb | ( | char | data ) |
Write to IODIRB.
I/O DIRECTION REGISTER Controls the direction of the data I/O.
- Parameters:
-
data The 8 bits to write to IODIRB register.
Definition at line 96 of file MCP23S17.cpp.
char iodirb | ( | ) |
Read IODIRB.
I/O DIRECTION REGISTER Controls the direction of the data I/O.
- Returns:
- The 8 bits read.
Definition at line 91 of file MCP23S17.cpp.
char ipola | ( | ) |
Read IPOLA.
INPUT POLARITY REGISTER This register allows the user to configure the polarity on the corresponding GPIO port bits.
- Returns:
- The 8 bits read.
Definition at line 101 of file MCP23S17.cpp.
void ipola | ( | char | data ) |
Write to IPOLA.
INPUT POLARITY REGISTER This register allows the user to configure the polarity on the corresponding GPIO port bits.
- Parameters:
-
data The 8 bits to write to IPOLA register.
Definition at line 106 of file MCP23S17.cpp.
char ipolb | ( | ) |
Read IPOLB.
INPUT POLARITY REGISTER This register allows the user to configure the polarity on the corresponding GPIO port bits.
- Returns:
- The 8 bits read.
Definition at line 111 of file MCP23S17.cpp.
void ipolb | ( | char | data ) |
Write to IPOLB.
INPUT POLARITY REGISTER This register allows the user to configure the polarity on the corresponding GPIO port bits.
- Parameters:
-
data The 8 bits to write to IPOLB register.
Definition at line 116 of file MCP23S17.cpp.
char olata | ( | ) |
Read OLATA.
OUTPUT LATCH REGISTER The OLAT register provides access to the output latches. A read from this register results in a read of the OLAT and not the port itself. A write to this register modifies the output latches that modifies the pins configured as outputs.
- Returns:
- The 8 bits read.
Definition at line 251 of file MCP23S17.cpp.
void olata | ( | char | data ) |
Write to OLATA.
OUTPUT LATCH REGISTER The OLAT register provides access to the output latches. A read from this register results in a read of the OLAT and not the port itself. A write to this register modifies the output latches that modifies the pins configured as outputs.
- Parameters:
-
data The 8 bits to write to OLATA register.
Definition at line 256 of file MCP23S17.cpp.
char olatb | ( | ) |
Read OLATB.
OUTPUT LATCH REGISTER The OLAT register provides access to the output latches. A read from this register results in a read of the OLAT and not the port itself. A write to this register modifies the output latches that modifies the pins configured as outputs.
- Returns:
- The 8 bits read.
Definition at line 261 of file MCP23S17.cpp.
void olatb | ( | char | data ) |
Write to OLATB.
OUTPUT LATCH REGISTER The OLAT register provides access to the output latches. A read from this register results in a read of the OLAT and not the port itself. A write to this register modifies the output latches that modifies the pins configured as outputs.
- Parameters:
-
data The 8 bits to write to OLATB register.
Definition at line 266 of file MCP23S17.cpp.
char read | ( | char | reg_address ) |
Read an Register address.
- Parameters:
-
reg_address The selected register to read from.
- Returns:
- The 8 bits read, but if GPIO register only the value of the inputs (outputs is read as "0").
Definition at line 42 of file MCP23S17.cpp.
void reset | ( | ) |
Resetting the MCP23S17.
Reset has to be pull down for min. 1uS to insure correct reset. This function pull down the reset pin for 5uS.
Definition at line 73 of file MCP23S17.cpp.
void write | ( | char | reg_address, |
char | data | ||
) |
Write to Register address.
- Parameters:
-
reg_adress The selected register to write to. data The 8 bits to write to the register, but if GPIO only the output will change.
Definition at line 47 of file MCP23S17.cpp.
Generated on Tue Jul 12 2022 21:13:04 by
