I am trying to interface MCP23017 I2C based GPIO expander with nuvoton N76E003 microcontroller. I am developing in Keil C51.
Taking the reference of the given examples and some references online, I made a code to control the MCP23017's functions. However, I am not getting any response on the I2C bus. I am attaching my code with this. I am commented the code so it will be easier to understand.
I would be very glad for all your positive comments and criticism. Thanks. Sarth
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#define regular_I2C_pins 0
#define alternate_I2C_pins 1
#define regular_I2C_GPIOs() do{P13_OpenDrain_Mode; P14_OpenDrain_Mode; clr_I2CPX;}while(0)
#define alternative_I2C_GPIOs() do{P02_OpenDrain_Mode; P16_OpenDrain_Mode; set_I2CPX;}while(0)
#define I2C_GPIO_Init(mode) do{if(mode != 0){alternative_I2C_GPIOs();}else{regular_I2C_GPIOs();}}while(0)
#define I2C_CLOCK 0x27
#define MCP23017_SLA 0x20
#define MCP23017_WR 0
#define MCP23017_RD 1
#define ERROR_CODE 0x78
void Init_I2C(void)
{
I2C_GPIO_Init(regular_I2C_pins);
I2CLK = I2C_CLOCK; //Set I2C clock rate
set_I2CEN; //Enable I2C hardware
}
void I2C_Error(void)
{
while (1);
}
void I2C_Process(void)
{
/* Step1 */
set_STA; /* Send Start bit to I2C device */
clr_SI;
while (!SI); //Check SI set or not
if (I2STAT != 0x08) //Check status value after every step
I2C_Error();
/* Step2 */
clr_STA; //STA=0
I2DAT = (MCP23017_SLA | MCP23017_WR);
clr_SI;
while (!SI); //Check SI set or not
if (I2STAT != 0x18)
I2C_Error();
/* Step3 */
I2DAT = 0x00; //address high for I2C EEPROM
clr_SI;
while (!SI); //Check SI set or not
if (I2STAT != 0x28)
I2C_Error();
/* Step4 */
I2DAT = 0x00; //address low for I2C EEPROM
clr_SI;
while (!SI); //Check SI set or not
if (I2STAT != 0x28)
I2C_Error();
/* Step5 */
I2DAT = (0x00); // Write to IODIRA register to declare port pins as output
clr_SI;
while (!SI);
if (I2STAT != 0x18)
I2C_Error();
I2DAT = (0x00); // Declare the pins as outputs
clr_SI;
while (!SI);
if (I2STAT != 0x18)
I2C_Error();
I2DAT = (0x00); // Write to IODIRB register to declare port pins as output
clr_SI;
while (!SI);
if (I2STAT != 0x18)
I2C_Error();
I2DAT = (0x00); // Declare the pins as outputs
clr_SI;
while (!SI);
if (I2STAT != 0x18)
I2C_Error();
I2DAT = (0x12); // Address port A
clr_SI;
while (!SI);
if (I2STAT != 0x18)
I2C_Error();
I2DAT = (0xFF); // Make port A pins high
clr_SI;
while (!SI);
if (I2STAT != 0x18)
I2C_Error();
I2DAT = (0x13); // Address port B
clr_SI;
while (!SI);
if (I2STAT != 0x18)
I2C_Error();
I2DAT = (0xFF); // Make port B pins high
clr_SI;
while (!SI);
if (I2STAT != 0x18)
I2C_Error();
/* Step6 */
do{
set_STO;
clr_SI;
set_STA; //Check if no ACK is returned by MCP23017
clr_SI;
while (!SI); //Check SI set or not
if (I2STAT != 0x08) //Check status value after every step
I2C_Error();
clr_STA;
I2DAT = (MCP23017_SLA | MCP23017_WR);
clr_SI;
while (!SI); //Check SI set or not
}while (I2STAT != 0x18);
/* Step7 */
set_STO;
clr_SI;
while (STO); /* Check STOP signal */
}
void main(void)
{
//Set_All_GPIO_Quasi_Mode;
Init_I2C(); //initial I2C circuit
I2C_Process();
while (1);
}
Check your slave address. It should be according to datasheet, page 15:
I2DAT = (((MCP23017_SLA | (A2 << 2) | (A1 << 1) | (A0 << 0)) << 1) | MCP23017_WR);