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 ﬂag sets when
eight data bits have been transferred from the master to the slave. The SPIF bit is cleared by ﬁrst
reading the SPSR after the SPIF ﬂag 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
4.7 SPI PROGRAMMING IN THE ARDUINO
The Arduino Development Environment provides the “shiftOut” command to provide ISP style
serial communications [www.Arduino.cc]. The shiftOut command requires four parameters when
• 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 signiﬁcant bit ﬁrst (MSBFIRST)
or least signiﬁcant bit ﬁrst (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: