/* Title: I2C_eeprom_24LC32_1.ino Author: Florin Dragan florind@mas.utcluj.ro Date Created: 01-10-2017 Last Modified: 02-04-2020 Purpose: This is an example how to communicate with 24LC32 EEPROM chip, using I2C interface with Arduino's Wire library implementation Write single byte and read single byte ------------------- Hardware ------------------------------------------- Tested Part: MICROCHIP - 24LC32 - EEPROM SERIELL 32Kbit 32768bit = 4096 Bytes MICROCHIP - 24LC256 - EEPROM SERIELL 256Kbit 262144bit = 16384 Bytes Number Name ConnectTo Note 1 a0 - 2 a1 - 3 a2 - 4 GND GND 5 SDA SDA 2kOhm PullUp 6 SCL SCL 2kOhm PullUp 7 WP GND 8 VCC (+3v3 ... +5V0) */ #include #define DEVICEADDRESS 0x57 // 0b0101 0 a2 a1 a0 #define EE24LC32MAXBYTES 4096 //32768 bits / 8 = 4096 bytes /* Functions definitions */ /* I2C initialization */ void WireEepromBegin(void) { Wire.begin(); } /* I2C EEPROM Write byte function */ void WireEepromWriteByte(uint8_t DEVICE_ADDRESS , uint16_t MemoryAddress, uint8_t u8Byte) { /* Send slave address with R/W bit 0 (write) first */ Wire.beginTransmission(DEVICE_ADDRESS); /* Send memory location address splitted in two bytes MSB and LSB */ Wire.write( (MemoryAddress >> 8) & 0xFF ); // MSB Wire.write( (MemoryAddress >> 0) & 0xFF ); // LSB /* Send byte to be written */ Wire.write(u8Byte); /* Send STOP condition */ Wire.endTransmission(); delay(5); } /* I2C EEPROM Read byte function */ uint8_t WireEepromReadByte(uint8_t DEVICE_ADDRESS, uint16_t MemoryAddress) { uint8_t u8retVal = 0; /* Send slave address first */ Wire.beginTransmission(DEVICE_ADDRESS); /* Send byte to be written */ Wire.write( (MemoryAddress >> 8) & 0xFF ); Wire.write( (MemoryAddress >> 0) & 0xFF ); /* Send STOP condition */ Wire.endTransmission(); delay(5); /* Send slave address with R/W bit 1 (read) first */ Wire.requestFrom(DEVICE_ADDRESS, 1); /* Read received byte */ u8retVal = Wire.read(); return u8retVal ; } void setup(void) { Serial.begin(9600); Wire.begin(); /* Write LSB of memory address in each location < 256 */ for (unsigned int MemoryAddress = 0; MemoryAddress < 256; MemoryAddress++) { WireEepromWriteByte(DEVICEADDRESS, MemoryAddress, (byte)MemoryAddress); } } //end setup void loop(void) { for (unsigned int MemoryAddress = 0; MemoryAddress < 256; MemoryAddress++) { Serial.print("Byte: "); Serial.print(MemoryAddress); Serial.print(", "); Serial.print((int)WireEepromReadByte(DEVICEADDRESS, MemoryAddress)); Serial.println("."); } /* for (unsigned int MemoryAddress = 0; MemoryAddress < 1024; MemoryAddress++) { WireEepromWriteInt(theDeviceAddress, MemoryAddress * 2, (int)MemoryAddress); } for (unsigned int MemoryAddress = 0; MemoryAddress < 1024; MemoryAddress++) { Serial.print("Int: "); Serial.print(MemoryAddress); Serial.print(", "); Serial.print(WireEepromReadInt(theDeviceAddress, MemoryAddress * 2)); Serial.println("."); } */ delay(1000); } //end loop