I Wrote some code to clear the EEPROM chip data and then write the data into it and read data from the chip.
this is my code:
#define SHIFT_DATA 2
#define SHIFT_CLK 3
#define SHIFT_LATCH 4
#define EEPROM_D0 5
#define EEPROM_D7 12
#define WRITE_EN 13
/*
* Output the address bits and outputEnable signal using shift registers.
*/
void setAddress(int address, bool outputEnable) {
shiftOut(SHIFT_DATA, SHIFT_CLK, MSBFIRST, (address >> 8) | (outputEnable ? 0x00 : 0x80));
shiftOut(SHIFT_DATA, SHIFT_CLK, MSBFIRST, address);
digitalWrite(SHIFT_LATCH, LOW);
digitalWrite(SHIFT_LATCH, HIGH);
digitalWrite(SHIFT_LATCH, LOW);
}
/*
* Read a byte from the EEPROM at the specified address.
*/
byte readEEPROM(int address) {
for (int pin = EEPROM_D0; pin <= EEPROM_D7; pin += 1) {
pinMode(pin, INPUT);
}
setAddress(address, /*outputEnable*/ true);
byte data = 0;
for (int pin = EEPROM_D7; pin >= EEPROM_D0; pin -= 1) {
data = (data << 1) + digitalRead(pin);
}
return data;
}
/*
* Write a byte to the EEPROM at the specified address.
*/
void writeEEPROM(int address, byte data) {
setAddress(address, /*outputEnable*/ false);
for (int pin = EEPROM_D0; pin <= EEPROM_D7; pin += 1) {
pinMode(pin, OUTPUT);
}
for (int pin = EEPROM_D0; pin <= EEPROM_D7; pin += 1) {
digitalWrite(pin, data & 1);
data = data >> 1;
}
digitalWrite(WRITE_EN, LOW);
delayMicroseconds(1);
digitalWrite(WRITE_EN, HIGH);
delay(10);
}
/*
* Read the contents of the EEPROM and print them to the serial monitor.
*/
void printContents() {
for (int base = 0; base <= 255; base += 16) {
byte data[16];
for (int offset = 0; offset <= 15; offset += 1) {
data[offset] = readEEPROM(base + offset);
}
char buf[80];
sprintf(buf, "%03x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
base, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15]);
Serial.println(buf);
}
}
// 4-bit hex decoder for common anode 7-segment display
byte data[] = { 0x81, 0xcf, 0x92, 0x86, 0xcc, 0xa4, 0xa0, 0x8f, 0x80, 0x84, 0x88, 0xe0, 0xb1, 0xc2, 0xb0, 0xb8 };
// 4-bit hex decoder for common cathode 7-segment display
// byte data[] = { 0x7e, 0x30, 0x6d, 0x79, 0x33, 0x5b, 0x5f, 0x70, 0x7f, 0x7b, 0x77, 0x1f, 0x4e, 0x3d, 0x4f, 0x47 };
void setup() {
//Put your setup code here, to run once:
pinMode(SHIFT_DATA, OUTPUT);
pinMode(SHIFT_CLK, OUTPUT);
pinMode(SHIFT_LATCH, OUTPUT);
digitalWrite(WRITE_EN, HIGH);
pinMode(WRITE_EN, OUTPUT);
Serial.begin(57600);
// Erase the entire EEPROM
Serial.print("Erasing EEPROM");
for (int address = 0; address <= 2047; address += 1) {
writeEEPROM(address, 0xff);
if (address % 64 == 0) {
Serial.print(".");
}
}
Serial.println(" done");
// Program data bytes
Serial.print("Programming EEPROM");
for (int address = 0; address < sizeof(data); address += 1) {
writeEEPROM(address, data[address]);
if (address % 64 == 0) {
Serial.print(".");
}
}
Serial.println(" done");
// Read and print out the contents of the EERPROM
Serial.println("Reading EEPROM");
printContents();
}
void loop() {
// put your main code here, to run repeatedly:
}
when I upload the code in Arduino, seems like clearing, writing, and reading are doing well but when I open the serial monitor it shows me garbage after several bytes...
I don't see any solution.
how can I solve that?
here is my output:
Erasing EEPROM................................ done
Programming EEPROM. done
Reading EEPROM
000: 81 cf 92 86 cc a4 a0 8f 80b84b8"%r(f1(G"%r(f1(G"010: ff0gf(ff fv!fn ff vg nf vg nf fd ff ff ff ff ff
020: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
030: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
040: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
050:& ff ff &f ff fd&f ff fd&` ff ff fG ff &f Gf fd fb
0&p:d gn0fb ff ff ff ff ff ff ff ff ff ff ff ff ff ff
070: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
080: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
090: ff ff ff ff ff ff ff ff "f ff fF ff ff f ff ff f
0g0> fn0gf(ff fv!fn f` vg nf vg nf ff ff ff ff ff ff
0b0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
0c0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
0d0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
0e0: foff0ff oF f0ff oF f6f )Ff 6f fofd0ff oF fv fb
0f0:$ of0ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
in the first line and first column, we see the right data that we write in the chip
but other bits are garbage...
I don't know why...