#29
SPI and the AVR
Butterfly DataFlash
by Joe Pardue
Follow along with this
series! Joe’s book & kits
are available at
www.nutsvolts.com
Recap
Last month, we got a little shifty and learned about
the 74HC595 and 74HC597 shift registers, wrote some
software SPI (Serial Peripheral Interface) code, and built a
new and improved (twice the eyes!) Cylon Eyes project. All
that was in preparation for learning about hardware SPI so
that we can use it to communicate with external Flash
EEPROM memory. This month, we will create a library with
hardware SPI functions. We will use the library with the
built-in 4-Mbit DataFlash on the Atmel AVR Butterfly shown in
Figure 1. The hardware used for the demonstration and the
AVR Butterfly are available in the C Programming Book and
Projects Kit combination that you can get from Nuts & Volts.
the number of available I/O pins. However, with the
hardware SPI we have only a single SPI – but it requires
that we generate our own slave select line, thus allowing
us to have many slaves as shown in Figure 2. The slaves
will only pay attention if their SS pin is held low, so by
simply holding the other slave SS lines high, we get them
to ignore us. We can also expand the number of slaves by
daisy-chaining them as shown in Figure 3. The difference
in using these two is that for the first, we have to select
only one device at a time; in the second, we select all the
devices at once, but must transfer all the data for each
device at once (in the case shown, it would be three bytes).
Hardware SPI
AVR SPI Library
62 December 2010
This month, we will create,
document, and share a library of
some AVR software and hardware
SPI functions. To help keep things
simple, we will only look at the SPI
master mode — but the general
principles apply to both master and
slave modes so if you need your
AVR to be a slave, you should be
able to figure out how to implement
this from the supplied code.
In Workshop 28, we learned
how to bit-bang (you control each
pin state) a software SPI bus so that
we could theoretically use any
available AVR 4 I/O pins to create
an SPI link. We wrote the code to
simplify the process of creating
multiple SPI links and for compiling
the code for multiple AVR devices
(ATmega169, ‘328, ‘644). This month,
we will extend that code and put it
into a library of source code modules.
With software SPI, we are only
limited in number of SPI devices by
Even after all these years, I still cringe when I open the
datasheets for a device. Atmel does
as good a job as any, but it is still a
daunting task to figure out among
all the choices given, what subset
to select for use in my particular
application. SPI is one of the simplest
peripherals that you can imagine,
but look at the datasheet for any of
the ATmegas we will use and skim
the SPI section. Good grief! Lucky
for you, though, I’ve selected a
nice little subset that works well
with the shift registers we looked at
last month, and the DataFlash that
is resident on the Butterfly.
■ FIGURE 1. Four-Mbit DataFlash
on Atmel AVR Butterfly.