Projects   /   Communication & Interfacing   /   PIC16F877A

Lecture 45 : PIC Serial Communication using Serial Peripheral Interface (SPI)

Objective To establish serial communication between two PIC16F877A microcontrollers The SDO for master acts as SDI for slave and vice-versa.

Published
Updated

At a glance

CategoryCommunication & Interfacing
PIC modelPIC16F877A
IncludesCircuit diagram
Reading time~3 min

Summary of Lecture 45 : PIC Serial Communication using Serial Peripheral Interface (SPI)

This experiment establishes serial communication between two PIC16F877A microcontrollers using the Serial Peripheral Interface (SPI). The master reads 8-bit digital input from Port-B and transmits it to the slave, which outputs the value to its own Port-B. Communication utilizes three lines: SDO, SDI, and SCK. The master controls the clock and initiates transmission via the SSPBUF register, while the slave uses interrupts to receive data and update its output port.

Parts used in the PIC16F877A SPI Communication Project:

  • PIC16F877A Microcontroller (Master)
  • PIC16F877A Microcontroller (Slave)
  • Port-B Input (Master)
  • Serial Data Out (SDO)
  • Serial Data In (SDI)
  • Serial Clock (SCK)
  • SSPCON Register
  • SSPBUF Register
  • SSPIF Flag

Objective

To establish serial communication between two PIC16F877A microcontrollers

Description
In this experiment, 8-bit digital input is applied at Port-B to one of the PIC16F877A microcontroller which acts as a master in serial communication. The input value is transmitted by the master serially via Serial Peripheral Interface (SPI) to the second PIC16F877A microcontroller, which acts as a slave. The slave then outputs the value to its Port-B. Following three lines are used for serial communication
  1. Serial Data Out (SDO)
  2. Serial Data In (SDI)
  3. Serial Clock (SCK)

Lecture 45 PIC Serial Communication using Serial Peripheral Interface (SPI) Schematic

The SDO for master acts as SDI for slave and vice-versa. Serial clock is invariably provided by the master. The SPI communication settings are done using SSPCON register.

SSPEN = 1                                         , Enables serial communication in SPI mode
CKP = 1                                              , Transmit happens on falling edge, receive on rising edge
SSPM3-SSPM0 = 0000                   , Device set in master mode with clock = Fosc/4
SSPM3-SSPM0 = 0100                   , Device set in slave mode with clock = SCK pin

Transmission is initiated by the Master by writing to the SSPBUF register. When transfer is complete, Synchronous Serial Port Interface Flag (SSPIF) will be set. This can cause an interrupt if peripheral interrupt is enabled.

Assembly Code

Master microcontroller code
; This program reads Port B value from Master PIC microcontroller
; and sends it to slave PIC microcontroller

org 0000H                                                  ; Reset address

Mainline:

movlw 00110000b                                    ; Enable SPI mode, clock, master
movwf SSPCON
movf PortB, W                                            ; Read PortB value
movwf SSPBUF                                         ;  Transmit value to slave
nop
nop
nop
goto Mainline                                            ; Repeat the process

; Slave microcontroller code
; This program receives 8-bit value from Master PIC microcontroller
; and sends it to Port B of slave PIC microcontroller

Lecture 45 PIC Serial Communication using Serial Peripheral Interface (SPI) Schematic

org 0000H                                                 ; Reset address
goto Mainline

org 0020H                                                 ; main program address

Mainline:           bsf STATUS, RP0                                     ; Bank 1

bsf PIE1, SSPIE                                       ; Enable SSP interrupt
bcf STATUS, RP0                                    ; Bank 0
bcf PIR1, SSPIF                                       ; clear SSP interrupt flag
bsf INTCON, PEIE                                   ; Enable peripheral interrupt
bsf INTCON, GIE                                     ; Enable global interrupt

loop:                   goto loop                                                   ; Infinite loop

org 0004H                                                 ; Interrupt sub-routine address
movf SSPBUF, W                                     ; Read SSPBUF
movwf PortB                                              ; write SSPBUF value to Port B
retie                                                             ; Return

 

For more detail: Lecture 45  PIC Serial Communication using Serial Peripheral Interface (SPI) Schematic

  • What is the objective of this experiment?
    To establish serial communication between two PIC16F877A microcontrollers.
  • How does the master initiate transmission?
    The master initiates transmission by writing to the SSPBUF register.
  • Which lines are used for serial communication?
    The project uses Serial Data Out, Serial Data In, and Serial Clock.
  • Does the slave provide the serial clock?
    No, the serial clock is invariably provided by the master.
  • What happens when the transfer is complete?
    The Synchronous Serial Port Interface Flag will be set.
  • Can the completion of a transfer cause an interrupt?
    Yes, this can cause an interrupt if peripheral interrupt is enabled.
  • What value is transmitted by the master?
    The master transmits the 8-bit digital input value applied at Port-B.
  • Where does the slave send the received value?
    The slave outputs the value to its Port-B.
Beginner Tutorials · PIC16F877A

PIC16F877 Driving LCD in 4-Bit Mode using PIC16F877 with Proteus Simulation

Introduction This microcontroller project shows how a PIC16F877 can drive an LCD display using 4-bit mode inside a Proteus simulation.The project demonstrates...

Programmers & ICSP · PIC16F877A

EPE PIC/LCD Demo using PIC16F877 with Proteus Simulation

IntroductionThe EPE PIC/LCD Demo is a classic microcontroller project that demonstrates how a PIC16F877 can fully control a PG12864F graphical LCD.Designed as...

Motors & Robotics · PIC16F877A

Efficient Sun-Following Robot: A Solar Tracker with Microcontroller Control

Abstract The impending global challenge of dwindling non-renewable fuel resources underscores the urgency of transitioning towards renewable energy alternatives like solar power....

Communication & Interfacing · PIC18F4520

CAN FD Node based on a PIC18 Microcontroller

The introduction sets the stage for an embedded systems project that involves developing a Embedded C CAN FD project using the MCP2517FD...

Communication & Interfacing

LAN9662 Gigabit Switch: Optimized for Industrial Ethernet

Introduction It should come as little surprise therefore that one element of what is now more commonly referred to in some circles...

Communication & Interfacing

PIC Microcontroller: Fundamentals & Applications for Students

Introduction Microcontrollers are the key components in the embedded systems market. A variety of embedded systems was formed and developed after the...

Ibrar Ayyub

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough...