Skip to main content

Reading and writing Atmega168 EEPROM

EEPROM (Electrically Erasable Programmable Read Only Memory) Is non-volatile memory, meaning it persists after power is removed. The ATmega168 microcontroller has 512 bytes of EEPROM which can be used to store system parameters and small amounts of data. This tutorial shows you how to read and write EEPROM.

Overview

Our code examples will be very simple. We have an ATmega168 microcontroller connected to an LCD display. In our first example we will:

  • Read an integer value from EEPROM
  • Increment that value
  • Output it to the LCD module
  • Store the value back to EEPROM

This counts how many times the device has been powered up or reset. In later examples we’ll be adding other datatypes and interracting with the EEPROM in slightly different ways.

Building the circuit

No need to reinvent the wheel here. We will use the circuit from the Character LCD Displays – Part 2 post we did last year, but build it on a breadboard instead..

Circuit Diagram

First we need to build a basic Atmega168 breadboard circuit. See “Atmega8 breadboard circuit” Part 1 and Part 2 but substitute an Atmega168 for the Atmega8.

Next we add the necessary connections for the LCD module.

Wiring up the LCD Module

Now we solder a single row header to the back of the LCD module and insert it into the breadboard.

Inserting the LCD Module into the breadboard

Using AVRdude Terminal Mode to read EEPROM

AVRdude terminal mode allows you to establish a session with the microcontroller and issue commands to it. To enter terminal mode use the following command

avrdude -p m168 -c usbasp -t

and to dump EEPROM to the standard output

dump eeprom 0 512

as shown in the example below.
AVRdude terminal mode

You will notice that all EEPROM values are “0xFF”. This are the default values for an erased EEPROM.

Reading and writing EEPROM Programmatically

To following program demonstrates how to read and write a double byte value to EEPROM.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <avr/eeprom.h> 
#include "hd44780.h"
 
#define NUM_EXECUTIONS_ADDRESS 0x00
 
int main (void)
{
    lcd_init();
 
    uint16_t num_executions = eeprom_read_word((uint16_t*)NUM_EXECUTIONS_ADDRESS);
    if (num_executions==0xFFFF)
        num_executions=0;
    num_executions++;
    eeprom_write_word((uint16_t*)NUM_EXECUTIONS_ADDRESS,num_executions);
 
    lcd_clrscr();
 
    char buffer[16];
    sprintf(buffer,"%d Executions", num_executions);
    lcd_puts(buffer);
 
    return(0);
}

Before we can use any of the EEPROM functions in the avr-libc library, we need to include the EEPROM header file. This is done at line 1.

At line 10 we define the num_executions variable and load it from EEPROM location 0. This variable is a double byte unsigned integer.

You will recall that the default values for an erase byte is 0xFF. At lines 11 & 12 we set num_executions to 0 if being run against an erase EEPROM (e.g. first execution).

Lastly at line 14 we write away the updated num_executions value.

Reading and writing other datatypes

In the previous example we read and wrote a double byte datatype. In this example we will write:

  • Double byte unsigned integer, as in the previous example
  • Single byte unsigned integer. This will be 0x00 for even instances of the counter and 0x01 for odd instances.
  • A text string. This will be “Even” for even instances of the counter and “Odd” for odd instances
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <avr/eeprom.h> 
#include <string.h>
#include "hd44780.h"
 
#define NUM_EXECUTIONS_ADDRESS      0x00
#define ODD_OR_EVEN_ADDRESS         0x02
#define ODD_OR_EVEN_TEXT_ADDRESS    0x03
#define ODD_OR_EVEN_TEXT_LENGTH     5
 
