You are on page 1of 19

PIC16F877A Microcontroller Project Tutorial

1.Introduction to the PIC16F877A


2.How to Hook up the PIC so it will run?
3. Wht do the Pins !en?
". How to Pro#r! the PIC16F877A?
$. %iscellneous Ad&ice
6. '() *linker
7. Photoresistor Input
8. Anlo# Input +Photoresistor, -onr sensor, I. sensor, /0
1. )i#itl Input +-witches0
12. 3utput %ess#es to Co!puter -creen
11. -er&o %otor 3utput
12. )C %otor 3utput
----------------------------------------------------------------------------------------------------------------------------
1.Introduction to the PIC16F877A
Microcontroller PIC16F877A is one of the PICMicro Family microcontroller which is popular at
this moment, start from beginner until all professionals !ecause "ery easy using PIC16F877A an#
use F$A%& memory technology so that can be write'erase until thousan# times (he superiority
this )isc Microcontroller compare# to with other microcontroller 8'bit especially at a spee# of an#
his co#e compression PIC16F877A ha"e *+ pin by ,, path of I-.
PIC16F877A perfectly fits many uses, from automoti"e in#ustries an# controlling home appliances
to in#ustrial instruments, remote sensors, electrical #oor loc/s an# safety #e"ices It is also i#eal for
smart car#s as well as for battery supplie# #e"ices because of its low consumption 00P).M
memory ma/es it easier to apply microcontrollers to #e"ices where permanent storage of "arious
parameters is nee#e# 1co#es for transmitters, motor spee#, recei"er fre2uencies, etc3 $ow cost,
low consumption, easy han#ling an# fle4ibility ma/e PIC16F877A applicable e"en in areas where
microcontrollers ha# not pre"iously been consi#ere# 1e4ample5 timer functions, interface
replacement in larger systems, coprocessor applications, etc3In %ystem Programmability of this
chip 1along with using only two pins in #ata transfer3 ma/es possible the fle4ibility of a pro#uct,
after assembling an# testing ha"e been complete# (his capability can be use# to create assembly'
line pro#uction, to store calibration #ata a"ailable only after final testing, or it can be use# to
impro"e programs on finishe# pro#ucts
PIC16F877A Pin Diagram


