90 4. SERIAL COMMUNICATION SUBSYSTEM
The SPI SCK is derived from the microcontroller’s system clock source. The system clock
is divided down to form the SPI SCK. The SPI Clock Rate Select bits SPR[1:0] and the
Double SPI Speed Bit (SPI2X) are used to set the division factor.The following divisions may
be selected using SPI2X, SPR1, SPR0:
000: SCK = system clock/4
001: SCK = system clock/16
010: SCK = system clock/64
011: SCK = system clock/128
100: SCK = system clock/2
101: SCK = system clock/8
110: SCK = system clock/32
111: SCK = system clock/64
SPI Status Register (SPSR) The SPSR contains the SPI Interrupt Flag (SPIF). The flag sets when
eight data bits have been transferred from the master to the slave. The SPIF bit is cleared by first
reading the SPSR after the SPIF flag has been set and then reading the SPI Data Register (SPDR).
The SPSR also contains the SPI2X bit used to set the SCK frequency.
SPI Data Register (SPDR) As previously mentioned, writing a data byte to the SPDR initiates
SPI transmission.
4.7 SPI PROGRAMMING IN THE ARDUINO
DEVELOPMENT ENVIRONMENT
The Arduino Development Environment provides the shiftOut command to provide ISP style
serial communications [www.Arduino.cc]. The shiftOut command requires four parameters when
called:
dataPin: the Arduino UNO R3 DIGITAL pin to be used for serial output.
clockPin: the Arduino UNO R3 DIGITAL pin to be used for the clock.
bitOrder: indicates whether the data byte will be sent most significant bit first (MSBFIRST)
or least significant bit first (LSBFIRST).
value: the data byte that will be shifted out.
To use the shiftOut command, the appropriate pins are declared as output using the pinMode
command in the setup() function. The shiftOut command is then called at the appropriate place
within the loop() function using the following syntax:

Get Arduino Microcontroller, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.