int main (void)
{
    lcd_init();
 
    uint16_t num_executions = eeprom_read_word((uint16_t*)NUM_EXECUTIONS_ADDRESS);
    if (num_executions==0xFFFF)
        num_executions=0;
    num_executions++;
    eeprom_write_word((uint16_t*)NUM_EXECUTIONS_ADDRESS,num_executions);
 
    uint8_t odd_or_even = eeprom_read_byte((uint8_t*)ODD_OR_EVEN_ADDRESS);
    if (odd_or_even==0xFF)
        odd_or_even=num_executions % 2;
    else
        odd_or_even = !odd_or_even;
    eeprom_write_byte((uint8_t*)ODD_OR_EVEN_ADDRESS,odd_or_even);
 
    char odd_or_even_string[ODD_OR_EVEN_TEXT_LENGTH];
    eeprom_read_block((void*)&odd_or_even_string, (const void*)ODD_OR_EVEN_TEXT_ADDRESS, ODD_OR_EVEN_TEXT_LENGTH);
    char ff_string[5]={0xFF,0xFF,0xFF,0xFF,0xFF};
    if (!strncmp(odd_or_even_string,ff_string,ODD_OR_EVEN_TEXT_LENGTH))
        strcpy(odd_or_even_string,"Odd");
    else if (strcmp(odd_or_even_string,"Even"))
        strcpy(odd_or_even_string,"Even");
    else
        strcpy(odd_or_even_string,"Odd");
    eeprom_write_block ((void*)&odd_or_even_string, (const void*)ODD_OR_EVEN_TEXT_ADDRESS, ODD_OR_EVEN_TEXT_LENGTH);
 
    lcd_clrscr();
    char buffer[16];
    sprintf(buffer,"%d Executions", num_executions);
    lcd_puts(buffer);
    lcd_goto(40);
    sprintf(buffer,"%d %s", odd_or_even, odd_or_even_string);
    lcd_puts(buffer);
 
    return(0);
}

Reading and writing the 8 bit value is almost identical to reading and writing the 16 bit value. If you look at lines 20-25 you will see different functions and datatypes being used but the differences are minor.

The interesting one I think is where we read and write the string value. This is done between lines 27 and 36. The 2 function we use are:

void  eeprom_read_block (void *__dst, const void *__src, size_t __n)
void  eeprom_write_block (const void *__src, void *__dst, size_t __n)

In c, “void *” is used to denote a generic pointer. When we pass real pointers to the function we need to cast them as “void *”. This is shown in lines 28 and 36.

The eeprom_read_block and eeprom_write_block function are very versatile as they can be used with any datatype. For example you could read/write structs or multi dimension arrays using these 2 functions.

Examining EEPROM using AVRdude Terminal Mode

After running the code and pressing reset a bunch of times, let’s examine the EEPROM using AVRdude.

AVRdude terminal mode

The values of “07 00 01 45 76 65 6e 00” correspond to

  • num_executions = 7 (notice the reverse byte encoding)
  • odd_or_even = 1
  • odd_or_even_string = “Even” (notice the null termination in the string)

Using the EEMEM attribute

In large projects, it can be tedious to define the addresses for each EEPROM stored variable. The EEMEM attribute is used to auto allocate addresses for the non-volatile variables. Consider the following code example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <avr/eeprom.h> 
#include <string.h>
#include "hd44780.h"
 
#define ODD_OR_EVEN_TEXT_LENGTH 5
 
uint16_t EEMEM ee_num_executions;
uint8_t EEMEM ee_odd_or_even;
char EEMEM ee_odd_or_even_string[ODD_OR_EVEN_TEXT_LENGTH];
 
int main (void)
{
    lcd_init();
 
    uint16_t num_executions = eeprom_read_word(&ee_num_executions);
    if (num_executions==0xFFFF)
        num_executions=0;
    num_executions++;
    eeprom_write_word(&ee_num_executions,num_executions);
 
    uint8_t odd_or_even = eeprom_read_byte(&ee_odd_or_even);
    if (odd_or_even==0xFF)
        odd_or_even=num_executions % 2;
    else
        odd_or_even = !odd_or_even;
    eeprom_write_byte(&ee_odd_or_even,odd_or_even);
 
    char odd_or_even_string[ODD_OR_EVEN_TEXT_LENGTH];
    eeprom_read_block((void*)&odd_or_even_string, (const void*)&ee_odd_or_even_string, ODD_OR_EVEN_TEXT_LENGTH);
    char ff_string[5]={0xFF,0xFF,0xFF,0xFF,0xFF};
    if (!strncmp(odd_or_even_string,ff_string,ODD_OR_EVEN_TEXT_LENGTH))
        strcpy(odd_or_even_string,"Odd");
    else if (strcmp(odd_or_even_string,"Even"))
        strcpy(odd_or_even_string,"Even");
    else
        strcpy(odd_or_even_string,"Odd");
    eeprom_write_block ((void*)&odd_or_even_string, (const void*)&ee_odd_or_even_string, ODD_OR_EVEN_TEXT_LENGTH);
 
    lcd_clrscr();
    char buffer[16];
    sprintf(buffer,"%d Executions", num_executions);
    lcd_puts(buffer);
    lcd_goto(40);
    sprintf(buffer,"%d %s", odd_or_even, odd_or_even_string);
    lcd_puts(buffer);
 
    return(0);
}

