XMEGA TWI

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

Dear all
I want to use TWID ATXMEGA128A1 for communication to 24C64.I use the AVR1308 app note.
And the settings are as follow. But I cannot read or write to the 24C64.

TWI_Master_t twiMaster_24C64;    /* TWI master module for 24C64. */
/* Enable LO interrupt level. */
PMIC.CTRL |= PMIC_LOLVLEN_bm;
sei();

/* Initialize TWI master for 24C64. */
TWI_MasterInit(&twiMaster_24C64, &TWID, TWI_MASTER_INTLVL_LO_gc, TWI_BAUD(F_CPU,100000));

//write the first byte as 0xaa
uint8_t data24C64 [10]={0,0,0xaa};
TWI_MasterWriteRead(&twiMaster_24C64,0xa0,data24C64,3,3);
/* Wait until transaction is complete. */
while (twiMaster_24C64.status != TWIM_STATUS_READY);

and here is the interrupt code

ISR(TWID_TWIM_vect)
{
	TWI_MasterInterruptHandler(&twiMaster_24C64);
}

is there something that I might forget to use? Is my code correct?

Thanks for your help.

I love Digital
and you who involved in it!

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

The code looks OK to me. Have you triple checked your hardware (pull-ups, slave address...)?

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

Quote:
The code looks OK to me. Have you triple checked your hardware (pull-ups, slave address...)?

I have checked the hardware and it is ok.

here are the read and write functions.

//read's a byte from memory 
char eepromread(int add)
{
    uint8_t local_data[2];
    local_data[0] = add>>8;
    local_data[1] = add;
 
	TWI_MasterWriteRead(&twiMaster_24C16, Dev24C16, local_data, 2, 0);
    while (twiMaster_24C16.status != TWIM_STATUS_READY);
	
	TWI_MasterWriteRead(&twiMaster_24C16, Dev24C16, 0, 0, 1);
    while (twiMaster_24C16.status != TWIM_STATUS_READY);
	
	return  twiMaster_24C16.readData[0];

}

//write's a byte to memory
void eepromwrite(unsigned char data,int add)
{
    uint8_t local_data[3];

    local_data[0] = add>>8;
    local_data[1] = add;
    local_data[2] = data;

    TWI_MasterWriteRead(&twiMaster_24C16, Dev24C16, local_data, 3, 0);
    while (twiMaster_24C16.status != TWIM_STATUS_READY);

}

and i still do not get the correct response.

I love Digital
and you who involved in it!

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

I decided to begin a new topic for AT24Cxx and XMEGA TWI connection problem. But by searching the site, I found there are similar topics with a common problem and with no final solution:
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=705851
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=100394&highlight=avr1308

I spent several hours with AVR1308 sample codes and finally I found the solution.
The key of problem is this line in AVR1308:

twi->address = address<<1;

In TWI transactions, slave address is shifted 1bit to left, So 0xA0 must not set as the eeprom address. When address lines tied to ground, The correct eeprom address is 0x50.

Ozhan KD
Knowledge is POWER

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

Quote:
In TWI transactions, slave address is shifted 1bit to left, So 0xA0 must not set as the eeprom address. When address lines tied to ground, The correct eeprom address is 0x50.

thanks

Thank you from the tips

Necessary to say that reading the information at the time there is a bug
Because that operation not get quite all the function returns an empty read buffer will remain

i am used for DS1307 and all of them ok
Correct Code

else if (twi->bytesToRead > 0) 
        {
			
            readAddress = twi->address | 0x01;
			twi->interface->MASTER.ADDR = readAddress; 
            
               while (twi->status != TWIM_STATUS_READY);
               for(i=0;ireadData[i];
   
            
		}