ATMEGA2560 i2c issues

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

Hi,

 

I am using using Peter Fluery's i2c library for my ATMEGA2560. I am trying to do simple scan of the bus. I only have 1 device on the bus at address 0xC8, however the library says there is another device at 0xC9.  When I connect an analyzer up I see it reads the the following write command as a read.

 

 

Here is my code:

 

#include <asf.h>
#include "uart_config.h"
#include <util/delay.h>
#include <avr/power.h>
#include "i2cmaster.h"

int main (void)
{
	unsigned char ret;	
	
	uint8_t ack = 0;
	board_init();
	usart_init();
	
	printf("ATMEGA2560 i2c Scanner Rev 2\n\r");	
		
	i2c_init(); // initialize I2C library

	uint8_t address = 0;	 
	
	for( address = 0; address < 255;address++){				
		
		ret = i2c_start(address+I2C_WRITE);       // set device address and write mode
		if ( ret ) {			
			i2c_stop();			
		} else {
			printf("Found Device on 0x%02X\n\r", address);
			i2c_stop();			
		}
	}		

	while(true){			
	
	}
}

I would expect the i2c_stop() to release the bus and start all over. Any help is appreciated. 

"When all else fails, read the directions"

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

You are using a 7-bit address = 0x64

Which is 0xC8 (Write) or 0xC9 (Read).

 

Fleury uses 8-bit addresses e.g.  0xC8/0xC9 pair.

Other libraries e.g. Wire.h use 7-bit address like 0x64.

 

If you are scanning for devices,  you just need to check the Write address e.g. 0xC8  

or  (0x64 << 1) | I2C_WRITE if you follow the 7-bit convention.

 

Note that a polled approach is equally happy with separate 8-bit (W) and (R)

But an interrupt approach tends to use 7-bit because the ISR() will need to use both read and write sequences. 

 

Incidentally,  what hardware device are you using which has 0x64 address ?

 

David.

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

david.prentice wrote:
Incidentally,  what hardware device are you using which has 0x64 address ?

 

Hi David - The ATSHA204a sets the default i2c address to 0xC8 (2.1.2 Configuration Zone).  Shifting the bits over fixed the issue. Thanks for help.

 

"When all else fails, read the directions"