Go Down

Topic: add ability to write to EEPROM (Read 3 times) previous topic - next topic

mrbbp

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

ccdust

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

Benoît ROUSSEAU

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: [Select]
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);
}
Cordialement,
Benoît ROUSSEAU

Benoît ROUSSEAU

#8
Jul 01, 2006, 07:26 pm Last Edit: Jul 01, 2006, 07:28 pm by Benoit Reason: 1
And...

Code: [Select]
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;
}
Cordialement,
Benoît ROUSSEAU

Kristian Gohlke

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: [Select]

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

Go Up