2.How to Hook up the PIC so it will run?
%terils
PIC16F877A
!rea#boar#
6+ Megahert7 crystal oscillator
6 7'6+pf capacitor
!lac/ 8 re# wires
976 "olt battery
For use with a higher "oltage supply, you will nee# to use a "oltage regulator to con"ert the higher
"oltage to :" (ypically a 78+: is use# $oo/ up the #atasheet by searching ;oogle <78+: "oltage
regulator=
If you #on>t /now how these materials wor/ or what they loo/ li/e, loo/ it up online?
Circuit
1 Insert PIC on brea#boar# )efer to pin #iagram abo"e for pin numbers (o pre"ent confusion,
orient the PIC so the #imple on top faces the same way as the pin #iagram
6 Connect brea#boar# si#e strips ' power to power 1re# strips3, groun# to groun# 1blac/ strips3
@ust li/e the bottom 6 wires below (hose strips are now referre# to as <power= 8 <groun#=
, Power to Pin 1 1App3 B re# wire 1connecte# "ia *7/ )esistor in between3
* Power to Pin 11 1A##3 B re# wire
: ;roun# to Pin 16 1Ass3 B blac/ wire
6 Power to Pin ,6 1A##3 B re# wire
7 ;roun# to Pin ,1 1Ass3 B blac/ wire
8 Crystal .scillator to Pins 1, 8 1* 1.%C1, .%C63, the polarity #oesn>t matter 1polarity C which
of the 6 wires goes in which hole3
D Capacitor into power 8 groun# B ma/e sure the polarity is correct accor#ing to the #atasheet for
the capacitor If not it coul# #amage the circuit
1+ ;roun# to battery groun# 1'3 B blac/ wire
11 Power 1re#3 to battery power 1E3 B re# wire .r, sol#er that wire to a switch 8 run another wire
from the switch to battery power, so you can switch the power on 8 off

I4 usin# &olt#e re#ultor5
raw battery power wire goes into "oltage regulator input
brea#boar# groun# strip goes to battery groun#
"oltage regulator output wire 1E: "olts3 goes to brea#boar# power strip
"oltage regulator groun# wire goes to brea#boar# groun# strip
3. Wht do the Pins !en?
(he pins )!+')!7, )C+')C7, an# )F+')F7 are #igital I-. pins (he pins CCP1 an# CCP6,
which share locations with )C1 an# )C6, can be use# for a PGM signal 1see FC Motor tutorial3
(he pins AH+'AH7 are for analog I-. 1see Photoresistor tutorial3 (I an# )I are for #ebugging
I-. 1see .utput Messages to Computer tutorial3 (he remaining pins #eal with power-groun#, the
cloc/ signal, an# programmer I-.
A PIC is ma#e of se"eral <ports= 0ach port is #esignate# with a letter, )!+')!7 are a port )C+'
)C7 an# )F+')F7 are a port as well )A+')A: an# )0+')06 are also ports, but with fewer pins
%ome of these pins ha"e special purposes, but most can be use# as basic input-output pins
For e4ample, you can set pin )!+ to be either an input pin, or an output pin As an input pin, the
#igital "oltage on the pin can be rea# in For e4ample, if )!+ is connecte# to groun# 1+"3, then you
woul# rea# a #igital + If )!+ was connecte# to power 1:"3, then you woul# rea# a #igital 1
.n the other han#, if you wante# to set )!. as an output pin, you coul# choose to ma/e )!+ either
be :", or +" (his can be use#, for e4ample, to turn off or on a $0F, or to turn off or on a motor
". How to Pro#r! the PIC16F877A?
I omit step by step #etails of how to program the PIC because software always changes
As of spring 6+1+, the club uses software from CC% 1http5--wwwccsinfocom-3 Ge use the PCG
Compiler to write 8 compile programs an# CC% $oa# an# the ICF'J*+ programmer to write
programs to the PIC Always loa# a he4 file onto the PIC B the compile# machine language
program (he he4 file shoul# be locate# in the same fol#er as your proKect file
Lou nee# to connect the ICF>s 0thernet cable pins to the appropriate pins on the PIC in or#er to
loa# a program Lou also nee# to ha"e the PIC running 1ie un#er E:A power3 when programming
%ee <&ow #o I connect the CC% ICF to my own har#wareM= in the CC% FAN,
http5--wwwccsinfocom-fa2phpMpageCic#Oconnection
Fon Crouse, the 6+1+ presi#ent, ma#e custom programming boar#s to #o that (hey might still be
in the lab Lou can also cut up the wires on an 0thernet cable 8 connect them to the appropriate
pins on the brea#boar# If you #on>t want to ma/e your own #e"ice, you can buy what you nee# to
program the PIC
Another a"ailable programmer the club most li/ely ha# is the PicPit 6 (o program with a PicPit,
connect the pin mar/e# by the arrow on the PicPit #e"ice to the mclr pin Pin 6 1ne4t to the arrow3
is E:, Pin , is ;n#, Pin * is !7, Pin : is !6, an# Pin 6 is the optional #ebug pin P;M 1not
necessary to connect3 Also, connect the PIC to power 8 groun# 1the A##-Ass pins3 (o program a
PIC, connect the PIC IC to the PicPit, an# a usb mini cable from the PicPit to the computer (o use
the PicPit, run the PicPit A6 program on the #es/top of the PC (he first necessary step, if the
PicPit is configure# to program a #ifferent pic than your own, is to select the family of the PIC For
the 16f877A, you must select Qbase #e"iceQ in the #e"ice options In or#er to program a he4 file, it
must be loa#e# - importe# first Fo this by going to file an# importing your programRs he4 file
After the file is loa#e#, clic/ on the Qwrite he4Q button (his shoul# write your he4 file an# say it
programme# successfully (he program will automatically start running e"en with the PicPit
connecte# unless the -MC$) chec/bo4 is selecte#
Hote5 (he PicPit has the capability to supply power to the pic "ia the J%! bus It will
automatically #etect if the #e"ice is powere# when trying to program If you want to run your PIC
Kust from the J%!, you can select the QA## .nQ chec/bo4 (his can supply up to 1A of current at
:A Ma/e sure the "oltage number ne4t to the chec/bo4 is set to : B unless your processor #oes not
support :A (he PIC use# in this tutorial is a :" #e"ice
$. %iscellneous Ad&ice
Lou must ha"e the PIC wire# up 8 be able to compile 8 loa# programs to #o the other tutorials
(o run the other tutorials, follow these steps5
1 Connect circuit as #escribe#
6 Compile the e4ample co#e with whate"er compiler you are using
, $oa# the program onto the PIC
* (urn on the power to run the program
If you #on>t /now how to co#e something, loo/ at ol# PIC co#e on the robotics lab>s computers I
ha"e foun# that we nee# to sol"e similar programming problems year after year, especially when
we are tal/ing about sensor input 8 motor output Chances are goo# that somebo#y alrea#y wrote
it (he PIC MCJ C compiler manual is a goo# reference too
I recommen# that you try to be neat when attaching wires 8 other components Cut your wires so
that they lie flat on the brea#boar#, not stic/ing up Ma/e sure the bare en#s are long enough to
ma/e a soli# connection with the brea#boar# B you #on>t want a loose wireS !e or#erly when you
run wires to the "arious sensors 8 motors, too Lou will ha"e a confusing spaghetti forest when
you a## sensors, motors, switches, an# lights
Another goo# i#ea is to use wire colors consistently For e4ample, e"ery time you ha"e a wire
going to groun#, use the same color, usually blac/ (hat way, you can un#erstan# what it #oes with
a glance (his is important when you ha"e :+E wires on the boar# I use re# for power 1the E on the
battery 8 brea#boar#3, blac/ for groun# 1the '3, blue for inputs, an# green for outputs
Always #ouble chec/ your connections before flic/ing the switch If Kust one wire is in the wrong
position, it coul# #isable your circuit or burn something out
Lou can buil# your circuit with test $0Fs, to ma/e sure it is wor/ing when you turn it on I ha"e a
power light #irectly connecte# to the battery to so I /now when the circuit is on I also ha"e an $0F
controlle# by the PIC In my programs, I ma/e the PIC turn the $0F on when it starts running (hat
way I /now that the PIC is wor/ing correctly
(he Ano#e of the $0F goes to the E:" rail out of the regulator (he catho#e of the $0F goes to a
current limiting resistor B any "alue between 66+ an# 1/ is fine (he other en# of the resistor goes
to ;roun# (his $0F shoul# turn on at full brightness when the circuit is turne# on If the $0F
#oes not turn on, you may ha"e the $0F in bac/war#s If you /now the $0F is in correctly, an# it
#oes not turn on or turns on #imly when the power is turne# on, (J)H .FF L.J) CI)CJI(, an#
chec/ your wiring (his most li/ely means there is a short somewhere with your power rails
If you ha"e been running the robot for a while, chec/ the power battery "oltage If the "oltage gets
too low, the robot will beha"e strangely #ue to PIC #oing a <brownout=
6. '() *linker
%terils
Circuit from <Intro#uction to the PIC16F877A=
1++ .hm resistor
$0F
Circuit
1 Pin )!7 to resistor
6 )esistor to $0F
, $0F to groun#
Code
--all these T below set up the PIC
Tinclu#e U16F877Ah9
T#e"ice a#cC8
TFJ%0% H.GF( --Ho Gatch Fog (imer
TFJ%0% &% --&ighspee# .sc 9 *mh7
TFJ%0% PJ( --Power Jp (imer
TFJ%0% H.P).(0C( --Co#e not protecte# from rea#ing
TFJ%0% H.F0!J; --Ho Febug mo#e for ICF
TFJ%0% H.!).GH.J( --Ho brownout reset
TFJ%0% H.$AP --Ho low "oltage prgming, !,1PIC163 or !:1PIC183 use# for I-.
TFJ%0% H.CPF --Ho 00 protection
Tuse #elay1cloc/C6+++++++3 -- %ets crystal oscillator at 6+ megahert7
Tuse rs6,61bau#CD6++, 4mitCPIHOC6, in"ert3 --%ets up serial port output pin 8 bau# rate
--main program starts here
"oi# main13 V
--Infinite program loop starts $0F blin/s fore"er
while1true3V
outputOhigh1PIHO!73W --sen# a <1= to pin )!7, ma/ing )!7 <&igh= at :"
--this will turn on the $0F hoo/e# to )!7
#elayOms1:++3W --wait half a secon#, #elays for :++ms
outputOlow1PIHO!73W --sen# a <+= to pin )!7, ma/ing )!7 <$ow= at +"
#elayOms1:++3W --wait half a secon#, #elays for :++ms
X
X
6otes
Lou can easily a## more $0Fs an# ma/e them flash in #ifferent patterns
For more rea#able co#e, use
T#efine )0FO$0F PIHO!7
T#efine ;)00HO$0F PIHO!6
Y
Y
outputOhigh1)0FO$0F3W
outputOhigh1;)00HO$0F3W
7. Photoresistor Input
%terils
Circuit from <Intro#uction to the PIC16F877A=
Photoresistor
1P ohm resistor B or whate"er is appropriate for your photoresistor
Gire
Circuit B the #iagram on the right
1 !rea#boar# power 1Acc3 to 1P'ohm resistor
6 1P'ohm resistor to photoresistor power
, Photoresistor groun# to brea#boar# groun#
* Gire from between resistor 8 photoresistor to PIC pin AH+
If )1 is the photoresistor, the "oltage will increase with increasing light intensity If )6 is the
photoresistor, the "oltage will #ecrease with increasing light intensity
Code 4or sin#le photoresistor
--all these T below set up the PIC
Tinclu#e U16F877Ah9
T#e"ice a#cC8
TFJ%0% H.GF( --Ho Gatch Fog (imer
TFJ%0% &% --&ighspee# .sc 9 *mh7
TFJ%0% PJ( --Power Jp (imer
TFJ%0% H.P).(0C( --Co#e not protecte# from rea#ing
TFJ%0% H.F0!J; --Ho Febug mo#e for ICF
TFJ%0% H.!).GH.J( --Ho brownout reset
TFJ%0% H.$AP --Ho low "oltage prgming, !,1PIC163 or !:1PIC183 use# for I-.
TFJ%0% H.CPF --Ho 00 protection
Tuse #elay1cloc/C6+++++++3 --crystal oscillator at 6+++++++ hert7
Tuse rs6,61bau#CD6++, 4mitCPIHOC6, in"ert3 --serial port output pin 8 bau# rate
--run photoresistor signal wire to pin AH+
--connect $0F-resistor to pin )!7
"oi# main13V
int16 photoC+W --16 bit integer, safer than using int because
--int is only 8 bit which might lea# to o"erflow problems for a##, multiply
setupOa#c1AFCOC$.CPOIH(0)HA$3W --configure analog to #igiral con"erter
setupOa#cOports1A$$OAHA$.;3W --set pins AH+'AH7 to analog 1can rea# "alues from +'6::
instea# of Kust +,13
while1true3V --loop fore"er
setOa#cOchannel1+3W--set the pic to rea# from AH+
#elayOus16+3W--#elay 6+ microsecon#s to allow PIC to switch to analog channel +
photoCrea#Oa#c13W --rea# input from pin AH+5 +UCphotoUC6::
--turn on $0F when input 9 167, else turn off $0F
--Put finger o"er photoresistor 8 ta/e it off to see $0F turn on-off
--167 may not be the actual "alue that separates light from #ar/, so try #ifferent "alues
if1photo 9 1673V
outputOhigh1PIHO!73W
X
elseV
outputOlow1PIHO!73W
X
X
X
Code 4or !ultiple photoresistors
--all these T below set up the PIC
Tinclu#e U16F877Ah9
T#e"ice a#cC8
TFJ%0% H.GF( --Ho Gatch Fog (imer
TFJ%0% &% --&ighspee# .sc 9 *mh7
TFJ%0% PJ( --Power Jp (imer
TFJ%0% H.P).(0C( --Co#e not protecte# from rea#ing
TFJ%0% H.F0!J; --Ho Febug mo#e for ICF
TFJ%0% H.!).GH.J( --Ho brownout reset
TFJ%0% H.$AP --Ho low "oltage prgming, !,1PIC163 or !:1PIC183 use# for I-.
TFJ%0% H.CPF --Ho 00 protection
Tuse #elay1cloc/C6+++++++3 --crystal oscillator at 6+++++++ hert7
Tuse rs6,61bau#CD6++, 4mitCPIHOC6, in"ert3 --serial port output pin 8 bau# rate
--rea# input from , photoresistors
--run photoresistor signal wires to pin AH+, AH1, AH6
"oi# main13V
int16 photo+C+W --16 bit integer, safer than using int
--int is only 8 bit which might lea# to o"erflow problems for a##, multiply
int16 photo1C+W
int16 photo6C+W
setupOa#c1AFCOC$.CPOIH(0)HA$3W --configure analog to #igiral con"erter
setupOa#cOports1A$$OAHA$.;3W --set pins AH+'AH7 to analog 1can rea# "alues from +'6::
instea# of Kust +,13
while1true3V --loop fore"er
setOa#cOchannel1+3W--set the pic to rea# from AH+
#elayOus16+3W--#elay 6+ microsecon#s to allow PIC to switch to analog channel +
photo+Crea#Oa#c13W --rea# input from pin AH+5 +UCphotoUC6::
setOa#cOchannel113W--set the pic to rea# from AH1
#elayOus16+3W
photo1Crea#Oa#c13W
setOa#cOchannel163W --set the pic to rea# from AH6
#elayOus16+3W
photo6 C rea#Oa#c13W
--Lou coul# a## , $0Fs an# turn them on if photo+-1-6 9 167
--Kust as with co#e for single photoresistor
X
X
8. Anlo# Input +Photoresistor, -onr sensor, I. sensor, /0
Materials & Circuit
Fepen#s on the #e"ice For e4ample, a sonar sensor might ha"e , wires5 power, groun# 8 signal
Lou woul# connect the signal wire to one of pins AH+'AH7 %ee the #e"ice>s #atasheet for help
Code
--the rest of the co#e is i#entical to co#e for photoresistor input, e4cept here we call it Zsonar>
int16 sonar C +W
setOa#cOchannel113W--set the pic to rea# from AH1
#elayOus16+3W
sonarCrea#Oa#c13W
6otes
Lou can use the photoresistor tutorial co#e to rea# any sort of analog input #e"ice, as long as
the #e"ice is #esigne# so you can run a signal wire to the input pin1s3
)emember, only the AH+'AH7 pins allow analog input, an# you ha"e to call setupOa#c
functions 8 setOa#cOchannel as in photoresistor tutorial
)oughly, PIC will rea# + "olts as +, an# : "olts as 6::
1. )i#itl Input +-witches0
%terils
Circuit from <Intro#uction to the PIC16F877A=
$0F
%witch
1P ohm resistor
1+ or *7P ohm resistor
Gire
Circuit
Pin )!7 to 1Pohm resistor )esistor to $0F $0F to groun#
Pin )F1 to 1+-*7Pohm resistor )esistor to groun#
Pin )F1 to switch %witch to power
(his circuit has a <pull'#own resistor= Ghen the switch is close#, the PIC rea#s : "olts an# when it
is open it rea#s + "olts If we ha# no pull #own resistor then the pin woul# be in a <floating= state
when the switch was open, meaning that the "oltage can fluctuate Ge nee# to connect the pin to
groun# so the PIC always rea#s + when the switch is open If there was a wire connecte# to groun#
instea# of a resistor, it woul# create a short that burns up the circuit

Code
Tinclu#e U16F877Ah9
T#e"ice a#cC8
TFJ%0% H.GF( --Ho Gatch Fog (imer
TFJ%0% &% --&ighspee# .sc 9 *mh7
TFJ%0% PJ( --Power Jp (imer
TFJ%0% H.P).(0C( --Co#e not protecte# from rea#ing
TFJ%0% H.F0!J; --Ho Febug mo#e for ICF
TFJ%0% H.!).GH.J( --Ho brownout reset
TFJ%0% H.$AP --Ho low "oltage prgming, !,1PIC163 or !:1PIC183 use# for I-.
TFJ%0% H.CPF --Ho 00 protection
Tuse #elay1cloc/C6+++++++3 -- %ets crystal oscillator at 6+ megahert7
Tuse rs6,61bau#CD6++, 4mitCPIHOC6, in"ert3 -- serial port output 8 bau# rate
--close switch to see $0F turn on
--open switch to see $0F turn off
--if the pin is low 1+ "olts3 4 C +, or FA$%0
--if the pin is high 1: "olts3 4 C 1, or ()J0
"oi# main13 V
int 4 C +W
while1true3V
4 C input1PIHOF13W
if14CC13V
outputOhigh1PIHO!73W
X
elseV
outputOlow1PIHO!73W
X
X
X
12. 3utput %ess#es to Co!puter -creen
I ha"e use# 6 metho#s
1 PIC/it 6 Fe"elopment Programmer-Febugger %ee their instructions in the program or website
6 %erial port I use# &yper(erminal to rea# te4t from the serial port 8 #isplay on screen, but other
programs can #o that too Ma/e sure the bau# rate is D6++ bits - sec, Kust li/e in the PIC co#e5 Tuse
rs6,61bau#CD6++, 4mitCPIHOC6, in"ert3
-eril Port -teps +i4 usin# H7per8er!inl0
1 Gire serial port pin 6 to pin (I 1same as )C63 on the PIC
6 Gire serial port pin : to groun#
, Connect serial port to computer Jse a serial'J%! a#apter if computer has no serial port
* .pen &yper(erminal
: 0nter any name, select the icon that says <MCI=
6 Connect using C.M:-C.M1,-etc If there are multiple C.Ms5 pull out the J%!, reopen
&yper(erminal, 8 see which C.M has "anishe# (hat>s the one you want
7 %et bits per secon# C D6++
8 &yper(erminal is set up It will /eep rea#ing input until you close the program
D $oa# co#e onto PIC
1+ &it switch to turn on PIC
Code
Tinclu#e U16F877Ah9
T#e"ice a#cC8
TFJ%0% H.GF( --Ho Gatch Fog (imer
TFJ%0% &% --&ighspee# .sc 9 *mh7
TFJ%0% PJ( --Power Jp (imer
TFJ%0% H.P).(0C( --Co#e not protecte# from rea#ing
TFJ%0% H.F0!J; --Ho Febug mo#e for ICF
TFJ%0% H.!).GH.J( --Ho brownout reset
TFJ%0% H.$AP --Ho low "oltage prgming, !,1PIC163 or !:1PIC183 use# for I-.
TFJ%0% H.CPF --Ho 00 protection
Tuse #elay1cloc/C6+++++++3 -- %ets crystal oscillator at 6+ megahert7
Tuse rs6,61bau#CD6++, 4mitCPIHOC6, in"ert3 --%ets up serial port output pin 8 bau# rate
"oi# main13V
int 4 C +W
while1true3V
4 C 4 E 1W
--(his is an or#inary C language printf statement that will #isplay on the screen of your PC
--!ut, you nee# to open a special program to rea# serial port input, li/e &yper(erminal
--Ma/e sure the bau# rate of the program matches this co#e>s bau# rate 1D6++ bits - secon#3
printf1Qhello, 4C[#?r?nQ,43W --sen# this te4t to serial port
#elayOms11++3W --wait 1++ millisecon#s
X
X
11. -er&o %otor 3utput
%terils
Circuit from <Intro#uction to the PIC16F877A=
%er"o Motor 1this tutorial teste# on Paralla4 %tan#ar# %er"o3
Gire
Circuit
%ee ser"o motor #atasheet Most li/ely it will be
1 %er"o power wire to power
6 %er"o groun# wire to groun#
, %er"o signal wire to pin )F6 1coul# be any I-. pin on the PIC, Kust change the co#e3
Code
Tinclu#e U16F877Ah9
T#e"ice a#cC8
TFJ%0% H.GF( --Ho Gatch Fog (imer
TFJ%0% &% --&ighspee# .sc 9 *mh7
TFJ%0% PJ( --Power Jp (imer
TFJ%0% H.P).(0C( --Co#e not protecte# from rea#ing
TFJ%0% H.F0!J; --Ho Febug mo#e for ICF
TFJ%0% H.!).GH.J( --Ho brownout reset
TFJ%0% H.$AP --Ho low "oltage prgming, !,1PIC163 or !:1PIC183 use# for I-.
TFJ%0% H.CPF --Ho 00 protection
Tuse #elay1cloc/C6+++++++3 -- %ets crystal oscillator at 6+ megahert7
Tuse rs6,61bau#CD6++, 4mitCPIHOC6, in"ert3 --%ets up serial port output pin 8 bau# rate
--pin )F6 wire# to ser"o
--ser"o connecte# to power, groun#, an# the signal wire from the PIC
--this program steps the Paralla4 stan#ar# ser"o slowly from + to 18+ #egrees,
--then rushes bac/ to + #egrees to restart
--for co#e rea#ability, coul# use T#efine %0)A.OPIH PIHOF6 ''9 outputOhigh1%0)A.OPIH3W
"oi# main13V
int16 pulseOwi#th C 1+++W
int iW
while1true3V
--sen# short pulse to ser"o in#icating which angle it shoul# mo"e to
--for e4ample, for one type of ser"o, 1+++usC1ms in#icates + #egrees,
--while 6+++usC6ms in#icates 18+ #egrees
outputOhigh1PIHOF63W
#elayOus1pulseOwi#th3W --sen#ing : "olts to ser"o for pulseOwi#th microsecon#s
outputOlow1PIHOF63W
#elayOms16+3W --wait 6+ millisecon#s ' refresh cycle of typical ser"os
pulseOwi#th C pulseOwi#th E 1W --each time, ser"o is instructe# to mo"e a little farther
if1pulseOwi#th CC 6++13V --if ser"o reache# angle 18+, reset5 it will rush bac/ to angle +
pulseOwi#th C 1+++W
X
-\
If want ser"o to go to an angle 8 stay there, nee# to sen# the same pulse se"eral times 1:+ is goo#3
If only sen# 1 pulse, the motor wonRt get all the way there, an# it will stop, waiting for
another pulse 04ample below shows how to mo"e ser"o to D+ #egrees
for1iC1WiUC:+W iEE3V
outputOhigh1PIHOF63W
#elayOus11:++3W --want ser"o to mo"e to D+ #egrees
outputOlow1PIHOF63W
#elayOms16+3W
X
\-
X
X
6otes
.ther ser"os may ha"e #ifferent pulse wi#th re2uirements An#, they might go only D+ #egrees, or up
to ,6+ #egrees (here are also issues where ser"os #raw too much current or waste heat, which is
beyon# the scope of this tutorial
12. )C %otor 3utput
Hormally, you will use an & bri#ge for motor control (here are many internet articles about & bri#ges
if you want to learn how they wor/
%terils
Circuit from <Intro#uction to the PIC16F877A=
FC motor
& bri#ge chip
Gire

Circuit
Fepen#s on the type of & bri#ge I ha"e use# an ol# %H7:**1+ 2ua#ruple half'h #ri"er to run 6 motors
at once I also use# a custom & bri#ge circuit boar# #esigne# by club presi#ent Fon Crouse %ee the
#ata sheet
Important5 whene"er you change the #irection, you shoul# set all motor signal lines to + 1outputOlow3
(his pre"ents short circuits
Code 9 6otes
I will not inclu#e an entire program here, because each & bri#ge mo#el has #ifferent rules for motor
control (he #atasheet will tell you how to set the signal lines
(here are two maKor types of output signals on most & bri#ges
(he first signal turns the motor on or off It is often calle# the <0nable Pin= Ghene"er you want to
turn the motor on, respecti"ely off, type outputOhigh1M.(.)O0HA!$0OPIH3W an#
outputOlow1M.(.)O0HA!$0OPIH3W
(he secon# type are the signals in#icating which #irection the motor shoul# turn, whether the motor
free runs, an# whether the motor bra/es %en# + or 1 to those control signals5
outputOhigh1M.(.)OFI)0C(I.HOPIHO13, for e4ample
A simple illustration5
)irection Pin (n:le Pin .esults
+ 1outputOlow3 1 1outputOhigh3 .ne #irection
1 1outputOhigh3 1 1outputOhigh3 .ther #irection
I 1#oesn>t matter3 + 1outputOlow3 Free run 1slow #own 8 stop3
%ome & bri#ges will only let you set the motor #irection In that case, you bra/e by re"ersing the motor
#irection for a short time !eware5 the current #raw can spi/e if you #o this, an# the PIC might blac/
out !ut sometimes su##en #irection re"ersal is .PW you will ha"e to see for yourself
If you ha"e set the signals to ma/e the motor go left, it will /eep going left until you e4plicitly tell it to
stop 1or go right3
It is a goo# i#ea to write functions li/e <forwar#=, <bac/war#=, <left=, <right=, <stop= (hat way you
can Kust call the function 8 not ha"e to worry about the #etails
!elow are co#e e4cerpts from motor control for %H7:**1+ & bri#ge, controlling 6 motors Lour motor
control co#e will H.( be the same, because you will use a #ifferent & bri#ge or ha"e #ifferent nee#s
!ut it will be similar to this
T#efine $0F(OC.H().$O6A PIHOC6 --6A
T#efine $0F(OC.H().$O1A PIHOC1 --1A
T#efine $0F(OM.(.) PIHOF1 --1,60H
T#efine )I;&(OM.(.) PIHOF6 --,,*0H
T#efine )I;&(OC.H().$O*A PIHOF: --*A
T#efine )I;&(OC.H().$O,A PIHOF* --,A
int goingOforwar#W --current motion
int goingObac/war#W
int goingOleftW
int goingOrightW
int goingOhar#OleftW
int goingOhar#OrightW
Y
Y
--bra/e by re"ersing motor #irection, then turn motors off
"oi# halt13V
if 1goingOforwar#3
bac/war#13W
else if 1goingObac/war#3
forwar#13W
else if 1goingOleft3
right13W
else if 1goingOright3
left13W
else if 1goingOhar#Oleft3
har#Oright13W
else if 1goingOhar#Oright3
har#Oleft13W
#elayOms1:+3W --re"erse #irection for :+ ms, enough to ma/e robot stop
--but not long enough to ma/e it start mo"ing in opposite #irection
resetMotorControl13W
X
"oi# forwar#13V
resetMotorControl13W
outputOhigh1$0F(OC.H().$O1A3W
outputOhigh1)I;&(OC.H().$O,A3W
outputOhigh1$0F(OM.(.)3W
outputOhigh1)I;&(OM.(.)3W
goingOforwar#C1W
X
"oi# bac/war#13V
resetMotorControl13W
outputOhigh1$0F(OC.H().$O6A3W
outputOhigh1)I;&(OC.H().$O*A3W
outputOhigh1$0F(OM.(.)3W
outputOhigh1)I;&(OM.(.)3W
goingObac/war#C1W
X
--run only 1 motor for gentler turn
"oi# right13V
resetMotorControl13W
outputOhigh1)I;&(OC.H().$O,A3W
outputOhigh1)I;&(OM.(.)3W
goingOrightC1W
X
--run only 1 motor for gentler turn
"oi# left13V
resetMotorControl13W
outputOhigh1$0F(OC.H().$O1A3W
outputOhigh1$0F(OM.(.)3W
goingOleftC1W
X
--run one motor forwar# an# one bac/war# for sharper turn
"oi# har#Oleft13V
resetMotorControl13W
outputOhigh1$0F(OC.H().$O1A3W
outputOhigh1)I;&(OC.H().$O*A3W
outputOhigh1$0F(OM.(.)3W
outputOhigh1)I;&(OM.(.)3W
goingOhar#OleftC1W
X
--run one motor forwar# an# one bac/war# for sharper turn
"oi# har#Oright13V
resetMotorControl13W
outputOhigh1$0F(OC.H().$O6A3W
outputOhigh1)I;&(OC.H().$O,A3W
outputOhigh1$0F(OM.(.)3W
outputOhigh1)I;&(OM.(.)3W
goingOhar#OrightC1W
X
--%et all pins to &'bri#ge low to pre"ent short circuits
--)eset Qcurrent motionQ flags
"oi# resetMotorControl13V
outputOlow1$0F(OM.(.)3W
outputOlow1)I;&(OM.(.)3W
outputOlow1$0F(OC.H().$O1A3W
outputOlow1$0F(OC.H().$O6A3W
outputOlow1)I;&(OC.H().$O,A3W
outputOlow1)I;&(OC.H().$O*A3W
goingOforwar#C+W
goingObac/war#C+W
goingOleftC+W
goingOrightC+W
goingOhar#Oleft C +W
goingOhar#Oright C +W
#elayOus16+3W
X
-pecil 8opic5 PW%
How you /now how to run the motor at full spee# !ut a situation might arise where you want half
spee#, or ] spee#, etc In that case, you can sen# a PGM signal to the & bri#ge enable pin (here are
many internet articles about PGM if you want to learn how it wor/s
(o use PGM in motor control, replace outputOhigh1M.(.)O0HA!$0OPIH3 with setOpwm1O#uty143
Ma/e sure that the <enable pin= wire goes into the PIC pin CCP1 or CCP6 (hose are the only pins
that can ha"e PGM signal
PGM co#e e4cerpt5
setupOtimerO61(6OFIAO!LO*,6::,13W
setupOccp11CCPOPGM3W --set pin CCP1 as a PGM pin instea# of a regular I-. pin
setupOccp61CCPOPGM3W --set pin CCP6 as a PGM pin instea# of a regular I-. pin
while1true3V
--if setting #uty cycle 1setOpwm1O#uty3, only gi"e "alues between +'6::
--+ means the PGM signal is always at + 168 means it is 1 half the time 8 + the other half
--lea#ing to an a"erage# Q1-6 powerQ signal 6:: means it is 1 all the time, motor full spee#
setOpwm1O#uty16::3W --enable pin for motor 15 PIC pin CCP1
setOpwm6O#uty16::3W --enable pin for motor 65 PIC pin CCP6
#elayOms1,+++3W --full spee# for , secon#s
setOpwm1O#uty11683W
setOpwm6O#uty11683W
#elayOms1,+++3W --half spee# for , secon#s
setOpwm1O#uty1+3W
setOpwm6O#uty1+3W
#elayOms1,+++3W --motors off for , secon#s
X
--Jse PGM in your FC motor control co#e to change the spee# that the robot goes
forwar#-bac/war#-left-right
--call forwar#16::3W to go forwar# at full spee# Call forwar#11683W to go forwar# at half spee#
"oi# forwar#1int spee#3V
resetMotorControl13W
outputOhigh1$0F(OC.H().$O1A3W
outputOhigh1)I;&(OC.H().$O,A3W
setOpwm1O#uty1spee#3W --wire from PIC pin CCP1 to right motor enable pin
setOpwm6O#uty1spee#3W --wire from PIC pin CCP6 to left motor enable pin connecte# to
goingOforwar#C1W
X
6otes
If you want to use higher "oltage FC motors, or run motors at high spee#s, you might ha"e to construct
a special circuit to #eal with the e4tra current #raw, heat, high "oltage batteries, etc FC motors ha"e
their own set of issues to #eal with, which is beyon# the scope of this tutorial