You are on page 1of 7

University of Engineering and Technology, Lahore Spring 2018

LAB 2: PIC18F452 I/O PORTS


Name : Date :
Regd-No :
OBJECTIVES:
▪ Implement the basic starting circuit for the PIC18F452
▪ Write a code for manipulating I/O ports.
▪ Use the .hex file for simulation in Proteus.
SUGGESTED READING:
▪ Theory Lecture 3: WREG and GPR
▪ Theory Lecture 4: Introduction to Assembly PIC18
▪ PIC18F4550 Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/39632e.pdf
▪ Microchip’s website: www.microchip.com/
▪ Assembler/Linker/Librarian’s user guide (Microchip):
ww1.microchip.com/downloads/en/DeviceDoc/33014J.pdf
▪ PIC18F4550 and Assembly Language Overview:
http://www.pic18f.com/tutorial/2007/12/04/18f4550-and-assembly-overview/
▪ Using MPLab to create hex files:
http://picprojects.org.uk/projects/mplab/mplabhowto.htm
▪ MPLab step by step with PIC18F4550:
http://www.auelectronics.com/forum/index.php?topic=203.0
▪ Microchip’s PicKit3 In-circuit debugging and programming guide:
http://ww1.microchip.com/downloads/en/DeviceDoc/52116A.pdf

Please read through all the suggested reading before you come to lab.

EQUIPMENT AND COMPONENTS:


▪ PIC18F452 (or similar microcontroller) and associated startup circuitry
▪ Light Emitting Diodes
▪ Resistors (220 Ohms, 10kOhms)
▪ Capacitors (10uF, 470uF)
▪ Jumper wires
▪ Push Button
▪ Power Adapter (7.5 Volts) and voltage regulation circuit (5V)
▪ Universal Programmer or PicKit v3

SOFTWARES REQUIRED:
▪ MPLAB IDE or MPLABX IDE
▪ Proteus
---------------------------------------------------------------------------------------------------------------------

MCT-222L: EMBEDDED SYSTEMS LAB


1
Department of Mechatronics and Control Engineering, U.E.T Lahore
LAB 2
University of Engineering and Technology, Lahore Spring 2018
PIC18F452 and its PORTS:
PIC18F452 is a medium range 8-bit controller that packs powerful processing
capabilities with many peripheral interfaces useful for a wide range of
communication and sensor interfacing. The microcontroller has 35 I/O pins. These
pins are arranged into ports A, B, C, D and E. These pins are usable for a
combination of inputs, outputs, analog-to-digital or digital-to-analog conversion
and PWM. Additionally, you can use these pins for serial communication, USB
communication, or to trigger interrupts.

Pin Description:

Fig.2.1: 40 pin DIP package pin-out for PIC18F452

PORTS:
40 pins of PIC18F452 are divided into 5 ports. Out of which, 34 pins are Input-
Output pins which can be configured for general Input or Output by setting registers
associated with them. Please refer to the pin-out diagram above for a clear idea about
location of these pins on the microcontroller.

Ports Number of pins Pin Name


PORTA 7 RA0-RA6
PORTB 8 RB0-RB7
PORTC 8 RC0-RC7
PORTD 8 RD0-RD7
PORTE 3 RE0-RE2

MCT-222L: EMBEDDED SYSTEMS LAB


2
Department of Mechatronics and Control Engineering, U.E.T Lahore
LAB 2
University of Engineering and Technology, Lahore Spring 2018

Each port in PIC18F452 is associated with three Special Function Registers for I/O
operations.

▪ TRISX (8 bit)
▪ LATx (8 bit)
▪ PORTx (8 bit)

TRISx : where X is the name of the ports either of A, B, C, D, E. For example


TRISA, TRISB etc.This register assigns the direction of the pins (Input or Output).
For example “TRISB = 0xFF”, will set all the pins in port B to Input. Also, to output
any data to any of the pins of the PORTB, we must first put 0s into the TRISB
register to make it an output port.
LATx: The latch registers reads and modifies the write operation on the value of I/O
pin and stored the output data that is to be passed on to the external hardware.
PORTx: Reads the device level, stores the Input level of the pins and reads and
registers the input signal from the external device if the pin is configured as Input.

