ducnn1908
Posts: 21
Joined: Mon Nov 17, 2014 7:25 am

I2C problem with Atmega128A

Sat Jan 24, 2015 1:53 am

Hi all,
I have some problem with I2C interface between Raspberry and Atmega128A.
I hope receiving some great advices from our forum.
This is my code to talk to Atmega128A by I2C bus:
I use wiringPi library of Gordons.
The writing function:

Code: Select all

void I2C_array_write(int I2C_fd,unsigned char *data,int bytes_num)
{
	int dem=0;
	int e[bytes_num];
	
	for (dem;dem<bytes_num;dem++)
	{
		if(I2C_fd<0)
		{
			printf("error opening i2c channel\n\r");
		}
		e[dem]=wiringPiI2CWrite(I2C_fd,*(data+dem));
		if(e[dem]<0)
		{
			printf("error i2c writing to slave\n\r");
		}
		else
		{
			//printf("writing hex:0x%i size:%i\n\r",test,sizeof(test));
		}
	}
	//delay(50);	
}
The reading function:

Code: Select all

void I2C_array_read(int I2C_fd,unsigned char *Rec_data,int bytes_num)
{
	int dem=0;
	for (dem;dem<bytes_num;dem++)
	{
		if(I2C_fd<0)
		{
			printf("error opening i2c channel\n\r");
		}
		*Rec_data++=wiringPiI2CRead(I2C_fd);//*Rec_data++ is element that Rec_data pointed before data was incremented
		if(*Rec_data<0)
		{
			printf("error i2c reading to slave\n\r");
		}
		else
		{
			//printf("Rec_Data[%i] :%c\n\r",dem,*Rec_data);
		}
		//delay(5);
	}
}
My code worked at the first time. But now, it like on and off no stable.
Can you give me some solution to get the I2C talking with high stability?
Thanks

ducnn1908
Posts: 21
Joined: Mon Nov 17, 2014 7:25 am

Re: I2C problem with Atmega128A

Sat Jan 24, 2015 6:46 am

I found my problem. Because I set the conflict address for Atmega12A.
At the first time I choose 0x02 as the slaver address. When I change to 0x05, it worked.
Thanks.

Return to “C/C++”