cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Address for M24SR

thao
Associate II
Posted on March 28, 2014 at 10:46

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

&sharpinclude <avr/io.h>

&sharpdefine F_CPU 8000000UL // internal oszillator 8MHz

&sharpinclude <util/delay.h>

&sharpinclude <compat/twi.h>

&sharpdefine MAX_TRIES 100

&sharpdefine EEPROM_ADDR  0xAC       //  EEPROM Device Address M24SR = 0xAC, M24LR = 0xA6

&sharpdefine I2C_START 0

&sharpdefine I2C_DATA  1

&sharpdefine 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;

}

#i2c #m24sr
1 ACCEPTED SOLUTION

Accepted Solutions
Rene Lenerve
ST Employee
Posted on July 29, 2015 at 18:28

Hello Ahmadi,

your Microcontroller is based on a different architecture from ARM so you cannot import all files in your project as this is an STM32 project.

The files you can import in your project are the NFC libraries and drivers files located in the folder Libraries/M24SR. As the driver is using i2c from STM32 you have to adapt it with your HAL, principally drv_i2c_M24SR.c and .h (this driver uses standard lib for STM32).

Then you can study main.c and menu.c files (in the folder Project/M24SR/M24SR_DISCOVERY_MB1138/src) to understand how the M24Sr is initialised and used.

I hope that it can helped you to start porting the code to your project.

Have a nice day,

ST NFC support.

View solution in original post

5 REPLIES 5
Anais GALLIGANI
Senior II
Posted on April 01, 2014 at 11:51

Hello Thao,

M24SR shall not be adressed as a standard EEPROM.

There is a bundle of command to use (Open session...)

I hope that these applications note will help you,

Have a nice day,

ST NFC RFID dynamic tag support

awong1900
Associate II
Posted on May 22, 2014 at 13:58

Maybe use 7-bit address 0x56. I use this address with Arduino platform.

--

Ten 

kahmadi29
Associate II
Posted on July 29, 2014 at 14:02

Hello Thao,

I bought a M24SR module and as you know the firmware is with Keil software and also the ARM Microcontroller used in it. I want to use a ATXMEGA 32A4U instead of the ARM microcontroller and I don't know which changes I have to apply to current code in stsw-m24sr001?

I am in a hurry with this project,would you please help me soon or if you have a code,Please send me.

Thanks a lot. 

kahmadi29
Associate II
Posted on July 29, 2014 at 14:05

Hello everybody,

I bought a M24SR module and as you know the firmware is with Keil software and also the ARM Microcontroller used in it. I want to use a ATXMEGA 32A4U instead of the ARM microcontroller and I don't know which changes I have to apply to current code in stsw-m24sr001?

I am in a hurry with this project,would you please help me soon or if you have a code,Please send me.

Thanks a lot. 

Rene Lenerve
ST Employee
Posted on July 29, 2015 at 18:28

Hello Ahmadi,

your Microcontroller is based on a different architecture from ARM so you cannot import all files in your project as this is an STM32 project.

The files you can import in your project are the NFC libraries and drivers files located in the folder Libraries/M24SR. As the driver is using i2c from STM32 you have to adapt it with your HAL, principally drv_i2c_M24SR.c and .h (this driver uses standard lib for STM32).

Then you can study main.c and menu.c files (in the folder Project/M24SR/M24SR_DISCOVERY_MB1138/src) to understand how the M24Sr is initialised and used.

I hope that it can helped you to start porting the code to your project.

Have a nice day,

ST NFC support.