# set all portA pin as outputsudoi2cset-y10x200x000x00# set gister 0x0csudoi2cset-y10x200x140x01
sudoi2cset-y10x200x140x00
Registers
The MCP23017 supports two register addressing modes:
12
Bank 0 (Sequential Addressing - Default Mode)
Bank 1 (Split Addressing Mode)
demo
The demo above use register
register 0x00(IODIRA) as set portA all pin to output
register 0x14(OLATA) set as 0x00/0x01 to set port PA0 low/high
Datasheet
3.5.1 I/O DIRECTION REGISTER
Controls the direction of the data I/O.
When a bit is set, the corresponding pin becomes an
input. When a bit is clear, the corresponding pin
becomes an output.
3.5.11 OUTPUT LATCH REGISTER (OLAT)
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.
register address
The data sheet use bank1 address mode
The address is 0x14 for OLATA register in bank0 mode
#include<iostream>#include<fcntl.h>#include<unistd.h>#include<linux/i2c-dev.h>#include<sys/ioctl.h>#include<chrono>#include<thread>#include<cstdint> // For fixed-width integersintmain(){intfile;constchar*i2cBus="/dev/i2c-1";// I2C busconstintdeviceAddress=0x20;// MCP23017 I2C address// Open the I2C busfile=open(i2cBus,O_RDWR);if(file<0){std::cerr<<"Failed to open I2C bus\n";return-1;}// Connect to the MCP23017 deviceif(ioctl(file,I2C_SLAVE,deviceAddress)<0){std::cerr<<"Failed to connect to I2C device\n";close(file);return-1;}// Set IODIRA register (0x00) to 0x00 (all output)uint8_tconfig[2]={0x00,0x00};if(write(file,config,2)!=2){std::cerr<<"Failed to write to I2C device\n";}// Toggle PA0 port using GPIOA (0x14)for(inti=0;i<10;i++){uint8_tstate=i%2;uint8_toutput[2]={0x14,state};if(write(file,output,2)!=2){std::cerr<<"Failed to write GPIOA\n";}std::this_thread::sleep_for(std::chrono::seconds(1));}// // Read GPIOA (0x12)// uint8_t reg = 0x12;// if (write(file, ®, 1) != 1) {// std::cerr << "Failed to set register address\n";// }// uint8_t data;// if (read(file, &data, 1) != 1) {// std::cerr << "Failed to read from I2C device\n";// } else {// std::cout << "GPIOA Data: 0x" << std::hex << (int)data << std::dec << std::endl;// }close(file);return0;}
fromsmbus2importSMBusimporttimeI2C_BUS=1# Raspberry Pi uses I2C bus 1I2C_ADDR=0x20# MCP23017 I2C address# MCP23017 Register AddressesIODIRA=0x00# I/O direction register for GPIOAGPIOA=0x12# Register to read/write GPIOAOLATA=0x14# Output latch register for GPIOAwithSMBus(I2C_BUS)asbus:# Set all GPIOA pins as output (0x00 means all pins are outputs)bus.write_byte_data(I2C_ADDR,IODIRA,0x00)whileTrue:# Turn PA0 ON (set bit 0 to 1)bus.write_byte_data(I2C_ADDR,OLATA,0x01)print("PA0 ON")time.sleep(1)# Turn PA0 OFF (set bit 0 to 0)bus.write_byte_data(I2C_ADDR,OLATA,0x00)print("PA0 OFF")time.sleep(1)