Hi all,
Good day. interfaced MCP23017 I2C I/O port expander with Atmega32. and my reference link is http://www.ermicro.com/blog/?p=1050. its working fine with MCP23S17 SPI I/O enpander. but its not ok with i2c expander. I am not getting the continuous output. If i pressed the button, i want to write and then read (led's on). But i am getting one time read only.
At this time output is:
in main
write
w_data=0x28
write
w_data=0x0
write
w_data=0xff
write
w_data=0xff
write
w_dat
a=0x0
in loop
read inp: 0xff
in loop
read
if i pressed button, then it was stopped with write loop it self.
in main
write
w_data=0x28
write
w_data=
0x0
write
w_data=0xff
write
w_data=0xff
write
w_data=0x0
in loop
read inp: 0xfb
togbut
:0x1
write
If i removed the ground connection to the mcp23017, then i am getting continuous loop message.
write_I2C_SLAVE_WRITE failed..
read
read_I2C_SLAVE_WRITE failed..
pat_coun
t_1: 0x1
count: 0
in loop
read
read_I2C_SLAVE_WRITE failed.. inp: 0x1
write
wr
ite_I2C_SLAVE_WRITE failed..
read
read_I2C_SLAVE_WRITE failed..
pat_count_1: 0x3
cou
nt: 1
in loop
read
read_I2C_SLAVE_WRITE failed.. inp: 0x1
write
write_I2C_SLAVE_WR
ITE failed..
read
read_I2C_SLAVE_WRITE failed..
pat_count_1: 0x4
count: 2
in loop
readread_I2C_SLAVE_WRITE failed.. inp: 0x1
write
write_I2C_SLAVE_WRITE failed..
readread_I2C_SLAVE_WRITE failed..
pat_count_1: 0x8
count: 3
in loop
read
read_I2C_SLAVE_W
RITE failed.. inp: 0x1
write
write_I2C_SLAVE_WRITE failed..
read
read_I2C_SLAVE_W
RITE failed..
pat_count_1: 0x10
count: 4
in loop
read
read_I2C_SLAVE_WRITE failed..
inp: 0x1
write
write_I2C_SLAVE_WRITE failed..
read
read_I2C_SLAVE_WRITE failed..
pat_
count_1: 0x20
count: 5
in loop
read
read_I2C_SLAVE_WRITE failed.. inp: 0x1
write
wr
ite_I2C_SLAVE_WRITE failed..
read
read_I2C_SLAVE_WRITE failed..
pat_count_1: 0x40
co
unt: 6
in loop
read
read_I2C_SLAVE_WRITE failed.. inp: 0x1
write
write_I2C_SLAVE_WR
ITE failed..
read
read_I2C_SLAVE_WRITE failed..
pat_count_1: 0x80
count: 7
in loop
rea
d
read_I2C_SLAVE_WRITE failed.. inp: 0x1
write
write_I2C_SLAVE_WRITE failed..
readread_I2C_SLAVE_WRITE failed..
pat_count_1: 0x0
count: 8
in loop
read
read_I2C_SLAVE_W
RITE failed.. inp: 0x1
write
write_I2C_SLAVE_WRITE failed..
read
read_I2C_SLAVE_W
RITE failed..
pat_count_1: 0xff
count: 9
in loop
read
read_I2C_SLAVE_WRITE failed..
inp: 0x1
write
write_I2C_SLAVE_WRITE failed..
read
read_I2C_SLAVE_WRITE failed..
pat_
count_1: 0x0
count: 10
in loop
read
read_I2C_SLAVE_WRITE failed.. inp: 0x1
write
wr
ite_I2C_SLAVE_WRITE failed..
read
read_I2C_SLAVE_WRITE failed..
pat_count_1: 0xff
co
unt: 11
in loop
read
read_I2C_SLAVE_WRITE failed.. inp: 0x1
write
write_I2C_SLAVE_WR
ITE failed..
read
read_I2C_SLAVE_WRITE failed..
I am not getting where the problem is, whether in code or in hardware.
/* * MCP23017_I2C_IO_EXPANDER.c * * Created: 11/11/2014 10:40:57 AM * Author: Dev5 */ #define F_CPU 8000000UL #include <avr/io.h> #include <avr/pgmspace.h> #include <util/delay.h> #include <stdio.h> #include "UART_routines.h" #include "i2c_routines.h" //ATMEGA32_I2C_PORT //#define I2C_PORT PORTC //#define I2C_DDR DDRC //MCP23017_SLAVE_ADDRESS_I2C_INTERFACE #define I2C_SLAVE_WRITE 0x40 #define I2C_SLAVE_READ 0x41 // MCP23017 Registers Definition for BANK=0 (default) #define IODIRA 0x00 #define IODIRB 0x01 #define IOCONA 0x0A #define GPPUA 0x0C #define GPPUB 0x0D #define GPIOA 0x12 #define GPIOB 0x13 //TWI initialize // bit rate:18 (freq: 100Khz) void twi_init(void) { TWCR= 0X00; //disable twi TWBR= 0x12; //set bit rate TWSR= 0x03; //set prescale TWCR= 0x44; //enable twi } //end of twi_init() void init_devices(void) { uart0_init(); twi_init(); } //end of init_devices() unsigned char i2c_read(unsigned char addr) { //char temp_r[20]; unsigned char errorStatus; unsigned char rd_data; transmitString("\nread"); errorStatus = i2c_start(); if(errorStatus == 1) { transmitString("\nread_i2c start failed.."); i2c_stop(); return(1); } errorStatus = i2c_sendAddress(I2C_SLAVE_WRITE); if(errorStatus == 1) { transmitString("\nread_I2C_SLAVE_WRITE failed.."); i2c_stop(); return(1); } errorStatus = i2c_sendData(addr); //sprintf(temp_r,"\nr_addr=0x%x",addr); //transmitString(temp_r); if(errorStatus == 1) { transmitString("\nread_address failed.."); i2c_stop(); return(1); } errorStatus = i2c_repeatStart(); if(errorStatus == 1) { transmitString("\nrepeat start failed.."); i2c_stop(); return(1); } errorStatus = i2c_sendAddress(I2C_SLAVE_READ); if(errorStatus == 1) { transmitString("\nread_I2C_SLAVE_READ failed.."); i2c_stop(); return(1); } rd_data = i2c_receiveData_ACK(); //sprintf(temp_r,"\nrd_data=0x%x",rd_data); //transmitString(rd_data); i2c_stop(); //_delay_ms(1000); return rd_data; } //end of i2c_read() unsigned char i2c_write(unsigned char addr, unsigned char data) { char temp_w[20]; unsigned char errorStatus; transmitString("\nwrite"); errorStatus = i2c_start(); if(errorStatus == 1) { transmitString("\nwrite_i2c start failed.."); i2c_stop(); return(1); } errorStatus = i2c_sendAddress(I2C_SLAVE_WRITE); if(errorStatus == 1) { transmitString("\nwrite_I2C_SLAVE_WRITE failed.."); i2c_stop(); return(1); } errorStatus = i2c_sendData(addr); //sprintf(temp_w,"\nw_addr=0x%x",addr); //transmitString(temp_w); if(errorStatus == 1) { transmitString("\nwrite addr failed.."); i2c_stop(); return(1); } errorStatus = i2c_sendData(data); sprintf(temp_w,"\nw_data=0x%x",data); transmitString(temp_w); if(errorStatus == 1) { transmitString("\nwrite data failed.."); i2c_stop(); return(1); } i2c_stop(); //transmitString("\nwrite_end"); return (0); } //end of i2c_write() int main(void) { unsigned char togbutton,inp; unsigned int cnt; char temp[20]; char pattern[12] = {0b00000001,0b00000011,0b00000100,0b00001000,0b00010000,0b00100000, 0b01000000,0b10000000,0b00000000,0b11111111,0b00000000,0b11111111}; init_devices(); _delay_ms(1000); transmitString("\nin main"); //I2C_DDR = (1<<PC0)|(1<<PC1); PORTC = 0x00; DDRC = 0xFF; // Initial the MCP23017 I2C I/O Expander i2c_write(IOCONA,0x28); // I/O Control Register: BANK=0, SEQOP=1, HAEN=1 (Enable Addressing) _delay_ms(500); i2c_write(IODIRA,0x00); // GPIOA As Output _delay_ms(500); i2c_write(IODIRB,0xFF); // GPIOB As Input _delay_ms(500); i2c_write(GPPUB,0xFF); // Enable Pull-up Resistor on GPIOB _delay_ms(500); i2c_write(GPIOA,0x00); // Reset Output on GPIOA _delay_ms(500); togbutton=0; // Toggle Button cnt=0; _delay_ms(100); while(1) { //TODO:: Please write your application code transmitString("\nin loop"); inp = i2c_read(GPIOB); // Read from GPIOB sprintf(temp,"\t inp: 0x%x",inp); transmitString(temp); _delay_ms(500); if (inp == 0xFB) //(inp != 0xFF) // Button(GPIOB2) is pressed { _delay_ms(1); togbutton^=0x01; sprintf(temp,"\ntogbut:0x%x",togbutton); transmitString(temp); //if (togbutton == 0x00) //{ //i2c_write(GPIOA,0x00); // Write to MCP23017 GPIOA //cnt=0; //} } // end of if (inp == 0xFB) if (togbutton > 0x00) { //_delay_ms(200); i2c_write(GPIOA,pattern[cnt]); // Write to MCP23017 GPIOA _delay_ms(500); i2c_read(GPIOA); sprintf(temp,"\npat_count_1: 0x%x",pattern[cnt]); transmitString(temp); sprintf(temp,"\ncount: %d",cnt); transmitString(temp); _delay_ms(500); cnt++; if (cnt >= 12) cnt=0; } // end of if (togbutton > 0x00) _delay_ms(2000); } return 0; }