Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Write/read the internal EEPROM in AVR using avrgcc (winAVR)

The following macro can be used to simplify the process of using the internal AVR EEPROM

Code:
#include <avr/eeprom.h>
// defines for eeprom access	
#define read_eeprom_byte(address) eeprom_read_byte ((const uint8_t*)address)
#define write_eeprom_byte(address,value) eeprom_write_byte ((uint8_t*)address,(uint8_t)value)
#define read_eeprom_word(address) eeprom_read_word ((const uint16_t*)address)
#define write_eeprom_word(address,value) eeprom_write_word ((uint16_t*)address,(uint16_t)value)
#define read_eeprom_dword(address) eeprom_read_dword ((const uint32_t*)address)
#define write_eeprom_dword(address,value) eeprom_write_dword ((uint32_t*)address,(uint32_t)value)
#define read_eeprom_float(address) eeprom_read_float ((const float *)address)
#define write_eeprom_float(address,value) eeprom_write_float ((float*)address,(float)value)
#define read_eeprom_array(address,value_p,length) eeprom_read_block ((void *)value_p, (const void *)address, length)
#define write_eeprom_array(address,value_p,length) eeprom_write_block ((const void *)value_p, (void *)address, length)

uint8_t	my_byte;
uint16_t my_word;
uint32_t my_dword;
float my_float;
char my_text[10]={"123456789\0"};

//--------------- code inside main-----------------
// [COLOR="red"]these will be written at runtime (and stored in first run), data will not be included in the eep file[/COLOR]
write_eeprom_byte(1,0x0A);		  // write in eeprom position 1 the value 0x0A
write_eeprom_word(3,0x0AAA);		  // write in eeprom position 3 the value 0x0AAA
write_eeprom_dword(10,0x0AAAAAAA);// write in eeprom position 10 the value 0x0AAAAAAA
write_eeprom_float(5,0.123456);// write in eeprom position (5) the value 0.123456 , note it will occupy 4 bytes in eeprom 5,6,7,8
write_eeprom_array(10,my_text,8);    // write in eeprom position 10 the 8 first characters of my_text array


// and you can read them using
my_byte=read_eeprom_byte(1);			
my_word=read_eeprom_word(3);		
my_dword=read_eeprom_dword(10);
my_float=read_eeprom_float(5);
read_eeprom_array(10,my_text,5); // read to my_text array 5 char starting from eeprom position 10

or if you don't want to bother about specific eeprom address
Code:
uint8_t EEMEM	  my_eeprom_byte;
uint16_t EEMEM	  my_eeprom_word;
uint32_t EEMEM    my_eeprom_dword;
float EEMEM my_eeprom_float;
uint8_t 	  my_byte;
uint16_t	  my_word;
uint32_t 	  my_dword;
float 	  my_float;


//--------------- code inside main-----------------

write_eeprom_byte(&my_eeprom_byte,0x0f);		// [COLOR="red"]this will be written at runtime (and stored in first run), it will not be included in the eep file[/COLOR]
write_eeprom_word(&my_eeprom_word,0x0fff);		// [COLOR="red"]this will be written at runtime (and stored in first run), it will not be included in the eep file[/COLOR]
write_eeprom_dword(&my_eeprom_dword,0x0fffffff);	// [COLOR="red"]this will be written at runtime (and stored in first run), it will not be included in the eep file[/COLOR]
write_eeprom_float(&my_eeprom_float, 0.1234567);	// [COLOR="red"]this will be written at runtime (and stored in first run), it will not be included in the eep file[/COLOR]

// and you can read them using
my_byte=read_eeprom_byte(&my_eeprom_byte);			
my_word=read_eeprom_word(&my_eeprom_word);		
my_dword=read_eeprom_dword(&my_eeprom_dword);
my_float=read_eeprom_float(&my_eeprom_float);

if you want to store some data in the eeprom when you compile the code (so they are added to the eep file and use them to fill the avr eeprom when you program the device),
you need to use the EEMEM and initialize it with a value like the following example:

Code:
//

uint8_t EEMEM eeprombyte=0x10;		[COLOR="red"]//store initial byte to eeprom, eep file will include this[/COLOR]

uint16_t EEMEM eepromword=0x5555;	[COLOR="red"]//store initial word to eeprom, eep file will include this[/COLOR]

uint8_t EEMEM eepromstring[5]={"Test\0"}; 	[COLOR="red"]//store string to eeprom, eep file will include this[/COLOR]

int main(void)
{
 uint8_t RAMbyte;		//RAM byte variable
 uint16_t RAMword;		//RAM word variable
 uint8_t RAMstring[5];		//RAM array of bytes

 RAMbyte = read_eeprom_byte(&eeprombyte);	    //read byte from EEPROM and store to RAM 
 RAMword = read_eeprom_word(&eepromword);	    //read word from EEPROM and store to RAM
 read_eeprom_array(&eepromstring,&RAMstring,5);		//copy string from EEPROM to RAM
....
}

Alex

Comments

There are no comments to display.

Part and Inventory Search

Blog entry information

Author
alexan_e
Read time
3 min read
Views
1,402
Last update

More entries in AVR

More entries from alexan_e

Share this entry

Back
Top