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

Lecture 44 : Blinking LED with PIC16F877A

Objective
To demonstrate blinking of an LED interfaced with PIC16F877A
Description:
A LED is connected to RA0 pin of Port A. This pin has built-in internal pull-up which can source a current of up to 25mA. The circuit diagram is shown as follows -
Fig 44.1  Circuit diagram for blinking LED experiment
Assembly Code:
Sampling an analog signal periodically and displaying its digital value using PIC16F877A
Aim
To continuously sample convert an analog signal to its digital equivalent and read the digital value from one port of the PIC16C74A microcontroller
Components/Software
  1. PIC16F877A microcontroller
  2. Programmer (to program the microcontroller)
  3. Computer System with Windows 98 or later operating system and RS-232 Cable
  4. +5v D.C Power Supply
  5. Resistors, Capacitors
  6. 10 MHz crystal oscillator
  7. Signal Generator and CRO
Description
An analog signal is converted to digital value using an A/D converter (which is in-built in the PIC microcontroller). After conversion is complete, the eight digital bits are written to Port D. The process is then repeated. The A/D conversion is done on an interrupt basis.
Procedure
  1. Write the code using a desktop PC. Compile it and transfer it to the microcontroller using the programmer.
  2. Wire the microcontroller as shown in the diagram.
  3. Switch on the supply and push Reset button
  4. Observe the results
  5. Switch off the supply
Assembly Code
include PIC16F877A
org 000H
goto Mainline
org 004H
bcf PIR1, ADIF
movf PORTD, w
goto Mainline
org 20H
Mainline:
bsf STATUS, RPO
clrf ADCON1
movlw 30H
movwf TRISA
movlw 30H
movwf TRISE
bsf PIE1, ADIE
bsf STATUS, RPO
movlw 081H
movwf ADCON0
bcf PIR1, ADIF
bsf INTCON, PEIE
bsf INTCON, GIE
bsf ADCON, GO
Observation
The digital equivalents of the applied analog signal were observed. The digital bits were observed, one at a time, using the CRO.
Conclusion:
An analog signal is converted to digital one. The digital signal is observed at Port D of the PIC microcontroller.
Circuit Diagram:
 

Lecture 43 : Interfacing PIC16F877 Microcontroller with an LCD


Aim
To interface LCD (Displaytech 162A) with PIC16F877microcontroller and to display "IITK" in the Liquid Crystal Display (LCD).
Components/Softwares
  1. MPLAB IDE (PIC microcontrollers simulator)
  2. PIC BURNER 3 with software to load the code
  3. LCD (Displaytech 162A)
  4. Computer System with Windows operating system and RS 232 cable
  5. PIC16F877 Microcontroller
  6. +5V D.C Power Supply
  7. Resistors - 10K Ω-1,50Ω-1
  8. Capacitors - 27 µ F-2
  9. Potentiometers - 10K Ω -1
  10. 20MHz Crystal oscillator
  11. SPST switches -1
Fig 43.1 A view of the PIC programmer connected to the PC serial Port (COM1)
Procedure
  1. Write the assembly code in MPLAB IDE simulator , compile it and check for errors
  2. Once the code was error free, run it and check the output in the simulator.
  3. After checking the code in the simulator, load the code (in .HEX format) into PIC16F877 microcontroller using PIC BURNER3.
  4. Make connections as shown in the circuit diagram.
  5. Switch on the power supply and observe "IITK" displayed in the LCD.
ConclusionLCD was successfully interfaced with PIC16F877 microcontroller and displayed "IITK" in the LCD.
Assembly Code
Circuit Diagram
Fig 43.2 Circuit Diagram
Fig 43.3 Circuit assembly on breadboard
Liquid Crystal Display (LCD-Displaytech 162A )LCD Displaytech 162A consists of a LCD panel, a controller IC (KS0070B) and a back light LED. The LCD module consists of total 16 pins in which, 2 are for power supply, 2 pins for Backlight LED, one pin for contrast adjustment, 3 pins are for control signals and 8 pins are data pins. In order to display any data, we need to do certain initiations. The following are the main three steps in displaying any data in the LCD display.
  1. Initializing LCD by sequence of instructions
  2. Executing commands depending on our settings in the LCD
  3. Writing data into the DRAM locations of LCD in the Standard Character Pattern of LCD
For doing above steps, refer the manual for LCD and follow the instructions and timing diagrams strictly.
MPLABIDEMPLABIDE is a free software which can be downloaded from the website www.microchip.com
Working with MPLABIDE :
MPLABIDE is a simulator for PIC microcontrollers to write and edit the code in assembly language, compile it and also to run the code. Output can be verified using simulator.
Steps to Use MPLABIDE
  • After Installing the software MPLABIDEv7.2,open MPLABIDE.
  • To built a new project, open
    Project  Project Wizard
    Project wizard New
    Device  16F877
    Location  (Ex:C:\ProgramFiles\Microchip\MPASM Suite\MPASMWIN.EXE)  Next
    <Project name>&<Project Directory>  Next
    (Add file "f877tmpo.asm" which was located in programfiles  microchip  MPASMSuite  Template  Object)
    (Add file "16f877.lkr" which was located in programfiles  microchip  MPASMSuite  LKR)
    Next  Finish
    To have more clear refer to MPLABIDE help files.
  • After building the project open the editor f877tmpo.asm and write the assembly code
  • After writing the assembly code in the editor, build the project by clicking on the following option
    Project  Build all
  • Check for the errors in the output window
    View  Output
  • Once the error free code was made, simulate the code by following option
    Debugger  Select Tool  MPLAB SIM
  • Simulator options are
    1. Step into - Each time only one instruction will be executed (Single stepping mode)
    2. Run - To run the whole code at once.
    3. Animate - to animate the executing the code
  • Additional things:
    1. To view DRAM, program memory, SFRs, and External memory use the option VIEW
    2. To set break points in the code (where simulation stops at that point). Debugger  Breakpoints
  • To stop the simulation
    Debugger  Halt
After checking the code in the simulator, the code (file with .HEX extension) is loaded into 16F877 microcontroller using PIC BURNER 3.PIC BURNER3PIC BURNER3 can be used to program PIC microcontrollers. The steps to be followed to program the IC safely are as follows.
  1. Connect the PIC BURNER3 through RS232 Port to computer system with windows98 as operating system.
  2. Execute the file "icprog" which was in the software that comes with PIC BURNER3.
  3. Set the device as PIC16F877
  4. Switch on the power supply of PIC BURNER3
  5. Settings  Hardware  { JDM Programmer,Com1,Direct I/O}
  6. Settings  Hardware check  1. on clicking "Enable Data out", Data in must be clicked automatically
    2. on clicking Enable MCLR, red LED on the PICBURNER3 must glow
  7. Settings  Options  Confirmation [ Erasing the devise,Code Protecting the Devise]
  8. Settings  Options  MISC  Process Priority  Normal
  9. Settings Options  Programming  Verify After Programming.
  10. Remaining options keep them at default settings. [Refer Manual of PICBURNER3 for detail]
  11. Now insert the 16F877 microcontroller into the slot provided on the PICBURNER3 as the direction specified in the manual of PICBURNER3.
  12. load the .hex file
    File  open file
  13. Command  Erase All
  14. Command  Blank Check
    Then there should be a notice on the window that "Device is Blank "
  15. Command  Program All
  16. Command  Blank Check
    Then there should be a notice on the window that "Device is not blank at address 0x0000H".
  17. Close the window, remove the IC from the PIC BURNER3 and switch off the power supply for PIC BURNER3.