I am trying to understand how to write and read to an EEPROM. I found examples that write value = 110 but instead of reading the value, it reads the address = 255. I'm stuck on how to change the value. If I get past this bug, I eventually want to learn how to write a string into the memory. Im using MX25L6406E as my EEPROM and Arduino DUE. Shield is attached. Please help me... here's my code
#include "Wire.h"
#define memoryAddress 0x57
#define WP_Pin 9 //Write Protect Pin
#define PowerCtrl 11 //Shield power 3VDC power control pin
#define RelayCtrl 12
#define SPIPin 10
void setup()
{
//3V Power Pin: LOW to Activate
pinMode(PowerCtrl, OUTPUT);
digitalWrite(PowerCtrl, LOW);
pinMode(RelayCtrl, OUTPUT); //Enable Relay Control
digitalWrite(RelayCtrl, HIGH); //Set HIGH to activate
pinMode(SPIPin, OUTPUT);
digitalWrite(SPIPin, LOW); //Set LOW to activate
//Write Protect OFF
pinMode(WP_Pin, OUTPUT);
digitalWrite(WP_Pin, LOW);
//Connect to I2C bus as master
Wire.begin();
Serial.begin(9600);
int address = 0;
byte val = 110;
writeAddress(memoryAddress, address, val);
byte readVal = readAddress(memoryAddress, address);
Serial.print("The returned value is ");
Serial.println(readVal); //Print in the same line
}
void loop()
{
}
void writeAddress(byte i2cAddress, int address, byte val)
{
Wire.beginTransmission(i2cAddress); //Begin Transmission to I2C EEPROM
/*
*Since 8 is a literal value, the compiler will not treat it like a decimal value.
*Casting the result guarantees that the result value is an int.
Wire.Write() is overloaded and one of the overloaded methods takes an int, although ultimately it only sends a single byte.
*/
Wire.write((int)(address >> 8)); // MSB
Wire.write((int)(address & 0xFF)); // LSB
//Send data to be stored
Wire.write(val);
Wire.endTransmission(); //End Transmission
delay(5); //Add 5ms delay for EEPROM. Required by EEPROM between writes
}
byte readAddress(byte i2cAddress, int address)
{
byte rData = 0xFF; //Define the byte for received data
Wire.beginTransmission(i2cAddress); //Begin transmission to I2C EEPROM
Wire.write((int)(address >> 8)); // MSB
Wire.write((int)(address & 0xFF)); // LSB
Wire.endTransmission(); //End Transmission
//Puts the byte of data into EEPROM's buffer
Wire.requestFrom(i2cAddress, 1); //Request one byte of data at current memory address
rData = Wire.read(); //Read the data and assign to variable
return rData; //Return the data as the function output
}
(SHIELD_2).pdf (32.6 KB)