PIC16F877 Microcontroller Tutorial
PIC16F877 Microcontroller Tutorial
PIC16F877 Microcontroller Tutorial
1. Introduction
This tutorial is to introduce the microcontroller technolgy the capabilities and the specifications of a
commonly used Microcontroller Microchip PIC16F877 and describe the experiments conducted using the
Development board Flash PIC development board which accomodates this microcontroller.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 2 of 36
2. Getting Started
2.1. Parts list:
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 3 of 36
Capacitor
1N4004 Radioshack 276-1103 $.79 1
Diode
100 ohm Radioshack 271-1311 $.99 1
resistor
Technie Toyz Compusa MD-TTF- $10.99 1
12V PC Fan 8025A-2B
9V battery Radioshack 23-875 $3.29 1
6-Ft. Serial Radio Shack 26-117 $13.49 1
RS-232C
Cable
1x40 pin Jameco 103270 0.75$ 1
header
Note: It is assumed that the user has the following common electrical equipment: 1x8 pin ribbon
cable, alligator clips, soldering iron and solder.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 4 of 36
After we put power on the FlashPIC development board, now we are ready to program the PIC16F877
chip.
1. Connect the ICD-S to the computer using the RS-232 cable, which came with the ICD-S.
2. Conncect the ICD-S to the developmet board using the phone jack. (See Figure2.2-2)
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 5 of 36
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 6 of 36
Command button”. After the .hex file is transferred, now we can run the software and test its
functionality by clicking on “Run Program” command button. (Figure2.2-4)
8. Since the software is tested now the ICD-S can be disconnected and the Development board can be
powered up and the .hex file can be run without being connected to the computer.
3. PIC16F877 Microcontroller
In this section, properties of PIC16F877 microcontroller,CCS compiler, ICD-S, the
reference book “Embedded C programming and the microchip PIC” are briefly explained to give a
general idea; it may seem confusing for a first time reader who is not familiar to microcontrollers
technology or C programming. However, as the funtionality of the components such as timers, A/D
converters, I/O Ports are explained in detail in Section 3 as they are being used in the experiments, the
fundemental concepts would be better understood and, the reader can flashback to this section to view
the schematics and the specifications.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 7 of 36
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 8 of 36
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 9 of 36
Usually, a microcontroller by itself is not sufficient to perform the intended tasks. For instance, an
oscillator chip is necessary to time the programmed instructions. In order to investigate the
capabilities or to test a given microcontroller, obviously it is vital to build the proper circuitary.
Example: potentiometer and a power supply to simulate analog inputs or LEDs to simulate the digital
outputs. Hence, some hardware and sofware vendors provide the microcontroller with the
supplemantary circuit elements on the same breadboard. These boards are called Development
Boards. One can also build a development board himself/herself if he/she is willing to go through the
painsaking process of building the circuit.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 10 of 36
The development board used in the series of experiments is Flash PIC development board.
(Figure3.1-1) It has the following features:
- RS232 through a 9-Pin D-Shell as well as screw terminals and a jumper header.
- Up to 32K words of In-System Programmable FLASH memory with up to 256
bytes of EEPROM and up to 1.5K of Internal RAM (depending on processor
selection).
- Up to 8, 10 bit, Analog Inputs, using either internal or user supplied reference.
- 9 I/O controlled LEDs, 8 of which are jumper selectable.
- 32KHz “watch” crystal for on-board Real-Time operations.
- A universal clock socket allows for “canned oscillators”, as well as a variety of
crystals, ceramic resonators, and passive terminations.
- 0.1” centered headers provide for simple connection to the processor special
function pins and I/O.
- A 6-pin, ICD connection is provided for in-system programming and debugging.
This connection is directly compatible with the Microchip ICD, ICD2 and CCS
ICD-S programming hardware. Flash PICs can also be programmed through
RS232 using an appropriate boot loader application.
- On-board regulation allows for power inputs from 8-38VDC with an LED power
indicator.
- Termination is provided for 5VDC output at 250ma
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 11 of 36
the PICmicro MCU hardware and the inability to treat ROM areas as data. On
the other hand, the compilers have knowledge about the hardware limitations
and do the work of deciding how to best implement your algorithms. The
compilers can efficiently implement normal C constructs, input/output operations
and bit twiddling operations.
The compiler can output 8 bit hex, 16 bit hex, and binary files. Two listing formats
are available. Standard format resembles the Microchip tools and may be
required by some third-party tools. The simple format is easier to read. The
debug file may either be a Microchip .COD file or Advanced Transdata .MAP file.
All file formats and extensions are selected via the Options|File Formats menu
option in the Windows IDE.
The usage of the copiler is explained in Section 2.0 Getting started. The reference book
“Embedded C Programming and the Microchip PIC” comes with a demo version of the compiler.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 12 of 36
3.4. The Reference Book: Embedded C Programming and the Microchip PIC by Barnett, Cox
and O’Cull
This book is a good guide for introducing the microcontroller technology. First chapter is dedicated to
teaching basic C programming however, this book shouldn’t be considered a C programming
handbook. One should always have a book like: Teach yourself C by Zhang ISBN 0-672-31861 as a
C programming reference guide for beginners.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 13 of 36
Included with the book is a CDROM containing samples all of the example programs from the
book as well as an evaluation version of the CCS-PICC Compiler.
Author`s note 01/22/04 : In the first examples and projects in chapter 1, functions like “scanf” and
“printf” are used that require prior knowledge to interface the board through RS-232 which is introduced
in the late chapters of the book. It might be discouraging for the student not being able to do the first
project of the book, hands-on. Although I was able to find answers to most of my questions about using
the C compiler and the hardware however, I had to do a lot of skipping between chapters and Appendixes
to find these answers which was time consuming. In general it is a nice and descriptive text book.
4. Experiments
The most quick and effective way of learning a new topic is, having hands on experience. In this section
we will conduct some experiments using the FlashPIC Development board to which would give us a
jump start on in PIC usage.
The digital I/O and the timers will be discussed with this experiment. The program will count
from 0 to 255 and output the binary equivalent of this number to PortD every 2 seconds. The digital I/O
peripherals are discussed in detail in the next experiment “Dipswitch”. In this experiment we will focus
on the usage timers.
Timers and counters are one of the most commonly used perpheral in a microcontroller. They can
be used to measure time periods, speed and provide output signals in a specified rate.
The PIC16F877 has 4 timers timer0, timer1, timer2 and watch dog timer. Timer0 and timer2 are 8
bit timers and timer1 is a 16bit timer. The inportant issue is to know when the counter reaches the
maximum value (255 in 8 bit timers and 65,535 is 16bit timers) and rolls over.
The watch dog timer is a safety device. When an unepected event occurs, the watch dog timer resets the
microcontroller. In this experiment we will use the watch dog timer as our timer to time the digital
output.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 14 of 36
The command line above, is used to tell the compiler to insert an appropriate code to automatically reload
the WDT during delay functions.
“delay_ms(2000)” command line is used to place a delay the event 2000ms in every cycle of the while
loop. “output_d(z)” command line is used to output the digital number through portD.
Instructions:
1. We type the code in Figure4.1-1, compile it and program the chip as explained in Section 2. The
code can also be downloaded from here ledwdt.c.
There are 5 I/O ports in the in the PIC16F877. The individual bits and the whole channel can set to
digital inputs or outputs. The following commands are used to set the ports to output:
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 15 of 36
set_tris_d( 0x00 )
set_tris_b( 0xFF )
With these command lines, port D and port B is set to output and input respectively. The following
commands to read and input from port B and write it to port D respectively.
z=input_b()
output_d(z)
The following command is also used to activate the port B pullups.
port_b_pullups(TRUE)
The procedure for the experiment:
1. Place the dipswitch, (4.2-1) 8 pin header and single pin header as illustrated in Figure4.2-2 on the
component side of the board and solder them on the solder side of the board. Only the holes which
have been used are illustrated in Figure 4.2-2.
Figure-4.2-
2 The layout of the dipswitch breadboard
2. One side of the switch is jumped together and tied to common ground. The single pin header is
jumped to the common ground of the development board using an alligotor clip. The other side of
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 16 of 36
the switch is tied to Port B using a ribbon cable. See Figure-4.2-3 and Figure-4.2-4.
4.2-4 Connection details of the dip switch circuit to the Development Board
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 17 of 36
3. We type the code in Figure4.2-6, compile it and program the chip as explained in Section 2. The
code can also be downloaded from here dip.c. Note that the I/O process is located in an infinite
loop with the while(1) command line. Hence, the program is constantly reading from port B and
writing to Port D.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 18 of 36
4. We can unplug the ICD-S and run the experiment and observe the binary numbers change as we
change the dip switch positions.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 19 of 36
Figure 4.3-2 The wiring schematics for the temperature sensor experiment
2. Type the following code (Figure 4.3-3) and burn it on the chip after compiling it. By implementing
the line “#device ADC=10”, the analog to digital conversion is set to 10bit resolution. The LM35
outputs the temperature with the sensitivity of 10mv/oC (Example: 25 oC corresponds to 250mv).
The range of the ADC of the PIC Development Board is 0 to 5V and the ADC conversion is 10bits.
Hence, the resolution is 210-1=1023. So the formula for analog to digital conversion would be;
Temperature in Celsius = 5.00 * Voltage read from LM35 * 100.00/1023.00
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 20 of 36
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 21 of 36
4. In this application, the RS-232 communications will be used as the data interface between the PC
and the PIC16F877 development board. Notice that PIC Flash Development board comes with 2
separate means of serial communications which are marked in Figure 4.3-5.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 22 of 36
cable/connector do not match the transmit and receive pins for the standard RS-232 cable
so, we have to use the screw terminal RS-232 communications which is labeled RS232. So we cut
the female connector off the standard RS-232 cable and strip the connector down to its shell.
(Figure 4.3-6)
Figure 4.3-8 View of the Development board with RS-232 communications established
5. After checking the wiring and making sure the LM35 is sealed properly, we can power up the
development board and plug in the RS-232 serial communications cable to the computer and start
the hyper terminal application which will be used for data acquisition. The usual path of the hyper
terminal is start menu>Programs>Accessories>Communications>hyperterminal. Settings of the
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 23 of 36
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 24 of 36
first and second values of the curve is 52 and 79. Since the water is boiled we would expect the first
reading to be 100oC. However, the coins and the plastic bulb, which is at room temperature
initially, absorbs some of the energy therefore the temperature drops significantly and reaches a
steady state 79 oC before decaying.
Figure 4.3-11 Cooling curve of the boiled water in ADC thermistor experiment
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 25 of 36
Figure 4.4-1 Generic Block Diagram of Closed Loop Temperature Control System
Open Loop (Bang Bang) Control: The fan is either turned on at maximum speed or turned off
depending on temperature feedback exceeding the temperature goal. Actually, this control
system can also be considered to be close loop since the control system responds to a
feedback, however since the feedback only triggers a switching operation, it is considered an
open loop operation. The generic block diagram of is given in Figure 4.4-2.
Figure 4.4-2 Generic Block Diagram of Open Loop Temperature Control System
The procedure of the experiment:
1. As the first step of the experiment, we will build our DAC/LF353 Op-Amp Circuit.
A schematic of the DAC /Op-Amp circuit is given in Figure 4.4-3. The theory of operation for this
circuit is that when an eight bit word is sent to the digital to analog converter (DAC), the DAC outputs a
current that is defined in equation 2.1.
Equation 4.4. 1
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 26 of 36
Vref word
I out = *
R fb 256
Where :
I out = Output Current
Vref = Reference Voltage
R fb = DAC' s Internal Resistance
word = Eight Bit Word Input to DAC
The output current is then fed into the op amps of the LF353 integrated circuit. The output from the
LF353 circuit is defined:
Equation 4.4. 2
⎛ word − 128 ⎞
Vout = Vref ⎜ ⎟
⎝ 128 ⎠
Where :
Vout = Voltage Output form Overall Circuit
Vref = Reference Voltage
word = Eight Bit Word Input to DAC
The DAC/LF353 card takes a digital word as an input, and outputs a proportional voltage. In this
application the digitalword of 181 will result in an output voltage of 4.2 V, given a reference voltage of
+12 V. 181 corresponds to fan’s minimum speed. Digitalword of 255 will produce output of 7.5V which
is the maximum speed of the fan. All the pins of PortD of the PIC16F877 is set for output and they are
wired to the DAC/LF353 circuit. Illustration of DAC/LF353 circuit is given in Figure4.4-4.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 27 of 36
2. As the second step we can build our TIP31 Power Transistor Circuit. A schematic of the TIP31
power transistor circuit may be found as Figure 4.4-5 This circuit is necessary because motors do not
run on voltage, rather they run on current. The current is obtained from an external power supply,
which is added proportionally to the voltage that is input to the TIP31 circuit. The TIP31 works by
reading a reference voltage from its base pin (b), then an equivalent voltage supplied by the a source
attached to the collector (c), is allowed to pass to the emitter (e). This allows for an external power
supply to be regulated by a low current circuit. The DAC/LF353 Op-amp circuit is illustrated in
Figure 4.4-6.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 28 of 36
3. For this experiment, as we review the schematics, it is evident that we need a power supply which
can give us the following voltages:12V,-12V and 5V. We can use a common PC power supply for
this application. However we need to make a slight modification to the power supply. The PC power
supplies are switching supplies. Hence, they require proper amount of load to be operational. The
following site is a nice guide for modifying PC power supplies for general use:
http://web2.murraystate.edu/andy.batts/ps/powersupply.htm. The main idea is to add a 10ohm 10watt
resistor across 5V and common to apply the necessary load for operation. However, the resistor gets
extremely hot; so, it would be appropriate to apply a heat sink on the resistor and place the resistor in
front of the power supply fan (Figure-4.4-7) to avoid resistor failure which would eventually lead to
power supply failure.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 29 of 36
4. Now we can start assembling the circuit. The reference voltage of DAC/LF353 is tied to +12V of the
power supply. The grounds of all the components (Development board, LM35, DAC/LF353, TIP31
Power transistor circuit/DC Fan) are tied to the power supply ground. The output voltage of Pin7 of
LF353 is tied to the TIP31 base. The LM35 is wired as described in section 4.3 ADC Experiment.
This time instead of using a 9V battery to power the LM35, we can use 5V power supply. The RS-
232 communications cable is wired as described in the ADC experiment in section 4.3 and is plugged
into the computer. We also need a blow-dryer for this experiment as a disturbance to the system as
described above. While doing the wiring, preparing the hardware necessary for this experiment, be
sure to check the wiring before putting power to it. Each sub-assembly should be tested separately
before putting the experiment setup together as a whole. For instance, once the DAC/LF353 circuit is
prepared, run the dip.c program and check if the voltages correspond to the digitalwords using a
voltmeter. The Experiment setup is illustrated in Figure-4.4-8.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 30 of 36
5. We are ready to program the chip. The code given in Figure4.4-9 is used for the close loop
application. The code is also provided in file project3.c. There aren’t any new commands introduced
with this code. The digitalword=181 can be considered as the threshold value which gives 4.2V and
generates enough current through the TIP31 circuit so that the fan rotation starts. Furthermore, the
digitalword=255 generates 7.5V which generates the highest speed using the hardware listed in this
section. So, we can control the speed through the digital word ranging from 181 to 255. The
resolution is 255-181=74 steps. We use Kp=2.5 as the proportional gain of the control system. This
seems to be an ideal value for the operational range and the cooling load involved.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 31 of 36
Figure4.4-9
The source code for the Open Loop (Bang-Bang ) Control is given in Figure4.4-10. It is also
available with file project3bb.c. The temp_comparator variable compares the temperature feedback with
the target temperature and switches the fan on at full speed or off.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 32 of 36
Figure4.4-10
6. We are ready to program the chip and start the experiments. In the first experiment, the initial
temperature of the water is chosen to be 42oC. As stated before, the target temperature is 27 oC. In the
second experiment the, the water is heated from 16 oC to the target temperature 27oC. These two
experiments are conducted both using the close loop and open loop program. The data recording is
performed via the hyperterminal screen as described in Section 4.3. All the recorded data is also
provided in Appendix A in Excel Spreadsheets.
Cooling Experiments:
In Figure4.4-11 and Figure4.4-12, the results Close Loop and Open Loop cooling are given
respectively. While reviewing the graphs, keep in mind that, the negative speed commands mean
digital words smaller than 181 which correspond to motor currents smaller than the threshold level for
the DC Fan to move. So, the DC Fan does not move with the negative speed commands. These
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 33 of 36
values are given only to illustrate how digitalword changes with varying temperature level.
The closed loop system cools down to the steady state between 500sec and 2200sec. So, it takes
roughly 1700 seconds for it to cool down. However for the open loop system, since the fan is
commanded to its maximum speed while the temperature is higher than the target, it takes much less
time (1800-500=1300sec) for it to cool down to the desired steady state. In the closed loop system,
the system seems to be critically damped since there is steady state error and no overshoot. We can
increase the gain and check the overshoot if a faster responding system with minimal steady state
error is desired.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 34 of 36
Heating Experiments:
In Figure4.4-13 and Figure4.4-14, the results Close Loop and Open Loop cooling are given
respectively. Since in both close loop and open loop system the fan is off before converging to steady
state, the rise time for both systems are roughly the same which is approximately 1000sec. We
observe intermittent electrical noise problems which do not impact the control system performance
significantly. In close loop control mode, around 2300sec mark, after the system reaches steady state,
the hairdryer is moved closer to the cup, in order to experiment increasing the effect of the
disturbance. 60 seconds later the temperature starts increasing. Roughly 100seconds later, the control
system starts to stabilize the increasing temperature and around 2500second mark, the temperature is
back to target temperature. This is a good indication that the system responds very slowly to varying
disturbances.
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 35 of 36
mc_prayer 7/3/2010
PIC16F877 Microcontroller Tutorial revA Page 36 of 36
Now that we have explored basics of the PIC16F877 and the compiler, we are ready to
conduct more advanced experiments. Good luck!
Note for Dr Oh`s student who will replicate this effort: Please don’t hesitate to contact me
from cy56@drexel.edu and phone:215 378 39 88 if you have any questions. Leave a message and
I definitely will return your call.
mc_prayer 7/3/2010