Serial EEPROMs (Electrically Erasable Programmable Read-Only Memory) are non-volatile memory devices. This means that the data is retained even if the chip is not being powered.
These type of EEPROMs can be written more than 1 million times and behave like tiny hard drives which make them ideal for data and program storage.
Enough of theory but first let me explain why I decided to develop such project. Around April 2013 one of the Arduinos I own, a Nano 3.0 to be more specific, had a faulty voltage regulator. This flaw put the 12V from the power supply directly into the 5V line causing a permanent damage to the uC (ATmega328P). Also, as I had attached to the circuit a 24C32 EEPROM, I needed to make sure if it was dead or not.
So, I wrote a few lines of code which would help me confirm for any memory bank flaws on the 24C EEPROM series. However, even though serial EEPROMs can be written more than 1 million times, do not run it for too long. The software will run indefinitely until it finds a flaw.
The circuit is extremely simple. You just need an Arduino and a 24C series EEPROM to test.
And now the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
/***************************************************************************** * Copyright (C) 2013 by Vasco Ferraz. All Rights Reserved. * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see <http://www.gnu.org/licenses/>. * * * * Author: Vasco Ferraz * * Contact: http://vascoferraz.com/contact/ * * Description: http://vascoferraz.com/projects/24c-eeprom-check/ * *****************************************************************************/ #include <Wire.h> #define EEPROM_I2C_ADDRESS 0x50 //uncomment accordingly: //int maxaddress=128-1; //24C01 -> 1024 bit -> 128 byte //int maxaddress=256-1; //24C02 -> 2048 bit -> 256 byte //int maxaddress=512-1; //24C04 -> 4096 bit -> 512 byte //int maxaddress=1024-1; //24C08 -> 8192 bit -> 1024 byte //int maxaddress=2048-1; //24C16 -> 16384 bit -> 2048 byte //int maxaddress=4096-1; //24C32 -> 32768 bit -> 4096 byte //int maxaddress=8192-1; //24C64 -> 65536 bit -> 8192 byte //int maxaddress=16384-1; //24C128 -> 131072 bit -> 16384 byte //int maxaddress=32768-1; //24C256 -> 262144 bit -> 32768 byte //unsigned int maxaddress=65536-1; //24C512 -> 524288 bit -> 65536 byte int address = 0; unsigned int value = 0; unsigned int check_value = 0; void setup(void) { Serial.begin(9600); Wire.begin(); randomSeed(analogRead(0)); } void loop() { for (address=0 ; address<=maxaddress ; address++) { value = random(256); // generates a random number between 0 and 255 writeEEPROM(EEPROM_I2C_ADDRESS, address, value); Serial.print(address); Serial.print(" - "); Serial.print(value); Serial.print(" - "); check_value = readEEPROM(EEPROM_I2C_ADDRESS, address); Serial.print(check_value); Serial.print(" - "); if (check_value != value) {Serial.println("NOK"); while(1);} else if (check_value == value) {Serial.println("OK");} } } void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) { if (maxaddress <= 255) { Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddress)); Wire.write(data); Wire.endTransmission(); } if(maxaddress >= 511) { Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddress >> 8)); // MSB Wire.write((int)(eeaddress & 0xFF)); // LSB Wire.write(data); Wire.endTransmission(); } delay(5); } byte readEEPROM(int deviceaddress, unsigned int eeaddress ) { byte rdata = 0xFF; if(maxaddress <= 255) { Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddress)); Wire.endTransmission(); } if(maxaddress >= 511) { Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddress >> 8)); // MSB Wire.write((int)(eeaddress & 0xFF)); // LSB Wire.endTransmission(); } Wire.requestFrom(deviceaddress,1); if (Wire.available()) rdata = Wire.read(); return rdata; } |
Now compile it, open the Serial Monitor and let it run for a while. The first number represents the byte position which will be tested. The second number is the random value which will be written into that byte. If the read value (third number) equals the written value an “OK” message will pop. Else, a “NOK” message will be thrown and the program stops.
I really hope that your EEPROM is fully functional 🙂
As homework you can try to change this code into a more effective and simpler one. My idea is to write in each byte the following bits: 0b10101010. And on the next run write its invert: 0b01010101. With a code like this you can check the entire EEPROM with only two write/read cycles.