add ability to write to EEPROM

The Atmel ATMEGA8 has 512 bytes of EEPROM on the chip. It would be very handy if we could use it with the Arduino language.

seek the "hidden powers" and ye shall find :wink:

look in the playground wiki under the code section
http://www.arduino.cc/playground/Main/CodeLib

cheers
b.

That's not quite enough info. The sample code below might help. Maybe someone can update the wiki? Also, this would be a great thing to have a library for; something like:

Eeprom.write(address, value, BYTE);
Eeprom.write(address, value, WORD);
Eeprom.read(address, BYTE);
Eeprom.read(address, WORD);

Here's the eeprom sample (copied by hand so maybe it's wrong):

#include <avr/eeprom.h>

unsigned int addr = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  long value = eeprom_read_byte((unsigned char *) addr);
  //long value = eeprom_read_word((unsigned int *) addr);
  //eeprom_write_byte((unsigned char *) addr, addr); // fill each address with its own value

  addr = (addr + 1) % 512;
  Serial.println(value);
  delay(1000);
}

you asked it for
the wiki is now updated

the Eeprom library can be a very good thing to understand how to code libraries...
I'll see if I can give it a try...if I only could find a spare life lying around :stuck_out_tongue:

thanks mellis
b.

I post it here just for completenedd
I've found the reference page in the AVR-libc manuals
http://www.sigterm.de/projects/sens-o-nuts/Dokumentation/atmel/libc/group__avr__eeprom.html

I can say that your code is way more clear :slight_smile:
but hey...it's just a reference

cheers
b.

some code to use the eeprom read/write is here... sorry in french :cry:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1146607557

hope this'll be helpfull

eric

How to write over 255 i use intiger but somehow it cant compile if i don't use char?

Hello,

I think, ...

Declaration's of eeprom_read_byte (const uint8_t *addr) look wrong with tionuint8 (BYTE) for type of addr.

I have made some functions to read/write from/to eeprom.

void eeprom_wr_byte (unsigned int const * ptr, byte val)
{ 
  register byte sreg; 

  sreg = SREG; 
  cli(); 
  while ( EECR & (1 << EEWE )) { 
    SREG = sreg; 
    cli (); 
  } 
  EEAR = (unsigned int)ptr; 
  EEDR = val; 
  EECR |= ( 1 << EEMWE ); 
  EECR |= ( 1 << EEWE ); 
  SREG = sreg; 
}

void eeprom_wr_word (unsigned int const * ptr, unsigned int val)
{
  unsigned int uiTmp = (unsigned int)ptr;
  byte bH = (val >> 8) & 0xFF;
  byte bL = val & 0xFF;
  eeprom_wr_byte ((unsigned int *)uiTmp, bL);
  uiTmp++;
  eeprom_wr_byte ((unsigned int *)uiTmp, bH);
}

And...

byte eeprom_rd_byte (unsigned int const * ptr)
{ 
  register byte sreg, tmp; 
  
  sreg = SREG; 
  cli(); 
  while ( EECR & (1 << EEWE )) { 
    SREG = sreg; 
    cli (); 
  } 
  EEAR = (unsigned int)ptr; 
  EECR |= (1 << EERE); 
  tmp = EEDR; 
  SREG = sreg; 
  return tmp; 
} 

unsigned int eeprom_rd_word (unsigned int const * ptr)
{
  unsigned int uiTmp = (unsigned int)ptr;
  unsigned int iRet = 0;
  byte bH = 0;
  byte bL = 0;
  bL = eeprom_rd_byte ((unsigned int *)uiTmp);
  uiTmp++;
  bH = eeprom_rd_byte ((unsigned int *)uiTmp);
  iRet = bH;
  iRet = iRet << 8;
  iRet |= bL;
  return iRet;
}

I played around with mellis pieces of code and made it work.
This code is tested and works for reading from and writing to the internal eeprom:

// simple read and write to internal eeprom
// by Kristian Gohlke / kr1 <http://filformat.net>
//
// reads or writes data to the eeprom and prints it to the screen
// the stored data is preserved even when the boards power connection is unplugged
// so you could think of the eeprom as arduinos amazing 512byte harddrive
// useful for storing the last few bytes of data from a sensor or to keep config settings 
//
// try storing some data to your eeprom unplug the boards power supply and relax
// get back to it some days later to find your data lying there waiting for you untouched
//
// created 13 August 2006
// (cleft) 2006 Kristian Gohlke

#include <avr/eeprom.h>

byte e_in;  //holds the value to be written to the eeprom
byte e_out; //holds the value that is read from the eeprom

//the adress inside the eeprom to write to
//the eeprom can hold 512 bytes, so the adress space is from 0 to 511
unsigned int addr = 0;  

//read or write ?
boolean writemode = true;  
//boolean writemode = false;  
 
void setup() {
  Serial.begin(9600);
  }

void loop() {              

//WRITE TO EEPROM
if(writemode){
  //set the value to be written to the eeprom  
  e_in = 90;                  //the character "Z" (ascii code 90)
  //e_in = (e_in + 1) % 256;  //ascii characters 0 - 256
  
  eeprom_write_byte((unsigned char *) addr, e_in);                  
  addr = (addr + 1) % 512;
  
  //debug:
  Serial.print("WRITE addr: ");
  Serial.print(int(addr));
  Serial.print(" / e_in: ");
  Serial.print(int(e_in));  //prints eeprom value as integer
  Serial.print(" = ");
  Serial.println(e_in);     //prints eeprom value as ascii character
  }

//READ FROM EEPROM
if(!writemode){
  e_out = eeprom_read_byte((unsigned char *) addr);
  addr = (addr + 1) % 512;
  
  //debug:
  Serial.print("READ addr: ");
  Serial.print(int(addr));
  Serial.print(" / e_out: ");
  Serial.print(int(e_out));   //prints eeprom value as integer
  Serial.print(" = ");        
  Serial.println(e_out);      //prints eeprom value as ascii character
  }
      
  delay(20);
}