Você está na página 1de 15

PIC 16F877A Tutorials

These tutorials demonstrate how to code simple input & output for your robot using the popular PIC
16F877 microcontroller! They help you get your sensors & motors wor"ing! The rest of the code is
up to you#
$uestions% &isit our website' http'((www!pitt!edu()sorc(robotics(
*mail' roboticsclubatpittsburgh+gmail!com
The tutorials assume basic "nowledge of programming and electronics! If you are new to
programming, you should learn the basics of C or C--! .ou should also "now rudimentary
electronics, and how to construct simple circuits!
I got most of this information from the PIC /C0 Compiler reference manual & the help of other
club members! .ou are welcome to edit or add new tutorials! Please email the club if you ma"e
another tutorial!
Introduction to the PIC16F877
How to hook up the PIC so it will run?
/aterials
PIC16F877
1readboard
23 /egahert4 crystal oscillator
2 7523pf capacitor
1lac" & red wires
67!2 7olt battery
For use with a higher 7oltage supply, you will need to use a 7oltage regulator to con7ert the
higher 7oltage to 87! Typically a 7838 is used! 9oo" up the datasheet by searching :oogle
;7838 7oltage regulator<!
If you don=t "now how these materials wor" or what they loo" li"e, loo" it up online!
Circuit
1! Insert PIC on breadboard! >efer to pin diagram abo7e for pin numbers! To pre7ent confusion,
orient the PIC so the dimple on top faces the same way as the pin diagram!
2! Connect breadboard side strips 5 power to power ?red strips@, ground to ground ?blac" strips@! Aust
li"e the bottom 2 wires below! Those strips are now referred to as ;power< & ;ground<!
B! Power to Pin 1 ?&pp@ C red wire ?connected 7ia D7" >esistor in between@
D! Power to Pin 11 ?&dd@ C red wire
8! :round to Pin 12 ?&ss@ C blac" wire
6! Power to Pin B2 ?&dd@ C red wire
7! :round to Pin B1 ?&ss@ C blac" wire
8! Crystal Escillator to Pins 1B & 1D ?EFC1, EFC2@, the polarity doesn=t matter! ?polarity G which of
the 2 wires goes in which hole@!
H! Capacitor into power & ground C ma"e sure the polarity is correct according to the datasheet for
the capacitor! If not it could damage the circuit!
13! :round to battery ground ?5@ C blac" wire!
11! Power ?red@ to battery power ?-@ C red wire! Er, solder that wire to a switch & run another wire
from the switch to battery power, so you can switch the power on & off!
If using 7oltage regulator'
raw battery power wire goes into 7oltage regulator input
breadboard ground strip goes to battery ground
7oltage regulator output wire ?-8 7olts@ goes to breadboard power strip!
7oltage regulator ground wire goes to breadboard ground strip
What do the pins mean?
The pins >135>17, >C35>C7, and >I35>I7 are digital I(E pins! The pins CCP1 and CCP2, which
share locations with >C1 and >C2, can be used for a PJ/ signal ?see IC /otor tutorial@! The pins
K35K7 are for analog I(E ?see Photoresistor tutorial@! TL and >L are for debugging I(E ?see
Eutput /essages to Computer tutorial@! The remaining pins deal with power(ground, the cloc" signal,
and programmer I(E!
PIC is made of se7eral ;ports!< *ach port is designated with a letter, >135>17 are a port! >C35>C7
and >I35>I7 are a port as well! >35>8 and >*35>*2 are also ports, but with fewer pins! Fome of
these pins ha7e special purposes, but most can be used as basic input(output pins!
For eMample, you can set pin >13 to be either an input pin, or an output pin! s an input pin, the
digital 7oltage on the pin can be read in! For eMample, if >13 is connected to ground ?37@, then you
would read a digital 3! If >13 was connected to power ?87@, then you would read a digital 1!
En the other hand, if you wanted to set >1E as an output pin, you could choose to ma"e >13 either
be 87, or 37! This can be used, for eMample, to turn off or on a 9*I, or to turn off or on a motor!
How to program the PIC?
I omit step by step details of how to program the PIC because software always changes!
s of spring 2313, the club uses software from CCF ?http'((www!ccsinfo!com(@! Je use the PCJ
Compiler to write & compile programs and CCF 9oad and the ICI50D3 programmer to write
programs to the PIC! lways load a !heM file onto the PIC C the compiled machine language
program! The heM file should be located in the same folder as your proNect file!
.ou need to connect the ICI=s *thernet cable pins to the appropriate pins on the PIC in order to load
a program! .ou also need to ha7e the PIC running ?i!e! under -8& power@ when programming!
Fee ;Oow do I connect the CCF ICI to my own hardware%< in the CCF F$,
http'((www!ccsinfo!com(faP!php%pageGicdQconnection!
Ion Crouse, the 2313 president, made custom programming boards to do that! They might still be in
the lab! .ou can also cut up the wires on an *thernet cable & connect them to the appropriate pins
on the breadboard! If you don=t want to ma"e your own de7ice, you can buy what you need to
program the PIC!
nother a7ailable programmer the club most li"ely had is the PicRit 2! To program with a PicRit,
connect the pin mar"ed by the arrow on the PicRit de7ice to the mclr pin! Pin 2 ?neMt to the arrow@ is
-8, Pin B is :nd, Pin D is 17, Pin 8 is 16, and Pin 6 is the optional debug pin P:/ ?not necessary to
connect@! lso, connect the PIC to power & ground ?the &dd(&ss pins@! To program a PIC, connect
the PIC IC to the PicRit, and a usb mini cable from the PicRit to the computer! To use the PicRit, run
the PicRit &2 program on the des"top of the PC! The first necessary step, if the PicRit is configured
to program a different pic than your own, is to select the family of the PIC! For the 16f877, you must
select Sbase de7iceS in the de7ice options! In order to program a heM file, it must be loaded ( imported
first! Io this by going to file and importing your programTs heM file! fter the file is loaded, clic" on the
Swrite heMS button! This should write your heM file and say it programmed successfully! The program
will automatically start running e7en with the PicRit connected unless the (/C9> chec"boM is
selected!
Kote' The PicRit has the capability to supply power to the pic 7ia the 0F1 bus! It will automatically
detect if the de7ice is powered when trying to program! If you want to run your PIC Nust from the
0F1, you can select the S&dd EnS chec"boM! This can supply up to 1 of current at 8&! /a"e sure
the 7oltage number neMt to the chec"boM is set to 8 C unless your processor does not support 8&!
The PIC used in this tutorial is a 87 de7ice!
is!ellaneous Ad"i!e
.ou must ha7e the PIC wired up & be able to compile & load programs to do the other tutorials!
To run the other tutorials, follow these steps'
1! Connect circuit as described!
2! Compile the eMample code with whate7er compiler you are using!
B! 9oad the program onto the PIC!
D! Turn on the power to run the program
If you don=t "now how to code something, loo" at old PIC code on the robotics lab=s computers!
I ha7e found that we need to sol7e similar programming problems year after year, especially when we
are tal"ing about sensor input & motor output! Chances are good that somebody already wrote it!
The PIC /C0 C compiler manual is a good reference too!
I recommend that you try to be neat when attaching wires & other components! Cut your wires
so that they lie flat on the breadboard, not stic"ing up! /a"e sure the bare ends are long enough to
ma"e a solid connection with the breadboard C you don=t want a loose wire# 1e orderly when you run
wires to the 7arious sensors & motors, too! .ou will ha7e a confusing spaghetti forest when you add
sensors, motors, switches, and lights!
nother good idea is to use wire colors consistently! For eMample, e7ery time you ha7e a wire
going to ground, use the same color, usually blac"! That way, you can understand what it does with a
glance! This is important when you ha7e 83- wires on the board! I use red for power ?the - on the
battery & breadboard@, blac" for ground ?the 5@, blue for inputs, and green for outputs!
lways double chec" your connections before flic"ing the switch! If Nust one wire is in the
wrong position, it could disable your circuit or burn something out!
.ou can build your circuit with test 9*Is, to ma"e sure it is wor"ing when you turn it on! I ha7e
a power light directly connected to the battery to so I "now when the circuit is on! I also ha7e an 9*I
controlled by the PIC! In my programs, I ma"e the PIC turn the 9*I on when it starts running! That
way I "now that the PIC is wor"ing correctly!
The node of the 9*I goes to the -87 rail out of the regulator! The cathode of the 9*I goes
to a current limiting resistor C any 7alue between 223 and 1" is fine! The other end of the resistor
goes to :round! This 9*I should turn on at full brightness when the circuit is turned on! If the 9*I
does not turn on, you may ha7e the 9*I in bac"wards! If you "now the 9*I is in correctly, and it
does not turn on or turns on dimly when the power is turned on, T0>K EFF .E0> CI>C0IT, and
chec" your wiring! This most li"ely means there is a short somewhere with your power rails!
If you ha7e been running the robot for a while, chec" the power battery 7oltage! If the 7oltage
gets too low, the robot will beha7e strangely due to PIC doing a ;brownout<!
9*I 1lin"er
/aterials
Circuit from ;Introduction to the PIC16F877<
133 Ehm resistor
9*I
Circuit
1! Pin >17 to resistor
2! >esistor to 9*I
B! 9*I to ground
Code
((all these U below set up the PIC
Uinclude V16F877!h6
Ude7ice adcG8
UF0F*F KEJIT ((Ko Jatch Iog Timer
UF0F*F OF ((Oighspeed Esc 6 Dmh4
UF0F*F P0T ((Power 0p Timer
UF0F*F KEP>ET*CT ((Code not protected from reading
UF0F*F KEI*10: ((Ko Iebug mode for ICI
UF0F*F KE1>EJKE0T ((Ko brownout reset
UF0F*F KE9&P ((Ko low 7oltage prgming, 1B?PIC16@ or 18?PIC18@ used for I(E
UF0F*F KECPI ((Ko ** protection
Uuse delay?cloc"G23333333@ (( Fets crystal oscillator at 23 megahert4
Uuse rs2B2?baudGH633, MmitGPIKQC6, in7ert@ ((Fets up serial port output pin & baud rate
((main program starts here
7oid main?@ W
((Infinite program loop starts! 9*I blin"s fore7er!
while?true@W
outputQhigh?PIKQ17@X ((send a ;1< to pin >17, ma"ing >17 ;Oigh< at 87
((this will turn on the 9*I hoo"ed to >17
delayQms?833@X ((wait half a second, delays for 833ms
outputQlow?PIKQ17@X ((send a ;3< to pin >17, ma"ing >17 ;9ow< at 37
delayQms?833@X ((wait half a second, delays for 833ms
Y
Y
Kotes
.ou can easily add more 9*Is and ma"e them flash in different patterns!
For more readable code, use
Udefine >*IQ9*I PIKQ17
Udefine :>**KQ9*I PIKQ16
Z
Z
outputQhigh?>*IQ9*I@X
outputQhigh?:>**KQ9*I@X
Photoresistor Input
/aterials
Circuit from ;Introduction to the PIC16F877<
Photoresistor
1R ohm resistor C or whate7er is appropriate for your photoresistor
Jire
Circuit C the diagram on the right
1! 1readboard power ?&cc@ to 1R5ohm resistor!
2! 1R5ohm resistor to photoresistor power!
B! Photoresistor ground to breadboard ground!
D! Jire from between resistor & photoresistor to PIC pin K3!
If >1 is the photoresistor, the 7oltage will increase with increasing light intensity! If >2 is the
photoresistor, the 7oltage will decrease with increasing light intensity!
Code for single photoresistor
((all these U below set up the PIC
Uinclude V16F877!h6
Ude7ice adcG8
UF0F*F KEJIT ((Ko Jatch Iog Timer
UF0F*F OF ((Oighspeed Esc 6 Dmh4
UF0F*F P0T ((Power 0p Timer
UF0F*F KEP>ET*CT ((Code not protected from reading
UF0F*F KEI*10: ((Ko Iebug mode for ICI
UF0F*F KE1>EJKE0T ((Ko brownout reset
UF0F*F KE9&P ((Ko low 7oltage prgming, 1B?PIC16@ or 18?PIC18@ used for I(E
UF0F*F KECPI ((Ko ** protection
Uuse delay?cloc"G23333333@ ((crystal oscillator at 23333333 hert4
Uuse rs2B2?baudGH633, MmitGPIKQC6, in7ert@ ((serial port output pin & baud rate
((run photoresistor signal wire to pin K3
((connect 9*I(resistor to pin >17
7oid main?@W
int16 photoG3X ((16 bit integer, safer than using int because
((int is only 8 bit which might lead to o7erflow problems for add, multiply

setupQadc?ICQC9ECRQIKT*>K9@X ((configure analog to digiral con7erter
setupQadcQports?99QK9E:@X ((set pins K35K7 to analog ?can read 7alues from 35288 instead of Nust 3,1@
while?true@W ((loop fore7er
setQadcQchannel?3@X((set the pic to read from K3
delayQus?23@X((delay 23 microseconds to allow PIC to switch to analog channel 3
photoGreadQadc?@X ((read input from pin K3' 3VGphotoVG288

((turn on 9*I when input 6 127, else turn off 9*I
((Put finger o7er photoresistor & ta"e it off to see 9*I turn on(off
((127 may not be the actual 7alue that separates light from dar", so try different 7alues
if?photo 6 127@W
outputQhigh?PIKQ17@X
Y
elseW
outputQlow?PIKQ17@X
Y
Y
Y
Code for multiple photoresistors
((all these U below set up the PIC
Uinclude V16F877!h6
Ude7ice adcG8
UF0F*F KEJIT ((Ko Jatch Iog Timer
UF0F*F OF ((Oighspeed Esc 6 Dmh4
UF0F*F P0T ((Power 0p Timer
UF0F*F KEP>ET*CT ((Code not protected from reading
UF0F*F KEI*10: ((Ko Iebug mode for ICI
UF0F*F KE1>EJKE0T ((Ko brownout reset
UF0F*F KE9&P ((Ko low 7oltage prgming, 1B?PIC16@ or 18?PIC18@ used for I(E
UF0F*F KECPI ((Ko ** protection
Uuse delay?cloc"G23333333@ ((crystal oscillator at 23333333 hert4
Uuse rs2B2?baudGH633, MmitGPIKQC6, in7ert@ ((serial port output pin & baud rate
((read input from B photoresistors
((run photoresistor signal wires to pin K3, K1, K2
7oid main?@W
int16 photo3G3X ((16 bit integer, safer than using int
((int is only 8 bit which might lead to o7erflow problems for add, multiply
int16 photo1G3X
int16 photo2G3X

setupQadc?ICQC9ECRQIKT*>K9@X ((configure analog to digiral con7erter
setupQadcQports?99QK9E:@X ((set pins K35K7 to analog ?can read 7alues from 35288 instead of Nust 3,1@
while?true@W ((loop fore7er
setQadcQchannel?3@X((set the pic to read from K3
delayQus?23@X((delay 23 microseconds to allow PIC to switch to analog channel 3
photo3GreadQadc?@X ((read input from pin K3' 3VGphotoVG288

setQadcQchannel?1@X((set the pic to read from K1
delayQus?23@X
photo1GreadQadc?@X

setQadcQchannel?2@X ((set the pic to read from K2
delayQus?23@X
photo2 G readQadc?@X

((.ou could add B 9*Is and turn them on if photo3(1(2 6 127
((Nust as with code for single photoresistor
Y
Y
nalog Input ?Photoresistor, Fonar sensor, I> sensor, Z@
/aterials & Circuit
Iepends on the de7ice! For eMample, a sonar sensor might ha7e B wires' power, ground & signal!
.ou would connect the signal wire to one of pins K35K7! Fee the de7ice=s datasheet for help!
Code
((the rest of the code is identical to code for photoresistor input, eMcept here we call it [sonar=
int16 sonar G 3X
setQadcQchannel?1@X((set the pic to read from K1
delayQus?23@X
sonarGreadQadc?@X
Kotes
.ou can use the photoresistor tutorial code to read any sort of analog input de7ice, as long as the
de7ice is designed so you can run a signal wire to the input pin?s@!
>emember, only the K35K7 pins allow analog input, and you ha7e to call setupQadc functions &
setQadcQchannel as in photoresistor tutorial!
>oughly, PIC will read 3 7olts as 3, and 8 7olts as 288!
Iigital Input ?Fwitches@
/aterials
Circuit from ;Introduction to the PIC16F877<
9*I
Fwitch
1R ohm resistor
13 or D7R ohm resistor
Jire
Circuit
Pin >17 to 1Rohm resistor! >esistor to 9*I! 9*I to ground!
Pin >I1 to 13(D7Rohm resistor! >esistor to ground!
Pin >I1 to switch! Fwitch to power!
This circuit has a ;pull5down resistor<! Jhen the switch is closed, the PIC reads 8 7olts and when it is
open it reads 3 7olts! If we had no pull down resistor then the pin would be in a ;floating< state when
the switch was open, meaning that the 7oltage can fluctuate! Je need to connect the pin to ground
so the PIC always reads 3 when the switch is open! If there was a wire connected to ground instead
of a resistor, it would create a short that burns up the circuit!
Code
Uinclude V16F877!h6
Ude7ice adcG8
UF0F*F KEJIT ((Ko Jatch Iog Timer
UF0F*F OF ((Oighspeed Esc 6 Dmh4
UF0F*F P0T ((Power 0p Timer
UF0F*F KEP>ET*CT ((Code not protected from reading
UF0F*F KEI*10: ((Ko Iebug mode for ICI
UF0F*F KE1>EJKE0T ((Ko brownout reset
UF0F*F KE9&P ((Ko low 7oltage prgming, 1B?PIC16@ or 18?PIC18@ used for I(E
UF0F*F KECPI ((Ko ** protection
Uuse delay?cloc"G23333333@ (( Fets crystal oscillator at 23 megahert4
Uuse rs2B2?baudGH633, MmitGPIKQC6, in7ert@ (( serial port output & baud rate
((close switch to see 9*I turn on
((open switch to see 9*I turn off
((if the pin is low ?3 7olts@ M G 3, or F9F*
((if the pin is high ?8 7olts@ M G 1, or T>0*
7oid main?@ W
int M G 3X
while?true@W
M G input?PIKQI1@X
if?MGG1@W
outputQhigh?PIKQ17@X
Y
elseW
outputQlow?PIKQ17@X
Y
Y
Y
Eutput messages to computer screen
I ha7e used 2 methods!
1! PIC"it 2 Ie7elopment Programmer(Iebugger! Fee their instructions in the program or website!
2! Ferial port! I used OyperTerminal to read teMt from the serial port & display on screen, but other
programs can do that too! /a"e sure the baud rate is H633 bits ( sec, Nust li"e in the PIC code' Uuse
rs2B2?baudGH633, MmitGPIKQC6, in7ert@
Ferial Port Fteps ?if using OyperTerminal@
1! Jire serial port pin 2 to pin TL ?same as >C6@ on the PIC!
2! Jire serial port pin 8 to ground!
B! Connect serial port to computer! 0se a serial50F1 adapter if computer has no serial port!
D! Epen OyperTerminal!
8! *nter any name, select the icon that says ;/CI<
6! Connect using CE/8(CE/1B(etc! If there are multiple CE/s' pull out the 0F1, reopen
OyperTerminal, & see which CE/ has 7anished! That=s the one you want!
7! Fet bits per second G H633!
8! OyperTerminal is set up! It will "eep reading input until you close the program!
H! 9oad code onto PIC
13! Oit switch to turn on PIC
Code
Uinclude V16F877!h6
Ude7ice adcG8
UF0F*F KEJIT ((Ko Jatch Iog Timer
UF0F*F OF ((Oighspeed Esc 6 Dmh4
UF0F*F P0T ((Power 0p Timer
UF0F*F KEP>ET*CT ((Code not protected from reading
UF0F*F KEI*10: ((Ko Iebug mode for ICI
UF0F*F KE1>EJKE0T ((Ko brownout reset
UF0F*F KE9&P ((Ko low 7oltage prgming, 1B?PIC16@ or 18?PIC18@ used for I(E
UF0F*F KECPI ((Ko ** protection
Uuse delay?cloc"G23333333@ (( Fets crystal oscillator at 23 megahert4
Uuse rs2B2?baudGH633, MmitGPIKQC6, in7ert@ ((Fets up serial port output pin & baud rate
7oid main?@W
int M G 3X
while?true@W
M G M - 1X
((This is an ordinary C language printf statement that will display on the screen of your PC!
((1ut, you need to open a special program to read serial port input, li"e OyperTerminal!
((/a"e sure the baud rate of the program matches this code=s baud rate ?H633 bits ( second@
printf?Shello, MG\d]r]nS,M@X ((send this teMt to serial port
delayQms?133@X ((wait 133 milliseconds
Y
Y
Fer7o /otor Eutput
/aterials
Circuit from ;Introduction to the PIC16F877<
Fer7o /otor ?this tutorial tested on ParallaM Ftandard Fer7o@
Jire
Circuit
Fee ser7o motor datasheet! /ost li"ely it will be
1! Fer7o power wire to power
2! Fer7o ground wire to ground
B! Fer7o signal wire to pin >I2! ?could be any I(E pin on the PIC, Nust change the code@
Code
Uinclude V16F877!h6
Ude7ice adcG8
UF0F*F KEJIT ((Ko Jatch Iog Timer
UF0F*F OF ((Oighspeed Esc 6 Dmh4
UF0F*F P0T ((Power 0p Timer
UF0F*F KEP>ET*CT ((Code not protected from reading
UF0F*F KEI*10: ((Ko Iebug mode for ICI
UF0F*F KE1>EJKE0T ((Ko brownout reset
UF0F*F KE9&P ((Ko low 7oltage prgming, 1B?PIC16@ or 18?PIC18@ used for I(E
UF0F*F KECPI ((Ko ** protection
Uuse delay?cloc"G23333333@ (( Fets crystal oscillator at 23 megahert4
Uuse rs2B2?baudGH633, MmitGPIKQC6, in7ert@ ((Fets up serial port output pin & baud rate
((pin >I2 wired to ser7o
((ser7o connected to power, ground, and the signal wire from the PIC
((this program steps the ParallaM standard ser7o slowly from 3 to 183 degrees,
((then rushes bac" to 3 degrees to restart!
((for code readability, could use Udefine F*>&EQPIK PIKQI2 556 outputQhigh?F*>&EQPIK@X
7oid main?@W
int16 pulseQwidth G 1333X
int iX

while?true@W
((send short pulse to ser7o indicating which angle it should mo7e to!
((for eMample, for one type of ser7o, 1333usG1ms indicates 3 degrees,
((while 2333usG2ms indicates 183 degrees!
outputQhigh?PIKQI2@X
delayQus?pulseQwidth@X ((sending 8 7olts to ser7o for pulseQwidth microseconds
outputQlow?PIKQI2@X
delayQms?23@X ((wait 23 milliseconds 5 refresh cycle of typical ser7os

pulseQwidth G pulseQwidth - 1X ((each time, ser7o is instructed to mo7e a little farther
if?pulseQwidth GG 2331@W ((if ser7o reached angle 183, reset' it will rush bac" to angle 3
pulseQwidth G 1333X
Y

(^
If want ser7o to go to an angle & stay there, need to send the same pulse se7eral times! ?83 is good@
If only send 1 pulse, the motor wonTt get all the way there, and it will stop, waiting for
another pulse! *Mample below shows how to mo7e ser7o to H3 degrees!
for?iG1XiVG83X i--@W
outputQhigh?PIKQI2@X
delayQus?1833@X ((want ser7o to mo7e to H3 degrees!
outputQlow?PIKQI2@X
delayQms?23@X
Y
^(
Y
Y
Kotes
Ether ser7os may ha7e different pulse width rePuirements! nd, they might go only H3 degrees, or
up to B63 degrees! There are also issues where ser7os draw too much current or waste heat, which
is beyond the scope of this tutorial!
IC /otor Eutput
Kormally, you will use an O bridge for motor control! There are many internet articles about O bridges
if you want to learn how they wor"!
/aterials
Circuit from ;Introduction to the PIC16F877<
IC motor
O bridge chip
Jire
Circuit
Iepends on the type of O bridge! I ha7e used an old FK78DD13 Puadruple half5h dri7er to run 2
motors at once! I also used a custom O bridge circuit board designed by club president Ion Crouse!
Fee the data sheet!
Important' whene7er you change the direction, you should set all motor signal lines to 3 ?outputQlow@!
This pre7ents short circuits!
Code & Kotes
I will not include an entire program here, because each O bridge model has different rules for motor
control! The datasheet will tell you how to set the signal lines!
There are two maNor types of output signals on most O bridges!
The first signal turns the motor on or off! It is often called the ;*nable Pin<! Jhene7er you want to
turn the motor on, respecti7ely off, type outputQhigh?/ETE>Q*K19*QPIK@X and outputQlow?/ETE>Q*K19*QPIK@X
The second type are the signals indicating which direction the motor should turn, whether the motor
free runs, and whether the motor bra"es! Fend 3 or 1 to those control signals'
outputQhigh?/ETE>QII>*CTIEKQPIKQ1@, for eMample!
simple illustration'
#ire!tion Pin $na%le Pin &esults
3 ?outputQlow@ 1
?outputQhigh@
Ene direction
1 ?outputQhigh@ 1
?outputQhigh@
Ether direction
L ?doesn=t
matter@
3 ?outputQlow@ Free run ?slow down &
stop@
Fome O bridges will only let you set the motor direction! In that case, you bra"e by re7ersing the
motor direction for a short time! 1eware' the current draw can spi"e if you do this, and the PIC might
blac" out! 1ut sometimes sudden direction re7ersal is ERX you will ha7e to see for yourself!
If you ha7e set the signals to ma"e the motor go left, it will "eep going left until you eMplicitly tell it to
stop ?or go right@!
It is a good idea to write functions li"e ;forward<, ;bac"ward<, ;left<, ;right<, ;stop<! That way you can
Nust call the function & not ha7e to worry about the details!
1elow are code eMcerpts from motor control for FK78DD13 O bridge, controlling 2 motors! .our motor
control code will KET be the same, because you will use a different O bridge or ha7e different needs!
1ut it will be similar to this!
Udefine 9*FTQCEKT>E9Q2 PIKQC2 ((2
Udefine 9*FTQCEKT>E9Q1 PIKQC1 ((1
Udefine 9*FTQ/ETE> PIKQI1 ((1,2*K
Udefine >I:OTQ/ETE> PIKQI2 ((B,D*K
Udefine >I:OTQCEKT>E9QD PIKQI8 ((D
Udefine >I:OTQCEKT>E9QB PIKQID ((B
int goingQforwardX ((current motion
int goingQbac"wardX
int goingQleftX
int goingQrightX
int goingQhardQleftX
int goingQhardQrightX
Z
Z
((bra"e by re7ersing motor direction, then turn motors off
7oid halt?@W
if ?goingQforward@
bac"ward?@X
else if ?goingQbac"ward@
forward?@X
else if ?goingQleft@
right?@X
else if ?goingQright@
left?@X
else if ?goingQhardQleft@
hardQright?@X
else if ?goingQhardQright@
hardQleft?@X
delayQms?83@X ((re7erse direction for 83 ms, enough to ma"e robot stop
((but not long enough to ma"e it start mo7ing in opposite direction
reset/otorControl?@X
Y
7oid forward?@W
reset/otorControl?@X
outputQhigh?9*FTQCEKT>E9Q1@X
outputQhigh?>I:OTQCEKT>E9QB@X
outputQhigh?9*FTQ/ETE>@X
outputQhigh?>I:OTQ/ETE>@X
goingQforwardG1X
Y
7oid bac"ward?@W
reset/otorControl?@X
outputQhigh?9*FTQCEKT>E9Q2@X
outputQhigh?>I:OTQCEKT>E9QD@X
outputQhigh?9*FTQ/ETE>@X
outputQhigh?>I:OTQ/ETE>@X
goingQbac"wardG1X
Y
((run only 1 motor for gentler turn
7oid right?@W
reset/otorControl?@X
outputQhigh?>I:OTQCEKT>E9QB@X
outputQhigh?>I:OTQ/ETE>@X
goingQrightG1X
Y
((run only 1 motor for gentler turn
7oid left?@W
reset/otorControl?@X
outputQhigh?9*FTQCEKT>E9Q1@X
outputQhigh?9*FTQ/ETE>@X
goingQleftG1X
Y
((run one motor forward and one bac"ward for sharper turn
7oid hardQleft?@W
reset/otorControl?@X
outputQhigh?9*FTQCEKT>E9Q1@X
outputQhigh?>I:OTQCEKT>E9QD@X
outputQhigh?9*FTQ/ETE>@X
outputQhigh?>I:OTQ/ETE>@X
goingQhardQleftG1X
Y
((run one motor forward and one bac"ward for sharper turn
7oid hardQright?@W
reset/otorControl?@X
outputQhigh?9*FTQCEKT>E9Q2@X
outputQhigh?>I:OTQCEKT>E9QB@X
outputQhigh?9*FTQ/ETE>@X
outputQhigh?>I:OTQ/ETE>@X
goingQhardQrightG1X
Y
((Fet all pins to O5bridge low to pre7ent short circuits
((>eset Scurrent motionS flags
7oid reset/otorControl?@W
outputQlow?9*FTQ/ETE>@X
outputQlow?>I:OTQ/ETE>@X
outputQlow?9*FTQCEKT>E9Q1@X
outputQlow?9*FTQCEKT>E9Q2@X
outputQlow?>I:OTQCEKT>E9QB@X
outputQlow?>I:OTQCEKT>E9QD@X
goingQforwardG3X
goingQbac"wardG3X
goingQleftG3X
goingQrightG3X
goingQhardQleft G 3X
goingQhardQright G 3X
delayQus?23@X
Y
Fpecial Topic' PJ/
Kow you "now how to run the motor at full speed! 1ut a situation might arise where you want half
speed, or _ speed, etc! In that case, you can send a PJ/ signal to the O bridge enable pin! There
are many internet articles about PJ/ if you want to learn how it wor"s!
To use PJ/ in motor control, replace outputQhigh?/ETE>Q*K19*QPIK@ with setQpwm1Qduty?M@ ! /a"e sure that the
;enable pin< wire goes into the PIC pin CCP1 or CCP2! Those are the only pins that can ha7e PJ/
signal!
PJ/ code eMcerpt'
setupQtimerQ2?T2QII&Q1.QD,288,1@X
setupQccp1?CCPQPJ/@X ((set pin CCP1 as a PJ/ pin instead of a regular I(E pin
setupQccp2?CCPQPJ/@X ((set pin CCP2 as a PJ/ pin instead of a regular I(E pin
while?true@W
((if setting duty cycle ?setQpwm1Qduty@, only gi7e 7alues between 35288
((3 means the PJ/ signal is always at 3! 128 means it is 1 half the time & 3 the other half
((leading to an a7eraged S1(2 powerS signal! 288 means it is 1 all the time, motor full speed
setQpwm1Qduty?288@X ((enable pin for motor 1' PIC pin CCP1
setQpwm2Qduty?288@X ((enable pin for motor 2' PIC pin CCP2
delayQms?B333@X ((full speed for B seconds
setQpwm1Qduty?128@X
setQpwm2Qduty?128@X
delayQms?B333@X ((half speed for B seconds
setQpwm1Qduty?3@X
setQpwm2Qduty?3@X
delayQms?B333@X ((motors off for B seconds
Y
((0se PJ/ in your IC motor control code to change the speed that the robot goes forward(bac"ward(left(right
((call forward?288@X to go forward at full speed! Call forward?128@X to go forward at half speed!
7oid forward?int speed@W
reset/otorControl?@X
outputQhigh?9*FTQCEKT>E9Q1@X
outputQhigh?>I:OTQCEKT>E9QB@X
setQpwm1Qduty?speed@X ((wire from PIC pin CCP1 to right motor enable pin
setQpwm2Qduty?speed@X ((wire from PIC pin CCP2 to left motor enable pin connected to
goingQforwardG1X
Y
Kotes
If you want to use higher 7oltage IC motors, or run motors at high speeds, you might ha7e to
construct a special circuit to deal with the eMtra current draw, heat, high 7oltage batteries, etc! IC
motors ha7e their own set of issues to deal with, which is beyond the scope of this tutorial!

Você também pode gostar