AnsweredAssumed Answered

I2C Address for M24SR

Question asked by truong.thao on Mar 28, 2014
Latest reply on Jul 29, 2015 by NFC software support 3
Hello everybody,

Please help me for this problem! I try to communicate the M24SR and M24LR to the atmega 8. I used the under code for my test. This code functions with the M24LR very well. But there is a problem for the M24SR. It's always stop by sending Address. 
Please help me! Thank you !

Best regards


#include <avr/io.h>
#define F_CPU 8000000UL // internal oszillator 8MHz
#include <util/delay.h>
#include <compat/twi.h>

#define MAX_TRIES 100

#define EEPROM_ADDR  0xAC       //  EEPROM Device Address M24SR = 0xAC, M24LR = 0xA6


#define I2C_START 0
#define I2C_DATA  1
#define I2C_STOP  2


unsigned char i2c_transmit(unsigned char type) {
     switch(type) {
          case I2C_START:    // Send Start Condition
          TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
          break;
          case I2C_DATA:     // Send Data
          TWCR = (1 << TWINT) | (1 << TWEN);
          break;
          case I2C_STOP:     // Send Stop Condition
          TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
          return 0;
     }


     // Wait for TWINT flag set in TWCR Register
     while (!(TWCR & (1 << TWINT)));


     // Return TWI Status Register, mask the prescaler bits (TWPS1,TWPS0)
     return (TWSR & 0xF8);
}


//int i2c_writebyte(unsigned int i2c_address, unsigned int dev_id,unsigned int dev_addr,char data)
int i2c_writebyte(unsigned int i2c_address,unsigned int dev_addr,char data)
{
     unsigned char n = 0;
     unsigned char twi_status;
     char r_val = -1;


     i2c_retry:
     if (n++ >= MAX_TRIES) return r_val;


     // Transmit Start Condition
     twi_status=i2c_transmit(I2C_START);


     // Check the TWI Status
     if (twi_status == TW_MT_ARB_LOST) goto i2c_retry;
     if ((twi_status != TW_START) && (twi_status != TW_REP_START)) goto i2c_quit;


     // Send slave address (SLA_W)
     
     TWDR = dev_addr | TW_WRITE;
     
     // Transmit I2C Data
     twi_status=i2c_transmit(I2C_DATA);


     // Check the TWSR status
     if ((twi_status == TW_MT_SLA_NACK) || (twi_status == TW_MT_ARB_LOST)) goto i2c_retry;
     if (twi_status != TW_MT_SLA_ACK) goto i2c_quit;


     // Send the High 8-bit of I2C Address
     TWDR = i2c_address >> 8;


     // Transmit I2C Data
     twi_status=i2c_transmit(I2C_DATA);


     // Check the TWSR status
     if (twi_status != TW_MT_DATA_ACK) goto i2c_quit;


     // Send the Low 8-bit of I2C Address
     TWDR = i2c_address;


     // Transmit I2C Data
     twi_status=i2c_transmit(I2C_DATA);


     // Check the TWSR status
     if (twi_status != TW_MT_DATA_ACK) goto i2c_quit;


     // Put data into data register and start transmission
     TWDR = data;


     // Transmit I2C Data
     twi_status=i2c_transmit(I2C_DATA);


     // Check the TWSR status
     if (twi_status != TW_MT_DATA_ACK) goto i2c_quit;


     // TWI Transmit Ok
     r_val=1;


     i2c_quit:
     // Transmit I2C Data
     twi_status=i2c_transmit(I2C_STOP);
     return r_val;
}




int main(void)
{
     char buffer[5]= {0x34,0x56,0xff,0x12,0x45};


     char data;//id1,id2;
     unsigned int dev_address,i;


     DDRB=0xFF;                   // Set PORTB as Output
     PORTB=0x00;                  // Set All PORTB to Low


     /* Initial TWI Peripheral */
     TWSR = 0x00;   // Select Prescaler of 1


     // SCL frequency = 11059200 / (16 + 2 * 48 * 1) = 98.743 khz
     TWBR = 0x30;   // 48 Decimal
     TWCR = (1<<TWEA)|(1<<TWEN)|(1<<TWIE); //Ack is set, TWI, Intermptis ENABLE


     // Read the EEPROM ID
     dev_address=0;              // Start at Address 0


     for(i=0;i < 5;i++) {
          i2c_writebyte(dev_address + i,EEPROM_ADDR,buffer[i]);
          //i2c_writebyte(dev_address + i,EEPROM_ID,EEPROM_ADDR,buffer[i]);
          int y;
          for (y=0;y<100;y++);
     }
     
     return 0;
}

Outcomes