PIC16F877A Microcontroller Project Tutorial

download PIC16F877A Microcontroller Project Tutorial

of 19

Transcript of PIC16F877A Microcontroller Project Tutorial

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    1/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. '() *linker7. Photoresistor Input

    8. Anlo# Input +Photoresistor, -onr sensor, I sensor, /0

    . )i#itl Input +-witches0

    1. utput %ess#es to Co!puter -creen

    11. -er&o %otor utput

    12. )C %otor utput----------------------------------------------------------------------------------------------------------------------------

    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# co#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 wheremicrocontrollers ha# not pre"iously been consi#ere# e4ample5 timer functions, interface

    replacement in larger systems, coprocessor applications, etc3In %ystem Programmability of this

    chip along 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

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    2/19

    PIC16F877A Pin Diagram

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    3/19

    2.How to Hook up the PIC so it will run?

    %terils

    PIC16F877A

    !rea#boar#

    + Megahert crystal oscillator

    7'+pf capacitor

    !lac/ re# wires

    97 "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

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    4/19

    Power to Pin 1 pp3 B re# wire connecte# "ia *7/ )esistor in between3

    * Power to Pin 11 ##3 B re# wire

    : ;roun# to Pin 1 ss3 B blac/ wire

    6 Power to Pin ##3 B re# wire

    7 ;roun# to Pin 1 ss3 B blac/ wire

    8 Crystal .scillator to Pins 1 1* .%C1, .%C3, the polarity #oesn>t matter polarity which

    of the wires goes in which hole3

    D Capacitor into power 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# '3 B blac/ wire

    11 Power re#3 to battery power E3 B re# wire .r, sol#er that wire to a switch run another wire

    from the switch to battery power, so you can switch the power on 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 E: "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# )+')7 are #igital I-. pins (he pins CCP1 an# CCP,

    which share locations with )C1 an# )C, can be use# for a PGM signal see C Motor tutorial3

    (he pins AH+'AH7 are for analog I-. see Photoresistor tutorial3 ( an# ) are for #ebugging

    I-. see .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

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    5/19

    #igital "oltage on the pin can be rea# in For e4ample, if )!+ is connecte# to groun# +"3, then you

    woul# rea# a #igital + If )!+ was connecte# to power :"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 $0, 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 +1+, the club uses software from CC% http5--wwwccsinfocom-3 Ge use the PCG

    Compiler to write compile programs an# CC% $oa# an# the IC'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 IC>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 ie un#er E: power3 when programming%ee 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 Picit (o program with a Picit,

    connect the pin mar/e# by the arrow on the Picit #e"ice to the mclr pin Pin ne4t to the arrow3

    is E:, Pin is ;n#, Pin * is !7, Pin : is !6, an# Pin 6 is the optional #ebug pin P;M not

    necessary to connect3 Also, connect the PIC to power groun# the ##-ss pins3 (o program a

    PIC, connect the PIC IC to the Picit, an# a usb mini cable from the Picit to the computer (o use

    the Picit, run the Picit program on the #es/top of the PC (he first necessary step, if thePicit 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 o 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 Picit

    connecte# unless the -MC$) chec/bo4 is selecte#

    Hote5 (he Picit has the capability to supply power to the pic "ia the J%! bus It willautomatically #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 Q## .nQ chec/bo4 (his can supply up to 1A of current at: Ma/e sure the "oltage number ne4t to the chec/bo4 is set to : B unless your processor #oes not

    support : (he PIC use# in this tutorial is a :" #e"ice$. %iscellneous Ad&ice

    Lou must ha"e the PIC wire# up be able to compile loa# programs to #o the other tutorials

    (o run the other tutorials, follow these steps5

    1 Connect circuit as #escribe#

    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 motor output Chances are goo# that somebo#y alrea#y wrote

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    6/19

    it (he PIC MCJ C compiler manual is a goo# reference too

    I recommen# that you try to be neat when attaching wires 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 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 the E on the

    battery brea#boar#3, blac/ for groun# the '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 $0s, 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 $0

    controlle# by the PIC In my programs, I ma/e the PIC turn the $0 on when it starts running (hatway I /now that the PIC is wor/ing correctly

    (he Ano#e of the $0 goes to the E:" rail out of the regulator (he catho#e of the $0 goes to a

    current limiting resistor B any "alue between + an# 1/ is fine (he other en# of the resistor goes

    to ;roun# (his $0 shoul# turn on at full brightness when the circuit is turne# on If the $0

    #oes not turn on, you may ha"e the $0 in bac/war#s If you /now the $0 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 getstoo low, the robot will beha"e strangely #ue to PIC #oing a

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    7/19

    TFJ%0% H.0!J; --Ho ebug mo#e for IC

    TFJ%0% H.!).GH.J( --Ho brownout reset

    TFJ%0% H.$P --Ho low "oltage prgming, !PIC163 or !:PIC183 use# for I-.

    TFJ%0% H.CP --Ho 00 protection

    Tuse #elaycloc/+++++++3 -- %ets crystal oscillator at + megahert

    Tuse rsbau#D6++, 4mitPIHOC6, in"ert3 --%ets up serial port output pin bau# rate

    --main program starts here

    "oi# main3 V

    --Infinite program loop starts $0 blin/s fore"er

    whiletrue3V

    outputOhighPIHO!73W --sen# a

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    8/19

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    9/19

    --all these T below set up the PIC

    Tinclu#e U16F877Ah9

    T#e"ice a#c8

    TFJ%0% H.G( --Ho Gatch og (imer

    TFJ%0% &% --&ighspee# .sc 9 *mh

    TFJ%0% PJ( --Power Jp (imer

    TFJ%0% H.P).(0C( --Co#e not protecte# from rea#ing

    TFJ%0% H.0!J; --Ho ebug mo#e for IC

    TFJ%0% H.!).GH.J( --Ho brownout reset

    TFJ%0% H.$P --Ho low "oltage prgming, !PIC163 or !:PIC183 use# for I-.

    TFJ%0% H.CP --Ho 00 protection

    Tuse #elaycloc/+++++++3 --crystal oscillator at +++++++ hert

    Tuse rsbau#D6++, 4mitPIHOC6, in"ert3 --serial port output pin bau# rate

    --rea# input from photoresistors

    --run photoresistor signal wires to pin AH+, AH1, AH

    "oi# main3V int16 photo++W --16 bit integer, safer than using int

    --int is only 8 bit which might lea# to o"erflow problems for a##, multiply

    int16 photo1+W

    int16 photo+W

    setupOa#cACOC$.COIH(0)HA$3W --configure analog to #igiral con"erter

    setupOa#cOportsA$$OAHA$.;3W --set pins AH+'AH7 to analog can rea# "alues from +'::

    instea# of Kust +,13

    whiletrue3V --loop fore"er setOa#cOchannel+3W--set the pic to rea# from AH+

    #elayOus+3W--#elay + microsecon#s to allow PIC to switch to analog channel +

    photo+rea#Oa#c3W --rea# input from pin AH+5 +UphotoU::

    setOa#cOchannel13W--set the pic to rea# from AH1

    #elayOus+3W

    photo1rea#Oa#c3W

    setOa#cOchannel3W --set the pic to rea# from AH

    #elayOus+3W photo rea#Oa#c3W

    --Lou coul# a## $0s an# turn them on if photo+-1- 9 17

    --Kust as with co#e for single photoresistor

    X

    X

    8. Anlo# Input +Photoresistor, -onr sensor, I sensor, /0Materials & Circuit

    epen#s on the #e"ice For e4ample, a sonar sensor might ha"e wires5 power, groun# signal

    Lou woul# connect the signal wire to one of pins AH+'AH7 %ee the #e"ice>s #atasheet for help

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    10/19

    Code

    --the rest of the co#e is i#entical to co#e for photoresistor input, e4cept here we call it Zsonar>

    int16 sonar +W

    setOa#cOchannel13W--set the pic to rea# from AH1

    #elayOus+3W

    sonarrea#Oa#c3W

    otes

    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 pins3

    )emember, only the AH+'AH7 pins allow analog input, an# you ha"e to call setupOa#c

    functions setOa#cOchannel as in photoresistor tutorial

    )oughly, PIC will rea# + "olts as +, an# : "olts as ::

    . )i#itl Input +-witches0

    %terils

    Circuit from

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    11/19

    Code

    Tinclu#e U16F877Ah9

    T#e"ice a#c8

    TFJ%0% H.G( --Ho Gatch og (imer

    TFJ%0% &% --&ighspee# .sc 9 *mh

    TFJ%0% PJ( --Power Jp (imer

    TFJ%0% H.P).(0C( --Co#e not protecte# from rea#ing

    TFJ%0% H.0!J; --Ho ebug mo#e for IC

    TFJ%0% H.!).GH.J( --Ho brownout resetTFJ%0% H.$P --Ho low "oltage prgming, !PIC163 or !:PIC183 use# for I-.

    TFJ%0% H.CP --Ho 00 protection

    Tuse #elaycloc/+++++++3 -- %ets crystal oscillator at + megahert

    Tuse rsbau#D6++, 4mitPIHOC6, in"ert3 -- serial port output bau# rate

    --close switch to see $0 turn on

    --open switch to see $0 turn off

    --if the pin is low + "olts3 4 +, or FA$%0

    --if the pin is high : "olts3 4 1, or ()J0

    "oi# main3 V

    int 4 +W

    whiletrue3V

    4 inputPIHO13W

    if413V

    outputOhighPIHO!73W

    X

    elseV outputOlowPIHO!73W

    X

    X

    X

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    12/19

    1. utput %ess#es to Co!puter -creen

    I ha"e use# metho#s

    1 PIC/it e"elopment Programmer-ebugger %ee their instructions in the program or website

    %erial port I use# &yper(erminal to rea# te4t from the serial port #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

    rsbau#D6++, 4mitPIHOC6, in"ert3

    -eril Port -teps +i4 usin# Hperer!inl0

    1 Gire serial port pin to pin ( same as )C63 on the PIC

    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

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    13/19

    "oi# main3V

    int 4 +W

    whiletrue3V

    4 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 D6++ bits - secon#3

    printfQhello, 4[#?r?nQ,43W --sen# this te4t to serial port

    #elayOms1++3W --wait 1++ millisecon#s

    XX

    11. -er&o %otor utput

    %terils

    Circuit from

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    14/19

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    15/19

    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 t matter3 + outputOlow3 Free run slow #own 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 .W 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 or go right3

    It is a goo# i#ea to write functions li/e

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    16/19

    can Kust call the function not ha"e to worry about the #etails

    !elow are co#e e4cerpts from motor control for %H7:**1+ & bri#ge, controlling 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().$OA PIHOC --A

    T#efine $0F(OC.H().$O1A PIHOC1 --1A

    T#efine $0F(OM.(.) PIHO1 --1,0H

    T#efine )I;&(OM.(.) PIHO --,*0H

    T#efine )I;&(OC.H().$O*A PIHO: --*A

    T#efine )I;&(OC.H().$OA PIHO* --A

    int goingOforwar#W --current motion

    int goingObac/war#Wint goingOleftW

    int goingOrightW

    int goingOhar#OleftW

    int goingOhar#OrightWY

    Y

    --bra/e by re"ersing motor #irection, then turn motors off

    "oi# halt3V

    if goingOforwar#3

    bac/war#3W

    else if goingObac/war#3

    forwar#3W

    else if goingOleft3

    right3W else if goingOright3

    left3W

    else if goingOhar#Oleft3

    har#Oright3W

    else if goingOhar#Oright3

    har#Oleft3W

    #elayOms:+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 resetMotorControl3W

    X

    "oi# forwar#3V

    resetMotorControl3W

    outputOhigh$0F(OC.H().$O1A3W

    outputOhigh)I;&(OC.H().$OA3W

    outputOhigh$0F(OM.(.)3W

    outputOhigh)I;&(OM.(.)3W

    goingOforwar#1W

    X

    "oi# bac/war#3V

    resetMotorControl3W

    outputOhigh$0F(OC.H().$OA3W

    outputOhigh)I;&(OC.H().$O*A3W

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    17/19

    outputOhigh$0F(OM.(.)3W

    outputOhigh)I;&(OM.(.)3W

    goingObac/war#1W

    X

    --run only 1 motor for gentler turn

    "oi# right3V

    resetMotorControl3W

    outputOhigh)I;&(OC.H().$OA3W

    outputOhigh)I;&(OM.(.)3W

    goingOright1W

    X

    --run only 1 motor for gentler turn"oi# left3V

    resetMotorControl3W

    outputOhigh$0F(OC.H().$O1A3W

    outputOhigh$0F(OM.(.)3W goingOleft1W

    X

    --run one motor forwar# an# one bac/war# for sharper turn

    "oi# har#Oleft3V

    resetMotorControl3W

    outputOhigh$0F(OC.H().$O1A3W

    outputOhigh)I;&(OC.H().$O*A3W

    outputOhigh$0F(OM.(.)3W

    outputOhigh)I;&(OM.(.)3W goingOhar#Oleft1W

    X

    --run one motor forwar# an# one bac/war# for sharper turn

    "oi# har#Oright3V

    resetMotorControl3W

    outputOhigh$0F(OC.H().$OA3W

    outputOhigh)I;&(OC.H().$OA3W outputOhigh$0F(OM.(.)3W

    outputOhigh)I;&(OM.(.)3W goingOhar#Oright1W

    X

    --%et all pins to &'bri#ge low to pre"ent short circuits

    --)eset Qcurrent motionQ flags

    "oi# resetMotorControl3V

    outputOlow$0F(OM.(.)3W

    outputOlow)I;&(OM.(.)3W

    outputOlow$0F(OC.H().$O1A3W

    outputOlow$0F(OC.H().$OA3W outputOlow)I;&(OC.H().$OA3W

    outputOlow)I;&(OC.H().$O*A3W

    goingOforwar#+W

    goingObac/war#+W

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    18/19

    goingOleft+W

    goingOright+W

    goingOhar#Oleft +W

    goingOhar#Oright +W

    #elayOus+3W

    X

    -pecil opic5 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 outputOhighM.(.)O0HA!$0OPIH3 with setOpwm1O#uty43

    Ma/e sure that the

  • 8/12/2019 PIC16F877A Microcontroller Project Tutorial

    19/19

    a special circuit to #eal with the e4tra current #raw, heat, high "oltage batteries, etc C motors ha"e

    their own set of issues to #eal with, which is beyon# the scope of this tutorial