Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

SPI (serial peripheral interface) using AVR microcontroller (ATmega16)- (Part 37/46)

By Ashutosh Bhatt July 7, 2010

There are different protocols for serial communication between two deceives like, USART, SPI, I2C etc. Before selecting any communication protocol, data transfer rate is an important parameter. SPI transfers data at high speed data. AVR microcontroller contains on chip SPI interface. This article will explore the hardware configuration and programming of SPI.

 

Serial Peripheral Interface is a synchronous, full-duplex protocol. SPI is also known as “3-wire interface” protocol because it needs 3 communication lines named MISO, MOSI and SCK. SPI protocol needs two devices for communication. One of them is considered as a MASTER and another one as a SLAVE. AVR microcontrollers contain both MASTER and SLAVE interface on single chip. Thus, a microcontroller can work as both master and slave device.
 
SPI interface:
Besides the MISO, MOSI and SCK pins, the SS is also included in SPI system. This pin is used to select slave device. Following table explains functionality of these pins:
 
Pin Functions of SPI in AVR chip
     Fig. 2: Pin Functions of SPI in AVR chip
 
How SPI works:
The SPI is synchronous data transfer protocol, so clock pulse is needed to synchronize both master and slave device. The clock pulse is generated from master side. The SCK pin of master provides clock pulse to slave device.
 
To make any device as master, the SS pin must be set as high. If it is configured as an output pin, then it made high using the software. If the SS is considered as input pin, it should make high externally. In slave mode SS is always an input pin, which should be connected to ground (to make it slave device).
 
The “MOSI” stands for “master output slave input”. So, the MOSI pin works as output pin for master device and input pin for salve device. Both master and slave devices contain a buffer register, called SPDR. Master transfers one bit from its SPDR to slave device in every clock cycle. It means to send one byte data, 8 clock pulses are needed.
 
Registers of SPI System:
The SPI system consists three register which are described below:
 
1. SPCR (SPI control register):
 
SPIE
SPE
DORD
MSTR
CPOL
CPHA
SPR1
SPR0
Fig. 3: Bit Structure of SPI Control Register in AVR 
 
SPIE (SPI Interrupt Enable) – To enable SPI interrupt this is set as high.
SPE (SPI Enable) – SPI system is enabled when this bit is set.
DORD (Data Order) – For DORD=1, LSB will be transmitted first.
For DORD=0, MSB will be transmitted first.
MSTR (Master/Slave Select) – For MSTR=1, to select device as master.
For MSTR=0, to select device as slave.
SPR [1:0] (SPI clock Rate) – This SPR [1:0] and SPI2X bit of SPSR register decides frequency of SCK. The combination of these three bits to select SCK frequency are shown in following table:

Bit Structure of SPI Control Register in AVR

Fig. 4: Bit Combination of SPSR Register to select SCK frequency
 
2. SPSR (SPI Status REGISTER) : 
SPIF
WCOL
–
–
–
–
–
SPI2X

Fig. 5: Bit Structure SPI Status REGISTER in AVR

 
SPIF (SPI Interrupt Flag) – This bit become set automatically after completion of serial transfer.
WCOL (Write Collision Flag) – This bit is set if SPDR is written during data transfer.
SPI2X (Double SPI Bit) – By setting 1 to this bit SCK frequency become double.
 
3. SPDR (SPI Data Register) – This is 8-bit data register used to store receive data and transmitting data.
 
Objective: To interface ATmega32 and Atmega16 microcontroller using SPI protocol. Consider ATmega32 as master and Atmega16 as slave.
 
Circuit description:
The connection of both master and slave microcontrollers are shown in circuit diagram. MOSI pins (pin no.6) of both master and slave are connected. The SCK (pin no 8) pins of both master and slave are connected. SS pin of slave is grounded.
 
Programming steps:
For Master mode:
1. Set SS, MOSI and SCK pin as output pins.
2. Enable MSTR bit to make it master device.
3. Program SPR [1:0] bits to select SCK frequency.
4. Set SPE bit to enable SPI.
5. Copy data in SPDR register to send.
6. Wait until SPI interrupt flag get set.
  
For slave mode:
1. Select MOSI pin as output and rest pins as input pins.
2. Set SPE to activate SPI.
3. Wait until SPI interrupt flag get set.
4. Receive data from SPDR register.

Project Source Code

###


// Program to SPI (serial peripheral interface) using AVR microcontroller (ATmega16)
#include<avr/io.h>
#include<util/delay.h>
 
#define MOSI PB5
 
void SPI_init();
unsigned char SPI_RX(void);
 
int main()
{
 
DDRD=0xFF;
PORTD=0x00;
 
SPI_init();  
while(1)
{
PORTD=SPI_RX(); // move SPDR value to POTRD
}
 
}
 
void SPI_init() //SPI initialization
{
DDRB=(1<<MOSI); // set MOSI as output pin, rest as input
SPCR=(1<<SPE); // Enable SPI
}
 
 
unsigned char SPI_RX()
{
while(!(SPSR &(1<<SPIF))); //wait until SPIF get high
return SPDR; // return SPDR value
}
 

###

 


Circuit Diagrams

Circuit-Diagram-of-SPI-serial-peripheral-interface-using-AVR-microcontroller-ATmega16

Project Components

  • ATmega16
  • LED

Project Video


Filed Under: AVR
Tagged With: atmega16, avr, microcontroller, spi
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Voltage mode pushpull is a nonsense SMPS?
  • Input impedance matching network
  • High Side current sensing
  • The comparison of different Tcl script checkers
  • Reducing "shoot-through" in offline Full Bridge SMPS?

RSS Electro-Tech-Online.com Discussions

  • Is AI making embedded software developers more productive?
  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe