Professional Documents
Culture Documents
Microcontroller
BootCamp (6)
The SPI interface
By Serial communication with each unit of information following the previous one is
Burkhard Kainka
actually the normal situation. That’s how we talk to each other in person or by
(Germany)
phone, read and write text—or in the case of Retronics: send telegrams.
In many cases all you need is a single line to transmit data. However, adding a
clock line makes things more reliable. Let‘s find out.
Q5
Q6
Q7
Q8
OE
QS
CL
Q1
Q2
Q3
Q4
1 2 3 4 5 6 7 8
clock line. The clock signal always clearly indi-
28 27 26 25 24 23 22 21 20 19 18 17 16 15 cates when the next bit is available on the data
line. That eliminates the need for precise agree-
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
D5
D6
D7
B0
X1
X2
16MHz
Port extension with a shift register
100n
The first thing we want to try out is actually not
Figure 1. 22p 22p
an SPI interface, but instead something entirely
140245 - 11
Connecting a type 4094 shift different. Shift registers have been around for
register. a long time (before microcontrollers were even
invented) and are good for understanding how terminal emulator. This makes it easy to com-
serial data transfer works. They can also be put pare the output levels of the shift register to the
to good use in combination with a microcontrol- digital value being output.
ler. That’s because port lines are always scarce, If you need more than eight outputs, you can
especially on Arduino boards. A port extension use the Qs output of the 4094 IC. Each bit that
with a shift register can help ease the scarcity. is clocked into the shift register appears at the
With a type 4094 8-bit shift register, you need
three lines to talk to it and you end up with
eight new outputs. You can increase this to 16 Listing 1. Output using a shift register.
by connecting a second shift register, or even
'------------------------------------
80 if you connect ten shift register ICs in series. 'UNO_shift.BAS Shift Register 4094
If you need a lot of outputs, that’s an especially '------------------------------------
low-cost way to meet the requirement. $regfile = "m328pdef.dat"
$crystal = 16000000
Figure 1 shows the connections to the Uno board. $baud = 9600
The 8-bit shift register has a clock input (CL)
and a data input (D). The data is applied to the Dim Dat As Byte
D input one bit at a time, starting with the most Dim D As Byte
significant bit, and a rising edge is applied to the Dim N As Byte
clock input CL for each bit. The data is shifted Dim B As Bit
through the individual flip-flops of the register
step by step with each clock pulse. There is also
the strobe input STR. When a pulse is applied Sr Alias Portb.4 '4094 pin 1
to the strobe input, all the data present in the Da Alias Portb.3 '4094 pin 2
shift register is transferred to the type-D flip- Cl Alias Portb.2 '4094 pin 3
flops connected to the output pins. You could Config Portb = Output
also tie the strobe input to the supply voltage
...
Vcc, but then all the intermediate results of each
shift operation would appear on the outputs. By
Dat = 0
contrast, if you apply a strobe pulse after all the
Do
bits have been shifted in, you only see the final
Cls
result on the outputs.
Lcd Dat
Lcd " "
The software for all this (Listing 1) is simple. To Print Dat
output a byte D, the code first copies the most D = Dat
significant bit to the bit variable B (B = D.7) For N = 1 To 8
and puts it on the corresponding port pin. It B = D.7
then generates a positive clock pulse on CL with Da = B
a length of 1 millisecond. A microsecond would Waitms 1
also be sufficient, but the slower output is easier Cl = 1
to see on an oscilloscope. After the clock pulse, Waitms 1
a shift instruction (Shift D , Left) causes all Cl = 0
bits of D to be shifted left by one position. This Waitms 1
puts what used to be bit 6 on the output. This Shift D , Left
process is repeated until all eight bits have been Next N
shifted out. At the end comes the strobe pulse, Sr = 1
and then all eight bits are present at the outputs Waitms 1
of the 4094. Sr = 0
The code continuously increments the data byte Waitms 1
Dat = Dat + 1
to be transferred so the outputs of the shift regis-
Waitms 500
ter change while the program is running. The
Loop
current value is shown on the LCD if the Elektor
End
Extension shield is fitted, and it is output to the
Qs output eight clock pulses later. The D input of see the difference. There both parties have to
the next shift register can be connected to this agree on the transmission rate, and no breaks are
output. In this way you can connect as many allowed within an information unit. For example,
4094 ICs in series as desired, with the clock and if a radio operator wants to send an “X” (dash
strobe lines connected to all of them in parallel. dot dot dash) and stops in the middle to scratch
Of course, the software will have to be modi- his head, the two characters “N” (dash dot) and
fied accordingly. First it shifts out all of the bits “A” (dot dash) are sent instead. The situation is
necessary to fill the chain of shift registers (e.g. exactly the same with an asynchronous serial data
16 with two ICs or 80 with ten), and then it out- interface, where both parties have to agree on
puts the common strobe pulse. the baud rate. After the transmission of a byte
has started, all of the bits must be sent within
Manual data transmission a precise time frame. By contrast, with SPI the
Although you only need one line for the data, you timing is not critical and any desired delays are
also need a clock line when you use a clocked allowed. The additional clock signal makes the
serial interface, as in the above example with a transfer entirely independent of the speed. No
shift register or with the SPI bus. If you compare matter whether the data rate is just one bit per
this with Morse telegraphy, for example, you can minute or a million bits per second, the data will
1k
1k
Data Clock
You can try this for yourself manually, where you +5V
(as the user) assume the role of master. You can
transmit a byte by pushing the two buttons S1
and S2 (Figure 2). This is not the usual way of
28 27 26 25 24 23 22 21 20 19 18 17 16 15
doing things, but it helps you understand exactly
GND
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AVCC
AREF
how it works. One of the buttons is for the data,
ATmega328p
and the other is for the clock. Aside from that
GND
VCC
RES
there’s nothing new you have to learn, since you
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
already know how a byte is put together. Here’s
how it works: First you send bit 7. If it is a ‘1’, 16MHz
you press and hold button S2; otherwise you
don’t. Then you press button S1 briefly without 100n
changing the state of S2. The receiving end (the 22p 22p
140245 - 12 Figure 2.
slave, which in this case is the Arduino board) Manual input and output.
then knows when it should read the bit from the
data line. Now you repeat the process for bit 6,
bit 5, and so on until bit 0. marked by a clock pulse. It may not have been all
that easy, but the microcontroller had no problem
The program in Listing 2 displays the data in reading the data. You can regard this as a test
both directions. At first the microcontroller is of your concentration, and if the LCD shows the
the master and you are the slave. A byte with a right result, you pass the test.
random value is sent, with the clock signal indi-
cated by LED1 and the data indicated by LED2. If you look closely at Listing 2, you will see that
If you watch carefully, you can read the trans- the bits are inverted when they are read. That’s
mitted byte. However, that’s not easy, so the because pressing the data button yields a zero
byte is also shown on the LCD and sent to the bit value. This is not especially intuitive, so the
terminal emulator. The individual bits also appear result is inverted when the bit is read to make
one after the other on the LCD and the terminal things easier for you. Another interesting aspect is
emulator screen: using the instruction D = Rnd (255) to generate
a pseudo-random number. In fact, this always
83 generates the same sequence of numbers, but
01010011 the Bascom Help gives some suggestions for what
you can do about this.
Then the roles change. Now you are the master,
and your job is to send exactly the same byte From microcontroller
back to the microcontroller. Here you can see that to microcontroller
the ability to send data at any desired speed is a In this example, data is sent over the SPI bus
big advantage, since you can take all the time you from one microcontroller to another. The data
want to decide which bit value to send next. For is this case consists of 10-bit readings from the
example, suppose you want to send the decimal A/D converter. This shows another advantage of
number 100. Bit 7 corresponds to decimal 128, SPI, which is that the data width is not fixed. No
which is more than 100, so it is ‘0’. Next comes matter whether you send 8, 10, 12 or 16 bits,
bit 6 with a value of 64, so it’s a ‘1’, and you’re the procedure is always the same. If the only
left with 36 still to send. This means that bit 5 objective were to connect two microcontrollers
(32) is a ‘1’, which leaves 4. The next two bits together, it would actually be less effort to use
(bit 4 = 16 and bit 3 = 8) are ‘0’, bit 3 (4) is a an asynchronous serial interface with the TXD
‘1’, and the last two bits are ‘0’. Now you have and RXD lines. The SPI bus, by contrast, is better
sent the binary number 01100100, with each bit for controlling and communicating with external
MOSI
SCK
ICSP ICSP
CS
+5V +5V
28 27 26 25 24 23 22 21 20 19 18 17 16 15 28 27 26 25 24 23 22 21 20 19 18 17 16 15
GND
AVCC
GND
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
AREF
ATmega328p ATmega328p
GND
GND
VCC
VCC
RES
RES
D0
D1
D2
D3
D4
D5
D6
D7
B0
D0
D1
D2
D3
D4
D5
D6
D7
B0
X1
X2
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14
16MHz 16MHz
100n 100n
Figure 3.
22p 22p 22p 22p
140245 - 13
An SPI connection between
two microcontrollers.
hardware. Here the main purpose of the exercise is no chip select line, but the Reset line has the
is to illustrate the transmission protocol. same effect because programming takes place
As previously with the 4094 shift register, a third with the Reset line pulled low. Now we want to
line is involved here—in this case the chip select use these lines exactly as intended. This has the
line /CS. The slash (/) means that the signal on advantage that we can use the hardware SPI unit
this line is active Low. The chip select line allows of the microcontroller, if it has one. With hard-
you to connect several slave devices to a single ware SPI we do not have use program code to
master. In that case they share the data and clock put each bit individually on the data line as in the
lines, but each one has its own chip select line. previous examples, and everything is a lot faster.
When that line is low, the corresponding slave However, we still need a chip select line, and in
knows that it is selected. There’s also another this case we use the B2 line for this purpose.
benefit from using a chip select line. If there is The master uses the MOSI line as the output
any delay in enabling the slave, there may be and generates the clock and chip select signals
some confusion about which bits have already (Listing 3). The process is slowed down a bit by
been transferred. However, if the slave waits until three 1-millisecond delays so that all the signals
it sees a falling edge on its CS input (high to low can easily be seen on the oscilloscope. Besides,
signal transition), it knows that the transfer is we don’t want to make things too difficult for the
starting. And if a noise pulse is read as a clock slave. If you wish, you can test the boundaries
signal, the rest of the data for that transfer is by reducing the delays until transmission errors
trash, but on the next access everything is again start to occur.
as it should be. The three lines are inputs for the slave device
(Listing 4). It constantly waits for specific signal
The ATmega328 also uses the SPI bus for pro- edges on the /CS and SCK lines and then reads
gram download from an external programming in a bit from the MOSI line. Since everything is
device. The following lines are therefore avail- handled by software here, the code must wait for
able on the six-pin programming connector on each edge in a Do loop. This takes a bit of time,
the Arduino board and on the Elektor Extension so data transmission must be slower than with a
shield (ICSP in Figure 3): the clock line Serial hardware SPI implementation. The received data
Clock (SCK) on B5, the write data line Master is shown on the display and on the terminal emu-
Out Slave In (MOSI) on B3, and the read data lator. When you turn the potentiometer on the
line Master In Slave Out (MISO) on B4. There master board, the change is visible on the slave.
SPI EEPROM 25LC512 realm, but SPI types are generally preferred in
There is a wide range of ICs available with an the professional realm because they offer espe-
SPI interface, including A/D converters, memory cially high operational reliability.
devices and display drivers. Serial EEPROMs from Figure 4 shows the connections to the Uno board.
Microchip and other companies are available at The pins of the original SPI interface (2x3 pin
low cost and are widely used. The 25LC512 (not header) are again used here. That makes it easy
to be confused with the 24C512, which has a I²C to build a convenient plug-in memory module
bus interface) has a capacity of 64 KB, and it is a by fitting the IC in a socket soldered to a small
good solution when the 1-kilobyte capacity of the 6-pin socket header. You only have to connect
ATmega328’s internal EEPROM is not sufficient. one additional line. This is the chip select line,
I²C EEPROMs are more widely used in the hobby which is again assigned to B2 because the Reset
line present on the connector cannot be used for
this purpose.
8 7 6 5
Here there are two data lines. MOSI (Master Out
HOLD
SI
VCC
SCK
Slave In) is the data output line and is connected
25LC512
to the Serial Input (SI) pin of the EEPROM, while
GND
WP
SO
CS
AVCC
C5
C4
C3
C2
C1
C0
B5
B4
B3
B2
B1
AREF
D5
D6
D7
B0
X1
X2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
The relatively complex data sheet for the 25LC512
tells you what has to be sent to the device, as well
16MHz
as when and how. After the chip select line has
100n been pulled low, the memory chip first receives a
Figure 4. 22p 22p simple byte command that specifies what action
140245 - 14
Connecting a serial EEPROM. is to be performed. To read data from the mem-
ory, you have to send a ‘3’ command followed
by two address bytes forming the high-byte and
Listing 5. Reading and writing data over MOSI and MISO.
low-byte portions of the address. After that as
Sub Spioutin many data bytes as desired can be read out with
Din = 0 automatic address incrementing (see Listing 6).
For N = 0 To 7 The program displays the sequential addresses
Shift Din , Left and the data bytes that are read out. A brand-new
Din = Din + Miso or fully erased EEPROM always delivers only the
If Dout.7 = 1 Then Mosi = 1 Else Mosi = 0 value 255. Now let’s try to program some data.
Waitus 3 For that we use the byte command ‘2’. However,
Sck = 1
a bit of preparation is necessary first. Writing
Waitus 2
must be enabled by sending the command ‘6’
Sck = 0
(Listing 7). To check whether writing is enabled
Waitus 2
you can read the EEPROM status register, which
Shift Dout , Left
requires sending the command ‘5’. Each action
Next N
is only effective if you pull /CS low at the start
End Sub
and then return it to the high level at the end.
time for storing the data. If you exceed the page data is read back, that’s exactly what you see.
boundary (I tried it), the result is chaos. Then As usual, the entire program code (UNO_spiEE1.
the data you find in memory is totally different bas) can be downloaded from the Elektor website
from what you wanted to write to memory. For [1]. It performs the following actions in sequence:
this reason, the example program carefully obeys
the rules and writes 128 bytes to the first page • Command 6, write enable
from address 0 to address 127, with the data val- • Command 5, read status register; display for
ues in ascending order from 0 to 127. When the 1 second
• Command 2, write 128 bytes starting at fer a lot of data during a single active chip select
address 0 phase. Consequently, this line (on port pin B2)
• Command 3, read memory starting at will still be operated “manually”. The Mode = 0
address 0; endless loop setting is also important, because there are four
different SPI modes.
Data logger The program excerpt in Listing 8 shows how the
One practical application for the 64-KB memory software SPI is used to read data from the serial
is a data logger. The objective here is to acquire EEPROM. The instruction Spiout Dout , 1 sends
measurement data from the ADC4 analog input exactly one byte, which is transferred in the vari-
once per second and store the data. The memory able Dout. In the other direction, the instruction
will be full in approximately 18 hours. Spiin Din , 1 reads one byte, which is then
available in the variable Din. The entire program
You don’t always have to program everything reads all the data from the EEPROM and shows
yourself, since Bascom has a lot of ready-made the contents on the display and on the terminal
functions for many situations. In this case you emulator screen.
have the option of configuring an SPI interface as As usual, the entire program code (UNO_spiLog-
a software interface using any desired port pins or ger.bas) can be downloaded from the Elektor
as a hardware interface using the microcontroller website [1]. It is too large to be listed fully here.
pins designated for this purpose. The hardware Pressing S1 starts a measurement run. It can be
SPI is especially fast and is commonly used for stopped at any time by pressing S2, after which
tasks such as driving graphical displays. However, the stored data can be read out.
this involves a whole lot of parameters that must
be configured for each specific application, which A timer interrupt routine (excerpt in Listing 9)
requires a detailed study of the ATmega328 data is used to control the timing during data acqui-
sheet. Things are a bit easier with the software sition. The voltage on ADC4 is measured and
SPI function, and it provides a reasonably high stored once per second. The 128-byte block size
transmission rate. Although writing your own SPI of the EEPROM is taken into account. At the start
procedure from the ground up is not a bad idea of each block, a write access is started and the
because it allows you to implement the timing current address is transferred, followed by 128
diagrams in the data sheets very clearly, the bytes of data. At the end of the block, the /CS
ready-made software SPI is more convenient and line is pulled high to allow the EEPROM to store
faster, which is why we use it here. the entire block. Since the /CS line is connected
The interface configuration specifies which lines to port pin PB2, LED2 on the shield is lit when the
are to be used. For Din, Dout and Clock we use line is high. The LED therefore flashes each time
the familiar MISO, MOSI and SCK lines, which a block of data has been transferred to memory.
are already available on the ICSP connector. The (140245-I)
SS line corresponds to the /CS line. In this case
Web Link
this line should not be operated automatically by
Bascom because it is usually necessary to trans- [1] www.elektor-magazine.com/140245