Exp9B SPI Communication on ATMEGA32
Exp9B SPI Communication on ATMEGA32
(ATMEGA32)
For Example – UART, USART, USB, etc. For Example – SPI, I2C, CAN, LIN, MODBUS,
etc.
System – 1 System – 2
Sensors
Oscilloscopes, Signal
Generators, Analyzers,
Plotters, etc.
Displays
In the diagram above, the pink lines show Inter-System Protocols, which are used for
communication between System-1 and System-2.
2
Embedded System on AVR ATMEGA32:Exp9B
The purple lines indicate Intra-System Protocols, which handle communication between
different components within System-1.
● SPI Protocol
➔ SPI, which stands for Serial Peripheral Interface, is one of the most basic
communication protocols used in embedded systems. It was developed by Motorola
in the early 1980s.
➔ Many sensors and peripherals use this protocol, including SD card readers, flash
memories, RFID readers, graphical displays, DACs, ADCs, SRAMs, accelerometers, and
special driver ICs.
➔ The SPI connection between the master and slave devices can be established in two
ways: 3-wire interface and 4-wire interface. The 4-wire interface is more commonly
used and is shown below.
SCK SCK
➔ 4-Wire Interface Explanation: In the 4-wire interface, both the master and slave
devices require four pins for communication. These pins are described below:
3
Embedded System on AVR ATMEGA32:Exp9B
I. Chip Select / Slave Select (C̅S̅ / S̅S̅) pin: This pin allows the master device to
choose which slave device it wants to communicate with. The master controls
this pin using one of its regular GPIO pins.
The C̅S̅ / S̅S̅ pin is active low, meaning:
● The slave is selected when the master sends a 0 (LOW) signal.
● The slave is unselected when the master sends a 1 (HIGH) signal.
In this setup, the master's GPIO pin works as an output, while the slave's C̅S̅ /
S̅S̅ pin functions as an input.
II. Serial Clock (SCK) pin: The Serial Clock (SCK) pin is used to synchronize
communication between the master and slave devices.
● Master: Acts as an output, generating the clock signal.
● Slave: Acts as an input, receiving the clock signal.
The master device always provides the clock signal at the required frequency
to communicate with the slave. The correct clock frequency can be found in
the technical documents of the slave device. The entire data transfer process
between the master and slave is synchronized with this clock signal.
III. Master-Out Slave-In (MOSI) pin: The MOSI pin is used for data transfer from
the master to the slave.
● Master: Sends data (acts as an output).
● Slave: Receives data (acts as an input).
In older naming conventions:
● This pin is called SDO (Serial Data Out) on the master.
● It is called SDI (Serial Data In) on the slave.
IV. Master-In Slave-Out (MISO) pin: The MISO pin is used for data transfer from
the slave to the master.
● Slave: Sends data (acts as an output).
● Master: Receives data (acts as an input).
In older naming conventions:
● This pin is called SDO (Serial Data Out) on the slave.
● It is called SDI (Serial Data In) on the master.
➔ 3-Wire Interface Explanation: In the 3-wire interface, the connections for SCK (Serial
Clock), MOSI (Master-Out Slave-In), and MISO (Master-In Slave-Out) remain
unchanged. However, instead of being controlled by the master, the C̅S̅ / S̅S̅ (Chip
Select / Slave Select) pin of the slave is directly connected to ground, as shown below:
Master Device GND Slave Device
CS / SS
SCK SCK
4
Embedded System on AVR ATMEGA32:Exp9B
5
Embedded System on AVR ATMEGA32:Exp9B
● Standard SPI Interface = 1 bit per SPI clock cycle i.e., 1 byte is transferred in 8
SPI clock cycles.
● Dual – SPI Interface = 2 bits per SPI clock cycle i.e., 1 byte is transferred in 4
SPI clock cycles.
● Quad – SPI Interface = 4 bits per SPI clock cycle i.e., 1 byte is transferred in 2
SPI clock cycles.
● Octo – SPI Interface = 8 bits per SPI clock cycle i.e., 1 byte is transferred in 1
SPI clock cycle.
● Extended – SPI Interface = 16 bits per SPI clock cycle i.e., 2 bytes are
transferred in 1 SPI clock cycle.
➔ SPI protocol supports single master and multiple slave configuration only i.e., there
can be only one master device and multiple slave devices can be interfaced with it and
this can be achieved using 2 methods:
● Independent Slave Method
● Daisy – Chain Method
GPIO Pin 3
GPIO Pin 2
GPIO Pin 1
SCK
MOSI
MISO
➔ From the diagram, it is clear that the number of Chip Select (C̅S̅ / S̅S̅) lines corresponds
to the number of connected slave devices, while the SCK, MOSI, and MISO lines are
shared among all the slaves.
➔ Here, the data takes approximately the same amount of time to reach all the slave
devices since the data lines have been made common to all of them. For example, if
standard SPI protocol is used, then all the slave devices will receive their
corresponding data or command in 8 SPI clock cycles.
6
Embedded System on AVR ATMEGA32:Exp9B
➔ Since each slave device has its own Chip Select (C̅S̅ / S̅S̅) line, the master can
communicate with each slave independently. However, activating multiple C̅S̅ / S̅S̅
lines at the same time should be avoided, as it can cause data corruption on the MISO
line, making it impossible for the master to determine which slave is transmitting data.
➔ The main drawback of this method is that the number of slave devices that can be
connected to the master is limited. As more slaves are added, the number of Chip
Select (C̅S̅ / S̅S̅) lines increases, leading to greater circuit complexity and potential
exhaustion of the master’s GPIO pins.
➔ Here, all the slaves are selected and they receive the clock signal at the same time and
the data is sent from master to 1st slave and then 1st slave sends the data to 2nd slave
and so on but the last slave sends the data back to the master.
7
Embedded System on AVR ATMEGA32:Exp9B
➔ Since the data propagates from one slave to another, all the slave devices will not
receive the data or command in the same number of clock cycles. For example, if
standard SPI protocol is used, then 1st slave will receive data after 8 SPI clock cycles,
2nd slave will receive data after 16 SPI clock cycles and 3rd slave will receive data after
24 SPI clock cycles.
➔ One advantage of this method is that the circuit complexity will be much less as
compared to independent slave method because the chip / slave select line is common
to all the slaves.
➔ However, the number of slaves that can be connected using this method is also
limited. As the number of slaves increases, the data propagation time increases
depending on the slave's position in the chain, which is a disadvantage. Additionally,
very few devices support this method in practice.
8
Embedded System on AVR ATMEGA32:Exp9B
The data is transferred between master and slave in a serial manner using shift registers
as shown in the following figure:
➔ Here, the data is shifted out from the master's shift register and shifted into the slave's
shift register, one bit at a time per clock pulse of the SPI clock generator.
➔ As per the direction of the red arrows in the above diagram, the MSB of the data byte
will be shifted out 1st and then the subsequent bits (conventional method). If the LSB
of the data byte would be shifted out 1st and then the subsequent bits, then the
direction of the red arrows will get reversed.
➔ If there are multiple slaves present, then the overall SPI clock frequency will be
decided by the slave device that has the lowest value of SPI clock frequency.
➔ The datasheet of the slave device contains information about the various commands
that master needs to send to the slave for data extraction.
➔ When data transfer begins either from master to slave or vice – versa, the master
device will decide whether the MSB or the LSB of the data byte will be shifted out 1st
from the shift registers and the configurations to be done for the same will be available
in the master's datasheet.
9
Embedded System on AVR ATMEGA32:Exp9B
10
Embedded System on AVR ATMEGA32:Exp9B
● In SPI there are Master Shift Register and Slave Shift Register
then why are Data Read and Data Shift Out required? Is it not
possible to use like simple shift register cascaded together?
Analogy: Think of a shift register as a conveyor belt (data being shifted), and the
sampling process as a worker picking items off the belt. Even if the conveyor moves
smoothly, the worker needs to know the exact timing to pick up an item (sampling).
Without this timing, the worker might grab an item before fully delivered of the
previous one.
Another Latch is required with each shift register to overcome the above timing
mismatches.
From the below image, the Master device includes a shift register, a data latch, and a
clock generator to control data transfer. Similarly, the Slave device has a shift register
and a data latch. Both shift registers are connected in a loop, allowing data to be
exchanged between the Master and Slave simultaneously.
11
Embedded System on AVR ATMEGA32:Exp9B
During the positive edge of the clock signal, both the Master and Slave read an input
bit into the least significant bit (LSB) of their shift registers. During the negative
edge, they shift out a bit from the most significant bit (MSB). This means that for
every clock cycle, one bit is transferred in each direction—Master to Slave and Slave
to Master. Since a byte consists of 8 bits, it takes 8 clock cycles to fully exchange one
byte of data between the devices.
That is why Clock Polarity (CPOL) and Clock Phase (CPHA) come into the picture.
12
Embedded System on AVR ATMEGA32:Exp9B
13
Embedded System on AVR ATMEGA32:Exp9B
MSTR will be cleared, and SPIF in SPSR will become set. The user will then have to set
MSTR to re-enable SPI Master mode.
• Bit 3 – CPOL: Clock Polarity
When this bit is written to one, SCK is high when idle. When CPOL is written to zero,
SCK is low when idle. The CPOL functionality is summarized below:
Table: CPOL Functionality
14
Embedded System on AVR ATMEGA32:Exp9B
The SPI Data Register is a read/write register used for data transfer between the
Register File and the SPI Shift Register. Writing to the register initiates data
transmission. Reading the register causes the Shift Register Receive buffer to be
read.
15
Embedded System on AVR ATMEGA32:Exp9B
Master Code:
/* Atmega32 to Atmega32 SPI Communication */
// Master: Oscillator Frequency 1MHz
.INCLUDE "M32DEF.INC"
.ORG 0x0000
// Stack declaration
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
16
Embedded System on AVR ATMEGA32:Exp9B
OUT SPCR,R16
//Select the slave(Active Low)
CBI PORTB,PINB4
// Initialization Timer1
LDI R16,0x04 // CTC Mode: Count upto 1024 times
OUT OCR1AH,R16
LDI R16,0x00
OUT OCR1AL,R16 // First Set Output Compare value
LDI R16,0x00 // Normal Mode and timer clock frequency= oscillator frequency/1024
OUT TCCR1A,R16
LDI R16,0x0D
OUT TCCR1B,R16 // At last activate the timer counter clock
.ORG 0x0400
seven_segment: .dB 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F
Slave Code:
/* Atmega32 to Atmega32 SPI Communication */
// Slave: Oscillator Frequency 8MHz
.INCLUDE "M32DEF.INC"
.ORG 0x0000
// Stack declaration
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
17
Embedded System on AVR ATMEGA32:Exp9B
Make the above circuit with hardware and verify the SPI data format with logic analyzer.
18
Embedded System on AVR ATMEGA32:Exp9B
Class Assignment1: One ATmega32 works as a Master, and the other as a Slave in SPI
communication. Each microcontroller has a common cathode seven-segment display
connected to PORTA. The Master stores digit values (0-9) in its flash memory and sends them
descending order to the Slave via SPI to display on its seven-segment display. When the
Master sends a new digit, the previous digit is received back through MISO and shown on the
Master's display. Use CPOL=1 and CPHA=1. Verify the SPI data format with logic analyzer.
19
Embedded System on AVR ATMEGA32:Exp9B
Master Code:
/* Atmega32 to Atmega32 SPI Communication
Master: Oscillator Frequency 1MHz
Master Bit-Banging: CPOL=0 and CPHA=0
SCK= PORTC2, MOSI=PORTC7, MISO=PORTC0, SS=PORTC3
// Stack declaration
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
// Initialization Timer0 for generationg half time period of the master clock
LDI R16,0x01 //Normal Mode and timer clock frequency= oscillator frequency
OUT TCCR0,R16
// Initialization Timer1
LDI R16,0x04 // CTC Mode: Count upto 1024 times
OUT OCR1AH,R16
LDI R16,0x00
OUT OCR1AL,R16 // First Set Output Compare value
LDI R16,0x00 // Normal Mode and timer clock frequency= oscillator frequency/1024
OUT TCCR1A,R16
LDI R16,0x0D
OUT TCCR1B,R16 // At last activate the timer counter clock
// R19 is the equivalent of SPDR and R20 is used for data masking operation
LOOP: LPM R19,Z+
MOV R20,R19
LDI R21,8 //R21 is used to count 8bit data transfer
SPI_BitBang: ANDI R20,0x80
BRNE MOSI_1
CBI PORTC,PINC7 //MOSI=0
JMP Clock_Half_Time_Period
MOSI_1: SBI PORTC,PINC7
Clock_Half_Time_Period: CALL SCK_Half_Period
20
Embedded System on AVR ATMEGA32:Exp9B
Slave Code:
/* Atmega32 to Atmega32 SPI Communication */
// Slave: Oscillator Frequency 8MHz
.INCLUDE "M32DEF.INC"
.ORG 0x0000
// Stack declaration
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
21
Embedded System on AVR ATMEGA32:Exp9B
Make the above circuit with hardware and verify the SPI data format with logic analyzer.
22
Embedded System on AVR ATMEGA32:Exp9B
Class Assignment2: You are given two Atmega32 microcontrollers connected via SPI. One
microcontroller is configured as the Master and the other as the Slave. Each microcontroller
has a common cathode seven-segment display connected to PORTA.
Master: Atmega32 (SPI Internal Hardware)
Slave: Atmega32 (SPI Bit-Banged)
The Master stores digit values (0–9) in its flash memory.
The Master sends one digit at a time to the Slave using hardware SPI in
ascending order.
The Slave receives the digit using bit-banged SPI and displays it on its seven-
segment display.
Simultaneously, the previously displayed digit on the Slave is sent back via
MISO and shown on the Master's display.
The SPI mode used is CPOL = 0, CPHA = 0.
Slave Bit-Bang configuration pins SCK= PORTC2, MOSI=PORTC0,
MISO=PORTC7, SS=PORTC3.
Write the assembly language code to:
i. Configure the Master using internal SPI and display the received digits
on a seven segment display using PORTA.
ii. Implement bit-banged SPI on the slave side and display the received
digits on a seven segment display using PORTA.
23
Embedded System on AVR ATMEGA32:Exp9B
.INCLUDE "M32DEF.INC"
.ORG 0x0000
// Stack declaration
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
24
Embedded System on AVR ATMEGA32:Exp9B
LDI R16,0x03
OUT DDRD,R16 // Slave Select 1 and 2
// Initialization Timer1
LDI R16,0x04 // CTC Mode: Count upto 1024 times
OUT OCR1AH,R16
LDI R16,0x00
OUT OCR1AL,R16 // First Set Output Compare value
LDI R16,0x00 // Normal Mode and timer clock frequency= oscillator frequency/1024
OUT TCCR1A,R16
LDI R16,0x0D
OUT TCCR1B,R16 // At last activate the timer counter clock
.ORG 0x0400
seven_segment: .dB 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F
.INCLUDE "M32DEF.INC"
.ORG 0x0000
// Stack declaration
LDI R16,HIGH(RAMEND)
25
Embedded System on AVR ATMEGA32:Exp9B
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
26
Embedded System on AVR ATMEGA32:Exp9B
Master Code:
/* Atmega32 to Atmega32 SPI Communication with Daisy Chain Method */
// Master: Oscillator Frequency 1MHz
.INCLUDE "M32DEF.INC"
.ORG 0x0000
// Stack declaration
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
// Initialization Timer1
LDI R16,0x04 // CTC Mode: Count upto 1024 times
OUT OCR1AH,R16
LDI R16,0x00
OUT OCR1AL,R16 // First Set Output Compare value
LDI R16,0x00 // Normal Mode and timer clock frequency= oscillator frequency/1024
OUT TCCR1A,R16
LDI R16,0x0D
OUT TCCR1B,R16 // At last activate the timer counter clock
27
Embedded System on AVR ATMEGA32:Exp9B
OUT TIFR,R17
LOOP_WAIT: IN R18,TIFR
ANDI R18,0x10
BREQ LOOP_WAIT
RET
.ORG 0x0400
seven_segment: .dB 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F
.INCLUDE "M32DEF.INC"
.ORG 0x0000
// Stack declaration
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
28