#24
AVR Memory
Part 2: EEPROM
Follow along with this
series! Joe’s book & kits
are available at
www.nutsvolts.com
by Joe Pardue
Recap
More on Computer Memory
Last month, we introduced computer memory and
learned a little about how the C programming language
uses pointers for addresses to data variables. We also
learned to use Pelles C to test C programming concepts on
a PC. This month, we will get back to looking at examples
for the AVR memory as applied to reading the EEPROM.
Pointer Review
52 July 2010
Yes, we belabored this last month but I’ve never felt
comfortable explaining pointers because I still tend to
mess them up. (I remember how many false starts I had
trying to learn them.) On the surface, they are simple: A
pointer is a memory location containing the address of
another memory location. In C, a pointer is a data type for
a variable intended to hold the address of another variable.
You tell C that a data type is a pointer by marking it with
an asterisk ‘*’. So, when you define char *myCharPointer,
you are telling the C compiler that myCharPointer is the
address of a character. Then, if you want to set this variable
to the address of a char, you use the ‘&’ operator to extract
it. So, if you define char myChar = ‘a’, you can get the
address of myChar by stating myCharPointer = &myChar. This
may sound simple, but implementation can be the killer.
Cliff Lawson — the number one poster on AVRFreaks —
manages large software projects with 50+ programmers and
he says that 50% of the bugs come from pointers. And his guys
know what they are doing. So, expect to have to approach
learning about pointers many times and from many directions.
The best thing I can suggest to help you learn to safely use
pointers is to write small pieces of code and thoroughly
test them before including them in larger pieces of code.
When you get experienced enough that pointers
seem second nature, that’s when you will start to get into
real trouble and you’ll get bugs that drive you, well …
buggy. That’s part of the price of admission to C
programming of microcontrollers. (Hey, what a nice title
for a book — and coincidentally, a book that you can get
from Nuts & Volts, along with an excellent hardware
projects kit to give yourself a leg up on this C stuff.)
An ideal computer as imagined by John von Neumann
would have a single memory that holds the program and
data. In the real world, however, memory speed and cost
play a role, and few real computers are entirely von Neumann
in their architecture. Computers that use different kinds of
storage for programs and data are referred to as having a
Harvard Architecture, and most contemporary
microcontrollers use modifications of this concept.
When a computer is running, it must read the program
memory as rapidly as possible. Based on the instructions
contained in program memory, it reads and writes data
memory. The main difference in the program and data memory
areas is that the computer only needs to read program
memory, but it must both read and write to data memory. It
turns out that from a hardware perspective, it is much cheaper
to make memory that is mostly read from, than memory that
is both read and written to. So, to fit these different needs
and economies, two types of memory were developed. For
data, we use RAM (Random Access Memory) that can be
easily written to and read from. RAM can be volatile meaning
that it doesn’t matter if it loses its data when the power is
off. However, for the stored program that is mostly read
we use ROM (Read Only Memory). ROM must be
non-volatile so that it can retain the program when the power
is off. ROM is much cheaper to manufacture than RAM.
You often see computers with plentiful, cheap ROM, but
with much smaller amounts of the more expensive RAM.
The terminology isn’t 100% clear, of course, because
if ROM is read only, how do we store (write) the program
on it in the first place? The answer is that it’s really a ‘read
mostly’ design that can be written to — usually very slowly
and only with special programming equipment. But, as we
will see in a minute, one of the innovations of the AVR
was that it uses a special type of ROM (Flash EEPROM)
for the program memory that allows it to be programmed
by ISP (In System Programming).
SRAM
SRAM (Static Random Access Memory) is fast but