Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

PIC 16F877A Tutorials for Pitt Robotics Club

Lukas Hoffmann, 2010


These tutorials demonstrate how to code simple input & output for your robot using the popular P ! 1"#$%%& microcontroller' They help you get your sensors & motors working' The rest of the code is up to you( )uestions* +isit our website, http,--www'pitt'edu-.sorc-robotics/mail, roboticsclubatpittsburgh0gmail'com The tutorials assume basic knowledge of programming and electronics' f you are new to programming, you should learn the basics of ! or !11' 2ou should also know rudimentary electronics, and how to construct simple circuits' got most of this information from the P ! 3!4 !ompiler reference manual & the help of other club members' 2ou are welcome to edit or add new tutorials' Please email the club if you make another tutorial'

ntroduction to the P !1"#$%%&

How to hook up the PIC so it will run? 3aterials P !1"#$%%& 5readboard 20 3egahert6 crystal oscillator 2 %720pf capacitor 5lack & red wires 8%'2 9olt battery #or use with a higher 9oltage supply, you will need to use a 9oltage regulator to con9ert the higher 9oltage to :9' Typically a %$0: is used' Look up the datasheet by searching ;oogle <%$0: 9oltage regulator='

f you don>t know how these materials work or what they look like, look it up online'

!ircuit 1' nsert P ! on breadboard' ?efer to pin diagram abo9e for pin numbers' To pre9ent confusion, orient the P ! so the dimple on top faces the same way as the pin diagram' 2' !onnect breadboard side strips 7 power to power @red stripsA, ground to ground @black stripsA' Bust like the bottom 2 wires below' Those strips are now referred to as <power= & <ground='

C' E' :' "'

Power to Pin 1 @+ppA D red wire @connected 9ia E%k ?esistor in betweenA Power to Pin 11 @+ddA D red wire ;round to Pin 12 @+ssA D black wire Power to Pin C2 @+ddA D red wire

%' ;round to Pin C1 @+ssA D black wire $' !rystal Fscillator to Pins 1C & 1E @FG!1, FG!2A, the polarity doesn>t matter' @polarity H which of the 2 wires goes in which holeA' I' !apacitor into power & ground D make sure the polarity is correct according to the datasheet for the capacitor' f not it could damage the circuit' 10' ;round to battery ground @7A D black wire' 11' Power @redA to battery power @1A D red wire' Fr, solder that wire to a switch & run another wire from the switch to battery power, so you can switch the power on & off' f using 9oltage regulator, raw battery power wire goes into 9oltage regulator input breadboard ground strip goes to battery ground 9oltage regulator output wire @1: 9oltsA goes to breadboard power strip' 9oltage regulator ground wire goes to breadboard ground strip hat !o the pins "ean? The pins ?507?5%, ?!07?!%, and ?J07?J% are digital -F pins' The pins !!P1 and !!P2, which share locations with ?!1 and ?!2, can be used for a PK3 signal @see J! 3otor tutorialA' The pins &L07&L% are for analog -F @see Photoresistor tutorialA' TM and ?M are for debugging -F @see Futput 3essages to !omputer tutorialA' The remaining pins deal with power-ground, the clock signal, and programmer -F' & P ! is made of se9eral <ports'= /ach port is designated with a letter, ?507?5% are a port' ?!07?!% and ?J07?J% are a port as well' ?&07?&: and ?/07?/2 are also ports, but with fewer pins' Gome of these pins ha9e special purposes, but most can be used as basic input-output pins' #or eNample, you can set pin ?50 to be either an input pin, or an output pin' &s an input pin, the digital 9oltage on the pin can be read in' #or eNample, if ?50 is connected to ground @09A, then you would read a digital 0' f ?50 was connected to power @:9A, then you would read a digital 1' Fn the other hand, if you wanted to set ?5F as an output pin, you could choose to make ?50 either be :9, or 09' This can be used, for eNample, to turn off or on a L/J, or to turn off or on a motor' How to pro#ra" the PIC? omit step by step details of how to program the P ! because software always changes' &s of spring 2010, the club uses software from !!G @http,--www'ccsinfo'com-A' Ke use the P!K !ompiler to write & compile programs and !!G Load and the !J74E0 programmer to write programs to the P !' &lways load a 'heN file onto the P ! D the compiled machine language program' The heN file should be located in the same folder as your proOect file' 2ou need to connect the !J>s /thernet cable pins to the appropriate pins on the P ! in order to load a program' 2ou also need to ha9e the P ! running @i'e' under 1:+ powerA when programming' Gee <How do connect the !!G !J to my own hardware*= in the !!G #&), http,--www'ccsinfo'com-faP'php*pageHicdQconnection' Jon !rouse, the 2010 president, made custom programming boards to do that' They might still be in the lab' 2ou can also cut up the wires on an /thernet cable & connect them to the appropriate pins on the breadboard' f you don>t want to make your own de9ice, you can buy what you need to program the P !'

