Pages: [1]   Go Down
Author Topic: add ability to write to EEPROM  (Read 3300 times)
0 Members and 1 Guest are viewing this topic.
Brooklyn, NY, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 115
arduino for all
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
panta rei resist the resistors!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

seek the "hidden powers" and ye shall find ;-)

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

cheers
b.
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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):

Code:
#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);
}
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
panta rei resist the resistors!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-razz

thanks mellis
b.

Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
panta rei resist the resistors!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
but hey...it's just a reference

cheers
b.
Logged

Britanny
Offline Offline
Full Member
***
Karma: 0
Posts: 233
Mais vrai, j'ai trop pleuré, toute lune est atroce
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

hope this'll be helpfull

eric
Logged

Slovenia
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Poitiers (France)
Offline Offline
Full Member
***
Karma: 0
Posts: 136
Ca va j'vais le faire !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Code:
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);
}
Logged

Cordialement,
Benoît ROUSSEAU

Poitiers (France)
Offline Offline
Full Member
***
Karma: 0
Posts: 136
Ca va j'vais le faire !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And...

Code:
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;
}
« Last Edit: July 01, 2006, 12:28:02 pm by Benoit » Logged

Cordialement,
Benoît ROUSSEAU

Bremen, Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
// 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);
}
Logged

Pages: [1]   Go Up
Jump to: