11
\$\begingroup\$

Atmega16 datasheet says that it has

a) 16 Kbytes of In-System Self-programmable Flash program memory and b) 512 Bytes EEPROM.

Can a microcontoller have two separate ROMs which can be programmed through EEPROM technology and Flash technology?

Or Is my inference(as given above) from the datasheet is wrong?

I know that our program is stored in flash memory than why will anyone need EEPROM? What is its use if we have flash memory for the program?

Also can any one explain what is the term "In-System Self-programmable"

What I know : Flash technology can write the program in blocks of data whereas EEPROM can write data byte by byte.

\$\endgroup\$
17
\$\begingroup\$

Nowadays, Flash memory is used to hold program code, and EEPROM (Electrically Erasable Read-only Memory) is used to hold persistent data. Back some 30 years ago, before Flash came along, EEPROMs were used to hold program code.

Actually ROM (Read-Only Memory) came first, then PROM (Programmable ROM, once only), EPROM (PROM Erasable with UV light), EEPROM, and finally Flash. ROMs are still used for very high-volume, low-cost applications (e.g. talking greeting cards).

The important difference with current microcontrollers is that you cannot generally execute code out of EEPROM, and it is awkward for programs to store data in flash. (Data is stored in flash when for example you use the "const" keyword in a data declaration, or define a string, but that is handled behind the scenes by the compiler and linker.)

The EEPROM area can be used to hold configuration or other data which you want to be available across reboots including if the microcontroller has lost power and is then powered back up. Functionally, you can think of the EEPROM as a very small hard drive or SD card.

On microcontrollers without EEPROM, it is possible to store persistent data in flash memory, but this becomes difficult since microcontrollers were not really designed for this, and you have to find a special spot that will not interfere with the program code, and set this aside with the linker. Plus as mentioned below, you can usually update the EEPROM many times more than the flash.

If you do program data in flash, this doesn't mean you can access the data as variables in your C program, because there is no way to tell the compiler where these variables are in your code (i.e. you can't bind a const variable to this area of flash.) So reading them has to be done through the special set of registers that are used to write them. Note this restriction applies to the data in EEPROM also, so it has no advantage in this regard.

To program either flash or EEPROM, a block of memory first must be erased. Then it is programmed. For flash, writing is usually done a block at a time also. For EEPROMs, it can be done by blocks or a byte at a time, depending on the microcontroller.

For both flash and EEPROMs, there is a maximum number of times you can update them before you wear out the memory. This number is given in the datasheet as a minimum guaranteed value. It is usually much higher for EEPROMs than for flash memory. For flash, I have seen numbers as low as 1000. For EEPROMs, I have seen numbers as high as 1,000,000.

One advantage of EEPROMs over flash, is that you can erase them many more times than you can erase flash.

"In-System Self-programmable" simply means the microcontroller can update its own flash while running. The feature is usually used to updated code in the field. The trick is that you need to leave some code in the system while the main program is being updated, called the bootloader. This scheme is used in the Arduino system to program the chip.

\$\endgroup\$
  • \$\begingroup\$ Thank you for your response sir. So to enter data inside EEPROM I will need a EEPROM programmer as in a USBASP programmer to dump the program inside flash memory? Am i right Sir. \$\endgroup\$ – Jasser May 1 '15 at 5:53
  • 1
    \$\begingroup\$ @Jasser No, you don't need an external programmer. You will access the EEPROM from within your program. There will be a set of registers inside the ATmega which, by writing to them, will allow you to update the EEPROM. You will also need to use these registers to read out of the EEPROM, since it will not show up in the normal address map. The information on how to erase, write and read the EEPROM will be in the datasheet for the chip. \$\endgroup\$ – tcrosley May 1 '15 at 5:58
  • \$\begingroup\$ Since we can write or read from the EEPROM inside the microcontroller than the microcontroller should have a circuitry to store data inside EEPROM and that should be similar to the EEPROM programmer. This again leads me to another question that since EEPROM can be wirtten approximately 100,000 times than after that we might not be able to store data inside EEPROM through the registers. Are these statements meaningful? @tcrosley \$\endgroup\$ – Jasser May 1 '15 at 6:03
  • 1
    \$\begingroup\$ @Jasser Correct, there is a limit on the number of times you can update the EEPROM. This is typically much higher (sometimes an order of magnitude more) than the number of times you can update the flash. I should have mentioned that in my answer, and will update it. The number you quoted is a minimum guaranteed by the chip maker; I did some tests once with a chip that was spec'ed for 100,000 writes, and it went well over 500,000 before any errors were detected. \$\endgroup\$ – tcrosley May 1 '15 at 6:10
  • 1
    \$\begingroup\$ @Jasser Yes, it is a little more difficult to set up the data in flash. Also, as discussed in my update to my answer, you can erase and reprogram the EEPROM many times more (typically 10x) than the flash. \$\endgroup\$ – tcrosley May 1 '15 at 6:20
3
\$\begingroup\$

I'll add some more info to the excellent answer by @tcrosley.

The ATmega16 implements an Harvard architecture, i.e. a system topology where the data memory is separated from the program memory. Quoting the relevant paragraph from the Atmega16 datasheet (page 8):

In order to maximize performance and parallelism, the AVR uses a Harvard architecture – with separate memories and buses for program and data. Instructions in the program memory are executed with a single level pipelining. While one instruction is being executed, the next instruc- tion is pre-fetched from the program memory. This concept enables instructions to be executed in every clock cycle. The program memory is In-System Reprogrammable Flash memory.

Harvard architecture has the advantage of having no bus contention between instruction fetching cycles and data access cycles, since data and instructions don't share the same bus, like in your conventional PC architecture.

Therefore the flash memory is used as the program memory, whereas the data memory is split between SRAM (for transient data, like function call stack and the heap - if you are programming in C, for example) and the EEPROM (for permanent storage).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.