&nother a9ailable programmer the club most likely had is the PicRit 2' To program with a PicRit, connect the pin marked by the arrow on the PicRit de9ice to the mclr pin' Pin 2 @neNt to the arrowA is 1:, Pin C is ;nd, Pin E is 5%, Pin : is 5", and Pin " is the optional debug pin P;3 @not necessary to connectA' &lso, connect the P ! to power & ground @the +dd-+ss pinsA' To program a P !, connect the P ! ! 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 desktop of the P!' The first necessary step, if the PicRit is configured to program a different pic than your own, is to select the family of the P !' #or the 1"f$%%&, you must select Sbase de9iceS in the de9ice options' n order to program a heN file, it must be loaded - imported first' Jo this by going to file and importing your programTs heN file' &fter the file is loaded, click on the Swrite heNS button' This should write your heN file and say it programmed successfully' The program will automatically start running e9en with the PicRit connected unless the -3!L? checkboN is selected' Lote, The PicRit has the capability to supply power to the pic 9ia the 4G5 bus' t will automatically detect if the de9ice is powered when trying to program' f you want to run your P ! Oust from the 4G5, you can select the S+dd FnS checkboN' This can supply up to 1& of current at :+' 3ake sure the 9oltage number neNt to the checkboN is set to : D unless your processor does not support :+' The P ! used in this tutorial is a :9 de9ice' $iscellaneous A!%ice 2ou must ha9e the P ! wired up & be able to compile & load programs to do the other tutorials' To run the other tutorials, follow these steps, 1' !onnect circuit as described' 2' !ompile the eNample code with whate9er compiler you are using' C' Load the program onto the P !' E' Turn on the power to run the program f you don>t know how to code something, look at old P ! code on the robotics lab>s computers' ha9e found that we need to sol9e similar programming problems year after year, especially when we are talking about sensor input & motor output' !hances are good that somebody already wrote it' The P ! 3!4 ! compiler manual is a good reference too' recommend that you try to be neat when attaching wires & other components' !ut your wires so that they lie flat on the breadboard, not sticking up' 3ake sure the bare ends are long enough to make a solid connection with the breadboard D you don>t want a loose wire( 5e orderly when you run wires to the 9arious sensors & motors, too' 2ou will ha9e a confusing spaghetti forest when you add sensors, motors, switches, and lights' &nother good idea is to use wire colors consistently' #or eNample, e9ery time you ha9e a wire going to ground, use the same color, usually black' That way, you can understand what it does with a glance' This is important when you ha9e :01 wires on the board' use red for power @the 1 on the battery & breadboardA, black for ground @the 7A, blue for inputs, and green for outputs' &lways double check your connections before flicking the switch' f Oust one wire is in the wrong position, it could disable your circuit or burn something out' 2ou can build your circuit with test L/Js, to make sure it is working when you turn it on' ha9e a power light directly connected to the battery to so know when the circuit is on' also ha9e an L/J controlled by the P !' n my programs, make the P ! turn the L/J on when it starts running' That way know that the P ! is working correctly' The &node of the L/J goes to the 1:9 rail out of the regulator' The cathode of the L/J goes to a current limiting resistor D any 9alue between 220 and 1k is fine' The other end of the resistor goes to ;round' This L/J should turn on at full brightness when the circuit is turned on' f the L/J

does not turn on, you may ha9e the L/J in backwards' f you know the L/J is in correctly, and it does not turn on or turns on dimly when the power is turned on, T4?L F## 2F4? ! ?!4 T, and check your wiring' This most likely means there is a short somewhere with your power rails' f you ha9e been running the robot for a while, check the power battery 9oltage' f the 9oltage gets too low, the robot will beha9e strangely due to P ! doing a <brownout='

L/J 5linker
3aterials !ircuit from < ntroduction to the P !1"#$%%&= 100 Fhm resistor L/J !ircuit 1' Pin ?5% to resistor 2' ?esistor to L/J C' L/J to ground !ode
--all these U below set up the P ! Uinclude V1"#$%%&'h8 Ude9ice adcH$ U#4G/G LFKJT --Lo Katch Jog Timer U#4G/G HG --Highspeed Fsc 8 Emh6 U#4G/G P4T --Power 4p Timer U#4G/G LFP?FT/!T --!ode not protected from reading U#4G/G LFJ/54; --Lo Jebug mode for !J U#4G/G LF5?FKLF4T --Lo brownout reset U#4G/G LFL+P --Lo low 9oltage prgming, 5C@P !1"A or 5:@P !1$A used for -F U#4G/G LF!PJ --Lo // protection Uuse delay@clockH20000000A -- Gets crystal oscillator at 20 megahert6 Uuse rs2C2@baudHI"00, NmitHP LQ!", in9ertA --Gets up serial port output pin & baud rate --main program starts here 9oid main@A W -- nfinite program loop starts' L/J blinks fore9er' while@trueAW outputQhigh@P LQ5%AX --send a <1= to pin ?5%, making ?5% <High= at :9 --this will turn on the L/J hooked to ?5% delayQms@:00AX --wait half a second, delays for :00ms outputQlow@P LQ5%AX --send a <0= to pin ?5%, making ?5% <Low= at 09 delayQms@:00AX --wait half a second, delays for :00ms Y Y

Lotes 2ou can easily add more L/Js and make them flash in different patterns' #or more readable code, use
Udefine ?/JQL/J P LQ5% Udefine ;?//LQL/J P LQ5" Z Z outputQhigh@?/JQL/JAX outputQhigh@;?//LQL/JAX

Photoresistor nput
3aterials !ircuit from < ntroduction to the P !1"#$%%&= Photoresistor 1R ohm resistor D or whate9er is appropriate for your photoresistor Kire !ircuit D the diagram on the right

1' 5readboard power @+ccA to 1R7ohm resistor' 2' 1R7ohm resistor to photoresistor power' C' Photoresistor ground to breadboard ground' E' Kire from between resistor & photoresistor to P ! pin &L0' f ?1 is the photoresistor, the 9oltage will increase with increasing light intensity' f ?2 is the photoresistor, the 9oltage will decrease with increasing light intensity' !ode for single photoresistor
--all these U below set up the P ! Uinclude V1"#$%%&'h8 Ude9ice adcH$ U#4G/G LFKJT --Lo Katch Jog Timer U#4G/G HG --Highspeed Fsc 8 Emh6 U#4G/G P4T --Power 4p Timer U#4G/G LFP?FT/!T --!ode not protected from reading U#4G/G LFJ/54; --Lo Jebug mode for !J U#4G/G LF5?FKLF4T --Lo brownout reset U#4G/G LFL+P --Lo low 9oltage prgming, 5C@P !1"A or 5:@P !1$A used for -F U#4G/G LF!PJ --Lo // protection Uuse delay@clockH20000000A --crystal oscillator at 20000000 hert6 Uuse rs2C2@baudHI"00, NmitHP LQ!", in9ertA --serial port output pin & baud rate --run photoresistor signal wire to pin &L0 --connect L/J-resistor to pin ?5% 9oid main@AW int1" photoH0X --1" bit integer, safer than using int because --int is only $ bit which might lead to o9erflow problems for add, multiply setupQadc@&J!Q!LF!RQ LT/?L&LAX --configure analog to digiral con9erter setupQadcQports@&LLQ&L&LF;AX --set pins &L07&L% to analog @can read 9alues from 072:: instead of Oust 0,1A while@trueAW --loop fore9er setQadcQchannel@0AX--set the pic to read from &L0 delayQus@20AX--delay 20 microseconds to allow P ! to switch to analog channel 0 photoHreadQadc@AX --read input from pin &L0, 0VHphotoVH2:: --turn on L/J when input 8 12%, else turn off L/J --Put finger o9er photoresistor & take it off to see L/J turn on-off --12% may not be the actual 9alue that separates light from dark, so try different 9alues if@photo 8 12%AW outputQhigh@P LQ5%AX Y elseW outputQlow@P LQ5%AX Y Y Y

