728x90

728x90

Saturday, February 14, 2026

ATMega644P Serial Peripheral Interface (SPI) Tutorial

Overview

The Serial Peripheral Interface (SPI) is a high speed serial communication that commonly use three wires, serial clock, serial data and enable (master transmit only). Most of modern micro-controller has this serial communication interface. It is useful for board to board or to device communication due to wiring complexity compare to parallel port data transmission.

SPI is popular among graphical display, Flash memory, I/O expansion chip, SRAM, EEPROM, real time clock chip etc. Its transmission and reception speed could reach up to several Mbits/s. These are some example of SPI devices:

  • DS3234 Real Time Clock
  • Nokia 5510 graphical LCD module
  • MCP23S17 I/O Expansion Chip
  • SN74HC595N Serial In Parallel Out Shift Registers
  • ILI9341/ST7785 240x320 TFT Display
  • ST7735 128x160 TFT Display
  • W25Q64JVSSIQ Flash Memory Chip
  • SD MMC Card etc. 
ATMega644P Serial Peripheral Interface (SPI) Tutorial
W25Q64JVSSIQ

 ATMega644P Serial Peripheral Interface (SPI) Tutorial

ATMega644P Serial Peripheral Interface (SPI) Tutorial
ILI9341/ST7785 240x320 TFT Display

 

Some earlier MCU doesn't have an SPI module inside for instance the AT89S52 or PIC16F84A. However the programmer could emulate a software SPI by using the bit-banging technique. This method is popular but it yield a lower speed data transmission and reception. 

Typically a system operation contains a master MCU that transmit and receive data from its connected SPI slave devices.

ATMega644P Serial Peripheral Interface (SPI) Tutorial
Single master to single slave: basic SPI wiring
 

The pin names of the SPI module in the AVR micro-controller are different from other MCU such as PIC. These pins are:

Abbr.NameDescription
SS
Slave Select
Active-low chip select signal from master to
enable communication with a specific slave device
SCLK
Serial Clock
Clock signal from master
MOSI
Master Out Slave In
Serial data output from master
MISO
Master In Slave Out
Serial data output from slave


The master MCU must select any connected SPI slave device via its slave select (SS) pin. One slave device has a unique SS pin. So multiple slave devices have many SS pins.

ATMega644P Serial Peripheral Interface (SPI) Tutorial
A typical hardware setup using two shift registers to form an inter-chip circular buffer
The SS pin is active low or active high upon the specific device.

Its clock polarity is commonly positive (low to high). Somes device use a negative (high to low) clock polarity.

ATMega644P Serial Peripheral Interface (SPI) Tutorial
 

However its clock polarity and phase are configured by user software that requires an understanding of technical detail of an MCU (Dedicated SPI Module).

The master MCU can communicates with many different SPI slave devices (Multidrop SPI bus) on a single bus using additional SS pins.

ATMega644P Serial Peripheral Interface (SPI) Tutorial
Multidrop SPI bus
 

If multiple SPI slave devices with the same type connect to a single SPI bus we can use the Daisy chain configuration. For instance the SN74HC595N or SN74HC164 serial in parallel out shift registers.

ATMega644P Serial Peripheral Interface (SPI) Tutorial
Daisy chain configuration

 It is used for expanding a number of output ports for instance driving a dot matrix display.

 Currently there are a lot of newer method of SPI data transmission that is precise and high speed.

ATMega644P Serial Peripheral Interface

The SPI module of the ATMega644P has the following features:

    Full-duplex, Three-wire Synchronous Data Transfer
    Master or Slave Operation
    LSB First or MSB First Data Transfer
     Seven Programmable Bit Rates
    End of Transmission Interrupt Flag
    Write Collision Flag Protection
    Wake-up from Idle Mode
    Double Speed (CK/2) Master SPI Mode

Its operation mode and speed are configured in user program via its relevant special function registers.

ATMega644P Serial Peripheral Interface (SPI) Tutorial
SPI Block Diagram

 Its clock generator is divided form the MCU clock up to CK/2. Data is shifted in and out using its internal 8-bit shift register at each clock cycle of the SPI clock generator. It also generate interrupt flag whenever the shift registers is empty.

