2014-03-28 02:46 AM
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 2unsigned 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 #m24srSolved! Go to Solution.
2015-07-29 09:28 AM
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.2014-04-01 02:51 AM
2014-05-22 04:58 AM
Maybe use 7-bit address 0x56. I use this address with Arduino platform.
--Ten2014-07-29 05:02 AM
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.2014-07-29 05:05 AM
2015-07-29 09:28 AM
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.