!ode for multiple photoresistors


--all these U below set up the P ! Uinclude V1"#$%%&'h8

Ude9ice adcH$ U#4G/G LFKJT --Lo Katch Jog Timer U#4G/G HG --Highspeed Fsc 8 Emh6 U#4G/G P4T --Power 4p Timer U#4G/G LFP?FT/!T --!ode not protected from reading U#4G/G LFJ/54; --Lo Jebug mode for !J U#4G/G LF5?FKLF4T --Lo brownout reset U#4G/G LFL+P --Lo low 9oltage prgming, 5C@P !1"A or 5:@P !1$A used for -F U#4G/G LF!PJ --Lo // protection Uuse delay@clockH20000000A --crystal oscillator at 20000000 hert6 Uuse rs2C2@baudHI"00, NmitHP LQ!", in9ertA --serial port output pin & baud rate --read input from C photoresistors --run photoresistor signal wires to pin &L0, &L1, &L2 9oid main@AW int1" photo0H0X --1" bit integer, safer than using int --int is only $ bit which might lead to o9erflow problems for add, multiply int1" photo1H0X int1" photo2H0X setupQadc@&J!Q!LF!RQ LT/?L&LAX --configure analog to digiral con9erter setupQadcQports@&LLQ&L&LF;AX --set pins &L07&L% to analog @can read 9alues from 072:: instead of Oust 0,1A while@trueAW --loop fore9er setQadcQchannel@0AX--set the pic to read from &L0 delayQus@20AX--delay 20 microseconds to allow P ! to switch to analog channel 0 photo0HreadQadc@AX --read input from pin &L0, 0VHphotoVH2:: setQadcQchannel@1AX--set the pic to read from &L1 delayQus@20AX photo1HreadQadc@AX setQadcQchannel@2AX --set the pic to read from &L2 delayQus@20AX photo2 H readQadc@AX --2ou could add C L/Js and turn them on if photo0-1-2 8 12% --Oust as with code for single photoresistor Y Y

&nalog nput @Photoresistor, Gonar sensor, ? sensor, ZA


3aterials & !ircuit Jepends on the de9ice' #or eNample, a sonar sensor might ha9e C wires, power, ground & signal' 2ou would connect the signal wire to one of pins &L07&L%' Gee the de9ice>s datasheet for help' !ode
--the rest of the code is identical to code for photoresistor input, eNcept here we call it [sonar> int1" sonar H 0X setQadcQchannel@1AX--set the pic to read from &L1 delayQus@20AX sonarHreadQadc@AX

Lotes 2ou can use the photoresistor tutorial code to read any sort of analog input de9ice, as long as the de9ice is designed so you can run a signal wire to the input pin@sA' ?emember, only the &L07&L% pins allow analog input, and you ha9e to call setupQadc functions & setQadcQchannel as in photoresistor tutorial' ?oughly, P ! will read 0 9olts as 0, and : 9olts as 2::'

Jigital nput @GwitchesA


3aterials !ircuit from < ntroduction to the P !1"#$%%&= L/J Gwitch

1R ohm resistor 10 or E%R ohm resistor Kire !ircuit Pin ?5% to 1Rohm resistor' ?esistor to L/J' L/J to ground' Pin ?J1 to 10-E%Rohm resistor' ?esistor to ground' Pin ?J1 to switch' Gwitch to power' This circuit has a <pull7down resistor=' Khen the switch is closed, the P ! reads : 9olts and when it is open it reads 0 9olts' f we had no pull down resistor then the pin would be in a <floating= state when the switch was open, meaning that the 9oltage can fluctuate' Ke need to connect the pin to ground so the P ! always reads 0 when the switch is open' f there was a wire connected to ground instead of a resistor, it would create a short that burns up the circuit'

!ode
Uinclude V1"#$%%&'h8 Ude9ice adcH$ U#4G/G LFKJT --Lo Katch Jog Timer U#4G/G HG --Highspeed Fsc 8 Emh6 U#4G/G P4T --Power 4p Timer U#4G/G LFP?FT/!T --!ode not protected from reading U#4G/G LFJ/54; --Lo Jebug mode for !J U#4G/G LF5?FKLF4T --Lo brownout reset U#4G/G LFL+P --Lo low 9oltage prgming, 5C@P !1"A or 5:@P !1$A used for -F U#4G/G LF!PJ --Lo // protection Uuse delay@clockH20000000A -- Gets crystal oscillator at 20 megahert6 Uuse rs2C2@baudHI"00, NmitHP LQ!", in9ertA -- serial port output & baud rate --close switch to see L/J turn on --open switch to see L/J turn off --if the pin is low @0 9oltsA N H 0, or #&LG/ --if the pin is high @: 9oltsA N H 1, or T?4/ 9oid main@A W int N H 0X while@trueAW N H input@P LQJ1AX if@NHH1AW outputQhigh@P LQ5%AX Y elseW outputQlow@P LQ5%AX

Y Y Y

Futput messages to computer screen


ha9e used 2 methods' 1' P !kit 2 Je9elopment Programmer-Jebugger' Gee their instructions in the program or website' 2' Gerial port' used HyperTerminal to read teNt from the serial port & display on screen, but other programs can do that too' 3ake sure the baud rate is I"00 bits - sec, Oust like in the P ! code, Uuse
rs2C2@baudHI"00, NmitHP LQ!", in9ertA

Gerial Port Gteps @if using HyperTerminalA 1' Kire serial port pin 2 to pin TM @same as ?!"A on the P !' 2' Kire serial port pin : to ground' C' !onnect serial port to computer' 4se a serial74G5 adapter if computer has no serial port' E' Fpen HyperTerminal' :' /nter any name, select the icon that says <3! = "' !onnect using !F3:-!F31C-etc' f there are multiple !F3s, pull out the 4G5, reopen HyperTerminal, & see which !F3 has 9anished' That>s the one you want' %' Get bits per second H I"00' $' HyperTerminal is set up' t will keep reading input until you close the program' I' Load code onto P ! 10' Hit switch to turn on P ! !ode
Uinclude V1"#$%%&'h8 Ude9ice adcH$ U#4G/G LFKJT --Lo Katch Jog Timer U#4G/G HG --Highspeed Fsc 8 Emh6 U#4G/G P4T --Power 4p Timer U#4G/G LFP?FT/!T --!ode not protected from reading U#4G/G LFJ/54; --Lo Jebug mode for !J U#4G/G LF5?FKLF4T --Lo brownout reset U#4G/G LFL+P --Lo low 9oltage prgming, 5C@P !1"A or 5:@P !1$A used for -F U#4G/G LF!PJ --Lo // protection Uuse delay@clockH20000000A -- Gets crystal oscillator at 20 megahert6 Uuse rs2C2@baudHI"00, NmitHP LQ!", in9ertA --Gets up serial port output pin & baud rate 9oid main@AW int N H 0X while@trueAW N H N 1 1X --This is an ordinary ! language printf statement that will display on the screen of your P!'

--5ut, you need to open a special program to read serial port input, like HyperTerminal' --3ake sure the baud rate of the program matches this code>s baud rate @I"00 bits - secondA printf@Shello, NH\d]r]nS,NAX --send this teNt to serial port delayQms@100AX --wait 100 milliseconds Y Y

Ger9o 3otor Futput


3aterials !ircuit from < ntroduction to the P !1"#$%%&= Ger9o 3otor @this tutorial tested on ParallaN Gtandard Ger9oA Kire !ircuit Gee ser9o motor datasheet' 3ost likely it will be 1' Ger9o power wire to power 2' Ger9o ground wire to ground C' Ger9o signal wire to pin ?J2' @could be any -F pin on the P !, Oust change the codeA

!ode
Uinclude V1"#$%%&'h8 Ude9ice adcH$ U#4G/G LFKJT --Lo Katch Jog Timer U#4G/G HG --Highspeed Fsc 8 Emh6 U#4G/G P4T --Power 4p Timer U#4G/G LFP?FT/!T --!ode not protected from reading U#4G/G LFJ/54; --Lo Jebug mode for !J U#4G/G LF5?FKLF4T --Lo brownout reset U#4G/G LFL+P --Lo low 9oltage prgming, 5C@P !1"A or 5:@P !1$A used for -F U#4G/G LF!PJ --Lo // protection Uuse delay@clockH20000000A -- Gets crystal oscillator at 20 megahert6 Uuse rs2C2@baudHI"00, NmitHP LQ!", in9ertA --Gets up serial port output pin & baud rate --pin ?J2 wired to ser9o --ser9o connected to power, ground, and the signal wire from the P ! --this program steps the ParallaN standard ser9o slowly from 0 to 1$0 degrees, --then rushes back to 0 degrees to restart' --for code readability, could use Udefine G/?+FQP L P LQJ2 778 outputQhigh@G/?+FQP LAX 9oid main@AW int1" pulseQwidth H 1000X int iX while@trueAW --send short pulse to ser9o indicating which angle it should mo9e to' --for eNample, for one type of ser9o, 1000usH1ms indicates 0 degrees, --while 2000usH2ms indicates 1$0 degrees' outputQhigh@P LQJ2AX

delayQus@pulseQwidthAX --sending : 9olts to ser9o for pulseQwidth microseconds outputQlow@P LQJ2AX delayQms@20AX --wait 20 milliseconds 7 refresh cycle of typical ser9os pulseQwidth H pulseQwidth 1 1X --each time, ser9o is instructed to mo9e a little farther if@pulseQwidth HH 2001AW --if ser9o reached angle 1$0, reset, it will rush back to angle 0 pulseQwidth H 1000X Y -^ f want ser9o to go to an angle & stay there, need to send the same pulse se9eral times' @:0 is goodA f only send 1 pulse, the motor wonTt get all the way there, and it will stop, waiting for another pulse' /Nample below shows how to mo9e ser9o to I0 degrees' for@iH1XiVH:0X i11AW outputQhigh@P LQJ2AX delayQus@1:00AX --want ser9o to mo9e to I0 degrees' outputQlow@P LQJ2AX delayQms@20AX Y ^Y Y

Lotes Fther ser9os may ha9e different pulse width rePuirements' &nd, they might go only I0 degrees, or up to C"0 degrees' There are also issues where ser9os draw too much current or waste heat, which is beyond the scope of this tutorial'

J! 3otor Futput
Lormally, you will use an H bridge for motor control' There are many internet articles about H bridges if you want to learn how they work' 3aterials !ircuit from < ntroduction to the P !1"#$%%&= J! motor H bridge chip Kire !ircuit Jepends on the type of H bridge' ha9e used an old GL%:EE10 Puadruple half7h dri9er to run 2 motors at once' also used a custom H bridge circuit board designed by club president Jon !rouse' Gee the data sheet' mportant, whene9er you change the direction, you should set all motor signal lines to 0 @outputQlowA' This pre9ents short circuits' !ode & Lotes will not include an entire program here, because each H bridge model has different rules for motor control' The datasheet will tell you how to set the signal lines' There are two maOor types of output signals on most H bridges' The first signal turns the motor on or off' t is often called the </nable Pin=' Khene9er you want to turn the motor on, respecti9ely off, type outputQhigh@3FTF?Q/L&5L/QP LAX and outputQlow@3FTF?Q/L&5L/QP LAX The second type are the signals indicating which direction the motor should turn, whether the motor free runs, and whether the motor brakes' Gend 0 or 1 to those control signals, outputQhigh@3FTF?QJ ?/!T FLQP LQ1A, for eNample'

& simple illustration, &irection Pin 0 @outputQlowA 1 @outputQhighA M @doesn>t matterA 'nable Pin 1 @outputQhighA 1 @outputQhighA 0 @outputQlowA Results Fne direction Fther direction #ree run @slow down & stopA

Gome H bridges will only let you set the motor direction' n that case, you brake by re9ersing the motor direction for a short time' 5eware, the current draw can spike if you do this, and the P ! might black out' 5ut sometimes sudden direction re9ersal is FRX you will ha9e to see for yourself' f you ha9e set the signals to make the motor go left, it will keep going left until you eNplicitly tell it to stop @or go rightA' t is a good idea to write functions like <forward=, <backward=, <left=, <right=, <stop=' That way you can Oust call the function & not ha9e to worry about the details' 5elow are code eNcerpts from motor control for GL%:EE10 H bridge, controlling 2 motors' 2our motor control code will LFT be the same, because you will use a different H bridge or ha9e different needs' 5ut it will be similar to this'
Udefine L/#TQ!FLT?FLQ2& P LQ!2 --2& Udefine L/#TQ!FLT?FLQ1& P LQ!1 --1& Udefine L/#TQ3FTF? P LQJ1 --1,2/L Udefine ? ;HTQ3FTF? P LQJ2 --C,E/L Udefine ? ;HTQ!FLT?FLQE& P LQJ: --E& Udefine ? ;HTQ!FLT?FLQC& P LQJE --C& int goingQforwardX --current motion int goingQbackwardX int goingQleftX int goingQrightX int goingQhardQleftX int goingQhardQrightX Z Z --brake by re9ersing motor direction, then turn motors off 9oid halt@AW if @goingQforwardA backward@AX else if @goingQbackwardA forward@AX else if @goingQleftA right@AX else if @goingQrightA left@AX else if @goingQhardQleftA hardQright@AX else if @goingQhardQrightA hardQleft@AX delayQms@:0AX --re9erse direction for :0 ms, enough to make robot stop --but not long enough to make it start mo9ing in opposite direction reset3otor!ontrol@AX Y 9oid forward@AW reset3otor!ontrol@AX outputQhigh@L/#TQ!FLT?FLQ1&AX outputQhigh@? ;HTQ!FLT?FLQC&AX outputQhigh@L/#TQ3FTF?AX outputQhigh@? ;HTQ3FTF?AX goingQforwardH1X Y

9oid backward@AW reset3otor!ontrol@AX outputQhigh@L/#TQ!FLT?FLQ2&AX outputQhigh@? ;HTQ!FLT?FLQE&AX outputQhigh@L/#TQ3FTF?AX outputQhigh@? ;HTQ3FTF?AX goingQbackwardH1X Y --run only 1 motor for gentler turn 9oid right@AW reset3otor!ontrol@AX outputQhigh@? ;HTQ!FLT?FLQC&AX outputQhigh@? ;HTQ3FTF?AX goingQrightH1X Y --run only 1 motor for gentler turn 9oid left@AW reset3otor!ontrol@AX outputQhigh@L/#TQ!FLT?FLQ1&AX outputQhigh@L/#TQ3FTF?AX goingQleftH1X Y --run one motor forward and one backward for sharper turn 9oid hardQleft@AW reset3otor!ontrol@AX outputQhigh@L/#TQ!FLT?FLQ1&AX outputQhigh@? ;HTQ!FLT?FLQE&AX outputQhigh@L/#TQ3FTF?AX outputQhigh@? ;HTQ3FTF?AX goingQhardQleftH1X Y --run one motor forward and one backward for sharper turn 9oid hardQright@AW reset3otor!ontrol@AX outputQhigh@L/#TQ!FLT?FLQ2&AX outputQhigh@? ;HTQ!FLT?FLQC&AX outputQhigh@L/#TQ3FTF?AX outputQhigh@? ;HTQ3FTF?AX goingQhardQrightH1X Y --Get all pins to H7bridge low to pre9ent short circuits --?eset Scurrent motionS flags 9oid reset3otor!ontrol@AW outputQlow@L/#TQ3FTF?AX outputQlow@? ;HTQ3FTF?AX outputQlow@L/#TQ!FLT?FLQ1&AX outputQlow@L/#TQ!FLT?FLQ2&AX outputQlow@? ;HTQ!FLT?FLQC&AX outputQlow@? ;HTQ!FLT?FLQE&AX goingQforwardH0X goingQbackwardH0X goingQleftH0X goingQrightH0X goingQhardQleft H 0X goingQhardQright H 0X delayQus@20AX Y

Gpecial Topic, PK3 Low you know how to run the motor at full speed' 5ut a situation might arise where you want half speed, or _ speed, etc' n that case, you can send a PK3 signal to the H bridge enable pin' There are many internet articles about PK3 if you want to learn how it works' To use PK3 in motor control, replace outputQhigh@3FTF?Q/L&5L/QP LA with setQpwm1Qduty@NA ' 3ake sure that the <enable pin= wire goes into the P ! pin !!P1 or !!P2' Those are the only pins that can ha9e PK3 signal'

PK3 code eNcerpt,


setupQtimerQ2@T2QJ +Q52QE,2::,1AX setupQccp1@!!PQPK3AX --set pin !!P1 as a PK3 pin instead of a regular -F pin setupQccp2@!!PQPK3AX --set pin !!P2 as a PK3 pin instead of a regular -F pin while@trueAW --if setting duty cycle @setQpwm1QdutyA, only gi9e 9alues between 072:: --0 means the PK3 signal is always at 0' 12$ means it is 1 half the time & 0 the other half --leading to an a9eraged S1-2 powerS signal' 2:: means it is 1 all the time, motor full speed setQpwm1Qduty@2::AX --enable pin for motor 1, P ! pin !!P1 setQpwm2Qduty@2::AX --enable pin for motor 2, P ! pin !!P2 delayQms@C000AX --full speed for C seconds setQpwm1Qduty@12$AX setQpwm2Qduty@12$AX delayQms@C000AX --half speed for C seconds setQpwm1Qduty@0AX setQpwm2Qduty@0AX delayQms@C000AX --motors off for C seconds Y --4se PK3 in your J! motor control code to change the speed that the robot goes forward-backward-left-right --call forward@2::AX to go forward at full speed' !all forward@12$AX to go forward at half speed' 9oid forward@int speedAW reset3otor!ontrol@AX outputQhigh@L/#TQ!FLT?FLQ1&AX outputQhigh@? ;HTQ!FLT?FLQC&AX setQpwm1Qduty@speedAX --wire from P ! pin !!P1 to right motor enable pin setQpwm2Qduty@speedAX --wire from P ! pin !!P2 to left motor enable pin connected to goingQforwardH1X Y

Lotes f you want to use higher 9oltage J! motors, or run motors at high speeds, you might ha9e to construct a special circuit to deal with the eNtra current draw, heat, high 9oltage batteries, etc' J! motors ha9e their own set of issues to deal with, which is beyond the scope of this tutorial'

You might also like