ATMega644P Serial Peripheral Interface (SPI) Tutorial
SPI Master-slave Interconnection

 These are its relevant registers:

  •   SPCR – SPI Control Register
ATMega644P Serial Peripheral Interface (SPI) Tutorial
SPCR – SPI Control Register
  •  SPSR – SPI Status Register 
ATMega644P Serial Peripheral Interface (SPI) Tutorial
SPSR – SPI Status Register
  • SPDR – SPI Data Register 
ATMega644P Serial Peripheral Interface (SPI) Tutorial
SPDR – SPI Data Register

The ATMega644P may operate in master transmit only, slave receive only or even full duplex (synchronous data transfer) depending on these registers.  For more detail please see the device datasheet. 

ATMega644P SPI Interfacing and Programming

We can configure the SPI module of the ATMega644P to operates in a specific mode depends on the worked application.

SPI Master Transmit Mode 

An SPI master transmits only is common. There are many SPI slave device that only need data reception such as the SN74HC595N or 74HC164 shift register chips. 

ATMega644P Serial Peripheral Interface (SPI) Tutorial
SN74HC595N and SN74HC164N Shift Registers

These chips is very popular due to its availability, low cost, ease of control etc. The SN74HC595N is commonly found in output expanding application such as relays driving, LED driving especially the dot matrix display driver.

ATMega644P Serial Peripheral Interface (SPI) Tutorial
SN74HC595N Pin Diagram

In this example the ATMega644P operate in master mode to transmit data to the SN74HC595N shifter register chip. The output port of this chip can connects to LED, 7-Segment displays or even a character LCD.

There are sample code of using the SPI transmit in device data sheet in both Assembly and C program.

ATMega644P Serial Peripheral Interface (SPI) Tutorial
Sample Code

 

LED Driving

I have a DIY single chip SN74HC595N LED module that is very easy to make. However we can install them together on a single breadboard.

ATMega644P Serial Peripheral Interface (SPI) Tutorial
A completed Soldering Board
ATMega644P Serial Peripheral Interface (SPI) Tutorial
Copper Side

 For more information about this DIY PCB see this post.

 The program is very simple.

  1. /*
  2. * 12-spi_74hc595_led.c
  3. *
  4. * Created: 2/14/2026 6:57:01 PM
  5. * Author : Admin
  6. */

  7. #include <avr/io.h>
  8. #include <util/delay.h>
  9. #define F_CPU 16000000UL

  10. #define DDR_SPI DDRB
  11. #define PRT_SPI PORTB
  12. #define DD_MOSI 5
  13. #define DD_SCK 7
  14. #define DD_SS 4

  15. void SPI_MasterInit(void)
  16. {
  17. /* Set MOSI and SCK output, all others input */
  18. DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS);
  19. /* Enable SPI, Master, set clock rate fck/16 */
  20. SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
  21. }

  22. void SPI_MasterTransmit(char cData)
  23. {
  24. /* Start transmission */
  25. SPDR = cData;
  26. /* Wait for transmission complete */
  27. while(!(SPSR & (1<<SPIF)))
  28. ;
  29. }

  30. int main(void)
  31. {
  32. /* Replace with your application code */
  33. SPI_MasterInit();
  34. char i=0;
  35. while (1)
  36. {
  37. SPI_MasterTransmit(1<<i);
  38. PRT_SPI&=~(1<<DD_SS);
  39. _delay_us(100);
  40. PRT_SPI=(1<<DD_SS);
  41. _delay_us(100);
  42. i++;
  43. if(i>8) i=0;
  44. _delay_ms(100);
  45. }
  46. }


Schematic:

ATMega644P Serial Peripheral Interface (SPI) Tutorial
SN74HC595N LED Driving

 AVR Experiment Board:

ATMega644P Serial Peripheral Interface (SPI) Tutorial
SN74HC595N LED Driving

 

ATMega644P Serial Peripheral Interface (SPI) Tutorial
SN74HC595N LED Driving

 

 

ATMega644P Serial Peripheral Interface (SPI) Tutorial
SN74HC595N LED Driving

 The program just shift each bits of the SN74HC595N output port.

 

 
 

 

 

 

 

 

 

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90