appendix b exemples
TRANSCRIPT
-
8/15/2019 Appendix b Exemples
1/65
• Home
• Development Tools
• Compilers
• Special Offer
• Easy Buy
• Projects
• Download
• Books
• Distributors
• Contact s
• Support
• TOC • !ntroduction
• C"# $
• C"# %
• C"# &
• C"# '
• C"# (
• C"# )
• C"# *
• C"# +
• C"# ,
• -pp# -
• -pp# B
• -pp# C
Appendix B: Examples
T"e purpose of t"is c"apter is to provide basic information about microcontrollers t"at one
needs to know in order to be able to use t"em successfully in practice# T"is c"apter. t"erefore.
does not contain any super interestin/ pro/ram or device sc"ematic wit" ama0in/ solutions#
!nstead. t"e followin/ e1amples are more proof t"at pro/ram writin/ is neit"er a privile/e nor
a talent issue but t"e ability of simply puttin/ pu00le pieces to/et"er usin/ directives# 2est
assured t"at desi/n and development of devices mainly consists of t"e followin/ met"od
3test4correct4repeat3# Of course. t"e more you are in it t"e more complicated it becomes since
t"e pu00le pieces are put to/et"er by bot" c"ildren and first4class arc"itects###
BASIC CONNECTING
-s seen in t"e fi/ure below. in order to enable t"e microcontroller to operate properly it is
necessary to provide5
•
Power Supply6• 2eset Si/nal6 and
http://www.mikroe.com/en/http://www.mikroe.com/en/http://www.mikroe.com/en/tools/http://www.mikroe.com/en/compilers/http://www.mikroe.com/en/offer/http://www.mikroe.com/en/tools/easybuy/http://www.mikroe.com/en/tools/easybuy/http://www.mikroe.com/en/projects/http://www.mikroe.com/en/download/http://www.mikroe.com/en/books/http://www.mikroe.com/en/distributors/http://www.mikroe.com/en/distributors/http://www.mikroe.com/en/contact/http://www.mikroe.com/en/support/http://www.mikroe.com/en/books/picmcubook/http://www.mikroe.com/en/books/picmcubook/ch0/http://www.mikroe.com/en/books/picmcubook/ch0/http://www.mikroe.com/en/books/picmcubook/ch1/http://www.mikroe.com/en/books/picmcubook/ch2/http://www.mikroe.com/en/books/picmcubook/ch3/http://www.mikroe.com/en/books/picmcubook/ch4/http://www.mikroe.com/en/books/picmcubook/ch5/http://www.mikroe.com/en/books/picmcubook/ch6/http://www.mikroe.com/en/books/picmcubook/ch7/http://www.mikroe.com/en/books/picmcubook/ch8/http://www.mikroe.com/en/books/picmcubook/ch9/http://www.mikroe.com/en/books/picmcubook/appa/http://www.mikroe.com/en/books/picmcubook/appc/http://www.mikroe.com/http://www.mikroe.com/en/http://www.mikroe.com/en/tools/http://www.mikroe.com/en/compilers/http://www.mikroe.com/en/offer/http://www.mikroe.com/en/tools/easybuy/http://www.mikroe.com/en/projects/http://www.mikroe.com/en/download/http://www.mikroe.com/en/books/http://www.mikroe.com/en/distributors/http://www.mikroe.com/en/contact/http://www.mikroe.com/en/support/http://www.mikroe.com/en/books/picmcubook/http://www.mikroe.com/en/books/picmcubook/ch0/http://www.mikroe.com/en/books/picmcubook/ch1/http://www.mikroe.com/en/books/picmcubook/ch2/http://www.mikroe.com/en/books/picmcubook/ch3/http://www.mikroe.com/en/books/picmcubook/ch4/http://www.mikroe.com/en/books/picmcubook/ch5/http://www.mikroe.com/en/books/picmcubook/ch6/http://www.mikroe.com/en/books/picmcubook/ch7/http://www.mikroe.com/en/books/picmcubook/ch8/http://www.mikroe.com/en/books/picmcubook/ch9/http://www.mikroe.com/en/books/picmcubook/appa/http://www.mikroe.com/en/books/picmcubook/appc/
-
8/15/2019 Appendix b Exemples
2/65
-
8/15/2019 Appendix b Exemples
3/65
microcontroller@# Dependin/ on w"ic" elements are in use as well as t"eir fre>uencies. t"e
oscillator can be run in four different modes5
•
uency w"ic" is isolated from c"an/es in temperature and volta/e power
supply as well# T"is fre>uency is normally labelled on t"e microcontroller packa/e#
-part from t"e crystal. capacitors C$ and C% must be also connected as per t"e sc"ematic
below# T"eir capacitance is not of /reat importance. t"erefore. t"e values provided in t"e table
s"ould be considered as a recommendation rat"er t"an a strict rule#
-
8/15/2019 Appendix b Exemples
4/65
-
8/15/2019 Appendix b Exemples
5/65
Ex!enal #s$illa!#
!f it is re>uired to sync"roni0e t"e operation of several microcontrollers or if for some reason
it is not possible to use any of t"e previous sc"ematics. a clock si/nal may be /enerated by an
e1ternal oscillator# 2efer to t"e fi/ure below#
A%%ITIONAL CO&PONENTS
2e/ardless of t"e fact t"at t"e microcontroller is a product of modern tec"nolo/y. it is of no
use wit"out bein/ connected to additional components# Simply. t"e appearance of volta/e ont"e microcontroller pins mean not"in/ if not used for performin/ certain operations ?turn
somet"in/ onFoff. s"ift. display etc#@#
T"is section intentionally covers only t"e most commonly used additional components in
practice suc" as resistors. transistors.
-
8/15/2019 Appendix b Exemples
6/65
for additional e1planation of "ow t"ese components operate# evert"eless. it is not so simple
in practice### T"en. w"at is it all about;
!t is about contact bounce4 a common problem wit" mec"anical switc"es# G"en t"e contacts
strike to/et"er. t"eir momentum and elasticity act to/et"er to cause bounce# T"e result is a
rapidly pulsed electrical current instead of a clean transition from 0ero to full current#
enerally. it mostly occurs due to vibrations. sli/"t rou/" spots and dirt between contacts#
T"is effect is usually unnoticeable w"en usin/ t"ese components in everyday life because t"e
bounce "appens too >uickly to affect most e>uipment. but causes problems in some analo/ueand lo/ic circuits t"at respond fast enou/" to misinterpret t"e on4off pulses as a data stream#
-nyway. t"e w"ole process does not last lon/ ?a few micro4 or milliseconds@. but lon/ enou/"
to be re/istered by t"e microcontroller# G"en usin/ only a pus"4button as a pulse counter.
errors occurs in almost $== of cases7
T"is problem may be easily solved by connectin/ a simple 2C circuit to surpress >uick
volta/e c"an/es# Since t"e bounce period is not defined. t"e values of components are not
precisely determined# !n most cases. it is recommended to use t"e values as s"own in fi/ure
below#
!f complete stability is needed t"en radical measures s"ould be taken7 T"e output of t"e
circuit. s"own in fi/ure below ?2S flip4flop@. will c"an/e its lo/ic state only after detectin/t"e first pulse tri//ered by contact bounce# T"is solution is more e1pensive ?SPDT switc"@.
but t"e problem is definitely solved7
-
8/15/2019 Appendix b Exemples
7/65
!n addition to t"ese "ardware solutions. t"ere is also a simple software solution# G"en a
pro/ram tests t"e state of an input pin and detects a c"an/e. t"e c"eck s"ould be done one
more time after a certain delay# !f t"e pro/ram confirms t"e c"an/e. it means t"at a
switc"Fpus"4button "as c"an/ed its position# T"e advanta/es of suc" solution are obvious5 it is
free of c"ar/e. effects of noises are eliminated and it can be applied to t"e poorer >uality
contacts as well#
RELAY
- relay is an electrical switc" t"at opens and closes under t"e control of anot"er electrical
circuit# !t is t"erefore connected to output pins of t"e microcontroller and used to turn onFoff
"i/"4power devices suc" as motors. transformers. "eaters. bulbs. etc# T"ese devices are almost
always placed away from t"e boards sensitive components# T"ere are various types of relays.
but all of t"em operate in t"e same way# G"en a current flows t"rou/" t"e coil. t"e relay is
operated by an electroma/net to open or close one or many sets of contacts# Similar to
optocouplers. t"ere is no /alvanic connection ?electrical contact@ between input and output
circuits# 2elays usually demand bot" "i/"er volta/e and current to start operation but t"ere arealso miniature ones t"at can be activated by a low current directly obtained from a
microcontroller pin#
-
8/15/2019 Appendix b Exemples
8/65
T"is fi/ure s"ows t"e most commonly used solution#
!n order to prevent t"e appearance of "i/" volta/e self4induction caused by a sudden stop of
current flow t"rou/" t"e coil. an inverted polari0ed diode is connected in parallel to t"e coil#
T"e purpose of t"is diode is to 3cut off3 t"e volta/e peak#
LE% %IO%ES
Iou probably know all you need to know about
-
8/15/2019 Appendix b Exemples
9/65
Qui$) *unin+
-
8/15/2019 Appendix b Exemples
10/65
• Dependin/ on w"et"er anodes or cat"odes are connected to t"e common pin. t"ere are
common anode displays and common cat"ode displays# T"e fi/ure above s"ows a
common anode display# uickly t"at one /ets
impression t"at all di/its of a number are active simultaneously#
Here is an e1planation on t"e fi/ure above# 8irst a byte representin/ units is applied on a
microcontroller port and a transistor T$ is activated simultaneously# -fter a w"ile. t"e
transistor T$ is turned off. a byte representin/ tens is applied on a port and transistor T% is
-
8/15/2019 Appendix b Exemples
11/65
activated# T"is process is bein/ cyclically repeated at "i/" speed for all di/its and
correspondin/ transistors#
- disappointin/ fact w"ic" indicates t"at t"e microcontroller is just a kind of miniature
computer desi/ned to understand only t"e lan/ua/e of 0eros and ones is fully e1pressed w"en
displayin/ any di/it# amely. t"e microcontroller does not know w"at units. tens or "undredsare. nor w"at ten di/its we are used to look like# T"erefore. eac" number to be displayed must
/o t"rou/" t"e followin/ procedure5
8irst of all. in a particular subroutine a multi4di/ital number must be split into units. tens etc#
T"en. t"ese must be stored in special bytes eac"# Di/its /et reco/ni0able format by
performin/ 3maskin/3# !n ot"er words. a binary format of eac" di/it is replaced by a different
combination of bits usin/ a simple subroutine# 8or e1ample. t"e di/it + ?==== $===@ is
replaced by binary number =$$$ $$$$ in order to activate all
-
8/15/2019 Appendix b Exemples
12/65
, = $ $ $ $ = $ $
!n addition to di/its from = to ,. t"ere are some letters4 -. C. E. J. 8. . H. uently used optocouplers are t"ose wit" p"ototransistors on t"eir
outputs# -dditionally. optocouplers wit" internal base4to4pin ) connection ?t"ere are also
optocouplers wit"out it@. t"e base may be left unconnected#
T"e 2FC network represented by t"e broken line in t"e fi/ure above denotes optionalconnection w"ic" lessens t"e effects of noises by eliminatin/ very s"ort pulses#
-
8/15/2019 Appendix b Exemples
13/65
LC% %ISPLAY
T"is component is specifically manufactured to be used wit" microcontrollers. w"ic" means
t"at it cannot be activated by standard !C circuits# !t is used for displayin/ different messa/es
on a miniature li>uid crystal display# T"e model described "ere is for its low price and /reat
capabilities most fre>uently used in practice# !t is based on t"e HD''*+= microcontroller? Hitachi@ and can display messa/es in two lines wit" $) c"aracters eac"# !t displays all t"e
letters of alp"abet. reek letters. punctuation marks. mat"ematical symbols etc# !n addition. it
is possible to display symbols made up by t"e user# Ot"er useful features include automatic
messa/e s"ift ?left and ri/"t@. cursor appearance.
-
8/15/2019 Appendix b Exemples
14/65
-
8/15/2019 Appendix b Exemples
15/65
-
8/15/2019 Appendix b Exemples
16/65
T"e addresses of C2O9 memory locations matc" t"e c"aracters of -SC!!# !f t"e pro/ram bein/ currently e1ecuted encounters a command 3send c"aracter P to port3 t"en t"e binary
value =$=$ ==== appears on t"e port# T"is value is t"e -SC!! e>uivalent to t"e c"aracter P# !t
is t"en written to
-
8/15/2019 Appendix b Exemples
17/65
G"at is -SC!!; 8rom t"eir inception till today. computers can reco/ni0e only numbers. but
not letters# !t means t"at all data a computer swaps wit" a perip"eral device "as a binary
format even t"ou/" t"e same is reco/ni0ed by t"e man as letters ?T"e keyboard is an e1cellent
e1ample@7 !tMs as simple as t"at4 every c"aracter matc"es t"e uni>ue combination of 0eroes
and ones# -SC!! is c"aracter encodin/ based on t"e En/lis" alp"abet# -SC!! code specifies a
correspondence between standard c"aracter symbols and t"eir numerical e>uivalents#
LC% Basi$ C#mmands
-ll data transferred to
-
8/15/2019 Appendix b Exemples
18/65
W-a! is Bus. 9la+
Compared to t"e microcontroller. t"e
-
8/15/2019 Appendix b Exemples
19/65
-
8/15/2019 Appendix b Exemples
20/65
!t is not a mistake7 !n t"is al/orit"m. t"e same value is transferred t"ree times in a row#
!n case of '4bit initiali0ation. t"e procedure is as follows5
-
8/15/2019 Appendix b Exemples
21/65
-
8/15/2019 Appendix b Exemples
22/65
-
8/15/2019 Appendix b Exemples
23/65
T"e purpose of t"e "eader and initial directives is briefly described below#
'eade:
T"e "eader is placed at t"e be/innin/ of t"e pro/ram and /ives basic information in t"e form
of comments ?name of t"e pro/ram. release date etc#@# Don:t be deluded into t"inkin/ t"at
after a few mont"s you will know w"at t"at pro/ram is about and w"y it is saved in your
computer#
Ini!ial die$!i=es:
list p=16f887
T"is directive defines processor to e1ecute a pro/ram#
-
8/15/2019 Appendix b Exemples
24/65
#include
-
8/15/2019 Appendix b Exemples
25/65
((c"nfi)
T"is directive is used to include confi/ word in t"e pro/ram upon compilin/# !t is not
necessary because t"e same operation is performed by software for loadin/ pro/ram into c"ip#
However. do you "ave any idea w"ic" software will be used by t"e end user; G"at options
will be set by default; Iou are t"e end user;7 Do you know w"ic" pro/ram you will be usin/for 9C pro/rammin/ ne1t year; 9ake life easier for yourself. take t"is directive as a
necessary one and include it in your pro/ram#
E;A&PLE 8
Usin+ p#+am l##p and in!enal #s$illa!# L/INTOSC
T"is is a continuation of t"e previous e1ample. but deals wit" a bit more complicated
problem### T"e idea is to make t"e
-
8/15/2019 Appendix b Exemples
26/65
Example 8: ,
, .ea#er,, D+/I&I&* 2I3+S cbloc% 0"50 , 3loc% of 6ariables starts at a##ress 50h conter1 , ariable 7conter17 at a##ress 50h en#c, org 0"0000 , ##ress of the first program instrction
ban%sel SCC& , Selects memory ban% containing , register SCC& bcf SCC& , Selects internal oscillator /I&'SC with bcf SCC&! , the fre9ency of :1;.<
bcf SCC&4 bsf SCC&0 , icrocontroller ses internal oscillator
ban%sel '2IS3 , Selects ban% containing register '2IS3 clrf '2IS3 , ll port 3 pins are configre# as otpts ban%sel >2'3 , Selects ban% containing register >2'3 loop mo6lw 3?01010101? , 3inary nmber 01010101 is written to @ mo6wf >2'3 , &mber is mo6e# to >2'3 mo6lw h?//? , &mber h// is mo6e# to @ mo6wf conter1 , &mber is mo6e# to 6ariable 7conter17
loop1 #ecfs< conter1 , ariable 7conter17 is #ecremente# by 1 goto loop1 , If reslt is 0 contineA If not
-
8/15/2019 Appendix b Exemples
27/65
, remain in loop1
mo6lw 3?10101010? , 3inary nmber 10101010 is mo6e# to @ mo6wf >2'3 , &mber is mo6e# to >2'3 mo6lw h?//? , &mber h// is mo6e# to @ mo6wf conter1 , &mber is mo6e# to 6ariable 7conter17
loop5 #ecfs< conter1 , ariable 7conter17 is #ecremente# by 1 goto loop5 , If reslt is 0 contineA If not , remain in loop5
goto loop , *o to label loop en# , +n# of program
E;A&PLE 7
Usin+ nes!ed l##p
T"e connection sc"eme is a/ain t"e same# To make t"is a bit more interestin/. a different
combination of port B bits c"an/e eac" ot"er# -nd. t"atMs not all of course# -s seen from t"e
previous two e1amples. t"e microcontroller is very fast and often. it needs to be slowed down#
T"e use of t"e built4in oscillator uart0
oscillator# Precisely speakin/. t"e number of pulses amounts to appro1imately $,) === since it
also takes some time to e1ecute jump instructions and decrement instructions# Ies. itMs true.
t"e microcontroller mostly waits and does not"in/###
Example 7: , .ea#er , D+/I&I&* 2I3+S
cbloc% 0"50 , 3loc% of 6ariables starts at a##ress 50h conter1 , ariable 7conter17 at a##ress 50h conter5 , ariable 7conter57 at a##ress 51h en#c, org 0"0000 , ##ress of the first program instrction
ban%sel '2IS3 , Selects ban% containing register '2IS3 clrf '2IS3 , Clears '2IS3
ban%sel >2'3 , Selects ban% containing register >2'3loop mo6lw 3?11110000? , 3inary nmber 11110000 is mo6e# to @ mo6wf >2'3 , &mber is mo6e# to >2'3 mo6lw h?//? , &mber h// is mo6e# to @ mo6wf conter5 , &mber is mo6e# to 6ariable 7conter57 loop5 mo6lw h?//? , &mber h// is mo6e# to @ mo6wf conter1 , &mber is mo6e# to 7conter17loop1
-
8/15/2019 Appendix b Exemples
28/65
#ecfs< conter1 , Decrements 7conter17 by 1A If reslt is0 goto loop1 , s%ip ne"t instrction
#ecfs< conter5 , Decrements 7conter57 by 1A If reslt is0
goto loop5 , s%ip ne"t instrction mo6lw 3?00001111? , 3inary nmber 00001111 is mo6e# to @
mo6wf >2'3 , &mber is mo6e# to >2'3 mo6lw h?//? , &mber h// is mo6e# to @ mo6wf conter5 , &mber is mo6e# to 6ariable 7conter57 loop4 mo6lw h?//? , &mber h// is mo6e# to @ mo6wf conter1 , &mber is mo6e# to 6ariable 7conter17loop: #ecfs< conter1 , Decrements 7conter17 by 1A If reslt is0
, s%ip ne"t instrction goto loop: #ecfs< conter5 , Decrements 7conter57 by 1A If reslt is0 goto loop4 , s%ip ne"t instrction goto loop , Bmp to label loop en# , +n# of program
Example 6
Usin+ !ime T&R2 and In!eup!s
!f you "ave read t"e previous e1ample. you would "ave noticed a disadvanta/e of providin/delays usin/ loops# !n all t"ese cases. t"e microcontroller is 3captive3 and does not"in/# !t
simply waits for some time to pass# Suc" wastin/ of time is an unacceptable lu1ury and some
ot"er met"od s"ould be applied#
Do you remember t"e story about t"e timers; -bout interrupts; T"is e1ample makes links
between t"em in a practical way# T"e sc"ematic is still t"e same as well as t"e c"allen/e# !t is
necessary to provide delay lon/ enou/" to notice c"an/es on a port# T"is time. t"e timer
T92= wit" t"e assi/ned prescaler is used for t"at purpose# !nterrupt occurs on every timer
re/ister overflow and interrupt routine increments t"e number in port B by $# T"e w"ole
procedure is performed 3be"ind t"e scenes3 of t"e w"ole process. w"ic" enables t"e
microcontroller to do ot"er t"in/s#
-
8/15/2019 Appendix b Exemples
29/65
Pay attention to a few details5
• Even t"ou/" it is unnecessary in t"is case. t"e contents of t"e most important re/isters
?G. ST-TS and PC
-
8/15/2019 Appendix b Exemples
30/65
, S'2' / >2*2 org 0"0000 , ##ress of the first programinstrction goto main , *o to label 7main7
, I&'+22>' 2'I&+ org 0"0004 , Interrpt 6ector mo6wf wtemp , Sa6es 6ale in register @ mo6f S''S , Sa6es 6ale in register S''S mo6wf statstemp mo6f >C'. , Sa6es 6ale in register >C'. mo6wf pclathtemp
ban%sel >2'3 , Selects ban% containing >2'3 incf >2'3 , Increments register >2'3 by 1
ban%sel I&'C& , Selects ban% containing I&'C&
bcf I&'C&'20I/ , Clears interrpt flag '20I/ mo6f pclathtempw , >C'. is gi6en its original content
mo6wf >C'. mo6f statstempw , S''S is gi6en its original content mo6wf S''S swapf wtempf , @ is gi6en its original content swapf wtempw
bsf I&'C&*I+ , *lobal interrpt enable# retfie , 2etrn from interrpt rotine , I& >2*2main , Start of the main program
ban%sel &S+ , 3an% containing register &S+ clrf &S+ , Clears registers &S+ an# &S+. clrf &S+. , ll pins are #igital
ban%sel '2IS3 , Selects ban% containing register '2IS3 clrf '2IS3 , ll port 3 pins are configre# asotpts
ban%sel >'I&2+* , 3an% containing register >'I&2+* bcf >'I&2+*'0CS , '20 conts plses from oscillator
bcf >'I&2+*>S , >rescaler is assign to timer '20
bsf >'I&2+*>S0 , >rescaler rate is 1E5! bsf >'I&2+*>S1 bsf >'I&2+*>S5
ban%sel I&'C& , 3an% containing register I&'C& bsf I&'C&'20I+ , '20 interrpt o6erflow enable# bsf I&'C&*I+ , *lobal interrpt enable#
ban%sel >2'3 , 3an% containing register >2'3 clrf >2'3 , Clears port 3loop goto loop , 2emain here
en# , +n# of program
-
8/15/2019 Appendix b Exemples
31/65
Example 5
Usin+ su*#u!ine> usin+ pus-(*u!!#ns
!n t"e previous e1amples t"e microcontroller e1ecutes t"e pro/ram wit"out bein/ influenced
in any way its surroundin/# !n practice. devices operatin/ in t"is way are very rare ?fore1ample. simple neon si/ns@# Iou /uess. amon/ ot"er components. input pins will also be
used in t"is e1ample# T"ere is a sc"ematic in t"e fi/ure below. w"ile t"e pro/ram is on t"e
ne1t pa/e# Everyt"in/ is still very simple#
-t t"e be/innin/ of t"e pro/ram. immediately upon definin/ variables. t"e microcontroller
pins are confi/ured by usin/ re/isters T2!S- and T2!SB#
!n t"e main pro/ram. one bit on port B is set first# T"en t"e contents of t"is re/ister is
constantly moved by one place to t"e left ?instruction rlf PO2TB@# !t /ives us t"e impression
t"at t"e lit
-
8/15/2019 Appendix b Exemples
32/65
,, .ea#er,
, D+/I&I&* 2I3+S
cbloc% 0"50 , 3loc% of 6ariables starts at a##ress50h conter1 , ariable 7conter17 at a##ress 50h conter5 , ariable 7conter57 at a##ress 51h en#c , 3loc% of 6ariables en#s , I& >2*2
org 0"0000 , ##ress of the first programinstrction ban%sel &S+ , Selects ban% containing register &S+ clrf &S+ , Clears registers &S+ an# &S+. to clrf &S+. , configre all inpts as #igital
ban%sel '2IS3 , Selects ban% containing register '2IS3 clrf '2IS3 , ll port 3 pins are configre# asotpts mo6lw 3?00000010? mo6wf '2IS , >in 21 is inpt
ban%sel >2'3 , Selects ban% containing register '2IS3 mo6lw 3?00000001? , @rites 1 to register @ mo6wf >2'3 , &mber is mo6e# to >2'3loop rlf >2'3 , >ort 3 bits rotates by one place left call D+F , Calls sbrotine 7D+F7loop: btfss >2'1 , 'ests the firs port bit goto loop: , 707 is applie# to pinA*o to label7loop:7 goto loop , 717 is applie# to pinA*o to label7loop7 , S32'I&+SD+F clrf conter5 , Clears 6ariable 7conter57loop1 clrf conter1 , Clears 6ariable 7conter17loop5 #ecfs< conter1 , Decrements 6ariable 7conter17 by 1 goto loop5 , 2eslt is not 0A *o to label loop5 #ecfs< conter5 , Decrements 6ariable 7conter57 by 1 goto loop1 , 2eslt is not 0A *o to lab loop1 retrn , 2etrn from sbrotine 7D+F7
en# , +n# of program
E;A&PLE 4
-
8/15/2019 Appendix b Exemples
33/65
T&R2 as a $#un!e> de9inin+ ne, =aia*les> usin+ ela.
T"is time. T92= is used as a counter# T"e idea is to connect t"e counter input to one
pus"button so t"at it counts one pulse at a time upon every button press# G"en t"e number of
counted pulses becomes e>ual to t"e number in re/ister TEST. lo/ic one volta/e ?(A@ will be
applied to t"e PO2TD. & pin# Since t"is volta/e activates an electro4mec"anical relay. t"is bitis called t"e same4 32elay3#
!n t"is e1ample. t"e TEST re/ister contains number (# aturally. it could be any number and
could be calculated or entered via t"e keyboard# !nstead of a relay. t"e microcontroller can
activate some ot"er device and instead of pus"4buttons it can use sensors# T"is e1ample
illustrates one of t"e most common uses of t"e microcontroller in industry# G"en somet"in/ is
done as many times as needed. t"en somet"in/ else s"ould be switc"ed on or off###
Example 4: ,, .ea#er,
, D+/I&I&* 2I3+S
-
8/15/2019 Appendix b Exemples
34/65
-
8/15/2019 Appendix b Exemples
35/65
T"ere are several ways to solve t"is problem# T"is pro/ram uses pro/ram delay known as
debounce# Basically. it is a simple procedure# pon input c"an/e detection ?button press@. a
s"ort pro/ram delay is provided and t"e pro/ram waits for anot"er c"an/e ?button release@#
Only after t"is. t"e pro/ram comes to a conclusion t"at t"e button is activated#
!n t"is very case. t"e pus"4button is tested by means of macro called button# Besides. t"is
macro contains a pro/ram delay w"ic" is provided by means of anot"er macro pausems#
T"e main pro/ram is relatively simple and enables t"e variable 3cnt3 to be incremented and
decremented by usin/ two pus"4buttons# T"is variable is t"ereafter copied to port B and
affects t"e
-
8/15/2019 Appendix b Exemples
36/65
incl#e 7paseAinc7 incl#e 7bttonAinc7
,main ban%sel &S+ , Selects ban% containing &S+
clrf &S+ , ll pins are #igital clrf &S+.
ban%sel '2IS3 bsf '2IS 0 bsf '2IS 1 clrf '2IS3
ban%sel >2'3 clrf cntoop btton >2'00Increment btton >2'10Decrement
goto oop Increment incf cnt f mo6f cnt w mo6wf >2'3 goto oopDecrement #ecf cnt f mo6f cnt w mo6wf >2'3 goto oop en# , +n# of program
&a$# ?pausems? ,pasems C2 arg1 local oop1 local #echi local Delay1ms local oop5 local +n#
mo6lw .igh(arg1) , .igher byte of argment is mo6e# , to .Icnt mo6wf .Icnt mo6lw ow(arg1) , ower byte of argment is mo6e#
, to cnt mo6wf cntoop1 mo6f cnt f , Decrements .Icnt an# cnt while btfsc S''S H , nee#e# an# calls sbrotine Delay1ms
goto #echi call Delay1ms #ecf cnt f goto oop1#echi mo6f .Icnt f btfsc S''S H goto +n# call Delay1ms #ecf .Icnt f
-
8/15/2019 Appendix b Exemples
37/65
#ecf cnt f goto oop1Delay1msE , Delay1ms pro6i#es #elay of mo6lw A100 , 10010s=1ms mo6wf >cnt , >cntJ-100oop5E
nop nop nop nop nop nop nop #ecfs< >cnt f goto oop5 , +"ection time of oop5 retrn , is 10 s+n# +&D,
&a$# ?*u!!#n? ,btton C2 portpinhilolabel local >resse#1 , ll labels are local local >resse#5 local +"it1 local +"it5
I/&D+/ #ebonce#elay , +nables #ebonce time to be #efine# , in main program G#efine #ebonce#elay A10 +&DI/
I/ (hilo == 0) , If pll-p se# btfsc port pin , If 717 psh-btton is presse# goto +"it1 pasems #ebonce#elay , @ait for 10ms #ebonce>resse#1 btfss port pin goto >resse#1 pasems #ebonce#elay , @ait ntil release# an# goto label , mp to specifie# a##ress+"it1 +S+ , If pll-#own se# btfss port pin goto +"it5 , If 707 psh-btton is release#
pasems #ebonce#elay , @ait for 10ms #ebonce>resse#5 btfsc port pin goto >resse#5 pasems #ebonce#elay , @ait ntil release# an# goto label , mp to specifie# a##ress+"it5 +&DI/
+&D,
E;A&PLE @
Usin+ !ime T&R1 and usin+ in!eup!
-
8/15/2019 Appendix b Exemples
38/65
-
8/15/2019 Appendix b Exemples
39/65
-
8/15/2019 Appendix b Exemples
40/65
T"is e1ample illustrates t"e use of timer T92%# T"e microcontroller uses internal oscillator
H8!TOSC wit" t"e fre>uency of (== kH0# T"e w"ole pro/ram works as follows5 -fter t"e
period of time defined by re/ister P2. prescaler and postscaler "as e1pired. an interrupt
occurs# !nterrupt routine decrements t"e content of t"e P2 re/ister and simultaneously
increments t"e content of port B# Since t"e number in re/ister P2. w"ic" determines w"en
interrupt is to occur is constantly decremented. interrupt will occur for s"orter and s"orter periods of time# !n ot"er words. countin/ will be carried out faster# - new cycle of accelerated
countin/ starts after every re/ister P2 overflow#
Example : , .ea#er, D+/I&I&* 2I3+S
cbloc% 0"50 , 3loc% of 6ariables starts at a##ress50h
wtemp , ariable at a##ress 50h pclathtemp , ariable at a##ress 51h statstemp , ariable at a##ress 55h en#c
, >2*2 S'2' org 0"0000 , ##ress of the first programinstrction goto main , Bmp to label 7main7
, I&'+22>' 2'I&+
org 0"0004 , Interrpt 6ector mo6wf wtemp , Sa6e register @
mo6f S''S , Sa6e register S''S mo6wf statstemp
mo6f >C'. , Sa6e register >C'. mo6wf pclathtemp
ban%sel >2'3 , Selects ban% containing >2'3 incf >2'3 , Increments >2'3 register by 1 ban%sel >25 , Selects ban% containing >25
#ecf >25 , >25 is #ecremente# by 1 mo6f pclathtempw , >C'. is gi6en its original state mo6wf >C'. mo6f statstempw , S''S is gi6en its original state mo6wf S''S swapf wtempf , @ is gi6en its original state swapf wtempw
ban%sel >I21 , Selects ban% containing >I21 bcf >I21'25I/ , Clears interrpt flag '25I/
bsf I&'C&*I+ , *lobal interrpt enable# retfie , 2etrn from interrpt rotine , I& >2*2
-
8/15/2019 Appendix b Exemples
41/65
main , Start of the main program ban%sel SCC& , Selects ban% containing registerSCC& bcf SCC& , Selects internal oscillator ./I&'SCwith bsf SCC&! , fre9ency of !00;.<
bsf SCC&4 bsf SCC&0 , icrocontroller ses internaloscillator
ban%sel &S+ , Selects ban% containing register &S+ clrf &S+ , Clears registers &S+ an# &S+. clrf &S+. , ll pins are #igital
ban%sel '2IS3 , Selects ban% containing register '2IS3 clrf '2IS3 , ll port 3 pins are configre# asotpts clrf >25
ban%sel '5C& , Selects ban% containing register '5C& mo6lw .?//? , Sets all control register bits mo6wf '5C& , prescaler=1E1 postscaler=1E1'25=& clrf >2'3
ban%sel >I+1 , Selects ban% containing register >I+1 bsf >I+1'25I+ , '25 interrpt enable# bsf &'C&>+I+ , >eripheral mo#les interrpt enable# , 'imer '25 belongs to peripheralmo#les bsf I&'C&*I+ , *lobal interrpt enable#loop goto loop , 2emain here en# , +n# of program
E;A&PLE 12
dule CCP1 as PW& si+nal +enea!#
Since t"e CCP modules "ave a wide ran/e of possibilities t"ey are commonly used in practice#
T"is e1ample illustrates t"e use of CCP$ module in PG9 mode# Bits of t"e CCP$CO
re/ister determine t"at t"e module operates as a sin/le4output PG9# T"e same bits determine
t"e PG9 fre>uency to be '#++ kH0# To make t"in/s more interestin/. t"e duration of t"e
output P$- ?PO2TC.%@ pulses may be c"an/ed by means of pus"4buttons symbolically called3D-23 and 3B2!HT3# Pus"4buttons are tested in interrupt routine initiated by t"e timer
T92$# -ny c"an/e affects t"e
-
8/15/2019 Appendix b Exemples
42/65
Example 12: , .ea#er, D+/I&I&* 2I3+S cbloc% 0"50 , 3loc% of 6ariables starts at a##ress50h wtemp , ariable at a##ress 50h
pclathtemp , ariable at a##ress 51h statstemp , ariable at a##ress 55h en#c
G#efine D2; >2'30 , >sh-btton 7D2;7 is connecte# , to >2'30 pin G#efine 32I*.' >2'31 , >sh-btton 732I*.'7 is connecte# , to >2'31 pin, >2*2 S'2'
org 0"0000 , /irst program instrction a##ress goto main , Bmp to label 7main7
, I&'+22>' 2'I&+
-
8/15/2019 Appendix b Exemples
43/65
org 0"0004 , Interrpt 6ector
mo6wf wtemp , Sa6e register @
mo6f S''S , Sa6e register S''S
mo6wf statstemp mo6f >C'. , Sa6e register >C'.
mo6wf pclathtemp
ban%sel CC>21 btfss D2; , 'ests psh-btton 7D2;7 #ecf CC>21 , >sh-btton is presse# - #ecrementCC>1 by 1 btfss 32I*.' , 'esting psh-btton 732I*.'7 incf CC>21 , >sh-btton is presse# - incrementCC>1 by 1
mo6f pclathtempw , >C'. is gi6en its original content mo6wf >C'. mo6f statstempw , S''S is gi6en its original content mo6wf S''S swapf wtempf , @ is gi6en its original content swapf wtempw
ban%sel >I21 , Selects ban% containing >I21 bcf >I21'21I/ , Clears interrpt flag '21I/
bsf '21.$ , ccelerates timer '20 conting bsf '21. , bsf I&'C&*I+ , *lobal interrpt enable# retfie , 2etrn from interrpt rotine , I& >2*2
main , Start of the main program ban%sel &S+ , Selects ban% containing register &S+ clrf &S+ , Clears registers &S+ an# &S+. clrf &S+. , ll pins are #igital
ban%sel >'I&2+* , Selects ban% containing register &S+ bcf >'I&2+*$ , >ll-p resistors enable#
bsf @>30 , >ll-p resistors enable# bsf @>31 , on port 3 pins 0 an# 1
ban%sel '2ISC , Selects ban% containing register '2ISC clrf '2ISC , ll port C pins are configre# asotpts
ban%sel '1C& , Selects ban% containing register '1C& bcf '1C&'21CS , '21 operates as a timer bcf '1C&'1C;>S0 , >rescaler rate is 1E8 bcf '1C&'1C;>S1 bsf '1C&'21& , cti6ates timer '21
ban%sel >I+1 , Selects ban% containing register >I+1 bsf >I+1'21I+ , Interrpt '21 is enable#
bsf I&'C&>+I+ , >eripheral mo#les interrpts are , enable#
-
8/15/2019 Appendix b Exemples
44/65
bsf I&'C&*I+ , *lobal interrpt enable#
mo6lw 3?11111101? , >rescaler '25 = 1E4 ban%sel '5C& mo6wf '5C& mo6lw 3?11111111? , &mber in register >25
ban%sel >25 mo6wf >25
ban%sel CC>1C& mo6lw 3?00001100? , 3its to configre CC>1 mo#le mo6wf CC>1C&loop goto loop , 2emain here en# , +n# of program
E;A&PLE 11
Usin+ Auite simple# - variable
analo/ si/nal is applied on t"e -% pin w"ile t"e result of conversion is s"own on port B as a
binary number# !n order to simplify t"e pro/ram as muc" as possible. only + lower bits of t"e
result of conversion are s"own# D is used as a ne/ative volta/e reference Aref4. w"ile
positive volta/e reference is applied on t"e -& pin# !t enables volta/e measurement scale to
3stretc" and s"rink3#
To make t"is clear. t"e -FD converter always /enerates a $=4bit binary result. w"ic" means
t"at it detects a total of $=%' volta/e levels ?%$=N$=%'@# T"e difference between two volta/e
levels is not always t"e same# T"e less t"e difference between ArefK and Aref. t"e less t"edifference will be between two of $=%' levels# -ccordin/ly. t"e -FD converter is able to
detect sli/"t c"an/es in volta/e#
-
8/15/2019 Appendix b Exemples
45/65
Example 11: , .ea#er, >2*2 S'2'
org 0"0000 , ##ress of the first programinstrction
ban%sel '2IS3 , Selects ban% containing register '2IS3 clrf '2IS3 , ll port 3 pins are configre# asotpts mo6lw 3?00001100? mo6wf '2IS , >ins 25 an# 2: are configre# asinpts
ban%sel &S+ , Selects ban% containing register &S+ mo6lw 3?00001100? , Inpts &5 an# &: are analog while mo6wf &S+ , all other pins are #igital clrf &S+.
ban%sel DC&1 , Selects ban% incl#ing register DC&1 bsf DC&1D/ , 2ight stification of reslt bcf DC&1C/*1 , oltage ss is se# as ref bsf DC&1C/*0 , 2: pin 6oltage is se# as refK
ban%sel DC&0 , Selects ban% containing registerDC&0 mo6lw 3?00001001? , D con6erter ses cloc% /oscL5 Dchannel mo6wf DC&0 , on 25 pin is se# for con6ersion an#
, D con6erter is enable#loop
-
8/15/2019 Appendix b Exemples
46/65
ban%sel DC&0 btfsc DC&01 , 'ests bit *LD&+ goto loop , Con6ersion in progress remain in , loop ban%sel D2+S mo6f D2+Sw , ower byte of con6ersion reslt is
, copie# to @ ban%sel >2'3 mo6wf >2'3 , 3yte is copie# to >2'3 bsf DC&01 , Starts new con6ersion goto loop , Bmp to label 7loop7 en# , +n# of program
E;A&PLE 18
Usin+ EEPRO& mem#.
T"is e1ample demonstrates write to and read from built4in EEP2O9 memory# T"e pro/ram
works as follows# T"e main loop constantly reads EEP2O9 memory location at address (?decimal@# T"is number is displayed on port D# T"e same loop tests t"e state of t"ree pus"4
buttons connected to port -# T"e pus"4buttons 3!C2E9ET3 and 3DEC2E9ET3 "ave
t"e same purpose like in e1ample * 4 increment and decrement t"e variable 3cnt3 w"ic" is
t"ereafter displayed on port B# T"e pus"4button 39E9O3 enables t"at variable to be written
to EEP2O9 memory# !n order to c"eck it. it is enou/" to press t"is pus"4button and switc"
off t"e device# On t"e ne1t switc" on. t"e pro/ram displays t"e value of t"e variable on port D
?at t"e moment of writin/. t"is value was displayed on port B@#
-
8/15/2019 Appendix b Exemples
47/65
Example 18: , .ea#er, Defining 6ariables in program cbloc% 0"50 , 3loc% of 6ariables starts at a##ress50h
.Icnt cnt >cnt cnt en#c , +n# of bloc% ,
-
8/15/2019 Appendix b Exemples
48/65
2* 0"000 , 2eset 6ector nop goto main , *o to start of the program (label7main7),
incl#e 7paseAinc7 incl#e 7bttonAinc7,main ban%sel &S+ , Selects ban% containing &S+ clrf &S+ clrf &S+. , ll pins are #igital
ban%sel '2IS3 bsf '2IS 0 , Inpt pin
bsf '2IS 1 , Inpt pinbsf '2IS 5 , Inpt pin clrf '2IS3 , ll port 3 pins are otpts clrf '2ISD , ll port D pins are otpts ban%sel >2'3 clrf >2'3 , >2'3=0 clrf >2'D , >2'D=0 clrf cnt , cnt=0oop ban%sel >2' btton >2'00Increment btton >2'10Decrement btton >2'50Sa6e
ban%sel ++D2 mo6lw A! , 2ea#s ++>2 memory location mo6wf ++D2 , at a##ress ! ban%sel ++C&1 bcf ++C&1++>*D bsf ++C&12D , 2ea#s #ata from ++>2 memory ban%sel ++D' mo6fw ++D' , o6es #ata to @ ban%sel >2'D mo6wf >2'D , Data is mo6e# from @ to >2'D goto oopIncrement , Increments nmber on port 3 incf cnt f mo6f cnt w mo6wf >2'3 goto oopDecrement , Decrements nmber on port 3 #ecf cnt f mo6f cnt w mo6wf >2'3 goto oopSa6e , Copies #ata from port 3 to ++>2 ban%sel ++D2 , memory location at a##ress ! mo6lw A! mo6wf ++D2 , @rites a##ress ban%sel >2'3
mo6fw >2'3 , Copies port 3 to register @ ban%sel ++D'
-
8/15/2019 Appendix b Exemples
49/65
mo6wf ++D' , @rites #ata to temporary register ban%sel ++C&1 bcf ++C&1++>*D bsf ++C&1@2+& , @rite enable#
bcf I&'C&*I+ , ll interrpts #isable#
btfsc I&'C&*I+ goto M-5
mo6lw !!h mo6wf ++C&5 mo6lw .?? mo6wf ++C&5 bsf ++C&1@2
btfsc ++C&1@2 , @ait for write to complete goto M-1
bsf I&'C&*I+ , Interrpt enable#
bcf ++C&1@2+& goto oop , 'ests psh-bttons again en# , +n# of program
E;A&PLE 17
T,#(di+i! LE% $#un!e> mul!iplexin+
!n t"is e1ample. t"e microcontroller operates as a two4di/it counter# Concretely. t"e variable
Dval is decremented ?slow enou/" to be visible@ and its value is displayed on twodi/it
-
8/15/2019 Appendix b Exemples
50/65
Example 17: , .ea#er
,, D+/I&I&* 2I3+S I& >2*2 wtemp +N 0"$D , ariable for sa6ing @ register
statstemp +N 0"$+ , ariable for sa6ing S''S register
pclathtemp +N 0"$/ , ariable for sa6ing >C'. register
C3C; 0"50 , 3loc% of 6ariables starts at a##ress50h
Digtemp Dig0 , ariables for #isplaying #igits - S3 Dig1 Dig5 Dig: , ariables for #isplaying #igits - S3 D6al , Conter 6ale ne , "iliary 6ariable which #etermineswhich , #isplay is to be switche# on
+&DC , +n# of bloc% of 6ariables
poc6r +N AOO , Initial conter 6ale is OO
incl#e 7DigbyteAinc7
-
8/15/2019 Appendix b Exemples
51/65
, 2* 0"0000 , /irst instrction a##ress goto main , Bmp to label 7main7,
2* 0"0004 , Interrpt 6ector a##ress
mo6wf wtemp , o6e w register to wtemp register
mo6f S''Sw , o6e S''S register to statstemp mo6wf statstemp , register
mo6f >C'.w , o6e >C'. register to pclathtemp mo6wf pclathtemp , register
, Start of interrpt rotineAAA
3&;S+ '20 mo6lw A100 mo6wf '20 bcf I&'C& '0I/
bcf >2' 0 bcf >2' 1 btfsc ne 0 goto s#on goto s#on s#on incf ne f mo6lw .I*. (3c#to$seg) mo6wf >C'. #igbyte D6al mo6f Dig1 w call 3c#to$seg , >lace 1 mas% on the >2'D mo6wf >2'D bsf >2' 1 goto IS2en# s#on incf ne f mo6lw .I*. (3c#to$seg) mo6wf >C'. #igbyte D6al mo6f Dig0 w call 3c#to$seg , >lace mas% on the >2'D mo6wf >2'D bsf >2' 0 goto IS2en#
, +n# of interrpt rotineAAA
IS2en# mo6f pclathtempw , >C'. register is gi6en its original mo6wf >C'. , state
mo6f statstempw , S''S register is gi6en its original
-
8/15/2019 Appendix b Exemples
52/65
mo6wf S''S , state
swapf wtempf , @ register is gi6en its original , state swapf wtempw retfie , 2etrn from interrpt rotine
main ban%sel &S+ , Selects ban% containing &S+ clrf &S+ , ll pins are #igital clrf &S+.
3&;S+ '2IS mo6lw b?11111100? , 20 an# 21 are configre# as otptsan# , se# for $-segment #isplaymltiple"ing , 25 is inpt psh-btton forinitiali'I&2+* mo6lw b?10000110? , '20 is incremente# each :5s(/cl%=8.'I&2+*
3&;S+ >2' mo6lw poc6r mo6wf D6al , D6al contains conter 6ale mo6lw b?00000001? , Initiali2' mo6lw A100 mo6wf '20 , '20 interrpt apprAe6ery 10ms bsf I&'C& *I+ , *lobal interrpt enable# bsf I&'C& '0I+ , 'imer '20 interrpt enable# bcf I&'C& '0I/oop btfss ne : , /alling e#ge encontere#P goto Dec , FesQ *o to Dec btfss >2' 5 , Conter reset btton presse#P goto 2eset , FesQ *o to 2eset goto oop , Decrement D6al conter by 1Dec btfss ne : goto Dec mo6f D6al f btfsc S''S H , Is D6al e9al to 0P goto oop , If it is go to loop an# wait for '5 #ecf D6al f , If D6al not e9al to 0 #ecrement itby 1 goto oop2eset btfss >2' 5 , @ait for rising e#ge goto 2eset
mo6lw poc6r mo6wf D6al , @rite initial 6ale to conter
-
8/15/2019 Appendix b Exemples
53/65
goto oop, 2* 0"0:00 , oo%p table is at the top of thir#page bt
, can be place# at some other place itis impor , tant to ha6e it all on one page3c#to$seg a##wf >C f D' 0":f 0"0 0"!b 0"4f 0" 0"# 0"$# 0"0$ 0"$f0"f, +&D , +n# of program
&a$# ?di+*.!e?:#igbyte C2 arg0
C +"it0 C +"it1 C +"it5
clrf Dig0 clrf Dig1 clrf Dig5
clrf Dig:
mo6f arg0 w mo6wf Digtemp mo6lw A100+"it5
incf Dig5 f sbwf Digtemp f btfsc S''S C goto +"it5 #ecf Dig5 f a##wf Digtemp f+"it1 mo6lw A10
incf Dig1 f sbwf Digtemp f btfsc S''S C goto +"it1 #ecf Dig1 f
a##wf Digtemp f+"it0 mo6f Digtemp w mo6wf Dig0 +&D
9acro di/byte is used to convert t"e number from di/ital to decimal format# Besides. di/its of
suc" decimal number are stored into special re/isters in order to enable t"em to be displayed
on usin+ ma$#s
-
8/15/2019 Appendix b Exemples
54/65
T"e /eneration of sound is a task commonly assi/ned to t"e microcontroller# Basically. it all
comes to /eneratin/ a pulse se>uence on one output pin# G"ile doin/ so. t"e proportion of
lo/ic 0ero ?=@ to lo/ic one ?$@ duration determines t"e tone pitc" and by c"an/in/ different
tones. different melodies arise#
!n t"is e1ample. any press on pus"4buttons T$ and T% /enerates a sound# T"e appropriateinstructions are stored in macro 3beep3 containin/ two ar/uments#
Example 16: , .ea#er , Defining 6ariables in program cbloc% 0"50 .Icnt , "iliary 6ariables for macro pasems cnt >cnt >2+SCwait
3eep'+>1 , 3elongs to macro 73++>7 3eep'+>5
-
8/15/2019 Appendix b Exemples
55/65
3eep'+>: en#c G#efine 3++>port >2'D 5 , Spea%er pinG#efine 3++>tris '2ISD 5
e"pan#
, 2* 0"0000 , 2+S+' 6ector a##ress goto main , Bmp to program start (label - main),, remaining co#e goes here
incl#e 7paseAinc7 incl#e 7bttonAinc7 incl#e 7beepAinc7main ban%sel &S+ , Selects ban% containing &S+
clrf &S+ , ll otpts are #igital clrf &S+.
ban%sel '2ISD mo6lw b?11111011? , >2' D initialiinit , acro 73eep7oop btton >2'D00>lay1 , >sh-btton 1 btton >2'D10>lay5 , >sh-btton 5 goto oop >lay1 , /irst tone 3++> 0"// 0"05 3++> 0"O0 0"0! 3++> 0"C0 0"0: 3++> 0"// 0"0: goto oop >lay5 , Secon# tone 3++> 0"33 0"05 3++> 0"8$ 0"0! 3++> 0"5 0"0:
3++> 0"O8 0"0: goto oop, +&D , +n# of program
&a$# ?*eep?:3++>init C2 bcf S''S 2>0 bcf S''S 2>1 bcf 3++>port bsf S''S 2>0 bcf S''S 2>1 bcf 3++>tris mo6lw b?00000111? , '20 prescaler rate 1E5! mo6wf >'I&2+* , >'I& J- @ bcf S''S 2>0 bcf S''S 2>1 +&D3++> C2 fre9 #ration
-
8/15/2019 Appendix b Exemples
56/65
bcf S''S 2>0 bcf S''S 2>1 mo6lw fre9 mo6wf 3eep'+>1 mo6lw #ration mo6wf 3eep'+>5
call 3++>sb +&D,, Sbrotines
3++>sb clrf '20 , Conter initialiport3++>a bcf I&'C& '0I/ , Clears '20 6erflow /lag3++>b bsf 3++>port
call 3@ait , ogic one 717 #ration bcf 3++>port call 3@ait , ogic 5 f , Is 3eep'+>5 = 0 P goto 3++>a , *o to 3++>a again retrn3@ait mo6f 3eep'+>1 w mo6wf 3eep'+>:3@aita #ecfs< 3eep'+>: f goto 3@aita retrn
E;A&PLE 15
Usin+ LC% displa.
T"is e1ample illustrates t"e use of t"e alp"anumeric
-
8/15/2019 Appendix b Exemples
57/65
Example 15: , .ea#er , D+/I&I&* 2I3+S I& >2*2
C3C; 0"50 , 3loc% of 6ariables starts at a##ress50h
.Icnt , 3elongs to macro 7pasems7 cnt >cnt
CDbf , 3elongs to fnctions 7CD"""7 CDtemp CDport3f , CD >ort 3ffer
Digtemp , 3elongs to macro 7#igbyte7 Dig0
Dig1 Dig5 Dig:
temp +&DC , +n# of bloc% CDport +N >2'3 , CD is on >2'3 (4 #ata lines on 230-23:)2S +N 4 , 2S line connecte# to 234+& +N ! , +& line connecte# to 23!
, 2* 0"0000 , 2eset 6ector a##ress
nop goto main , *o to beginning of the program (label7main7)
-
8/15/2019 Appendix b Exemples
58/65
, incl#e 7CDAinc7 incl#e 7#igbyteAinc7 incl#e 7paseAinc7,main
ban%sel &S+ , Selects ban% containing &S+ clrf &S+ , ll pins are #igital clrf &S+.
bcf S''S2>0 , 3an%0 acti6e only bcf S''S2>1 mo6lw A5: mo6wf temp , o6e arbitrary 6ale to 6ariable , is to be #isplaye# on CD lc#init , CD initiali1 clrf CDport3f mo6f CDport3f w mo6wf CDport
bsf S''S 2>0 , 3an%1 bcf S''S 2>1 clrf '2IS3 , CDport with otpt CD bcf S''S 2>0 , 3an%0 bcf S''S 2>1 , /nction set (4-bit mo#e change) mo6lw b?00100000? mo6wf CDbf swapf CDbf w mo6wf CDport3f bcf CDport3f 2S mo6f CDport3f w mo6wf CDport bsf CDport3f +& mo6f CDport3f w
-
8/15/2019 Appendix b Exemples
59/65
mo6wf CDport bcf CDport3f +& mo6f CDport3f w mo6wf CDport call Delay1ms , 1 ms #elay
, /nction set (#isplay mo#e set) lc#cm# b?00101100? call Delay1ms , 1 ms #elay , Display &L// Control lc#cm# b?00001100? call Delay1ms , 1 ms #elay , +ntry o#e Set lc#cm# b?00000110? call Delay1ms , 1 ms #elay , Display Clear
lc#cm# b?00000001? pasems A40 , 40 ms #elay , /nction set (4-bit mo#e change) mo6lw b?00100000? mo6wf CDbf swapf CDbf w mo6wf CDport3f bcf CDport3f 2S mo6f CDport3f w mo6wf CDport bsf CDport3f +& mo6f CDport3f w mo6wf CDport bcf CDport3f +& mo6f CDport3f w mo6wf CDport call Delay1ms , 1 ms #elay , /nction set (#isplay mo#e set) lc#cm# b?00101100? call Delay1ms , 1 ms #elay , Display &L// Control lc#cm# b?00001100? call Delay1ms , 1 ms #elay , +ntry o#e Set lc#cm# b?00000110? call Delay1ms , 1 ms #elay , Display Clear lc#cm# b?00000001? pasems A40 , 40 ms #elay
+&D
,, lc#cm# sen#s comman# to CD (see the table on the pre6ios page), lc#clr is the same as lc#cm# 0"01
,lc#cm# C2 CDcomman# , Sen# comman# to CD
-
8/15/2019 Appendix b Exemples
60/65
mo6lw CDcomman# call CDcom# +&D CDcom# mo6wf CDbf
bcf CDport3f 2S mo6f CDport3f w mo6wf CDport goto CDwrCD#ata mo6wf CDbf bsf CDport3f 2S mo6f CDport3f w mo6wf CDport goto CDwrCDwr swapf CDbf w call Sen#@
mo6f CDbf w call Sen#@ retrnSen#@ an#lw 0"0/ mo6wf CDtemp
mo6lw 0"/0 an#wf CDport3f f mo6f CDtemp w iorwf CDport3f f mo6f CDport3f w mo6wf CDport call Delay1ms bsf CDport3f +& mo6f CDport3f w mo6wf CDport bcf CDport3f +& mo6f CDport3f w mo6wf CDport call Delay1ms retrn ,, lc#te"t writes te"t containing 1 characters which represents a, macro argmentA 'he first argment select selects the line in which, te"t writing is to startA If select is 0 te"t writing starts from, crsor crrent positionA,lc#te"t C2 select te"t , 'his macro writes te"t from crsor , crrent positionA 'e"t is specifie# , in argment consisting of 1 charac , ters local essage local Start local +"it local i=0 goto Start essage D' te"t , Create loo%p table from argments D' 0
Start I/ (select == 1)
-
8/15/2019 Appendix b Exemples
61/65
lc#cm# b?10000000? +S+ I/ (select == 5) lc#cm# b?11000000? +&DI/ +&DI/
@.I+ (iJ1) , 2epeat con#itional program compiling1 times call essageKi , 2ea# loo%p table an# place 6ale in @ a##lw 0 b< +"it , ntil 0 is rea# call CD#ata , Call rotine #isplaying @ on CD i=iK1 +&D@+"it +&D ,
, 'his macro writes 6ale in sicntDelay10sE nop ,1s nop ,1s nop ,1s nop ,1s nop ,1s nop ,1s nop ,1s #ecfs< >cnt f ,1s goto Delay10s ,5s
retrn
E;A&PLE 14
RS878 seial $#mmuni$a!i#n
T"is e1ample illustrates t"e use of t"e microcontroller:s ES-2T module# Connection to t"ePC is enabled t"rou/" 2S%&% standard# T"e pro/ram works in t"e followin/ way5 Every byte
-
8/15/2019 Appendix b Exemples
62/65
received via t"e serial communication is displayed usin/ C'. w register
cbloc% 0"50 , 3loc% of 6ariables starts at a##ress50 h >ort , ariable at a##ress 50 h >ort3 , ariable at a##ress 51 h 2S5:5temp , ariable at a##ress 55 h 2Rchr , ariable at a##ress 5: h en#c , +n# of bloc% of 6ariables
, 2* 0"0000 , 2eset 6ector nop
-
8/15/2019 Appendix b Exemples
63/65
-
8/15/2019 Appendix b Exemples
64/65
bsf >ort 1 , Switches +D on ( 2' errorin#icator) mo6f >ort w mo6wf >2' goto 2S5:5e"it
2R5:5intprocCont mo6f 2C2+* @ , 2ea#s recei6e# #ata mo6wf 2Rchr mo6wf >2'3 mo6wf 'R2+* , Sen#s #ata bac% to >C 2S5:5e"it retrn , 2etrn from interrpt rotine,, ain program
main ban%sel &S+ , Selects ban% containing &S+
clrf &S+ , ll inpts are #igital clrf &S+.
,--------------------------------------- , >ort configration ,--------------------------------------- ban%sel '2IS mo6lw b?11111100? mo6wf '2IS mo6lw b?00000000? mo6wf '2IS3 ,--------------------------------------- , Setting initial 6ales ,--------------------------------------- ban%sel >2' mo6lw b?11111100? mo6wf >2' mo6wf >ort mo6lw b?00000000? mo6wf >2'3 mo6wf >ort3 ,--------------------------------------- , S2' - setting for :8400 bps ,--------------------------------------- ban%sel '2ISC bcf '2ISC , 2CL'RLC; = otpt bsf '2ISC $ , 2C$L2RLD' = inpt
ban%sel 3DC'bsf 3DC' 32*1
ban%sel S>32* mo6lw A!1 , ba# rate = :8400 , ( /oscL(4(S>32*K1)) ) +rror K0A1 mo6wf S>32* clrf S>32*.
ban%sel 'RS' bcf 'RS' 'RO , Data is 8-bit wi#e bsf 'RS' 'R+& , Data transmission enable# bcf 'RS' SF&C , synchronos mo#e
bsf 'RS' 32*. , .igh-spee# 3a# rate
-
8/15/2019 Appendix b Exemples
65/65
ban%sel 2CS' bsf 2CS' S>+& , 2RLD' an# 'RLC; otpts configration bcf 2CS' 2RO , Select mo#e for 8-bit #ata recei6e bsf 2CS' C2+& , 2ecei6e #ata enable# bcf 2CS' DD+& , &o a##ress #etection ninth bit may be , se# as parity bit
mo6f 2CS' @ mo6f 2C2+* @ ,--------------------------------------- , Interrpts enable# ,--------------------------------------- ban%sel >I+1 bsf >I+1 2CI+ , S2' 2" interrpt enable#
bsf I&'C& >+I+ , ll peripheral interrpts enable# bsf I&'C& *I+ , *lobal interrpt enable#
,--------------------------------------- , 2emain here
,--------------------------------------- goto M
en# , +n# of program
Previous C"apter Q Table of Contents Q e1t C"apter
• Support forums
• ews
• Site map
• Contact
•