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


To establish serial communication between two PIC16F877A microcontrollers

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


movlw 00110000b                                    ; Enable SPI mode, clock, master
movwf SSPCON
movf PortB, W                                            ; Read PortB value
movwf SSPBUF                                         ;  Transmit value to slave
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


READ  Building a PIC18F USB device

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

Leave a Comment

= 3 + 9

Read previous post:
Starting with PICmicro controllers

intro, first steps, tips, links, etc. (C) 2002 .. 2005 Wouter van Ooijen ([email protected]) Last change made at 2005 AUG...

Scroll to top