Tuesday, August 30, 2011

Project 1 - Arduino Software Investigation - the EEPROM library


 
EEPROM Library


The microcontroller on the Arduino boards has its own EEPROM:


Electrically Erasable Programmable Read Only Memory.


memory whose values are kept when the board is turned off (like a tiny hard drive).

The microcontrollers on the various Arduino boards have different amounts of EEPROM:


1024 bytes on the ATmega328

512 bytes on the ATmega168 and ATmega8, 

4 KB (4096 bytes) on the ATmega1280 and ATmega2560.


This library enables you to read, read(), and write, write(), those bytes.
 

byte EEPROM.read(address)

Description: Reads a byte from the EEPROM.
Locations that have never been written to have the value of 255.
Parameters: address: the location to read from, from 0 to 511 (int)
Returns: the value stored in that location (byte)

EEPROM.write(address, value)

Description: Writes a byte to the EEPROM.
Parameters:
address
: the location to write to, from 0 to 511 (int)
value
: the value to write, from 0 to 255 (byte)
Returns: none



The Atmega 168 datasheet says EEPROM memory has a specified life of 100000 write/erase cycles,
so there is a limit to how many times you can write information to that memory space.

Bear this in mind for long-lived projects or fast-moving data.
The datasheet also specifies that a write cycle takes 3.3 ms to complete.
Other EEPROM write and read requests will fail if executed in this time period.
This delay appears to be built into the EEPROM library,
as a casual test shows each cycle taking 3.33 ms to execute.
Hence, you do not specifically need to add a delay to an EEPROM write;
just be aware of the built-in time delay.

Below (pp 2,3) is some example code, demonstrating both Read & Write functions.



References
Arduino EEPROM Library. Retrieved from http://arduino.cc/en/Reference/EEPROM

Example

/*
 * Project:    EEPROM_ReadWrite_621project01
 * Author:     Jane-Maree Howard
 * Date:       Friday 0/08/2011
 * Platform:   Arduino 22
 * Purpose:    To demonstrate EEPROM Read & Write functions via the Serial Monitor (SM)
 * Operation:  Description:
               Include: EEPROM.h library
               Declare: EEPROM memory size; value read from EEPROM
               Functions: PrintSM(int,int) ReadEEPROM(int); EEPROM.write(int,int); 
               Setup(): SM @ 9600baud;  write to EEPROM            
               Loop():  read from EEPROM 
 * Comment:    an integer written to EEPROM will return its LSByte!
 */
#include  

//int  iMemSize  = 512; // ATmega168
int  iMemSize  = 1024;  // ATmega328
int iValue;             // value read from EEPROM

void setup()   
{
  Serial.begin(9600);  //SM @ 9600baud
  //now write to EEPROM
  for (int j=0; j
  {
    Serial.print("Writing to\t");
    PrintSM(j,j);
    WriteEEPROM(j,j);
  }//for   
}//end setup()

void loop()                     
{
  //now read EEPROM  
  for (int j=0; j
  {
    Serial.print("Reading from\t");
    iValue  = ReadEEPROM(j);
    PrintSM(j,iValue);
    delay(250);
  }//for
}//end loop()

/* PrintSM() - prints parameters to SM */
void PrintSM(int iAddr,int iVal)
{
  Serial.print(iAddr);
  Serial.print("\t");
  Serial.print(iVal);
  Serial.println();
}//printSM()

// below are the two feature functions, READ & WRITE

/* ReadEEPROM() - returns a byte from the parameter address */
int ReadEEPROM(int iAddr)
{
  // this is the READ function
  int iVal  = EEPROM.read(iAddr);
  // don't go beyond memory size
  if (iAddr >= iMemSize)
    iAddr  = 0;
  return iVal;
}//ReadEEPROM()


/* WriteEEPROM() - writes a parameter byte to the parameter address */
void WriteEEPROM(int iAddr,int iVal)
{
  // this is the WRITE function
  EEPROM.write(iAddr,iVal); 
  // don't go beyond memory size
  if (iAddr >= iMemSize)
    iAddr  = 0; 
}//WriteEEPROM()

//END


No comments:

Post a Comment