Microcontroller Memory Types

Computer Memory



In this article, we go over the various types of memory which you will find in a microcontroller for embedded applications.

There are normally 3 types of memory present in a microcontrollers. These are SRAM, FLASH, and EEPROM memories.

The architecture of a microcontroller may require that variables and constants be stored in different types of memory.

Data that will not change should be stored in one type of memory, while data that must be read from and written to repetitively in a program should be stored in another type of memory. A third type of memory can be used to store variable data that must be retained even when power is removed from the system.

SRAM is the type of memory where data must be read and written to repeatedly. This is the data will change with different code being uploaded to the AVR microcontroller circuit. By default, this is the most common and used type of memory.

Flash memory is the memory that normally stores data that does not change. This is the program memory. It stores the part of the microcontroller program that is fixed and will always stay permanent. This is similar to the BIOS of a general-purpose computer.

AVR Microcontroller

The AVR microcontroller was designed using Harvard architecture. This is an architecture with separate address spaces for data (SRAM), program (FLASH), and EEPROM memoriy.


Coding the Memory Types in a C Program

A compiler for AVRs called CodeVisionAVR® implement 3 types of memory descriptors to allow easy access to these very different types of memory.

The default or automatic allocation of variables, where no memory descriptor keyowrds is used, is SRAM. Constants can be placed in FLASH memory (program space) with the flash or const keyowrds. For variables to be placed in EEPROM, the eeprom keyword is used.

When declarations are used using these keywords, the positions of flash and eeprom keywords become part of the meaning.

If const, flash, or eeprom appear first, this states to the compiler that the actual allocation of storage or the location of data is in that memory area. If the type is declared followed by the flash or eeprom keyword, this indicates that it is a variable that references FLASH or EEPROM, but the variable itself is physically located in SRAM. This type of case is used when declaring pointers into FLASH or EEPROM.

In this article, we just deal with the first option, allocating storage directly into the memory area. To find out about the second option, referencing other memory areas such as when declaring pointers into FLASH or EEPROM, see How to Code Pointers to Memory in Embedded C.

Examples

FLASH

The following code will place physical data directly in program memory (FLASH memory). These data values are all constant and cannot be changed in any way by program execution.

flash int numberValue= 1200;
flash char alphabet= 'b';
flash long Va
lue2= 99L;
flash long Value3= 0x10000000;
flash int arraynumbers[]= {1,2,3];
flash int arraynumbers2[10]= {1,2,};
flash int multiarray[2,3]= {{1,2,3},{4,5,6}}
falsh char string_constant1[]= "This is a string constant";
flash char string_constant2[]= "this is also a string constant";

flash struct {
int a;
char b[3], c[3];
} sf= {{0x000a}, {0xb1, 0xb2, 0xb3}, {0xb1, 0xb2, 0xb3}};


The above are many examples of code written to FLASH memory. These data values are all physically inserted into the FLASH memory, which again function as the program memory of the microcontroller circuit.

EEPROM

EEPROM memory is nonvolatile. This means that when power is removed from the microprocessor, the data will remain intact. But it is semi-permanent in that the programcan alter the data located in this region.

eeprom int cycle; //allocates an integer space in EEPROM
eeprom char stringvalue[20]; //allocates a 20-byte area in EEPROM

The above are examples of EEPROM memory in a microcontroller.

EEPROM memory has a life- it has a maximum number of write cycles that can be performed before it will electrically fail. This is due to the way that EEPROM itself is constructed, a function of electro-chemistry. In many cases, this memory area will have a rating of 10,0000 write operations, maximum. However, newer applications come out all the time, increasing this maximum number. See the datasheet for the microcontroller in use for this number. Some EEPROM memory can have write operations as high as the hundreds of thousands and millions, so this isn't a concern for the most part.

Data that needs to be kept and does not change frequently can be stored in this area. This region is great for low-speed data logging, calibration tables, runtime hour meters, and software setup, and configuration values.

SRAM

SRAM is the memory that is read and written to repetitively for a microcontroller circuit.

char mystring[30]= "This string is placed in SRAM";

If a string is declared with an initializer such as above, 30 bytes of SRAM will be allocated, and the text "This string is placed in SRAM" is physically placed in FLASH memory. How it works is this: On startup, this FLASH-resident data is copied to SRAM and the program works from SRAM whenever accessing mystring. This is a waste of 30 bytes of SRAM unless the string is intended for alteration by the program during run time. To prevent this loss of SRAM space, the string could be stored in FlASH directly. This would be achieved by placing the flash keyword as the first word in the above line.

These permanent (FLASH) and semi-permanent (EEPROM) memory areas have many system-specific uses in the embedded environment. FLASH space is an excellent area for non-changing data. The program code itself resides in this region. Declaring items such as text strings and arithmetic look-up tables in this region directly frees up valuable SRAM space.

HTML Comment Box is loading comments...