Professional Documents
Culture Documents
200s
Part One
Eigthth Edition, May 2005
Compiledby
U Maung Maung Hyat
ntemvnic Tldning Ccntrc
Romt N o. 44, B$frng N o. 7417a,
Connr { enrwrrfttt met ntd 54n sttltct
Ynngon, Mymnu
fi
PIC ${ierocontroller Course 2OG4 Eleetranics Trairting Centre Fage I
GhapterGne
Things to Rememher
1"f Logic l-evels e.g-
Digital Eleekonics is concerned wiih hvo-skte switch- BitT BitO BitS Bit4 Bir3 Slt2 Bitl BitO
ing iype ciicuits in which the signals are in the form af eiecirical 11010101
pulses shoi.;n in Fig 1. Their ougub and inpuis involve only
f"ve leveis of vcltage, referred tc as'HlGl*i' ancj 'LOW. 't{lGH'is The binary vaiue atposition Bit4 = f
near the suppiy voitage, e.g. +5V and'LOW Is near 0V. 'HlGtl'
is referred to as logic level 'l ( or just 1| and 'LgW' as logic
level 0 {or just 0}. t.3 Decirnal vs Binary
The .rabie shows how the decimai number from 0 to 15
are coded in the 4 bit hinary systern
Decifiaal binary
n 00m
1 0001
Fig.l-l logfc f evefs
2 ffi10
0011
HIGFi =logie f =1=$uFplyvoltage
LQW
4 01ffi
=LegicS=0=0\f 5 0101
o 0110
1.2 Bib
- lt is necessary tc uriderstand the concept of a bii i shctt 011r
form cf binary digit i. A bii is e singie part of an eiectronic inemory I lGOC
,'rrhich can be set io one of two states either to logic 1 or logic 0. o 1001
4,menrory iC can have any number ol bits contained '10
within ii.
1010
11 101"1
'1 nibbie=4bib 12 11m
2nibbles=8bib= I byte IJ 1101
14 1110
210=1024=1k 4fi 1111
fi24k= fi24xfi24 = 220 =
jM
hex2AF
+1 weight rc2 161 160
+0 = 2x256 + 10x16 +15x1 =687
+0
1.8 Decimal to Hexadecimal Conversion
+1 Decimal io hexadecimal conversion can be done using
+1 repeated divisior by 16
2. Quickerway
The decimal number is sirnpiy expressed as a sum of
powers of 2 (weights) and then 1s and 0s are written in the
api:i-opriate bit positions +7
+10 =A
25,0= 16 I 4 2 1
+1
= I 1 0 O 1,
423 ,o = 1A7H
1.6 Hexadecimal Nurnber System
The hexadecimal system uses base 16 lt uses the dig-
rts C through 9 plus the letiers, A, B, C, D, E, F asthe 16 digit 't .$ hlexadecimal to Binary Conversion
symlrols lt is important to renrember that hex digits A through F To convert a hexadecimal number to a binary, convert
are equivaient to the decimal values 10 through 15
each hexadecimal digit to its 4-bit equivalent
2945
0010 1001 0100 0101
uovf, ,rr, = gbt+
rfne.rnorS cz)L
{lt'p {Lg -s -. I bt{ shtc , Bf r$, ,A {o, qbt+
-t woY{ = btma15 (2,, tr + ,,^to,L
Re-Xtetev s -+ 1Y'+ 4 ft+ fl"p " rc!.f {Pfr tqbit oo-oottt +,*orl.
an*a* of I J.t
) Bbi+ reJt t rcbLl -> ,;
l
0CI00 0110 06h t 1
tI
lf rnemcry ce!!s are organized in groups they can be
used tc store a number of bib. For insiance, a registerccnsisting
00c0 0111 07h U U 0
of eight flip-ilops can be conside!'ed to be a memcry that is
storing an I bit word { a byte ) ln this case the mercory ".vord
size is 8-bit. Pcpulalriord sizes for tabi* illustrates a small 6 v*'ci-d memory. Each of these
This
rnicrocomputers are 8, 16, and 32 bits.
ward has a specific address.
!n a microconrputer tite addresses are issued by ihe
Word size = number cf bits per word
mrc!'oprcc6ssor.
:
Vclatile Mernory:
A way of specitying how many bib can be stare in a Any fgpe of memory ihai requires the aopiication of elec-
partlcrrlar memary device of ccmpiete n1emcry +*ical povrer rn crder to stoi-e inforrnatton. if the electrical power
sYstem' rrto, o[ Rego no' o[ ,ttorL is removed, ali infcrmati+n stsreci in the memory lvili be iast.
Many senriconductor rnemones are volatile.
Cepacify = Nr;nrber af words x word size
Fol exannple, in the PiCi6FB4A, GPR iGeneral l\lon-\folatile M*mory: retains its dab even though he powe;"
Furpose Register) extends ftom address *Ch ta 4Fh. Total is removed fiom the lC,
memcry positions are 68, i.e., number of rvoreis are 68. A 6 bit
binary wcrd can stcre in each positicn. i e., vrord size is I bit
rmer youp rEietnr
Then the capacity of the GFR is
aFtJ
"t
6Sxibyte=68byte
l!, +Y Ypt
oonltoL 1o <o>i\c
tD 9cL ellreqs ta'z3islrr c') sl -P/W to I 1o
:le913zt
lo E4ov i.t Pqt, s+b eAL -- ott i/s pt* ; rp
o
a
c2) 1cl AaLq. to 1'1s PLtt-
tO lo 11s6
PIC Microcontroller Course 2004 Compiled by U Maung Maung Myat Page 4
1.1 3 Semiconductor Memories into an EPROM by applying high voltage signals. Data is erased
There are two main types - Read Only Memories, i.e by shining ultraviolet light onto a transparent window that covers
ROMs, and Read and Write Memories called Random the lC. This process takes about 20 to 30 minutes. EPROMS
Access Memory i e. RAMs ( because they allow random have two rn-ajor disadvantages. First, they have to be removed
access) from their sockets in order to be erased and reprogrammed,
Fufthermore, erasure removes the complete memory contents;
this necessitates complete reprogramming even when only one
RAM (Random Access Memory)
memory word has changed
RAM srmpiy stands for Random Access Memory, and
rreans thai ii takes the same time to access any
ocation (i e all are equally accessible) EEPROM
Mosi semiconductor memories are RAMs A RAM nor- The EEPROM was developed as an improvementto the
mally loses the stored data as soon as the power to it is switched EPROM EEPROM can be programmed and erased in circuit
off i.e RAM is a volatile memory. by the application of suitable electrical pulses. Unlike the
.:. EPROM it may be selectively reprogrammed
RAMs are of two types - Static RAM (SRAM) and
Dyramic RAM (DRAM). ln a SRAM each memory cell consists No matter which non -volatile memory you are using in
basrcally of a bistable (flip-flop) whose contents are fixed ( i,e, your system application, the operating characterlstics are all
.i very similar.
a or 0) until the cell is written into or the power is switched off
ROM
fne iOttl is programmed by the manufacturer Once
clata has been entered into the device it cannot be altered
subsequently ROMs are used to store programs and data that
are not to change during the operation of a system. A major use
for ROMs is the storage of programs in microcomputers Since
all ROMs are non-volatile, these programs are not lost when
the microcomputer is turned otf At switch on the microcom-
euter can immeciiately begin executing the program stored in
ROM
PROM
A PROM may be programmed by the manufacturer or
the user. Once programmed the contents of the PROM cannot
be altered
EPROM
The main disadvantage of the PROM device is the fact
that it cannot be reprogrammed An EPROM ma5r be pro-
grammed by the manufacturer or:the user. Dbta is programmed
FIC {vli*rocontreller esurse ?SS4 eompiled by S $fiaung Maung Myat Fage 5
ly{uch olthe informadan aboi-rtthe ccrnmends which we Suopiy cui'rent is primarily a function of operaiing votfage,
present hei'e is, in niost instances, acpiicable to other members frequency and ilO pin loading and is iypicelly 2 rnA for a 4Mi-{z
of the PIC iamiiy. Once you r:nderstanC a PIC16F84 yor.r shouid clock osciilator. This drops to apprcximateiy 4CpA in the $I-EEF
have no difficulty applying your i<nowledge to cther FlCs. rncde.
Because the cjevice is CMQS, ail inputs must go
There are twc versions of these chips having the suffix - so!-rlewlrere- All unused inpub should be puiled up tc the supply
04 and -10 respecrJveiy. The suffix indicetes ihe rnaximum clock valtage i usually +5V i or connected tc grcund (OVi.
frequenc.v at ychich the chip can be used.: -04 means.4MHz, -
10 means i0MHz You nray use eitherspeed raiing. 23.2 Clock Oscillator
The following four differeni types of cicck osciilators
may be used.
2.2. Hardu*are of PIC!.6F844
RC - ResistorlCapacitor i less ihan l MHz )
The a,.chitecture cf the PiCs are based upon the RISC
iReeiuced' lnstruction Set Computer ) method. \tiith this, the
LD - Lovr Poviercrystal
MCLR
o
J OSC2yCLKOUT
o) 1Ck 20pF 625kHz
V"s -Tl Voo '!0k 220pF 80kHz
@
RBUINT A R87 10i( 0.1 pF 85Hz
RB1 R86
Recornended values for R and C : 5k < R (to$k, C>
R82 R85 ZCIpF
R83 R84
PIC Microcontroller Course 2OO4 Electronics Training Centre Page 6
RA2 RA1
RA3 RAO
!
- RA4/TOCKI !
o 3:fill
osc2/
XTA
OSC2/CLKOUT
'100 \/
v^^
O)
Vrs ,,22oF
ohm
-T'1
vte vDD
@
RBO/INT s RB7 RBOiINT R87
RB1 R86 RB1 RB6
R82 RB5 R82 RB5
RB3 R84 R83 R84
There are two timers that offer necessary delays on power One is the Oscillator Start-up Timer (OST), intended to keep
:he chip in RESET ,lntii the crystai oscillator is stable The cther is the Power-up Timer (PWRT), which provides a flxed delay of 72
o cyXt,
' hro'i 5
cmE
'.,'L'
Tzne
L.. L
h"o
PIC llicrocontroller Course 2OO4 Compiled by U tlaung Eaung tlyat Page t
2.3 Configuretion p'jc
Pnl.(I duta,or aiJ tltLt.
The lirst thing io underciand is that all PlCs must be corfigured for the applicatcn they are inl€nd€d to control The
CONFIG statement can be used to cantrol ihe connguration tuses of the PIC Such mniguraiion include th6 s6l€ction of
PortAloshl---XXXXX
TRt I 85h I -,- 11'-111
25 l/O Porls
-Ihe
PiCl6F€4 has 13 uO pihs organized astwo dis.rete
l/O p.rts PortA ( 5 bit wide ) aod PortB ( 8 bit wide )
Bt
orTMolime countet
lnputisSchmitt
PIC Micrscontroller Course 2004 Compiled by U Maung Maung Myat Page 1O
sll "p$b l/O pcrt pin TTL input evels and INDF: Dala memory contents by ndirect
rn iernai sofiwa re programmable
addressng
weak pu lup. Exiernal ntel(rpl TMRo Timercclr'rier
rnput
PCL Low croe. t bns ofthe proqr;m counter
STATUS: Flagolthe.alculanonres!i
rSR ndr r'l . a a IA rof cacre:, po ']le'
5 /O podpin, TTL nputleves and PORTA PORTA dAtA VO
lnternal software p rog €mrnable PORTB PORTB d€la l/O
'
reak pull-.rp :{erta rnterr!pl EED Dala foTEEPRON'I
input EEADR Address olEEPROirl
PCLATH Wlte bLrfier ior upper 5 b is oi the program
R86 6 l/O porl prn ITL rnput levels and
nterna software prog€mmab e
INTCON lnterruptconlro
OPTION REG l,rlode set
weak pul-up Exiern al lnteffLrpt TRISA [,1bde set for PORTA
nput TRISB lr]ode seifor PORTB
EECoNI Conro r€g st€r ior EEPRON]
R87 l/O poi( p n TTLrnput evelsand EECON2 Wnte proteclron reg sterior EEPROT'l
internal software programmable
weak pu I up, Externalinterrupt
0 RWO l_ 1
o
IRP RP1 RPO Z DC G
7 6 5 3 2 1 0 Bit.umber
f,
at"o,",ln.."
F|ow to Program the PIC
Laquages:
_CONFIG statemert The CONFIG statementcan be used
Tie lafg!age that you choose should produce to conko the con ngu ratron tuses of the P C These config uratio l
:,,:1.1i rl irc le,'rces r th. P C lne-up Mrc ochrp bts te the P C how to behave on power-Lrp n terrns of whal
. oe |\4PASIM'o .i .5ese)dLtl/whatvoJreqJtre type ofoscrllator s lo be used, whether or
I r,a nrost ;mportanr ad\lce s that oon't p ck one that nottlrewatchdog t mer s go ng to execuie etc
h des the features ofthe PIC microcontroller On top of
understandingthe PIC you also haveto learn the Labels Names when g ven to proqram llstrng addresses are
commonly known as LABELS One thing to note is that the
anguage
Tlrsi column is eserued lo. abels tite commands canrot siai
nrnearslcoLumnoltheirne Plt ng lhe commands on the frrsl
Sample ofthe body of the source file ..unnwrllconiuse IVIPASNI and probably wrll prcd uce errcrs
lMtutT as:r nftra con]ments prograr"n ORG 0 rs kiown as lbe Reset Vector adCress ll s to !h s ?dare.s
.a nes auihors faTne dates and descrplon rrratthe P C tLrmps,,!hen lt:s nlst run orslbsequ€nly RESET
LIST P=16FE4A, R=DEC
CONF]G H 3FFB' ORG 4 s known as ihe lnleirupt Vector add.ess li ls lo tl s
A rylh ng EQU 12 tPLrtrntneregster adoress that PIC turnps rf an nl€nlpl occurs The sublectot
ntetr!plwrllbe deaitw tlr aterlutorlals lgnore the concept,'.r
CPC C Reset;ectoraddress
GOTO 5 :Goto prograrn memory ORG 5 is the stad of the proglam rnemory, e tsihefrsi
..auori (address)5n ava lable pos lion w thin the PlC atwhch the actua: progranr
ORG 4 lntefiupt vector ad d ress
GOTO 5
ORG 5 tstart ofthe program memory GOTO command srmply ie s the PIC io go to the address sht€d
Ihe address c€n be any chosen by the program wnler
-CLRFAnylhng Clearihecontentscl Yo! will notrce lhal the addrcss th. 2h and 3h ate rcl
i:r. AnJ,lhing reg ster netcned These are rcserved by the Plc and are nol ava ab e
tcrnoma program lse
END On@ yo!r so;rrce prcgram s complete tle ast line rs an-
EllD'staiement Thlsstatementtells l,4PASl/lthatiireprcgram
END
Chapter Fo$r
Tutorials
m Commands
ma TUTORIALl
,I€LRF Charf
i3l{TUil ASu
:Selting Pod Bio ouiput mcd€ and turn on an LED
6F84A R=OEC
fis the address ofFile rcgister rctF 3F;3
( 00h to 7Fh ) iCP-OFF WDT'DlSAtsLE, PWRTE-ENABLE. OSC-RC
,-/(,rdd i r,lti ryolT
ORG0
OperaSon This command cfears the contents
rcgister and it sets Zflag
offle
uc I
n''
FIAg !tsetstheZiag,l.e-Z= 1
'h- \'' tl-cl U"
dt '(^"-
\' 'l
cvde @m.Z.ar4 tl e,xrqb.
lnstruclion !
' ^16 $ cLRFoa- drRf 5
b'ff, c5cto faof-o$ BSF 3,5 ;Sei bit 5 of STATU S regjster, io
2€CF gitClearf
lLabellBCFf,b
label is omitable.
fis the address ofFile register
( 00hto 7Fh ). b = bitposiiion 0to ?
Operaton Thls command clearsthe specified bit El.D ;end oiihe prosram
(make LOW) offle register.
Flag Nochanqe
lnskucton
3€SF BitSetf
o
llabel) ESF lb -n
labelis omitable
fis lhe address ofFile register
( 00h to 7Fh ), b = bitposition 0 io 7
FIag No change
fnst-uclion
n tne PIC data books s!ch name as STATUS and PORTB ihat addrcsses number has been noled and each tme the
assern bler encounieE a conrmand refercnce to the addrcss
named LOOPIT tSlbstitLtes the number oflhat address
3'g"aa(&u{
4DEFINE
Define a te* sirbsttutng abelorcommand
Syniax
8SF35 BSF STATT]S 5
#DEFlll€ < name > <va:ue>
RSF DoFlB0 * hot"!,
One concept thai ycu are likeiy 10 see n P C son\rae
Eeo + Case Sensitivity
The nar.es are case sensrtve n otherwords on.e
s li.Fl oide1 n n9 a frcqlerly ! sed conrnaid Jorma( as a
s ng e name Each trme lhe assembler encouniers thar name
etojraaf,?*.. lave eq!at€{r a name wlh a Nlirnber i!nher !se oithis d!fng assembly. tl-re denned command will be substiuted rl
LABELS
END
i\a r cs p.Dlrair strnq addresses
wtr. g !e r to as
r/ !i LOOPIT a,. conimon y kro(n as t.ABEI.S
PIC Microcontroller Course 2OO4 Electmnics Training Centre Page 15
Program Commands ORG O
GOTO 5
4{)ECFSZ ORG 4
DECrease File Skipif Zero GOTO 5
ORG 5
Fo.mat {abe$DECFSZf, d CIRF STORE1
f- address offle registeri CLRF PORTB
d = 0 - store result in Wreg, .tQF PoPl A
d = 1, store resultin File reg BANIfi
CLRF TRISB
Op€ration: This command SUBSTRACTS 1 b CLRF TRISA
lhe contenb ofthe Fileregister, ifthe result is do
EANKO
^5
zero skip lhe next instuction.
BSF PORTB. O -'s"{ -, Vult"'\
Flag: No c+lange
DELAY NOP
)
bECFSZ STORE1. F
lnshuctbn 1 cycle;when skipping, itis 2 cycles @TO DELAY
MF i PoRTB,c
-CONFIGH'3FF3
:CP-OFF, WDT-DISABLE, PWnTE-ENABLE, OSC RC
#DEFINE EANKO BCF STATUS. 5 The sbrooline must always .ettm ttre processng io the
#DEFINEBANK1 BSFSTATUS, 5
RETLW Do n.t return by lhe instru.tion ofJUMP
STATUS EOU 3
PORTA EOU 5 sEck
TRISA ECIU 5 The slack is lhe memorylvhich stores tjre relrn address
PORTB EQU 6 of the prodram In the progran v/hlch use!; the subrouline, ii
TRISB EQU O jlmps io rhe slbrouline using the instsuclton of CALL At this
EOU 1 time, the return address rs stored in the stack When the
procesjng by the subroutne ends ard bre inslruciion of R5-
TURN is ex€qned, itjumps to the rctum address whrch is Mitten
STOREl EQUHOC-
inthe srack The stack allows acomDination or up to I program
PIC Microcootroller Co{rrse 2OO4 Electronics'taaining Centre Page 16
-m-+ the
']'e , er ite
rne th€ n ne
'er!.. Tuiorial 4
3lvlrUl4asrn
Settrng Pod B to outpLrt mode and ilash AN LED
C rcutdiagram as shown in turona l
TCALL
CALLS'rbrouiine
CP OiF WDT DISABLE FWRTE.ENABLE OSC RC
#DEF]NE BANKO BCF S'AIUS 5
laire ) CALL k
#DEF]NE BANK,] BSF STATUS 5
EQU 3
tioni ltlrfrps tothe subroLlrne EOU 5
,,i/h cr, ir: iere lleld s.afrl EAU 5
EQU 6
Fiag: No.|ang-- EQU O
6,fiETUIiN GCTO
RETURN fronr subroLrrlne
Formar: liabeilRETURN
€tR-{€RE?
Operation: ,i reiurns unconcii oraly Jror. tfe PORTB
subroutrne tolhe callng point,po fled by the address .IAF PoQ.t A
SETIJP BANKl
CLRF msB
TR SA
Flag: Nochanle BANKI
Go-ic IVIAIN
NOP
NOP
NOP
RETURN u,iih tera lnW NOP
RETURN
Flaq: Nochange
lnstructron 2 cyc es
!a
5
LIST P=16F84A,R=DEC
CONFIG H'3FF3' ;CP OFF WDT DISABLE, PWRTE.ENABLE, OSC-RC
#DEFINE BANKO BCF STATUS,5
#DEFINE BANK1 BSF STATUS,5
STATUS EOU 3
PO EQU 5
TRISA EGU 5
PORTB EQU 6
TRISB EOU 6
ORG O
GOTO 5
- ORG 4
GOTO 5
ORG 5
SETUP K1
CLRFTRISA ->
CLRFTRISE _,
KO
CLRFPORTB
LOOPIT BSF PORTB, O<
BSFPORTB,1
BSF PORTB,2
BSF PORTB,3
BSF PORTB,4
BSF PORTB,5
BSF PORTB,6
BSF PORTB. T
CLRF PORTB
GOTO LOOPIT
END
PIC Microcontroller Course 2OO4 Electronrcs Training Centre Fage 18
1o-MOVF Movef
Mo-veeontents ofW register to
For,nat: lt4OVF f.d Fil€ register
f s ihe addrcss of F le regrsier
(00hto7Fh ) tormatr UO\,,\I/F f
o s destrnahon seieci(0 or T ) '=IIe eg \ie. dLoress 00- ,r r,
C;rcrrt;.i: Th:s co,r'fr:i.lfrove (copy )ihe Operaii.nr Th s ccmmand copy the contenls o,
.cnlefrs .j ine F: e eg stei io (he Wregrsier Lo lhe Fi:e reg ster at
selected desl nator sPecried address
d = 0 9orethe resu t rn W
Flag: Nochange
Sto e trre resu i rn F reg'ster
lnstruction r.yce
lfsir!.tion Cycle: I cyc e cycle
Flagr Nocharge
lnstuuction T cycle lns{ruction 1cl..
Cycle
1z-BTFSC
ilrt lesl i Skrp ifClear
Rotate Left File reg'sterthrough Carry
anstruction l ayce
PIC Microcontroller Cours€ 2004 Electronlcs Trainlng Centrc Page 19
16{RF TI,TORIAL 7
Rotate RbhtFile rcgbterthrough Cany I3MTUTT ASM
iSsmeprooram asTutons 6, butusins Z naq and RRF
tonnaE KKF r,c icircuit diagran as shown inTubnal5. Speed conEolwith
f = File register address
d = destination select ( 0 o.1 ) LIST P=16F84A, R:DEC
_CONFTG H'3FF3 iCP-oFF, PWRTE-
uPerauoni ENABLE,WDT-OFF, RC OSC
of the File registerto 1 bit
dgltt induding
theCarrybit
d = o : Store the result in lfJ
EOU 3
d= 1 : Store the resuhin F
STATUS
POR'A EOU 5
TRIS,q EQU 5
Fbg: lt set the leastsignifcant bitofthe PORTB EOU 6
reqisterto C TRISB EQU 6
EQU O
EQU 1
TUTORIAL 6
c EQU O
z EOU 2
:3MTUT6 ASM
;-,o pedorm LEDS on FG'IB to light ono at a tm€ and shift
'rp ORG O
Ieft
GOTO 5
:Ci.cuitdiagram as shown rn Tutorial5, Speed codroiwifr
ORG 4
VR1
GOTOS
ORG5
LIST P=16F844, R=OEC
CONFIG H'3FF3'
SETUP BANKl
;cp-oFF, Pll/RTE-ENABLE. IADT-OFF, RC OSC CLRFTRISA
CI,RFTRISB
#DEFINE BANKO ACF STATUS, 5
#DEFINE BANKI BSF STATUS,5
t
BANKO
CLRF PORTB
STATUS EQU 3
EQU 5
TRISA EOU 5
LOOPJ l\,tov 8 10000000'
PORIB
.iRISB
mu6 MOVIF PORTB
ECFSTATUS, C
EOU 6
ECIU O LOOP2
c
EQU 1
EOU O
MovF il;i;;" ? z.o;?"tsl.o
BTFSSSTATUS, Z
GOTO LOO72
ORG O
GOTO LOOP1
GOTO 5
END
ORG 4
GOTO 5
ORG 5
e'-
.i
SETUP BANKl
CLRF
CLRF
BANKO
CLRF
lL
LOOP1 t
2< M{.,F
BCF STATUS, C
LOOP2 RL-F PORTB, F
BTFSS STATUS,C
GOTO LOO72
GOTO LOOPl
END
PIC Microcontroller Course 2004 €lectronics Training Centre Page 20
TIJTORIAL 8
'17-INCF
3L1IUT8 AS[i
Fo.warc/Rc'reise runn nS qht NCrease Frle regrster
C,c.1t Cia:;,, iL as sfosi n-r,rto:ial 5 Set Vil i!
Format: {label} INCF f d
f - address offLle reg ster
LSIP=r6F84A R=DEC d=0 store resuLt nWreS
CONFIG FI'3FF3 d = T store result n File reg
CP OFF PWRTE ENABLE \,!DT OFF I]C OSC
#DEFINE BANKO BCF STATUS 5
Operation: Thrs conmand add 1 to the contents
#DFF!N[ BANKl ASF STATI.IS 5
ollhe i re reJ,:l.rr
sthirls ;ilu : Flag: When esu i:s zerc Z=l.wher
res,,rlt is n3l z€rc Z=0
EOU 1
c
1 EQU 2 18-INCFSZ
lNCrease File Skip lf Zero
ORG !.
GO|C 5
Format: {labe} INCFSZf d
GOTO 5 f-address of file reglster
ORG 5 d = 0 -store resrlt rn Wreg
d= 1 siore resuli in File reg
SETUP AANK]
_r:itsA
Operationi ThrscoinandADD 1 to
IE Sts the contefls ofihe Fi ,. regrsterfthe resu I s zero
sk o th-a nerl ns(Lr.r .n
;!"- 0
Flagr Nocnange
.
t,r \ Jtr]OVLW
MOWlF PORIR lnstruction 1 cycle whe. sklpping i ls 2 cyc es
BCF STATITS C
.;FSC STATUS C
co'io IAIN3 1g.DECF
GOTO MAiN2 DECrease F'le "g ster
TN!
Operation: Thrs command SUBSTRACTS I l.
the contenis ofihe Fre reg ster
lnst.ucilon l iy.iE
PIC Microcontroller Cou.se 2004 Electrcnics Training Centre Page 21
SWAPF TUTORIAL 1O
3NITUTT O ASM
Format {abell SWAPFf, d t:nc.ement ofco!nts by pressing a sw tch
f - address offile registeri .Circu tdragram as shown in page 23 brir SA2 not used
i Control the speed by VRT
d= 0
store resutl in W reg,
d = 1 siore resu t tn Fte reg L ST P=16F8.1A, R=DEC
CONFIG H 3FF3'
OpeEtion: This command swaps lower nibbles ;CP.OFF PWRTE ENABLE WDT-OFE RC OSC
wlihtheuppernlbbles #DEFINE BANKO BCF STATUS 5
#DEFINE BANK1 BSF STATUS 5
END
PIc Microcontroller course 2004 Electronics Training Centre Page 22
6Ao
9Az r /'trDw
'DtLrLL
ITTORIAL,Il
:itTL fl i ASr\,r
Lrsrng trc s$iches cn Poi* .i,. io rrcrease PodB LEO I\4A N BANKO
_of its ( SAC:PA!) ..d'.h..ih. .nc to decrease PodB I ED
colni (SA2/RA2) SA0 takes prior iy +)
; c! Ld agram as s rowr ripage?3 COUNT
c.nlrcl lhe speed ry vR1 SWTCH
Chapter five
REGISTER FILE MPC
List File and PCL
q'r et r
The sr f e forrnal by |,4PAS[,] ,s straqhl
; "",llJ1l*r,r6
Thenrstco umn offoL.rrdrg ts s the address
luf
oi program memory wih n the P C atwnrclr the l
sirlcbon \ir' be p aced Note that it starts at 0000
and lhen Jumps ska ght to 0004 the eddress to
whch ORG 4 staler.ent rele6 Code ORG 5states E !rnFrere.!:rs h*
thatLheprooram tselfis to start at iocation 5 (0005).
l'G 1: ,di::h{#]r:i.er
bL:columnhlo wh chhoid the 2-byte code are nor
Ls.d !n1 acommand seicoLrntered lnihs.ese PCL { Prosram counter Lit€ral )
.crmand CLRF PORTA generales the code
The prograrn counier (PC ) specrfres the address of lhe nstruci on to
0r 85
felch forexeclion The PC is 13 btwde The LOW byte ( b i 0 ta bft 7) s
MALN s the nane il ven lo the address at caled the PCL register This rcg ster rs readable and w tabLe The H GH
whrch the comnrand CLRF COUNT has been byte { bitOio blt 12 ) rs ca ed the PCH regsterand s nold rect y readabe
pla@d Durng assembled by l'/IPAS[4, thaiaddress or witabe All lpdates to the PCH reg ster so rhrcuEh the PCLATII regrs-
nlmber has been noted and each trne the
assernblerencc!nters a command L.le.ence to the
address named [,1ALN itslbsnlliesihe number for
12 11 10 6 2 llo
Refeffrng to Lst ng TUT13 the ine reads PCH regisler PCL reo sier
ON18 OIAO CO64 fulAN CLRF COUNT
Nore ihai0C18 in co !mr 1
There s af other sb ternen l:
000c 2818 00038 Gofo MAN
,vo Jno --.t
""L PJ l.&
Now nolethe 18ln colurnn 2 The twovalles
( co lmn In line 64 and co lmn 2 in line 38 ) are
dentcal and ntentonal The address ior whrch
l.4A N stlre relerence name at ocaton(ADDRESS)
oooo2
00003
ffill *t
00012 STATUS
EQU
EQU
2
3
00005 00013 TRISA EOL] 5
00005 0!014 PORrA EQU 5
0c006 0c015 TRlsa EQU 6
00006 mfis PoRTB EQU 6
00000 00017 w EQU O
!0001 00018 F EQU 1
00000 00019 c EOIJ O
00002 cw20 z EQU 2
00001 0C02i 61, EQU 1
00020 00022 couNr
bao21 00023 3! .;v'
ce" 00024
aoi, ,' egog' H
otfr2s
2d05 0002ri GOTO 5 o\4* ,4nlnh' '.oAL
aaa27 ORG 4
28tr 00028 coTo
oR5,
5
. nao)o
SYI!,IBOLTABLE
LABEL VALUE
BANKO
BANKl
c
COIMANODE
COUNT 00000020
TC 00000001
00000001
GETKEY 0000001A
IVIAIN 00000018
OUTPUT 00000026
PCL 00000002
PORTA 00000005
PORTB 00000006
STATUS 00000003
S\4,/ITCH 00000021
TRLSA 00000005
TR]SB 00000006
ISTPRV 0000001E
00000000
z 00000002
16F84A 00000001
\
Program MemoryWords Used 40 ( X co ,D5
;r0 t0 RETLW 5
^sft.tian N bypas..rC (sk,poed)and ihe one bcyonC,r s
gJlF LOOP MOIF PORTA W
c020 CALL TABLE
fcre,.ample: aJ21 fuIO\,1\F PORTB
ItiePCL sstlu,rhenfamE yrtw aJtomat.€ ! Qa22 ooTo LooP
rr.l lne io and rhs rexi rsttu!tonwillbethat:tl3.ard
'lself
i ' .ne after at w b. at ir .ic lr somehow w. rlqve"o
,'1 add i to rhe PCI- wi)le,t rs stili 12 tne liCL !r'ill irlccmts !O2r- END
' .),Jtwlistr aod its c,M.,rarle of i :c rtsell m.irng'14
Tl ,n th,. orcgraniwll :!rnpsr:qirt f:cm 1: (a 1.l,.rnitnqg h ihe aooveerample afler inrtrallsatlon. and before a.y
rsr !p.rs) th€ rnslfl,!tron at 13 ',?l e "L... ur,rl_ca r" .TdGOTOIOOP , .--
Stuoy ! re fo owl.g t:rogrrm i only part ortfe prcqrarn ri)e tablecommands AL lhe irs! comrnand of oop sw tch Caia
ftor PORTA s bro!ght nto W Ihe CALLI€ble command tlrsr
roLrtesihe progmm to the f6Lcommand within the tab e ANDIW
08 tocP I ,vl,.lvLl/v 1 15 Ailhe commandADDu/F PCI Fth€ANDecvalre.er3i r!
OJ i4CVWF PORTB in !ry s a.lded to the PCL and the cornmand wthn ihe i?ble
10 BCF STATUS.C whrch corresponds to lhe newaddress rs pedormed
10
11 LOOP2 RLF PORIS F
12 BTFSS STATIJS.C ,Assu.ne Po.tc contents rs C000001(0lh)
13 GOTO LOOP2
GOTO LOOPl i]J LOOP MOVF PORTA W
Tutoriall2 22
Up Counter using 7 segment display
R86
;TUTORIAL 12 . 3MlUT12.AS[l
llp Couni ng on a 7'segrnent display CON]CATHODE ADD!\F PCL,F
RETLW 9'00111111 i0
st P=T6F84A, r=dec
RETLW 800000110 1
RETL\'! 8'01011011' 2
CONFIG H'3FFT
CP OFF PWRTE-ON,WDT-OFF CrystalXT OSC
RETLW B'O1OOT111' 3
f= 3 2768lvlHz RETLW B'O]1OO1TO' 4
RETLW 8 01101101 5
#DEFINE BANKO BCF STATUS 5 RETLW B'O1T111OT' 6
4DEFINE BANKl BSF STATUS 5 RETLW B 00000111 7
RETLW B'0T11,11T1' 8
PCL EQU 2 RETLW 8',01101111' 9
STATUS EQU 3
TRISA EOU 5 I\,!AIN CLRF COUNT
PORTA EOU 5
TRISB EOI-] 6 GETKEY
PORTB EQU 6 GOTO TSTPRV
WEQUO BCF SWTCH O
FEQU1 GOTO O]-]TPUT
C EOU O TSTPRV BTFSC SW]TCH, O
ZEQU2 GOTO GETKE'/
DC EQU 1
BSF SW TCH, O
COUNT EQU H 20' INCF CCUNT F
SWTCH EOU fi'21' I\,IOVF COI]NT W
ADDLW 6
ORG O
BTFSC STATUS DC
GOTO 5 CLRF OOUNT
ORG 4 OUTPUT rv . odt
GOTO 5 A 1 l'tr6+)<
ORG 5 CALL COIVICATHODE
I\,1O PORTB
CLRF PORTA GOTO GETKEY
CLRF PORTB
BANK,]
END
CLRF TRISB
[40vLW 800000007
BANKO
GOTO N4A N
l'lal Nl:frocontrotlcr Couse 2oo1 Lle(tlonirs Tr.rining ('crrtrc Page 9s
COIC TSTPR2
#DEF]NE BANKO ECF STATUS 5 3CF SWTCH 1
#D!F NE BANK' BSi STA]TUS 5 GO-,C OUTPUT
TSTPR2 BTF-SC SWTCH T
a_i !s E-OU
GOTQ GETKEY
EQU
3
5
BSF SWTCH 1
MovLw 1
EOU 5 SUB!,F COUNT. F
IRISB EOU 6 STFSS STATUS C
PORTB r-Qt 6 iO
GO SUBSFT
EOU O GOTO OUTPUT
EOU 1
QLi]i
BANKI
TR]SB 20,AND AND:iteralwith W
[4ov]_w 800000011
TRISA lLabellAN0LWk
BANKO label is onrliable
GOTO k iilera iie d 00h io FFh
OpeEtion Th s command execltes the add t on Operation Th s command executes lhe exclu
ofihe literaldata and the contents of sive OR (XOR) operal on oflhe
I the Wregrsterand the result is le contents of the fregrster and the !V
tained inWreg register
Fag Whenihe byte ovedlows, it seis C Fag When the result szero,Z=T (SET)
(C=1), when lhe 4 b ts of Lower part When Lhe resu t ls not zero Z = 0
ovedows. t sets DC When the resllt
rs zero, itsets z (z='1) lnstructon l cyc e
1 cycle
26-tORLW InclusiveOR literal with W
23-ADDWF ADDF and W
llabel|ORLW k
abe is om table
lLabellADDwFt,d k: iteralfie d 00h io FFh
labells omitab e
f= flle.eg ster address Operaton This command execules the inclu
d = 0 forWand d = 1 for Frle destr sive OR operation of the literaldala
and the contenls oftheW register
lnstrucllon
PIC Mirrccantroller Course 2004 Electronics Training Centre Page 30
29€UAWF SubtractWfromf
flabel]sUBWff,d
labelis omitable- I
l:file registe. address 00h to 7Fh;
l: destjnation select( 0 = W or 1 = F ,t
)
lnsaudion
2E'sUSLW subt'acrWfmm Literal
[LabeI}SUBLW fr
3060lrlF - coMplemenlthe contents ofthe File
k liieraltuid 00h to FFh register
L
n
:
2005
Part Two
Iriqthrh lldrtion. \'1i! 2005
{
i,
€o bl
U Maung Maung Myat
Etectfo , cs I raln tfig Lefltrt
Yangon, Mganmar
7
60
IJIC Microcorltroll cr Couse2ool flcctruricr 1-ra g Centre Pag€ 31
Chapter Six
Timer_Zero and applications
T:mer O (TMRo) - address Olh When used wlih T]\,1R0 the prescaler
The PlC16F84 has ore specla reg ster reserued lor use as an drvision ratios are 1r2 to 1 256 as shown above
IbitTmer,TMR0 li divides its input freq uency by 256 and can ae both iable The prescalercan aternatr\rely be
watlen io and read ftom Note thai f Tl,,lRo rs wrtten to the tirner is alocated for usewlth the Watchdog Trner
nh,b ted kom @!nting for t.!o clock cycles N,{ore lselulthan wring to (WDT), rn n,h ch mode each ofthese rattos s
IMRO s to use lts oliplt as a d vde by 256 counter and then use the
prescaLerto sub-.lLvde ihat rate as required The p.escaer drlrdes its PLC effective y runs at one quairer of the
nD'ri pulses by p.eseitab e powe6 oi i!/c Therc are e g h I possrble lnput clock irequency at prn 16 (OSC1/CLK N)
d v,sion rairos wh ch are set via bits 0 I and 2 (PSO, PSl and PS2) oi lllhen TMRo:s used as an interna Umer, the pu se
il counts also occur at one qlarter of the clock
frequency So lf the clock freq Liency (set by a crysia
OPTION REG
oscllator) s runn ng at 3 2768 N/Hz, TIT4RC wrl
coirnt 819200 Hz and rts 1 256 roll over ratewil be
lBPU INTEDG IOCS TOSE PSA PS2 PS1 PSO 3200 Hz This rate s then d vrded by the rat o sei
into the presca er lf we d ivided by 32, for exarnpie,
BTl 6 5 43 210 we oblarn raie of 100 Hz
bl7 FEE: PORTB pul-up enab e brt In Tl\4R0 mode when the prcscaler roils ove r
1= PORTB pullups are d sabled tozero, a fag is s-at in the INTCON regrster ai Dir2
{-0lE\ -le Fte_Lpl ca l oe nc-^Fo ov c ear ng
blt T0:E {INTCON bil 5) B t T0lF rnusi be cleared
= lnterrLpi on ris nS edge of RBO/ NT prn
1
0 = lnterupt on tal ing edge of RBO/|NT p n
bi5 ToCS TMRo C ock Solrce se ect b t
.,j,ar,tor or cAa-0ck p.
INTCON ( lnrerrupt control ) Register
lnle..upls
0 = lnternel irstruction cyc e clock
An nteffupl may be generated wh ch wilLc.lse
ihe PIC io dro! what t s do ng, take care of lhe rrme,
b14 diaie iask that it reeds to perform n response to the
rnterupland ihen reiurn to wlralltwas doing
M4/ToCK pin INTCCN reglster is used lo goba y enable n-
0 = lncrement on Low{o-High transltlon on ierr!pts and conlrol ihe response to dfferenl nterrrpl
M4/T0CKlprn
bi3 PSA Presca erAssrgnment bit INTCON brt narnes tlratends n E'are nterrl,pl
1 = Prescaier asslgned to ihe WDT -ndb e orls ,|/lp dnr n. ol g
Inieffupt requests wLLl set the coffespondrng rnterrupt
0 = prescala I ass igned io Tl\rlR0
request actve flags, wh ch has a bt name lharenlls rn
'F The requestactve fag musi be reset rn hardvr'are
brt2 -0 PS2- PSo P rescaler mte seleci blt and is not reset aLrlomalce y by the operat on ol the
bit0
01h
0Bh/8Bh RB]F
81h PSO
RETLWH'OE' '
RETLWH'38' L
RETLWH'54' n
RETLWH'3F' o
RETLWFl'73'
RETLWH'67' ,q
Tdtorial 15 RETLWH'50' I
RETLWH'6D ,S
3t/tTUT15 ASN4 RETLWH'78'
P'ocl!c nS:etters o.lre 7 seS d spLay
RETLWH'3E' ;U
h v+
'
1b 1
FIC icrocontroller Course 2004 Electronics Training Centre Page 34
,17
TUTORIAL
F-r i"J :;
COMCATI,]ODF
RFTLW B 00111111
RETIW 8',00
,B ITL'] LW 8 0101,'1011
RETLW B 0'1 00,'1 1 1 1
BCF INTCON,2
l:r4a!lT1 7 A St!]
!!.lliin.r !i i scl rale wtr BcD 10 59 co!rtro of 2 LED 7- GOTO IIIAN
-irqre ri.,sp ayr plrs lr25 Fc cou.ler MOVLW 25
fuLO\'\]\F CLKCNi
NcF clrisEc l:
rstp=16F44A. r=dec [401,F CLKSEC f,,
CLRF TR SA
CLRF TRSB
I
l\rovLvl/ B 1n000 10 o I o ENL)
"'oo I t|t'
lrlOVL\/11 25
clti c.KsEc
CLRF D]GIT
NTC(JN 2
GOTOtrtAtN
Gst$pdad - l{dn
v'
TUTORIAI, 1R
-{l-=
RB2
:: RB3
22 il RB!
pF !L nes
RB6
luz 'ecp
8C547 8C547
LA)
P"ANKO
[4ovLw
I.iOW\F CLKCNT
CLRF CLKSEC BCF iNTcoN2 yes
CLRF CLKi/]IN CALL CLXADD
CLRF CLKHRS
(tr)IO MAJN
ANDLW B'OOOO1111
XORLW 96 qA[ COMtrA]I]ODE
BTFSS STATIS Z CLRF FIORTA
t"to\l/!f PoRTB-
CALL DIGS'[A/
IIO\^/\F PORTA
IRSCLK D]G]T,W
'TNOVF
XORLW 1
BTFSC STATUS Z
BTFSC STATI]S,DC BSF PORTB,T
MO!\ lF CLKHRS tyes RE'IURN
, END
BTFSC STATUS Z
CIRF CLKT]RS
7'
, y* ," 11"'ttt1* hq+ ' turw qo.l.t.det fi *nf 4 'l*"' Pen
q
e. ll ea''
nS=0
Se!E- HlGfi toLOWand trren llcl
Ths cor.manddetermrnes 8-brda: 2 ies afd
lic ease conlrastlrtueto vsb e 2lines
3- To enter a character
RS = T (data enty mode )
Set data = 01000001 (4rh ) ihrs s the cod: ior a
n,,lr|' ry
. rn,l .(5
4_(,)
&) R.5'o
o) L'>v*
2 To set LCD to 2 l:nes, 8-bit data and 5x7 format 2. To set LCDio 2 lines, 8-bit data ancl 5x7 format
using Function Set Command using Function Sei Command
Code = 0011 1010 Code = 0011 T 010
RS=0
SetE -- HIGH to LOWandther flch Set E - HIGH lo LOW and then HlGil
3 llsing Display on/offCJ rsor command set 3. Using Display on/off Cursor co'nmand set
d'splay to ON, with undeni,re and Blinking crrsor display to ON, wiih underline and B:'nking cursor
Code = 0000 1111 Co.le=000! I l1
RS =0
SeiE H GH 1i LOW and then H GF Sei E - H Gl'l to LOWand the n I'lGH
rt. Enter all 26 cnaracters as fo:lows:
4 DisplayAddress Command code=0'i000001 (ior'A)
cursoraiaddress 0000 r000 (0Blr)
To set the RS ='1 (dala entry mode)
!si.g SetDspayAddresscommand.thec!rsorvr' lumpto Set E -- HIGH to LOW and ihen HlGn
Then enters clher.haracles sequentrally
address command = cursor OnJrAi.FrrLb3vsrae
-q!!play
address + 80h The Cusor/Drsplay Siifr command caf .oq De useC tr
scrol a tlre dls! ay lccaions to the eit sotratthelei€rsQ
code = l ooo looc .- '"'t' 9tD'ie
RS=0
Sei E - HICH to LOWancltrren f lcl_l
command code = 000r 1000 (18h)
Display edd resses 2Eh tc 3Fir ard 68h tc 7Fh Sei E -. HIGH 1o LOW afd then HICH
cannotbe !sa. Tne re:tonsh p bet.reer addresses and
Eaclr tme the cornrnaid rs entered and usinlt tlre E sw icl.
d splay ocrtsn vafles depend n! on the type of LCD
r.a! e be r! rsed Atypca l6character2lne sslrown the character c!n De sirltone place to tle Laft
00 01 a2 03 04 05 06 07 08 09 OA OB OC OD OE ol + u+tiov t9l
4F
PIC M icrocontroller Cout se 2OO4 E:ectronics Traininq Centre
t!. 0
::::\ )co;
l 2
0or I
4 5 6 7 8 9 B c D E F
l I
2 t-
3
I
5
l: i.i
:t ::q
B i
F
!itr
FS=0 E = H.L-H
Tir s sets Lh,p e rlry mocie to a!to- ncr3nrerU
.onr'ri )r ccd!, = 0001 1l0011c il
Set E - l_llcH 10 LOWafd ihe r H Gii
4. Enter a few number from 0 to 9
Erperiment4i Characterentry nrode re 30h to 39h
- S3.i s!riabe iuncno. sei Drspi.y cinrcli.riiiarx RS=- E = H-L-H
ch:.:cte.€pprars c!.lne flght-hanc afd sc.o"s as
clrrra.:r.s a . enl3r!o l!s: like : ncrma calcJlltDr
rls.0 E=ht-H
3 s.il characler enirv nrode corn! d
PIC Microcontroller Course 20O4 Electronics Train:ng Centre 4t
;3lvITUTl9 Using LCDwittr PIC iRS=o
;D splayrng a message BCF
N1OVLW H'OF'
ist p=16F84A R=DEC
r!lo!l^F PORfB
*h\cCONFIG H'3FF3'
CALL PULSE E
LUDE .P16Fe4A 'br) CALL SHORTDEL
#DEFINE BANKO BCF STATUS 5
CLRF CNTl
#DEFINE BANKl BSF STATUS.5
lnz/.ra
CBLOCK HOC cNtl CNT1 W
CNTl
,ELAll PORTA 2
DELAYT
DELAY2 )eAlz
ENDC ts o$q9 A,+a,)"$. ,uto, *,
I,IOVWF PORTB
ORG O
CALL PULSE E
GOTO 5
ORG 4
CALL SHORTDEL
GOTO 5
NCF CNT],W
XORLW 14
ORG 5
BTFSC STATUS,Z
INIT CNTl
GOTO STOP
n) DELAYl INCF CNTI,F
DELAY2 GOTO ]\,IESSAGE
STOP: GOTO STOP
oLD PORTB
SHORTDEL DECFSZ DELAY1 F
SETPORTS: BANKl GOTO SHORTDEL
TRISA RFTLW O
Circuitdiagrarn
-forLCDl asm
P lC Mic.ocontro ller Course E lectron ic Tra in in g Centre
CBLOCK rl OC
'!oO tr/ L,oro nsT,
usr l@ ,zl c,n, ?1,J:
CLKCNT
LCDMSE
STCRE
RSLNE
BsF RSLINE4
LCDMS2 I/iOVF
ENoc
CALL MESSAG
CAIL LCDOUT -)
ORG 0
GOTO 5
BTFSS LOOP3
ORG
GOTO 5
€OTO LCDMS2
ORG 5
NOMOte cOrO NOMORE
- -' - abt ort6
CLRi LCDOUT MOW'/F 5TC F i 5io*t -
CLRF
BANKl
( MOVLW 50
1 Nlo"v"F LooPA
CLRF DELAY 1oFc;s7 Io raF
CLRF
tull]v1\i/
TRSB a,b5 ]. eoro o'-rr'
N4(]!1,\F
CALL SENDT
BANKO
SEND]T SVLA:: STORE.F e qbu $q7* auEfl
GOTO
TABLCD ADDII]F
rvtovF sioRE.wi
AND-r]t 15
RETLW
RETLW
RET -V/
RETLW
RETLW
lttTLW
rtlTL!1r
RETLW
4f
PAi]S I t\rlovLw 5
[10!x1/F CLKCNT
,/i.::EssAG ADDWF pcL.F
RETLV/ 'R pause
CLRF NICON
RETLW E'
BTFSS INTCON,2
RETLVL]
RETLW
RETLV' :? BCF INTCON.2
RETLW
- ILTLV\]
itf rlw 'c
ENO
LCD IMODULE
"_ )",
lNcLuDE<P]6F84AlNC> epeo e t r n t BSF RSLINE.4
#DEFINE BANKN BCFSTAT S 4 - LCDMS2
+DEFINE BANKT BSF STATUS,s CPf,L MESSAG
CAL LCDOUT
CBLOCK H20 INCF
BTFSS
CLKCNT itll coTo LCDM52
CLKSEC llll CLRF CLKSEC
CLKMIN 211'l h, rntr, (c I' CLRF CLKMIN
CLKHRS L CLRI CLKNRS
STOREl MOVLW
STORE2 MO/\ /F CLKCNT
RSLNE
hLFSEC ITAN BTFSS )NttoN2 , t.l44o o,'.' laqJ
EVENT GOTO
BCF ]NTCON2
ENDC CALL CLKADD
GOTO MAIN
ORG 0
GOTO CLKADD DECFSZ CLKCNTF
ORG
GOTO 5 25'
ORG 5 CLKCNT
GEIKEY
HLFSEC F
BIFSC HLFSEC O
BANKl
800011111 RETURN
TRISB CLKSEC
810000101 FS
OPTION REG 3
BANKO
GOTO SETUP STOREl
LCDSt2 MOVIW
CATL TABLCD CAI LCDOUT
PIC Microcontroller course Electronic Training Centre 45
T IMSEI
ANDLY/ 15
oRLW 800110000
MOVLW 6
ANDLW 15
BTFS' S'IAiUS,DC
DELAY
GOTO DELAY SUB\ATF STORE2.F
CALL SEND T BTFSS STA1 L]S C
GOTC SUBSET
BTFSC STATUS.DC
GOTO ENDSUB
ANDLW B ]]1]OOOO
: Lr\ tsCF RSLINE4 ORLW 9
CALL LCDOUT
60T0 ENDSUts
sL_rBsE t NCF E\EN]-!!
G'TIIY
GOTO CHKS!\2 ENDSUB GOtr) CtKSltv
BSF EVENT,O
rvlovLr4r cL-KuFs \95q
C')TO TIIISET
aiKSL!2
BTFSS INTCON 2
GOTO
INTCCN 2
DEC:SZ
GOTO
ENO
LCD IMODULE
D7 D6 n5 n4 n3 D2 nl
16
3 2764
22p 22p
\u4 sod,E
SA0 ' minus, SA1'plus, SA2 - Minute,
SA3 - Hour, SA4- Clear sec
Fis 2. 24 hour Prc d_siraL"l9+ !,il.jj,
PIC Mic.ocontroller Course Electronic Training Centre 46
Chapter I
Programming Data EEPROM memory p-e,86 Lt" $
0 = The wrile operat on ls not comp ete or has not
DATA EEPROM MEMORY
?tostdrtu 4 dotg
bt3 I/VRERR: EEPROIV] EffoI F ag b|t
1 =Awnieoperaton rsprenraiureLyierrn nated
(any [4CLR Resei or any WDT Resetdurlng noma
nol drrecty mapped in the reg ster fle space lnsiead I s
tt ?norF,f te
bt7 5 Unimplemented:
After tlral aa@ ls copred lroh STOREI nlo W and then c.rnrand tvtOVF eEOATA W
PIC TUTOR
C]RCU T DIAGRAI\T] FOR 3IT,ITUT33
Binary to Hex converter
D7
D6
D5
tc1
PIC M icrocontroller Cou rse Electronic Training Centre 48
3MTUT23.ASM Ys,
Binaryto Hexadecimal converte. using Data EEPROM
LCDSET
llLstrating lse of€EPROrl rcad/M Le clri RSLINE
Convers on oiblnary to hexadecrma LCDST2
VALUE2
RSLINE
CLKCNT
STOREl
ORG 0
GOTO 5
INCVALl
5
5 INCVAI2
,,'4" qs J ."J-? :
STOREIT
goto MAN
B,ANK] NCVALO
800001111
INCVAL1
8100001r0 sHorwaLs
OPTiO].] REG
BANKO INCVAL2
SBO!\'VALS
MAIN
TABLCD
relN 8001100'11' STOREIT vlrueo,w
€dw 8001100'11'
EUw 8'00110010 EE
Edw 800000110
mo!4
retlw 800001100
mor {,1
VALUEI W
retM 8 0000000'j' STOREI
retlw 800000010
c SETPRM
B0000111't movf VALUE2 W
movuf STOREI
'0 molhv 2
'l *T SETFRJ]
'2
3 B 11001000 ) .B
As,r @L l€L'
5'
6
,q"43.Lff
rnovw
"4< 4 "[t
S
a SloAt call LCDOUT
9
'B
€ll LCDOUT
c
rnovw O
'D cal LCDOUT
E
ca LCDO]_IT
movlw E'
€L LCDOUT
PIC Mic rocontroller Course Electron ic Tralning Cenk€ 49
8110!1000
DLIN
<> LCOLIN: RSLINE.4
q,f,
.-- ' fl41 LCDOUT
oour 5e, Q, RSLINE4
rcDOUT
LCOOUT STORE
].cDour
$
LCDOUT DELAY
DELAY
LCDOUI SENDIT
LCDOUT SENDT
SHOV\VALS RSLINE,!1]
#$oao , V., ,,,a., vo
LCDOUI
LCOOUT
'f 5
LCDOUT CLKCNT
'0 INTCON
:cDoLn INTCON.2
LCDOUT
}NTCON 2
HEXTABLE
LC'OUT
HEXTABLE
LCDOUT ili]RITE DATATO EEPROM ROUTINE
B 1mfi000'
LCDLIN SETPRM:
BANKl
LCDOU-T EECONl,WREN
aANKO
LCDOUT STORE1 W
'f EEDATA
LCDOUT
''I BANK]
LCOOUT ll55
EECON2
LCOOUT
EECON2
HE(TPALE to'-l.i!l,L EECONl,I&R
LCTOUI
VALUEl,W EECON1 4
HEXTABLE
LCDO{JT EECONI. EN
811000000 EECON1.4
LCDL N BANi<O
rNrcoN6 ) Lirt ltrl .re-'
LCDOUT
LCDOUT EEDATA,W
HEXTABLE
PIC lllicroco ntroller Co u rse 2004 Electronics Training Cenlre Page 50
Chapter Nine
Projects on Sound Effects
$Y It tLY
A,algf" 9 t,,,i '2'
+5v b +ft,'l
L-,. .1. I qS
CLRF POR]B
CBLOCK H OC'
STOREl
STORE2
STORE3
DELAY2 OE SIORE4
DECFSZ SIORE2, F STORE5
coTo DELAY2 STORE6
RETURN STORET
ENDC
DECFsz STORE2 F
GOTO DELAY3
DECFSZ ORG O
c,oTo DELAY3 GOTO 5
ORG 4
DECf GOTO 5
sToREl, W ORG 5
TABLE
STORE6 SETIJP BANKI
CLFF TRISS
STOREl, W Fonb as ouiplr, PonA s rpulas a delault
-TABLE
BANKO
STORE5
PORTB,T CLRF STORE2
MAIN2 OELAY CLRF STORE3
DECFSZ CLRF STORE4
@TO CLRF STORES
BCF CLRF STORE6
MAN3 DELAY CLRF STORET
DECFSZ STOREs, F
GO tO SETUPl MOVLW 1
NOP
NOP
NOP
RETURN
C"q[ DELAY
DECFSZ STORE2, F
GOTO OELAY2
PIC Microcontroller Course 2004 Page 52
DECFSZ SIORE2, F
GOTO DELAY3 SETUP BANKI
DECFSZ STORE3 F TRSB
GOTO OEIAY3 &ANKO
GETKEY
GOTO GETKEY STORE2
DECF STOREI, F STORE3
sToRe.l
STORE5
CALL TABLE
S REN MOVLW H'30
INCF STOREI, F MO\'\\F STOREI
MOVF STORE1,W N10W1rF STORE2
CALL TABLE MOVLW H'50
MO\,l/\F STORE4 lvr STORE3
DELAY2
t:r8[ '*o
CALL DELAY2 COTO OFF
DEIAY2 DECFSZ STORE2, F
GOTO REPEAT
DECF STORE4. F
TABLE NCF STORE],F
STORET MO\f STORE,] W
MOVLW
STORET W DECFSZ STORE3, F
STATUS.Z GOTO REPEAT
GOIO GOTO SIREN
GOTO MAN
END
DELAY3
DELAY3 ;3MTUT27.ASM
GOTO ;Sound E fect 4: Creating a S:ren sound
iSIREN SOUND s geneGledwhen SA0 is pressed. Conlinue
END
;Press RESET butlon lo siop
5
PIC lllicrocontroll€. Coutse 2004 Electronics Traininq Centre Page 53
.N DECFSZ STOREs F
GOTO ON
MOW\F STORES
CLRF STOREI BCF PORTB.T
IiCVF STORE4 W
MCII\F STOREs
BSF PORTB 7
Chapter Ten
Using Interrupts
STARI NOP
GOTO STARf
p luT2a.AsM
;TMROINTERRUPT
INTTIVROASM
iProesslng lnieiilpl caused by OVERFLOWon TMRo
iThls program illoskales how inlerupt on TMRo ;ove.now can
lcl
LISTP=16i_34A, R=DEC
_coNFrcH3pFl
INCLUDE <P16F84A INC>
ERRORLEVEL -302
GOTO MAIN
ORG 4
GOTO INTRPT
ORGsj
CLRF PORTA
CLRF PORTB
BSF STATIJS 5
CLRF TRISA
CLRF TRISB
MO\tW 810000100'
I/IO!\AF OPTON REG
;3MTUT29,Aslrl
MOVLW B 10100000
;R3O/INYTNTERRUPT.
INTRBO-ASM
tProcessing ntenlpl elsed by changing onpin RBo/ NT This
NOP program demonstretes use ot:interupl on RBo/ NT UPon
GOTO LOOP
iaitJ6li.gedg€of lhe pusecomingtoRBo/|NTpr,prog.am
tunp6 lo:slbroulrne for processrng rnlerupl
BSF PORTB,T
,rhsrou ne lhen perfoms a @nain operation lhat
'blinks ihe LED of R87
NOP
r126AM9/1/2004
C
lnlo NOP LISTP=l5F34A R=DEC
itoP coNFlG H 3E4S 3!r I
tBor.", BCF PORTB,T !NCLUDE <PI€F84A INC>
scF tNTcoN.2 ERRORLEVEL .302
RETFIE ORG O
GOTO IVIAIN
END ORG
GOTO INTRPT
ORG 5
CLRF FIRTA
CLRF PORTB
BSF STATUS 5
MOVLW ]
MOVLW 811000111'
rvlo!1^,F OPTION_REG
ECF STATUS,g :
PIC Microcontroll€r Course 2004 Elect.onics Training Centre Page 55
GOTO LED2
GOTO LEO3
GOTO INTEND
BCF NTCON i LEDO
RETFE
GOTO INTEND
END
LEDl BCF PORTB 1
GOTO IN'IEND
teD2
GOTO INTEND
;3MTUT3O.ASM LED3
INTRB4T.AST]I
Pio€ssrng .lerupl caused by PORTCI.TAT\IGE ON R81 INTEi..]D BCF N-TCON O
i87 howr.teFupl car be empoyed for
ThLs DrograJn Llust ales RETFIE
fdr.atrng cha rles o pns RB4- Ra7 upof plshng a.y ollhe
swilches of PORTB. program enlers the interuptroutine and END
detem nes whrch pn.alses rnt€rupl
L ST P= 16F34A, RiDEC
GOTO N,I,A N
I
tF--
ca_
c1
E3