CS-424, Lab # 4
Microcontroller Digital I/O Ports
2-20-09, Report Due: 2-27-09

In this lab you will use PIC18F452 microcontroller digital I/O ports to 
control external circuitry you design and build on the QuikProto board. 
You will also design and build an entire micontroller-based microcomputer 
on the proto board using a PIC16F84A microcontroller chip. You will use 
an EPIC PROM Programmer to program the PIC16F84A to control external 

Part 1. The Hardware

In this part of the experiment you are to wire up circuitry on the 
QuikProto board that will display the states (open or closed) of four 
DIP switches on four light emitting diodes. Whenever any switch is 
closed, the corresponding LED should come on; it should be off when the 
switch is open. Wire up four of the leads of an 8-position DIP switch
between +5 volts and ground through a 10K resister. Connect this 
circuitry to bits PC0-PC3 of PORTC of the PIC18F452 that have been 
brought over to the proto board from the QuikFlash board. The wiring 
should be done in such a way that when the switch is open +5 volts is 
input to the port and when it is closed, 0 volts (ground) goes to the 
port. (Use the wiring of switch SW3 in the QuikFlash board schematic on 
Page 332 of your text book as a guide.) Connect each of four LEDs in 
series with four 1K resistors to bits PB0-PB3 of PORTB of the PIC18F452 
that have been brought over to the proto board, so that the LEDs are 
either turned on or off depending on the value of the bit stored in the 
port. Note that there are two ways of doing this: either by connecting 
to +5 volts or to ground. (Again refer to the schematic on page 332 of 
your text, concentrating on diodes D2, D4, D5, and D6.) When you wire 
up this part of the circuit be sure that you get the polarity of the 
LEDs right. (The flat side of the LED with the shorter of the two wires 
is the cathode--the side that corresponds to the vertical line and NOT 
the triangle on the LED schematic symbol.) This is very important, 
since if you wire the LEDs backwards, they could literally blow up on 
you!! Before you apply power to the board, be sure you show your 
circuit to the lab instructor.

Be sure to leave at least nine consecutive positions on the 
protoboard free. You will need these for Part 2 of the experiment.

The Software

A. Write a program that will configure PORTB and PORTC correctly, turn 
off all of the LEDs, and then continually read the switches and send 
their bit pattern to the output port, thereby displaying them on the 

B. Write another program that does different things when different 
switches are closed. Specifically: if the switch that's connected to PC0 
is closed, all the LEDs should be turned off; if the one connected to PC1
is closed, all the LEDs should be turned on; if the one connected to PC2
is closed, the LEDs should simultaneously blink on and off; if the one 
connected to PC3 is closed, the PB0 LED should be lit and turned off, 
then the PB1 LED, then the PB2, LED, then the PB3 LED. This action
should continue until a different switch is closed. For this part you 
may assume that only one switch at a time is closed. Demonstrate to the 
lab instructor that everything is working correctly.

Part 2.

In this part of the experiment you are to use a different microcontroller 
(the PIC16F84A) to control the same hardware you wired up on the proto
board in Part 1.

The Hardware

Insert a PIC16F84A microcontroller into the protoboard so that its pins 
occupy the nine consecutive positions you left available in Part 1. Use 
extreme caution when you insert the PIC16F84A into the protoboard. Its 
pins are very fragile and easily broken. Wire up the PIC16F84A "Core 
Circuit" shown in the following diagram. Here VCC is the same as VDD 
(+5 volts) brought over to the protoboard from the QuikFlash board; VSS 
is ground (also brought over from the QuikFlash). Be sure to get the 
polarity of the tantalum capacitor correct.

Connect the four DIP switches and the four LEDs from Part 1 of the 
experiment to bits RA0-RA3 of PORTA and bits RB0-RB3 of PORTB of the 
PIC16F84A, respectively. For the pin numbers of those port bits you 
should refer to the PIC16F84A data sheet (available from the 
Microchip.com web site: 

The Software

Use MPLAB to develop an assembly language program for the PIC16F84A that 
will perform the same task as in Part A of the first section of this lab. 
(PORT A will now be the input port.) Be sure that you set the MPLAB
configuration for the PIC16F84. You will again need to refer to that 
microcontroller's data sheet to see how to configure the two ports for 
input or output. The process is a bit more complex than for the PIC18F452. 
Basically, to get access to the memory bank that contains TRISA and TRISB 
(the tristates that control the direction of the ports), you need to 
manipulate the RP0 bit of the STATUS register: If that bit is set, 
subsequent memory accesses will be to "Bank 1", which contains TRISA and 
TRISB; if the bit is cleared, subsequent accesses will be to "Bank 0", 
which contains PORTA and PORTB. As for the PIC18F452, setting any bit in 
TRISA or TRISB programs the corresponding port for input; clearing a bit 
programs it for output. You will also need to have the following lines at 
the start of your program:

#include "P16F84.inc"


Once the program is developed and built, use the EPIC programmer to 
"burn" the program into the microcontroller's flash program memory. 
To do this, carefully remove the PIC16F84A from the protoboard. Once 
again, the pins of the PIC16F84A are quite fragile and easily broken, so 
use a chip remover or a small screwdriver to very slowly and carefully pry 
the chip out of the protoboard. Carefully insert it into the programmer's 
IC socket and connect the programmer to the PC's parallel port using the 
provided cable. Start the EPICWIN program and refer to the "Help" to learn 
how to use the programmer. Once the program is downloaded, very carefully 
remove the microcontroller from the EPIC's socket and place it back into 
the protoboard. Apply power and you should be able to change the switch 
settings and observe the results on the LEDs. Show the working system to 
the lab instructor.

Lab Report

Your lab report should contain detailed diagrams of all circuits developed 
and implemented in this lab as well as the assembled listings (.LST) and 
.HEX files of all of your programs.