Hi to all,
I have an Arduino Mega and i'm trying to use a 24WC16P EEPROM.
So i followed this (but official Arduino Wire Documentation also) guide:
http://10kohms.com/arduino-external-eeprom-24lc256
using digital pin 20 and 21 and not 4 and 5 (because i have an Arduino Mega)
with this code:
#include <Wire.h>
#include <LiquidCrystal.h>
#define EEPROM_ADDRESS 0x50
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data )
{
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // MSB
Wire.send((int)(eeaddress & 0xFF)); // LSB
Wire.send(rdata);
Wire.endTransmission();
}
// WARNING: address is a page address, 6-bit end will wrap around
// also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length )
{
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddresspage >> 8)); // MSB
Wire.send((int)(eeaddresspage & 0xFF)); // LSB
byte c;
for ( c = 0; c < length; c++)
Wire.send(data[c]);
Wire.endTransmission();
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // MSB
Wire.send((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.receive();
return rdata;
}
// maybe let's not read more than 30 or 32 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // MSB
Wire.send((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
int c = 0;
for ( c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.receive();
}
int temp = 0;
void setup(void)
{
lcd.begin(20, 4);
lcd.clear();
lcd.print("EEPROM Test");
Wire.begin();
i2c_eeprom_write_byte( EEPROM_ADDRESS, 0, 200 );
delay(20);
}
void loop(void)
{
temp = i2c_eeprom_read_byte( EEPROM_ADDRESS, 0 );
lcd.setCursor(0,1);
lcd.print(temp);
delay(8000);
}
But always appears 255 on the LCD display, because, during the reading, after this command:
Wire.requestFrom(deviceaddress,1);
the next:
Wire.available()
is equals to NULL!
Where is the mistake? Also tried with an other EEPROM (SLA24C14D), but always appears 255! :-/