In lines 7 to 9 we define 3 variables that use the EEMEM attribute. The EEMEM attribute tells the compiler that these variables are store in EEPROM.

Even though these variables look like any other variable, we cannot use them directly. We still need to use the EEPROM read and write functions, but this time we use a pointer to the variable to denote the EEPROM address. This can be seen in lines 15, 19, 21, 26, 29 & 37.

Disabling EEPROM erasure

By default when we run AVRdude to upload the firmware a chip erase is performed. This erases FLASH and EEPROM memory.

The AVRdude -D option can disable the erasure, but this is not recommended as FLASH sections that are not being written with the new firmware will remain as they are. A much better approach is to use fuse settings.

By default the Atmega168 ships with fuse settings of 0x62 and 0xDF (low and high). By setting the EESAVE bit, we can prevent an EEPROM erasure during the chip erase cycle. To do this, use the following command.

avrdude -p atmega168 -P usb -c usbasp -U hfuse:w:0xd7:m

and to revert back to EEPROM erasing mode

avrdude -p atmega168 -P usb -c usbasp -U hfuse:w:0xdf:m

Note: Care should always be taken when writing fuse settings. It is very easy to render your microcontroller inoperable by setting incorrect fuse settings.

Writing default EEPROM Values

When the program is compiled, an eep file is generated. This contains an EEPROM map which can be uploaded to the microcontroller if required. The contents of this file can be set by initializing the non-volatile variables as follows.

1
2
3
uint16_t EEMEM ee_num_executions = 100;
uint8_t EEMEM ee_odd_or_even = 0;
char EEMEM ee_odd_or_even_string[ODD_OR_EVEN_TEXT_LENGTH] = "Even";

The eep file can be uploaded to the microcontroller by adding “-U eeprom:w:main.eep” to your AVRdude command string. If you are using the “make program” command to upload your firmware, simply uncomment line 208 of the makefile as follows

AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep

This will ensure that the eep file is uploaded with every “make program” command.

Share the love

If you like this post, please post it on twitter, Facebook or your blog. Your support is very much appreciated.

Code Downloads

References

Related News

Atmega168 Timer interrupts

This tutorial will teach you about the 8 and 16 bit timers on an ATmega168...

ATmega8 breadboard circuit – Part 3 of 3 – The Firmware

This tutorial continues on from ATmega8 breadboard circuit Part 1 and ATmega8 breadboard circuit Part...

Analogue to Digital Conversion on an ATmega168

Many AVR microcontrollers are capable of doing Analogue to Digital Conversion. The ATmega168 has 6...

3 Comments

  1. Dave Kern

    I just wonder how to adapt the technique so I can reprogram a corrupt BIOS eeprom on motherboard. I know I can port to my rs232. Has anyone used the method to get a motherboard running again?

  2. Devin Eutsler

    Great tutorial. I am developing an idea in which the device needs to have initial settings that are adjustable. Once these settings are adjusted/changed from their initial values, the device must be able to remember the new user settings so that when the batteries die and then are replaced the device will use the new adjusted settings instead of the initial values. I have not tested it yet, but I am pretty sure with the help of this tutorial I have it figured out.

  3. Arup Basak

    WOW. Bookmarked it.
    Can we connect an PS2 keyboard somehow to enter text and the text saved to eeprom also with text display facility?

Leave a reply

Shopping Cart