Web Hosting

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

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)
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. It is shown as follows -
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.
Fig 45.1   SPI Communication Schematic
Circuit Diagram:
Fig 45.2   Circuit diagram for SPI communication
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
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

No comments:

Post a Comment