Example 2.1:
Write a program to blink an LED attached to pin RB7
1. Simulate it on MPLab-IDE simulator and Proteus ISIS™
2. Implement the circuit on a bread-board and program the microcontroller using
a programmer device.

Fig.2.2: Circuit for blinking a single LED

MCT-222L: EMBEDDED SYSTEMS LAB


3
Department of Mechatronics and Control Engineering, U.E.T Lahore
LAB 2
University of Engineering and Technology, Lahore Spring 2018

Fig.2.2 shows the bare minimum for implementing the circuit on the bread-board.
▪ The Oscillator is used to provide clock to run the controller.
▪ The push button and the resistor R1 form the reset circuit to reset the
microcontroller.
▪ LED is connected to pin RB7 via a current limiting resistor R2.

Following is the sample code written in assembly for blinking the LED.

#include<P18f452.INC>
CONFIG WDT=OFF ; disable watchdog timer
MYREG EQU 12H ; Assigning name to register
ORG 0H
MOVLW 0H
MOVWF TRISB ; TRISB=00H makes the portB output
BACK MOVLW 80H
MOVWF PORTB ; Send logic 1 to pin RB7
CALL DELAY ; Call delay loop
MOVLW 00H
MOVWF PORTB ; Send logic 0 to pin RB7
CALL DELAY ; Call delay loop
GOTO BACK ; Go back to label: BACK
;--------------------------
ORG 300H ; Location of delay loop in ROM
DELAY MOVLW 0XFF
MOVWF MYREG
AGAIN NOP
NOP
NOP
DECF MYREG, F ; Decrement value in MYREG by 1
BNZ AGAIN ; Branch if not zero: to label AGAIN
RETURN ; Return to main program
END

Simulate the code on MPLab SIM and implement the circuit on Proteus by
loading the resultant .hex file.

Using Proteus to Simulate:

MCT-222L: EMBEDDED SYSTEMS LAB


4
Department of Mechatronics and Control Engineering, U.E.T Lahore
LAB 2
University of Engineering and Technology, Lahore Spring 2018
1. Add the required components to the workspace.

2. Double click on the microcontroller and load the hex file to the controller, and
set the clock frequency to 20MHz.

3. Simulate the design using simulate button on the


bottom left corner of the screen.
4. Implement the circuit on the bread board and run by uploading (burning) the
code to the controller.

MCT-222L: EMBEDDED SYSTEMS LAB


5
Department of Mechatronics and Control Engineering, U.E.T Lahore
LAB 2
University of Engineering and Technology, Lahore Spring 2018
Tutorial: Programming PIC19F4550 using PicKit3™
Introduction to PicKit3:
The PICkit 3 In-Circuit
Debugger/Programmer (see Figure 2.3) is a
simple, low-cost in-circuit debugger that is
controlled by a PC running MPLAB X IDE
software on a Windows® platform. The
PICkit 3 In-Circuit Debugger/Programmer is
an integral part of the development
engineer’s tool suite. The application usage
can vary from software development to
hardware integration.
In addition to debugger functions, the PICkit
3 In-Circuit Debugger/Programmer system
also may be used as a development
programmer.
Fig.2.3: PicKit3 in-circuit Debugger/Programmer

Step 1: The connections

Fig.2.4: PicKit3 ICSP connections with PIC18F452

Connect the PIC18F452with the PicKit3 as shown in Figure 2.4. PicKit3 can
power the controller, so there is no actual need to power the controller externally.

Step 2: Selecting PicKit from MPLab


Select PicKit3 as the programmer.
Programmer > Select Programmer > PicKit 3

MCT-222L: EMBEDDED SYSTEMS LAB


6
Department of Mechatronics and Control Engineering, U.E.T Lahore
LAB 2
University of Engineering and Technology, Lahore Spring 2018

Step 3: Upload the program


Upload the program using the Program button from the PicKit3 toolbar.

Task 2.1:

1. Add eight LEDs to PORTB.


2. Modify example 2.1 to blink all the LEDs alternatively.
3. Simulate the circuit on Proteus and implement on the bread-board.

Bonus Task:
Blink the LEDs in a sequence (from 1 to 8) in a loop

MCT-222L: EMBEDDED SYSTEMS LAB


7
Department of Mechatronics and Control Engineering, U.E.T Lahore
LAB 2

You might also like