In SPI communication we use MCP23S17 with SPI transmission. In this article we use same IC MCP23017 with I2C transmission. Let’s look at the connection diagram.

The I2C protocol for MCP23017 is as follow-

Here the first 4 bits is the logical address and last 3 bits A2: A0 are the physical address. Since we have 23 = 8 combination, we can connect up to 8 MCP23017 IC at the same time. If A2 = 0, A1 = 0 and A0 = 0 then the device has-

For write operation the I2C protocol is-

uint8_t I2C_Write_IO(char add,char data) -> Write Data
#define IOEXPENDER_Write_Addess 0x40 //0b0100A2A1A00
uint8_t I2C_Write_IO(char add,char data)
{
I2C_Start();
I2C_Write(IOEXPENDER_Write_Addess);
I2C_Write(add);
I2C_Write(data);
I2C_Stop();
}
For read operation the I2C protocol is-

uint8_t I2C_Read_IO(char add) -> Read Data
#define IOEXPENDER_Read_Addess 0x41 //0b0100A2A1A01
uint8_t I2C_Read_IO(char add)
{
uint8_t value;
I2C_Start();
I2C_Write(IOEXPENDER_Write_Addess);
I2C_Write(add);
I2C_Start();
I2C_Write(IOEXPENDER_Read_Addess);
value=I2C_Read_Nack();
I2C_Stop();
return value;
}
We develop the read and write the function. Let’s modify the program of I/O port Expender in SPI section program with I2C protocol.
#include<avr/io.h>
#include<util/delay.h>
#include<util/twi.h>
#include"IO_Expender.h"
int main(void)
{
twi_init();
uint8_t pushButton,shift=1;
/******************** initialize MCP23017 *******************/
// I/O Control Register: BANK=0, SEQOP=1, HAEN=1(Enable Addressing)
I2C_Write_IO(IOCONA,0x28);
I2C_Write_IO(IODIRA,0x00); //GPIOA as output
I2C_Write_IO(IODIRB,0xff); //GPIOB as input
I2C_Write_IO(GPPUB,0xff); //enable pull-up resister for GPB
_delay_ms(5);
/*************************************************************/
while(1)
{
pushButton=I2C_Read_IO(GPIOB);
if(pushButton == 0b11111011)
{
for(int i=7;i>=0;i--)
{
I2C_Write_IO(GPIOA,(1<<i));
_delay_ms(500);
}
}else
I2C_Write_IO(GPIOA,shift);
_delay_ms(500);
shift=(shift<<1);
if(shift == 0)
shift=1;
}
return 0;
}








Visit Today : 50
Total Visit : 28465