[SOLVED] ATMEL AVR ATMEGA32 AND MCP23017 I2C I/O EXPANDER

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

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
                                                                            read

read_I2C_SLAVE_WRITE failed..    inp: 0x1
                                         write
                                              write_I2C_SLAVE_WRITE failed..
                                                                            read

read_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..
                                                                            read

read_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;
}

This topic has a solution.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Mon. Nov 17, 2014 - 12:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Why do you put series resistors in series with the I2C lines?????

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The 4k7 resistors, if connected as shown in the schematic, are not connected as pull-ups. Without them, the I2C will not work properly.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:

Why do you put series resistors in series with the I2C lines?????

 

sorry js. that is pull up resistor. by mistake i drawn in series.

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Mon. Nov 17, 2014 - 07:30 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:

The 4k7 resistors, if connected as shown in the schematic, are not connected as pull-ups. Without them, the I2C will not work properly.

 

sorry brain. that is pull-up resistor.  by mistake i drawn in series.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Mon. Nov 17, 2014 - 07:31 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

But i am getting one time read only

Do you think that communications between the chip and the micro is OK then? Do you have a debugger to see where the code is getting stuck?

 

Are the output pins getting programmed as outputs? Easy to check, if the pins are low and you put a 1K resistor to VCC on one of the pins then the LED should stay off and the pin should stay at zero or close. Otherwise the chip is not being initialised correctly.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:

But i am getting one time read only

Do you think that communications between the chip and the micro is OK then? Do you have a debugger to see where the code is getting stuck?

 

Are the output pins getting programmed as outputs? Easy to check, if the pins are low and you put a 1K resistor to VCC on one of the pins then the LED should stay off and the pin should stay at zero or close. Otherwise the chip is not being initialised correctly.

 

yes output pins(GPIOA) programmed as outputs. then as per your point i connected 1K resistor between VCC and LED(+).

In output side (GPIOA-0x00(as per datasheet)) while power up, led is on and while started to write to register led off.

 

In input side (GPIOB-0xFF(as per datasheet)) while power up, led is on until power-down it is in on state only.

 

So what the problem is? code or circuit? i am not getting which one is fault.

 

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Mon. Nov 17, 2014 - 11:01 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I always advise people to use respected I2C libraries.

 

Then all we need do is look at a few lines of main()

It is generally fairly obvious where you have called a library function with the wrong arguments.

 

But i am getting one time read only.

This is because you should always NAK the last read.      Your enormous functions do not do this properly.

 

I have not used either of those M*crochip expanders.     If you can use the SPI version,    then the I2C version should be very similar.

e.g.   write a value to a specific register.

e.g.   read a value from a specific register.

 

Both of those 'helper' functions are easy to implement with Fleury, Procyon, Codevision,   Arduino, ...

 

David.

 

Edit.   Please can you correct your schematic.     i.e.  put the pull-up resistors in the right place.

Last Edited: Mon. Nov 17, 2014 - 11:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

david.prentice wrote:

I always advise people to use respected I2C libraries.

 

Then all we need do is look at a few lines of main()

It is generally fairly obvious where you have called a library function with the wrong arguments.

 

But i am getting one time read only.

This is because you should always NAK the last read.      Your enormous functions do not do this properly.

 

I have not used either of those M*crochip expanders.     If you can use the SPI version,    then the I2C version should be very similar.

e.g.   write a value to a specific register.

e.g.   read a value from a specific register.

 

Both of those 'helper' functions are easy to implement with Fleury, Procyon, Codevision,   Arduino, ...

 

David.

 

Edit.   Please can you correct your schematic.     i.e.  put the pull-up resistors in the right place.

 

yes david. i will do it now.

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

I always advise people to use respected I2C libraries.

 

Then all we need do is look at a few lines of main()

It is generally fairly obvious where you have called a library function with the wrong arguments.

 

But i am getting one time read only.

This is because you should always NAK the last read.      Your enormous functions do not do this properly.

 

I have not used either of those M*crochip expanders.     If you can use the SPI version,    then the I2C version should be very similar.

e.g.   write a value to a specific register.

e.g.   read a value from a specific register.

 

Both of those 'helper' functions are easy to implement with Fleury, Procyon, Codevision,   Arduino, ...

 

David.

 

Edit.   Please can you correct your schematic.     i.e.  put the pull-up resistors in the right place.

 

yes david now it is working fine. the problem is because of NAK the last read.

 

the change is 

unsigned int totalChar = 1;

 

for(i=0;i<totalChar;i++)
    {
        //      transmitString("in loop");
        if(i == (totalChar-1))       //no Acknowledge after receiving the last byte
        rd_data = i2c_receiveData_NACK();
        else
        rd_data = i2c_receiveData_ACK();
    }

 

 

/*
 * 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;
	unsigned int totalChar = 1, i=0;
	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();
	for(i=0;i<totalChar;i++)
	{
		//      transmitString("in loop");
		if(i == (totalChar-1))       //no Acknowledge after receiving the last byte
		rd_data = i2c_receiveData_NACK();
		else
		rd_data = i2c_receiveData_ACK();
	}
	
	sprintf(temp_r,"\nrd_data=0x%x",rd_data);
	transmitString(temp_r);
	
	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 pat_cnt;
	
	char pattern[12] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0xFF,0x00,0xFF};

	/*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);
	i2c_write(GPIOB,0xFF);    //  Reset GPIOB As Input
	_delay_ms(500);
	
	
	togbutton=0;              // Toggle Button
	cnt=0;
	_delay_ms(100);
	
	
	/*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)*/
	
    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)
		{
				
			sprintf(temp,"\ncount: %d",cnt);
			transmitString(temp);
			
			i2c_write(GPIOA,pattern[cnt]);   // Write to MCP23017 GPIOA
			_delay_ms(500);
			
			pat_cnt = i2c_read(GPIOA);
			sprintf(temp,"\npat_count_1: 0x%x",pat_cnt);
			transmitString(temp);
			
			_delay_ms(500);
			
			cnt++;
			
			if (cnt >= 12) cnt=0;
		}      // end of if (togbutton > 0x00)
		_delay_ms(2000);
	}
	return 0;
}

 

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks to david, js, brain and all.  Thanks alot :) . Thank you somuch.  happy coding and debugging. :)

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN

Last Edited: Mon. Nov 17, 2014 - 12:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I am pleased that you have it 'working'.

 

I still reckon that you should replace your TWI functions with proper library calls.

Bear in mind that you will be writing similar apps for other I2C chips in the future.

 

So it is worth choosing sensible names for your helper functions.    e.g. MCP23017_read() and MCP23017_write()

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

I am pleased that you have it 'working'.

 

I still reckon that you should replace your TWI functions with proper library calls.

Bear in mind that you will be writing similar apps for other I2C chips in the future.

 

So it is worth choosing sensible names for your helper functions.    e.g. MCP23017_read() and MCP23017_write()

 

David.

 

Good Day. Thanks david. Yes exactly correct what you told. Now i changed. Thanks david smiley

-----------------------

Thanks & Regards,

Sasi

-----------------------

GO GREEN