380 80 2MB
English Pages 186 Year 2000
PIC book
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
P I C m ic ro c o n t ro lle rs
Ne x t pa g e
for beginners,too!
Au t h o r: Ne b o js a Ma t ic P a p e rb a c k - 2 5 2 p a g e s ( Ma y 1 5 , 2 0 0 0 ) Dimensions (in inches): 0.62 x 9.13 x 7.28 PIC m icro co n t ro lle rs ; lo w- co s t co m p u t e rs - in - a - ch ip ; a llo ws e le ct ro n ics d e s ig n e rs a n d h o b b yis t s a d d in t e llig e n ce a n d fu n ct io n s t h a t m im ic b ig co m p u t e rs fo r a lm o s t a n y e le ct ro n ic p ro d u ct o r p ro je ct . Th e p u rp o s e o f t h is b o o k is n o t t o m a ke a m icro co n t ro lle r e xp e rt o u t o f yo u , b u t t o m a ke yo u e q u a l t o t h o s e wh o h a d s o m e o n e t o g o t o fo r t h e ir a n s we rs .
I n t h is b o o k y o u c a n fin d : P ra c t ic a l c o n n e c t io n s a m p le s fo r Re la ys , Op t o co u p le rs , LCD's , Ke ys , Dig it s , A t o D Co n ve rt e rs , S e ria l co m m u n ica t io n e t c.
I n t ro d u c t io n t o m ic ro c o n t ro lle rs Le a rn wh a t t h e y a re , h o w t h e y wo rk, a n d h o w t h e y ca n b e h e lp fu l in yo u r wo rk.
As s e m b le r la n g u a g e p ro g ra m m in g Ho w t o writ e yo u r firs t p ro g ra m , u s e o f m a cro s , a d d re s s in g m o d e s . . . .
I n s t ru c t io n S e t De s crip t io n , s a m p le a n d p u rp o s e fo r u s in g e a ch in s t ru ct io n . . . . . . . .
MP LAB p ro g ra m p a c k a g e Ho w t o in s t a ll it , h o w t o s t a rt t h e firs t p ro g ra m , fo llo win g t h e p ro g ra m s t e p b y s t e p in t h e s im u la t o r. . . .
Co n t e n t s CHAP TER I I N TROD UCTI ON TO MI CROCON TROLLERS In t ro d u ct io n His t o ry Micro co n t ro lle rs ve rs u s m icro p ro ce s s o rs 1.1 1.2 1.3 1.4 1.5 1.6 1.7
Me m o ry u n it Ce n t ra l p ro ce s s in g u n it Bu s e s In p u t - o u t p u t u n it S e ria l co m m u n ica t io n Tim e r u n it Wa t ch d o g
http://www.mikroelektronika.co.yu/english/product/books/PICbook/0_Uvod.htm (1 of 5) [4/2/2003 16:17:25]
PIC book
1 . 8 An a lo g t o d ig it a l co n ve rt e r 1 . 9 Pro g ra m
CHAP TER I I MI CROCON TROLLER P I C1 6 F8 4 In t ro d u ct io n CIS C, RIS C Ap p lica t io n s Clo ck/ in s t ru ct io n cycle Pip e lin in g Pin d e s crip t io n 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8
Clo ck g e n e ra t o r - o s cilla t o r Re s e t Ce n t ra l p ro ce s s in g u n it Po rt s Me m o ry o rg a n iza t io n In t e rru p t s Fre e t im e r TMR0 EEPROM Da t a m e m o ry
CHAP TER I I I I N S TRUCTI ON S ET In t ro d u ct io n In s t ru ct io n s e t in PIC1 6 Cxx m icro co n t ro lle r fa m ily Da t a Tra n s fe r Arit h m e t ic a n d lo g ic Bit o p e ra t io n s Dire ct in g t h e p ro g ra m flo w In s t ru ct io n e xe cu t io n p e rio d Wo rd lis t
CHAP TER I V AS S EMBLY LAN GUAGE P ROGRAMMI N G In t ro d u ct io n S a m p le o f a writ t e n p ro g ra m Co n t ro l d ire ct ive s ● ● ● ● ● ●
4.1 4.2 4.3 4.4 4.5 4.6
d e fin e in clu d e co n s t a n t va ria b le set equ
http://www.mikroelektronika.co.yu/english/product/books/PICbook/0_Uvod.htm (2 of 5) [4/2/2003 16:17:25]
PIC book ● ●
4 . 7 o rg 4.8 e nd
Co n d it io n a l in s t ru ct io n s ● ● ● ● ● ● ●
4 . 9 if 4 . 1 0 e ls e 4 . 1 1 e n d if 4 . 1 2 wh ile 4.13 e ndw 4 . 1 4 ifd e f 4 . 1 5 ifn d e f
Da t a d ire ct ive s ● ● ● ● ●
4.16 4.17 4.18 4.19 4.20
cb lo ck endc db de dt
Co n fig u ra t in g a d ire ct ive ● ●
4 . 2 1 _ CONFIG 4 . 2 2 Pro ce s s o r
As s e m b le r a rit h m e t ic o p e ra t o rs File s cre a t e d a s a re s u lt o f p ro g ra m t ra n s la t io n Ma cro s CHAP TER V MP LAB In t ro d u ct io n 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8
In s t a llin g t h e MPLAB p ro g ra m p a cka g e In t ro d u ct io n t o MPLAB Ch o o s in g t h e d e ve lo p m e n t m o d e De s ig n in g a p ro je ct De s ig n in g n e w a s s e m b le r file Writ in g a p ro g ra m MPS IM s im u la t o r To o lb a r
CHAP TER VI THE S AMP LES In t ro d u ct io n 6 . 1 Th e m icro co n t ro lle r p o we r s u p p ly 6 . 2 Ma cro s u s e d in p ro g ra m s
http://www.mikroelektronika.co.yu/english/product/books/PICbook/0_Uvod.htm (3 of 5) [4/2/2003 16:17:25]
PIC book ● ●
Ma cro s WAIT, WAITX Ma cro PRINT
6 . 3 S a m p le s ● ● ●
● ● ●
● ● ● ●
Lig h t Em it t in g Dio d e s Ke yb o a rd Op t o co u p le r ❍ Op t o co u p le rin g t h e in p u t lin e s ❍ Op t o co u p le rin g t h e o u t p u t lin e s Re la ys Ge n e ra t in g a s o u n d S h ift re g is t e rs ❍ In p u t s h ift re g is t e r ❍ Ou t p u t s h ift re g is t e r 7 - s e g m e n t Dis p la ys ( m u lt ip le xin g ) LCD d is p la y 1 2 - b it AD co n ve rt e r S e ria l co m m u n ica t io n
AP P EN D I X A I N S TRUCTI ON S ET
AP P EN D I X B N UMERI C S YS TEMS In t ro d u ct io n B. 1 De cim a l n u m e ric s ys t e m B. 2 Bin a ry n u m e ric s ys t e m B. 3 He xa d e cim a l n u m e ric s ys t e m AP P EN D I X C GLOS S ARY
http://www.mikroelektronika.co.yu/english/product/books/PICbook/0_Uvod.htm (4 of 5) [4/2/2003 16:17:25]
PIC book
Se nd us a co m m e nt abo ut a bo o k S u b je c t : Cooment about book PIC microcontrollers
Na m e : Sta te : USA
E- m a il :
Yo u r m e s s a g e :
Submit
Reset
© C o p y r i g h t 2 0 0 1 . m i k r o E l e k t r o n i k a. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/0_Uvod.htm (5 of 5) [4/2/2003 16:17:25]
Chapter 1 - Introduction to Microprocessors
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
CHAP TER 1 I n t ro d u c t io n t o Mic ro c o n t ro lle rs In t ro d u ct io n His t o ry Micro co n t ro lle rs ve rs u s m icro p ro ce s s o rs 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
Me m o ry u n it Ce n t ra l p ro ce s s in g u n it Bu s e s In p u t - o u t p u t u n it S e ria l co m m u n ica t io n Tim e r u n it Wa t ch d o g An a lo g t o d ig it a l co n ve rt e r Pro g ra m
I n t ro d u c t io n Circu m s t a n ce s t h a t we fin d o u rs e lve s in t o d a y in t h e fie ld o f m icro co n t ro lle rs h a d t h e ir b e g in n in g s in t h e d e ve lo p m e n t o f t e ch n o lo g y o f in t e g ra t e d circu it s . Th is d e ve lo p m e n t h a s m a d e it p o s s ib le t o s t o re h u n d re d s o f t h o u s a n d s o f t ra n s is t o rs in t o o n e ch ip . Th a t wa s a p re re q u is it e fo r p ro d u ct io n o f m icro p ro ce s s o rs , a n d t h e firs t co m p u t e rs we re m a d e b y a d d in g e xt e rn a l p e rip h e ra ls s u ch a s m e m o ry, in p u t - o u t p u t lin e s , t im e rs a n d o t h e r. Fu rt h e r in cre a s in g o f t h e vo lu m e o f t h e p a cka g e re s u lt e d in cre a t io n o f in t e g ra t e d circu it s . Th e s e in t e g ra t e d circu it s co n t a in e d b o t h p ro ce s s o r a n d p e rip h e ra ls . Th a t is h o w t h e firs t ch ip co n t a in in g a m icro co m p u t e r , o r wh a t wo u ld la t e r b e kn o wn a s a m icro co n t ro lle r ca m e a b o u t .
His t o ry It wa s ye a r 1 9 6 9 , a n d a t e a m o f Ja p a n e s e e n g in e e rs fro m t h e BUS ICOM co m p a n y a rrive d t o Un it e d S t a t e s wit h a re q u e s t t h a t a fe w in t e g ra t e d circu it s fo r ca lcu la t o rs b e m a d e u s in g t h e ir p ro je ct s . Th e p ro p o s it io n wa s s e t t o INTEL, a n d Ma rcia n Ho ff wa s re s p o n s ib le fo r t h e p ro je ct . S in ce h e wa s t h e o n e wh o h a s h a d e xp e rie n ce in wo rkin g wit h a co m p u t e r ( PC) PDP8 , it o ccu re d t o h im t o s u g g e s t a fu n d a m e n t a lly d iffe re n t s o lu t io n in s t e a d o f t h e s u g g e s t e d co n s t ru ct io n . Th is s o lu t io n p re s u m e d t h a t t h e fu n ct io n o f t h e in t e g ra t e d circu it is d e t e rm in e d b y a p ro g ra m s t o re d in it . Th a t m e a n t t h a t co n fig u ra t io n wo u ld b e m o re s im p le , b u t t h a t it wo u ld re q u ire fa r m o re m e m o ry t h a n t h e p ro je ct t h a t wa s p ro p o s e d b y Ja p a n e s e e n g in e e rs wo u ld re q u ire . Aft e r a wh ile , t h o u g h Ja p a n e s e e n g in e e rs t rie d fin d in g a n e a s ie r s o lu t io n , Ma rcia n 's id e a wo n , a n d t h e firs t m icro p ro ce s s o r wa s b o rn . In t ra n s fo rm in g a n id e a in t o a re a d y m a d e p ro d u ct , Fre d e rico Fa g g in wa s a m a jo r h e lp t o INTEL. He t ra n s fe rre d t o INTEL, a n d in o n ly 9 m o n t h s h a d s u cce e d e d in m a kin g a p ro d u ct fro m it s firs t co n ce p t io n . INTEL o b t a in e d t h e rig h t s t o s e ll t h is in t e g ra l b lo ck in 1 9 7 1 . Firs t , t h e y b o u g h t t h e lice n s e fro m t h e BUS ICOM co m p a n y wh o h a d n o
http://www.mikroelektronika.co.yu/english/product/books/PICbook/1_Poglavlje.htm (1 of 9) [4/2/2003 16:17:33]
Chapter 1 - Introduction to Microprocessors
id e a wh a t t re a s u re t h e y h a d . Du rin g t h a t ye a r, t h e re a p p e a re d o n t h e m a rke t a m icro p ro ce s s o r ca lle d 4 0 0 4 . Th a t wa s t h e firs t 4 - b it m icro p ro ce s s o r wit h t h e s p e e d o f 6 0 0 0 o p e ra t io n s p e r s e co n d . No t lo n g a ft e r t h a t , Am e rica n co m p a n y CTC re q u e s t e d fro m INTEL a n d Te xa s In s t ru m e n t s t o m a ke a n 8 - b it m icro p ro ce s s o r fo r u s e in t e rm in a ls . Eve n t h o u g h CTC g a ve u p t h is id e a in t h e e n d , In t e l a n d Te xa s In s t ru m e n t s ke p t wo rkin g o n t h e m icro p ro ce s s o r a n d in Ap ril o f 1 9 7 2 , firs t 8 - b it m icro p ro ce s s o r a p p e a rd o n t h e m a rke t u n d e r a n a m e 8 0 0 8 . It wa s a b le t o a d d re s s 1 6 Kb o f m e m o ry, a n d it h a d 4 5 in s t ru ct io n s a n d t h e s p e e d o f 3 0 0 0 0 0 o p e ra t io n s p e r s e co n d . Th a t m icro p ro ce s s o r wa s t h e p re d e ce s s o r o f a ll t o d a y's m icro p ro ce s s o rs . In t e l ke p t t h e ir d e ve lo p m e n t s u p in Ap ril o f 1 9 7 4 , a n d t h e y p u t o n t h e m a rke t t h e 8 - b it p ro ce s s o r u n d e r a n a m e 8 0 8 0 wh ich wa s a b le t o a d d re s s 6 4 Kb o f m e m o ry, a n d wh ich h a d 7 5 in s t ru ct io n s , a n d t h e p rice b e g a n a t $ 3 6 0 . In a n o t h e r Am e rica n co m p a n y Mo t o ro la , t h e y re a lize d q u ickly wh a t wa s h a p p e n in g , s o t h e y p u t o u t o n t h e m a rke t a n 8 - b it m icro p ro ce s s o r 6 8 0 0 . Ch ie f co n s t ru ct o r wa s Ch u ck Pe d d le , a n d a lo n g wit h t h e p ro ce s s o r it s e lf, Mo t o ro la wa s t h e firs t co m p a n y t o m a ke o t h e r p e rip h e ra ls s u ch a s 6 8 2 0 a n d 6 8 5 0 . At t h a t t im e m a n y co m p a n ie s re co g n ize d g re a t e r im p o rt a n ce o f m icro p ro ce s s o rs a n d b e g a n t h e ir o wn d e ve lo p m e n t s . Ch u ck Pe d d le le a ve d Mo t o ro la t o jo in MOS Te ch n o lo g y a n d ke p t wo rkin g in t e n s ive ly o n d e ve lo p in g m icro p ro ce s s o rs . At t h e WES CON e xh ib it in Un it e d S t a t e s in 1 9 7 5 , a crit ica l e ve n t t o o k p la ce in t h e h is t o ry o f m icro p ro ce s s o rs . Th e MOS Te ch n o lo g y a n n o u n ce d it wa s m a rke t in g m icro p ro ce s s o rs 6 5 0 1 a n d 6 5 0 2 a t $ 2 5 e a ch , wh ich b u ye rs co u ld p u rch a s e im m e d ia t e ly. Th is wa s s o s e n s a t io n a l t h a t m a n y t h o u g h t it wa s s o m e kin d o f a s ca m , co n s id e rin g t h a t co m p e t it o rs we re s e llin g 8 0 8 0 a n d 6 8 0 0 a t $ 1 7 9 e a ch . As a n a n s we r t o it s co m p e t it o r, b o t h In t e l a n d Mo t o ro la lo we re d t h e ir p rice s o n t h e firs t d a y o f t h e e xh ib it d o wn t o $ 6 9 . 9 5 p e r m icro p ro ce s s o r. Mo t o ro la q u ickly b ro u g h t s u it a g a in s t MOS Te ch n o lo g y a n d Ch u ck Pe d d le fo r co p yin g t h e p ro t e ct e d 6 8 0 0 . MOS Te ch n o lo g y s t o p p e d m a kin g 6 5 0 1 , b u t ke p t p ro d u cin g 6 5 0 2 . Th e 6 5 0 2 wa s a 8 - b it m icro p ro ce s s o r wit h 5 6 in s t ru ct io n s a n d a ca p a b ilit y o f d ire ct ly a d d re s s in g 6 4 Kb o f m e m o ry. Du e t o lo w co s t , 6 5 0 2 b e co m e s ve ry p o p u la r, s o it wa s in s t a lle d in t o co m p u t e rs s u ch a s : KIM- 1 , Ap p le I, Ap p le II, At a ri, Co m o d o re , Aco rn , Oric, Ga le b , Ora o , Ult ra , a n d m a n y o t h e rs . S o o n a p p e a re d s e ve ra l m a ke rs o f 6 5 0 2 ( Ro ckwe ll, S zn e rt e k, GTE, NCR, Rico h , a n d Co m o d o re t a ke s o ve r MOS Te ch n o lo g y) wh ich wa s a t t h e t im e o f it s p ro s p e rit y s o ld a t a ra t e o f 1 5 m illio n p ro ce s s o rs a ye a r! Ot h e rs we re n o t g ivin g u p t h o u g h . Fre d e rico Fa g g in le a ve s In t e l, a n d s t a rt s h is o wn Zilo g In c. In 1 9 7 6 Zilo g a n n o u n ce d t h e Z8 0 . Du rin g t h e m a kin g o f t h is m icro p ro ce s s o r, Fa g g in m a d e a p ivo t a l d e cis io n . Kn o win g t h a t a g re a t d e a l o f p ro g ra m s h a ve b e e n a lre a d y d e ve lo p e d fo r 8 0 8 0 , Fa g g in re a lize d t h a t m a n y wo u ld s t a y fa it h fu l t o t h a t m icro p ro ce s s o r b e ca u s e o f g re a t e xp e n d it u re wh ich re d o in g o f a ll o f t h e p ro g ra m s wo u ld re s u lt in . Th u s h e d e cid e d t h a t a n e w p ro ce s s o r h a d t o b e co m p a t ib le wit h 8 0 8 0 , o r t h a t it h a d t o b e ca p a b le o f p e rfo rm in g a ll o f t h e p ro g ra m s wh ich h a d a lre a d y b e e n writ t e n fo r 8 0 8 0 . Be s id e t h e s e ch a ra ct e ris t ics , m a n y n e w o n e s h a ve b e e n a d d e d , s o t h a t Z8 0 wa s a ve ry p o we rfu l m icro p ro ce s s o r in it s t im e . It wa s a b le t o a d d re s s d ire ct ly 6 4 Kb o f m e m o ry, it h a d 1 7 6 in s t ru ct io n s , a la rg e n u m b e r o f re g is t e rs , a b u ilt in o p t io n fo r re fre s h in g t h e d yn a m ic RAM m e m o ry, s in g le - s u p p ly, g re a t e r s p e e d o f wo rk e t c. Z8 0 wa s a g re a t s u cce s s a n d e ve ryb o d y co n ve rt e d fro m 8 0 8 0 t o Z8 0 . It co u ld b e s a id t h a t Z8 0 wa s wit h o u t a d o u b t co m m e rcia lly m o s t s u cce s s fu l 8 - b it m icro p ro ce s s o r o f t h a t t im e . Be s id e s Zilo g , o t h e r n e w m a n u fa ct u re rs like Mo s t e k, NEC, S HARP, a n d S GS a ls o a p p e a re d . Z8 0 wa s t h e h e a rt o f m a n y co m p u t e rs like S p e ct ru m , Pa rt n e r, TRS 7 0 3 , Z- 3 . In 1 9 7 6 , In t e l ca m e u p wit h a n im p ro ve d ve rs io n o f 8 - b it m icro p ro ce s s o r n a m e d 8 0 8 5 . Ho we ve r, Z8 0 wa s s o m u ch b e t t e r t h a t In t e l s o o n lo s t t h e b a t t le . Alt o u g h a fe w m o re p ro ce s s o rs a p p e a re d o n t h e m a rke t ( 6 8 0 9 , 2 6 5 0 , S C/ MP e t c. ) , e ve ryt h in g wa s a ct u a lly a lre a d y d e cid e d . Th e re we re n 't a n y m o re g re a t im p ro ve m e n t s t o m a ke m a n u fa ct u re rs co n ve rt t o s o m e t h in g n e w, s o 6 5 0 2 a n d Z8 0 a lo n g wit h 6 8 0 0 re m a in e d a s m a in re p re s e n t a t ive s o f t h e 8 b it m icro p ro ce s s o rs o f t h a t t im e .
Mic ro c o n t ro lle rs v e rs u s Mic ro p ro c e s s o rs Micro co n t ro lle r d iffe rs fro m a m icro p ro ce s s o r in m a n y wa ys . Firs t a n d t h e m o s t im p o rt a n t is it s fu n ct io n a lit y. In o rd e r fo r a m icro p ro ce s s o r t o b e u s e d , o t h e r co m p o n e n t s s u ch a s m e m o ry, o r
http://www.mikroelektronika.co.yu/english/product/books/PICbook/1_Poglavlje.htm (2 of 9) [4/2/2003 16:17:33]
Chapter 1 - Introduction to Microprocessors
co m p o n e n t s fo r re ce ivin g a n d s e n d in g d a t a m u s t b e a d d e d t o it . In s h o rt t h a t m e a n s t h a t m icro p ro ce s s o r is t h e ve ry h e a rt o f t h e co m p u t e r. On t h e o t h e r h a n d , m icro co n t ro lle r is d e s ig n e d t o b e a ll o f t h a t in o n e . No o t h e r e xt e rn a l co m p o n e n t s a re n e e d e d fo r it s a p p lica t io n b e ca u s e a ll n e ce s s a ry p e rip h e ra ls a re a lre a d y b u ilt in t o it . Th u s , we s a ve t h e t im e a n d s p a ce n e e d e d t o co n s t ru ct d e vice s .
1 . 1 Me m o ry u n it Me m o ry is p a rt o f t h e m icro co n t ro lle r wh o s e fu n ct io n is t o s t o re d a t a . Th e e a s ie s t wa y t o e xp la in it is t o d e s crib e it a s o n e b ig clo s e t wit h lo t s o f d ra we rs . If we s u p p o s e t h a t we m a rke d t h e d ra we rs in s u ch a wa y t h a t t h e y ca n n o t b e co n fu s e d , a n y o f t h e ir co n t e n t s will t h e n b e e a s ily a cce s s ib le . It is e n o u g h t o kn o w t h e d e s ig n a t io n o f t h e d ra we r a n d s o it s co n t e n t s will b e kn o wn t o u s fo r s u re .
Me m o ry co m p o n e n t s a re e xa ct ly like t h a t . Fo r a ce rt a in in p u t we g e t t h e co n t e n t s o f a ce rt a in a d d re s s e d m e m o ry lo ca t io n a n d t h a t 's a ll. Two n e w co n ce p t s a re b ro u g h t t o u s : a d d re s s in g a n d m e m o ry lo ca t io n . Me m o ry co n s is t s o f a ll m e m o ry lo ca t io n s , a n d a d d re s s in g is n o t h in g b u t s e le ct in g o n e o f t h e m . Th is m e a n s t h a t we n e e d t o s e le ct t h e d e s ire d m e m o ry lo ca t io n o n o n e h a n d , a n d o n t h e o t h e r h a n d we n e e d t o wa it fo r t h e co n t e n t s o f t h a t lo ca t io n . Be s id e re a d in g fro m a m e m o ry lo ca t io n , m e m o ry m u s t a ls o p ro vid e fo r writ in g o n t o it . Th is is d o n e b y s u p p lyin g a n a d d it io n a l lin e ca lle d co n t ro l lin e . We will d e s ig n a t e t h is lin e a s R/ W ( re a d / writ e ) . Co n t ro l lin e is u s e d in t h e fo llo win g wa y: if r/ w= 1 , re a d in g is d o n e , a n d if o p p o s it e is t ru e t h e n writ in g is d o n e o n t h e m e m o ry lo ca t io n . Me m o ry is t h e firs t e le m e n t , a n d we n e e d a fe w o p e ra t io n o f o u r m icro co n t ro lle r .
1 . 2 Ce n t ra l P ro c e s s in g Un it Le t a d d 3 m o re m e m o ry lo ca t io n s t o a s p e cific b lo ck t h a t will h a ve a b u ilt in ca p a b ilit y t o m u lt ip ly, d ivid e , s u b t ra ct , a n d m o ve it s co n t e n t s fro m o n e m e m o ry lo ca t io n o n t o a n o t h e r. Th e p a rt we ju s t a d d e d in is ca lle d "ce n t ra l p ro ce s s in g u n it " ( CPU) . It s m e m o ry lo ca t io n s a re ca lle d re g is t e rs .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/1_Poglavlje.htm (3 of 9) [4/2/2003 16:17:33]
Chapter 1 - Introduction to Microprocessors
Re g is t e rs a re t h e re fo re m e m o ry lo ca t io n s wh o s e ro le is t o h e lp wit h p e rfo rm in g va rio u s m a t h e m a t ica l o p e ra t io n s o r a n y o t h e r o p e ra t io n s wit h d a t a wh e re ve r d a t a ca n b e fo u n d . Lo o k a t t h e cu rre n t s it u a t io n . We h a ve t wo in d e p e n d e n t e n t it ie s ( m e m o ry a n d CPU) wh ich a re in t e rco n n e ct e d , a n d t h u s a n y e xch a n g e o f d a t a is h in d e re d , a s we ll a s it s fu n ct io n a lit y. If, fo r e xa m p le , we wis h t o a d d t h e co n t e n t s o f t wo m e m o ry lo ca t io n s a n d re t u rn t h e re s u lt a g a in b a ck t o m e m o ry, we wo u ld n e e d a co n n e ct io n b e t we e n m e m o ry a n d CPU. S im p ly s t a t e d , we m u s t h a ve s o m e "wa y" t h ro u g h d a t a g o e s fro m o n e b lo ck t o a n o t h e r.
1 . 3 Bu s Th a t "wa y" is ca lle d "b u s ". Ph ys ica lly, it re p re s e n t s a g ro u p o f 8 , 1 6 , o r m o re wire s Th e re a re t wo t yp e s o f b u s e s : a d d re s s a n d d a t a b u s . Th e firs t o n e co n s is t s o f a s m a n y lin e s a s t h e a m o u n t o f m e m o ry we wis h t o a d d re s s , a n d t h e o t h e r o n e is a s wid e a s d a t a , in o u r ca s e 8 b it s o r t h e co n n e ct io n lin e . Firs t o n e s e rve s t o t ra n s m it a d d re s s fro m CPU m e m o ry, a n d t h e s e co n d t o co n n e ct a ll b lo cks in s id e t h e m icro co n t ro lle r.
As fa r a s fu n ct io n a lit y, t h e s it u a t io n h a s im p ro ve d , b u t a n e w p ro b le m h a s a ls o a p p e a re d : we h a ve a u n it t h a t 's ca p a b le o f wo rkin g b y it s e lf, b u t wh ich d o e s n o t h a ve a n y co n t a ct wit h t h e o u t s id e wo rld , o r wit h u s ! In o rd e r t o re m o ve t h is d e ficie n cy, le t 's a d d a b lo ck wh ich co n t a in s s e ve ra l m e m o ry lo ca t io n s wh o s e o n e e n d is co n n e ct e d t o t h e d a t a b u s , a n d t h e o t h e r h a s co n n e ct io n wit h t h e o u t p u t lin e s o n t h e m icro co n t ro lle r wh ich ca n b e s e e n a s p in s o n t h e e le ct ro n ic co m p o n e n t .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/1_Poglavlje.htm (4 of 9) [4/2/2003 16:17:33]
Chapter 1 - Introduction to Microprocessors
1 . 4 I n p u t - o u t p u t u n it Th o s e lo ca t io n s we 've ju s t a d d e d a re ca lle d "p o rt s ". Th e re a re s e ve ra l t yp e s o f p o rt s : in p u t , o u t p u t o r b id ie ct io n a l p o rt s . Wh e n wo rkin g wit h p o rt s , firs t o f a ll it is n e ce s s a ry t o ch o o s e wh ich p o rt we n e e d t o wo rk wit h , a n d t h e n t o s e n d d a t a t o , o r t a ke it fro m t h e p o rt .
Wh e n wo rkin g wit h it t h e p o rt a ct s like a m e m o ry lo ca t io n . S o m e t h in g is s im p ly b e in g writ t e n in t o o r re a d fro m it , a n d it co u ld b e n o t ice d o n t h e p in s o f t h e m icro co n t ro lle r.
1 . 5 S e ria l c o m m u n ic a t io n Be s id e s t a t e d a b o ve we 've a d d e d t o t h e a lre a d y e xis t in g u n it t h e p o s s ib ilit y o f co m m u n ica t io n wit h a n o u t s id e wo rld . Ho we ve r, t h is wa y o f co m m u n ica t in g h a s it s d ra wb a cks . On e o f t h e b a s ic d ra wb a cks is t h e n u m b e r o f lin e s wh ich n e e d t o b e u s e d in o rd e r t o t ra n s fe r d a t a . Wh a t if it is b e in g t ra n s fe rre d t o a d is t a n ce o f s e ve ra l kilo m e t e rs ? Th e n u m b e r o f lin e s t im e s n u m b e r o f kilo m e t e rs d o e s n 't p ro m is e t h e e co n o m y o f t h e p ro je ct . It le a ve s u s h a vin g t o re d u ce t h e n u m b e r o f lin e s in s u ch a wa y t h a t we d o n 't le s s e n it s fu n ct io n a lit y. S u p p o s e we a re wo rkin g wit h t h re e lin e s o n ly, a n d t h a t o n e lin e is u s e d fo r s e n d in g d a t a , o t h e r fo r re ce ivin g , a n d t h e t h ird o n e is u s e d a s a re fe re n ce lin e fo r b o t h t h e in p u t a n d t h e o u t p u t s id e . In o rd e r fo r t h is t o wo rk, we n e e d t o s e t t h e ru le s o f e xch a n g e o f d a t a . Th e s e ru le s a re ca lle d p ro t o co l. Pro t o co l is t h e re fo re d e fin e d in a d va n ce s o t h e re wo u ld n 't b e a n y m is u n d e rs t a n d in g b e t we e n t h e s id e s t h a t a re co m m u n ica t in g wit h e a ch o t h e r. Fo r e xa m p le , if o n e m a n is s p e a kin g in Fre n ch , a n d t h e o t h e r in En g lis h , it is h ig h ly u n like ly t h a t t h e y will q u ickly a n d e ffe ct ive ly u n d e rs t a n d e a ch o t h e r. Le t 's s u p p o s e we h a ve t h e fo llo win g p ro t o co l. Th e lo g ica l u n it "1 " is s e t u p o n t h e t ra n s m it t in g lin e u n t il t ra n s fe r b e g in s . On ce t h e t ra n s fe r s t a rt s , we lo we r t h e t ra n s m is s io n lin e t o lo g ica l "0 " fo r a p e rio d o f t im e ( wh ich we will d e s ig n a t e a s T) , s o t h e re ce ivin g s id e will kn o w t h a t it is re ce ivin g d a t a , a n d s o it will a ct iva t e it s m e ch a n is m fo r re ce p t io n . Le t 's g o b a ck n o w t o t h e t ra n s m is s io n s id e a n d s t a rt p u t t in g lo g ic ze ro s a n d o n e s o n t o t h e t ra n s m it t e r lin e in t h e o rd e r fro m a b it o f t h e lo we s t va lu e t o a b it o f t h e h ig h e s t va lu e . Le t e a ch b it s t a y o n lin e fo r a t im e p e rio d wh ich is e q u a l t o T, a n d in t h e e n d , o r a ft e r t h e 8 t h b it , le t u s b rin g t h e lo g ica l u n it "1 " b a ck o n t h e lin e wh ich will m a rk t h e e n d o f t h e t ra n s m is s io n o f o n e d a t a . Th e p ro t o co l we 've ju s t d e s crib e d is ca lle d in p ro fe s s io n a l lit e ra t u re NRZ ( No n - Re t u rn t o Ze ro ) .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/1_Poglavlje.htm (5 of 9) [4/2/2003 16:17:33]
Chapter 1 - Introduction to Microprocessors
As we h a ve s e p a ra t e lin e s fo r re ce ivin g a n d s e n d in g , it is p o s s ib le t o re ce ive a n d s e n d d a t a ( in fo . ) a t t h e s a m e t im e . S o ca lle d fu ll- d u p le x m o d e b lo ck wh ich e n a b le s t h is wa y o f co m m u n ica t io n is ca lle d a s e ria l co m m u n ica t io n b lo ck. Un like t h e p a ra lle l t ra n s m is s io n , d a t a m o ve s h e re b it b y b it , o r in a s e rie s o f b it s wh a t d e fin e s t h e t e rm s e ria l co m m u n ica t io n co m e s fro m . Aft e r t h e re ce p t io n o f d a t a we n e e d t o re a d it fro m t h e re ce ivin g lo ca t io n a n d s t o re it in m e m o ry a s o p p o s e d t o s e n d in g wh e re t h e p ro ce s s is re ve rs e d . Da t a g o e s fro m m e m o ry t h ro u g h t h e b u s t o t h e s e n d in g lo ca t io n , a n d t h e n t o t h e re ce ivin g u n it a cco rd in g t o t h e p ro t o co l.
1 . 6 Tim e r u n it S in ce we h a ve t h e s e ria l co m m u n ica t io n e xp la in e d , we ca n re ce ive , s e n d a n d p ro ce s s d a t a .
Ho we ve r, in o rd e r t o u t ilize it in in d u s t ry we n e e d a fe w a d d it io n a lly b lo cks . On e o f t h o s e is t h e t im e r b lo ck wh ich is s ig n ifica n t t o u s b e ca u s e it ca n g ive u s in fo rm a t io n a b o u t t im e , d u ra t io n , p ro t o co l e t c. Th e b a s ic u n it o f t h e t im e r is a fre e - ru n co u n t e r wh ich is in fa ct a re g is t e r wh o s e n u m e ric va lu e in cre m e n t s b y o n e in e ve n in t e rva ls , s o t h a t b y t a kin g it s va lu e d u rin g p e rio d s T1 a n d T2 a n d o n t h e b a s is o f t h e ir d iffe re n ce we ca n d e t e rm in e h o w m u ch t im e h a s e la p s e d . Th is is a ve ry im p o rt a n t p a rt o f t h e m icro co n t ro lle r wh o s e u n d e rs t n d in g re q u ire s m o s t o f o u r t im e .
1 .7 Wa tch do g On e m o re t h in g is re q u irin g o u r a t t e n t io n is a fla wle s s fu n ct io n in g o f t h e m icro co n t ro lle r d u rin g it s ru n - t im e . S u p p o s e t h a t a s a re s u lt o f s o m e in t e rfe re n ce ( wh ich o ft e n d o e s o ccu r in in d u s t ry) o u r m icro co n t ro lle r s t o p s e xe cu t in g t h e p ro g ra m , o r wo rs e , it s t a rt s wo rkin g in co rre ct ly.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/1_Poglavlje.htm (6 of 9) [4/2/2003 16:17:33]
Chapter 1 - Introduction to Microprocessors
Of co u rs e , wh e n t h is h a p p e n s wit h a co m p u t e r, we s im p ly re s e t it a n d it will ke e p wo rkin g . Ho we ve r, t h e re is n o re s e t b u t t o n we ca n p u s h o n t h e m icro co n t ro lle r a n d t h u s s o lve o u r p ro b le m . To o ve rco m e t h is o b s t a cle , we n e e d t o in t ro d u ce o n e m o re b lo ck ca lle d wa t ch d o g . Th is b lo ck is in fa ct a n o t h e r fre e - ru n co u n t e r wh e re o u r p ro g ra m n e e d s t o writ e a ze ro in e ve ry t im e it e xe cu t e s co rre ct ly. In ca s e t h a t p ro g ra m g e t s "s t u ck", ze ro will n o t b e writ t e n in , a n d co u n t e r a lo n e will re s e t t h e m icro co n t ro lle r u p o n a ch ie vin g it s m a xim u m va lu e . Th is will re s u lt in e xe cu t in g t h e p ro g ra m a g a in , a n d co rre ct ly t h is t im e a ro u n d . Th a t is a n im p o rt a n t e le m e n t o f e ve ry p ro g ra m t o b e re lia b le wit h o u t m a n 's s u p e rvis io n .
1 . 8 An a lo g t o D ig it a l Co n v e rt e r As t h e p e rip h e ra l s ig n a ls u s u a lly a re s u b s t a n t ia lly d iffe re n t fro m t h e o n e s t h a t m icro co n t ro lle r ca n u n d e rs t a n d ( ze ro a n d o n e ) , t h e y h a ve t o b e co n ve rt e d in t o a p a t t e rn wh ich ca n b e co m p re h e n d e d b y a m icro co n t ro lle r. Th is t a s k is p e rfo rm e d b y a b lo ck fo r a n a lo g t o d ig it a l co n ve rs io n o r b y a n ADC. Th is b lo ck is re s p o n s ib le fo r co n ve rt in g a n in fo rm a t io n a b o u t s o m e a n a lo g va lu e t o a b in a ry n u m b e r a n d fo r fo llo w it t h ro u g h t o a CPU b lo ck s o t h a t CPU b lo ck ca n fu rt h e r p ro ce s s it .
Fin n a ly, t h e m icro co n t ro lle r is n o w co m p le t e d , a n d a ll we n e e d t o d o n o w is t o a s s e m b le it in t o a n e le ct ro n ic co m p o n e n t wh e re it will a cce s s in n e r b lo cks t h ro u g h t h e o u t s id e p in s . Th e p ict u re b e lo w s h o ws wh a t a m icro co n t ro lle r lo o ks like in s id e .
P h y s ic a l c o n fig u ra t io n o f t h e in t e rio r o f a m ic ro c o n t ro lle r Th in lin e s wh ich le a d fro m t h e ce n t e r t o wa rd s t h e s id e s o f t h e m icro co n t ro lle r re p re s e n t wire s co n n e ct in g in n e r b lo cks wit h t h e p in s o n t h e h o u s in g o f t h e m icro co n t ro lle r s o ca lle d b o n d in g lin e s . Ch a rt o n t h e fo llo win g p a g e re p re s e n t s t h e ce n t e r s e ct io n o f a m icro co n t ro lle r.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/1_Poglavlje.htm (7 of 9) [4/2/2003 16:17:33]
Chapter 1 - Introduction to Microprocessors
Mic ro c o n t ro lle r o u t lin e w it h it s b a s ic e le m e n t s a n d in t e rn a l c o n n e c t io n s Fo r a re a l a p p lica t io n , a m icro co n t ro lle r a lo n e is n o t e n o u g h . Be s id e a m icro co n t ro lle r, we n e e d a p ro g ra m t h a t wo u ld b e e xe cu t e d , a n d a fe w m o re e le m e n t s wh ich m a ke u p a in t e rfa ce lo g ic t o wa rd s t h e e le m e n t s o f re g u la t io n ( wh ich will b e d is cu s s e d in la t e r ch a p t e rs ) .
1 . 9 P ro g ra m Pro g ra m writ in g is a s p e cia l fie ld o f wo rk wit h m icro co n t ro lle rs a n d is ca lle d "p ro g ra m m in g ". Try t o writ e a s m a ll p ro g ra m in a la n g u a g e t h a t we will m a ke u p o u rs e lve s firs t a n d t h e n wo u ld b e u n d e rs t o o d b y a n yo n e . S TART REGI S TER1 = MEMORY LOCATI ON _ A REGI S TER2 = MEMORY LOCATI ON _ B P ORTA= REGI S TER1 + REGI S TER2 EN D
http://www.mikroelektronika.co.yu/english/product/books/PICbook/1_Poglavlje.htm (8 of 9) [4/2/2003 16:17:33]
Chapter 1 - Introduction to Microprocessors
Th e p ro g ra m a d d s t h e co n t e n t s o f t wo m e m o ry lo ca t io n s , a n d vie ws t h e ir s u m o n p o rt A. Th e firs t lin e o f t h e p ro g ra m s t a n d s fo r m o vin g t h e co n t e n t s o f m e m o ry lo ca t io n "A" in t o o n e o f t h e re g is t e rs o f ce n t ra l p ro ce s s in g u n it . As we n e e d t h e o t h e r d a t a a s we ll, we will a ls o m o ve it in t o t h e o t h e r re g is t e r o f t h e ce n t ra l p ro ce s s in g u n it . Th e n e xt in s t ru ct io n in s t ru ct s t h e ce n t ra l p ro ce s s in g u n it t o a d d t h e co n t e n t s o f t h o s e t wo re g is t e rs a n d s e n d a re s u lt t o p o rt A, s o t h a t s u m o f t h a t a d d it io n wo u ld b e vis ib le t o t h e o u t s id e wo rld . Fo r a m o re co m p le x p ro b le m , p ro g ra m t h a t wo rks o n it s s o lu t io n will b e b ig g e r. Pro g ra m m in g ca n b e d o n e in s e ve ra l la n g u a g e s s u ch a s As s e m b le r, C a n d Ba s ic wh ich a re m o s t co m m o n ly u s e d la n g u a g e s . As s e m b le r b e lo n g s t o lo we r le ve l la n g u a g e s t h a t a re p ro g ra m m e d s lo wly, b u t t a ke u p t h e le a s t a m o u n t o f s p a ce in m e m o ry a n d g ive s t h e b e s t re s u lt s wh e re t h e s p e e d o f p ro g ra m e xe cu t io n is co n ce rn e d . As it is t h e m o s t co m m o n ly u s e d la n g u a g e in p ro g ra m m in g m icro co n t ro lle rs it will b e d is cu s s e d in a la t e r ch a p t e r. Pro g ra m s in C la n g u a g e a re e a s ie r t o b e writ t e n , e a s ie r t o b e u n d e rs t o o d , b u t a re s lo we r in e xe cu t in g fro m a s s e m b le r p ro g ra m s . Ba s ic is t h e e a s ie s t o n e t o le a rn , a n d it s in s t ru ct io n s a re n e a re s t a m a n 's wa y o f re a s o n in g , b u t like C p ro g ra m m in g la n g u a g e it is a ls o s lo we r t h a n a s s e m b le r. In a n y ca s e , b e fo re yo u m a ke u p yo u r m in d a b o u t o n e o f t h e s e la n g u a g e s yo u n e e d t o co n s id e r ca re fu lly t h e d e m a n d s fo r e xe cu t io n s p e e d , fo r t h e s ize o f m e m o ry a n d fo r t h e a m o u n t o f t im e a va ila b le fo r it s a s s e m b ly. Aft e r t h e p ro g ra m is writ t e n , we wo u ld in s t a ll t h e m icro co n t ro lle r in t o a d e vice a n d ru n it . In o rd e r t o d o t h is we n e e d t o a d d a fe w m o re e xt e rn a l co m p o n e n t s n e ce s s a ry fo r it s wo rk. Firs t we m u s t g ive life t o a m icro co n t ro lle r b y co n n e ct in g it t o a p o we r s u p p ly ( p o we r n e e d e d fo r o p e ra t io n o f a ll e le ct ro n ic in s t ru m e n t s ) a n d o s cilla t o r wh o s e ro le is s im ila r t o t h e ro le t h a t h e a rt p la ys in a h u m a n b o d y. Ba s e d o n it s clo cks m icro co n t ro lle r e xe cu t e s in s t ru ct io n s o f a p ro g ra m . As it re ce ive s s u p p ly m icro co n t ro lle r will p e rfo rm a s m a ll ch e ck u p o n it s e lf, lo o k u p t h e b e g in n in g o f t h e p ro g ra m a n d s t a rt e xe cu t in g it . Ho w t h e d e vice will wo rk d e p e n d s o n m a n y p a ra m e t e rs , t h e m o s t im p o rt a n t o f wh ich is t h e s killfu ln e s s o f t h e d e ve lo p e r o f h a rd wa re , a n d o n p ro g ra m m e r's e xp e rt is e in g e t t in g t h e m a xim u m o u t o f t h e d e vice wit h h is p ro g ra m .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/1_Poglavlje.htm (9 of 9) [4/2/2003 16:17:33]
Ne x t pa g e
Chapter 2 - Microcontroller PIC16F84
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
CHAP TER 2 Mic ro c o n t ro lle r P I C1 6 F8 4 In t ro d u ct io n CIS C, RIS C Ap p lica t io n s Clo ck/ in s t ru ct io n cycle Pip e lin in g Pin d e s crip t io n 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8
Clo ck g e n e ra t o r - o s cilla t o r Re s e t Ce n t ra l p ro ce s s in g u n it Po rt s Me m o ry o rg a n iza t io n In t e rru p t s Fre e t im e r TMR0 EEPROM Da t a m e m o ry
I n t ro d u c t io n P I C1 6 F8 4 b e lo n g s t o a cla s s o f 8 - b it m icro co n t ro lle rs o f RIS C a rch it e ct u re . It s g e n e ra l s t ru ct u re is s h o wn o n t h e fo llo win g m a p re p re s e n t in g b a s ic b lo cks . P ro g ra m m e m o ry ( FLAS H) - fo r s t o rin g a writ t e n p ro g ra m . S in ce m e m o ry m a d e in FLAS H t e ch n o lo g y ca n b e p ro g ra m m e d a n d cle a re d m o re t h a n o n ce , it m a ke s t h is m icro co n t ro lle r s u it a b le fo r d e vice d e ve lo p m e n t . EEP ROM - d a t a m e m o ry t h a t n e e d s t o b e s a ve d wh e n t h e re is n o s u p p ly. It is u s u a lly u s e d fo r s t o rin g im p o rt a n t d a t a t h a t m u s t n o t b e lo s t if p o we r s u p p ly s u d d e n ly s t o p s . Fo r in s t a n ce , o n e s u ch d a t a is a n a s s ig n e d t e m p e ra t u re in t e m p e ra t u re re g u la t o rs . If d u rin g a lo s s o f p o we r s u p p ly t h is d a t a wa s lo s t , we wo u ld h a ve t o m a ke t h e a d ju s t m e n t o n ce a g a in u p o n re t u rn o f s u p p ly. Th u s o u r d e vice lo o s e s o n s e lf- re lia n ce . RAM - d a t a m e m o ry u s e d b y a p ro g ra m d u rin g it s e xe cu t io n . In RAM a re s t o re d a ll in t e r- re s u lt s o r t e m p o ra ry d a t a d u rin g ru n - t im e . P ORTA a n d P ORTB a re p h ys ica l co n n e ct io n s b e t we e n t h e m icro co n t ro lle r a n d t h e o u t s id e wo rld . Po rt A h a s five , a n d p o rt B e ig h t p in s . FREE- RUN TI MER is a n 8 - b it re g is t e r in s id e a m icro co n t ro lle r t h a t wo rks in d e p e n d e n t ly o f t h e p ro g ra m . On e ve ry fo u rt h clo ck o f t h e o s cilla t o r it in cre m e n t s it s va lu e u n t il it re a ch e s t h e m a xim u m ( 2 5 5 ) , a n d t h e n it s t a rt s co u n t in g o ve r a g a in fro m ze ro . As we kn o w t h e e xa ct t im in g b e t we e n e a ch t wo in cre m e n t s o f t h e t im e r co n t e n t s , t im e r ca n b e u s e d fo r m e a s u rin g t im e wh ich is ve ry u s e fu l wit h s o m e d e vice s . CEN TRAL P ROCES S I N G UN I T h a s a ro le o f co n n e ct ive e le m e n t b e t we e n o t h e r b lo cks in t h e
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_01Poglavlje.htm (1 of 5) [4/2/2003 16:17:37]
Chapter 2 - Microcontroller PIC16F84
m icro co n t ro lle r. It co o rd in a t e s t h e wo rk o f o t h e r b lo cks a n d e xe cu t e s t h e u s e r p ro g ra m .
CI S C, RI S C It h a s a lre a d y b e e n s a id t h a t PIC1 6 F8 4 h a s a RIS C a rch it e ct u re . Th is t e rm is o ft e n fo u n d in co m p u t e r lit e ra t u re , a n d it n e e d s t o b e e xp la in e d h e re in m o re d e t a il. Ha rva rd a rch it e ct u re is a n e we r co n ce p t t h a n vo n - Ne u m a n n 's . It ro s e o u t o f t h e n e e d t o s p e e d u p t h e wo rk o f a m icro co n t ro lle r. In Ha rva rd a rch it e ct u re , d a t a b u s a n d a d d re s s b u s a re s e p a ra t e . Th u s a g re a t e r flo w o f d a t a is p o s s ib le t h ro u g h t h e ce n t ra l p ro ce s s in g u n it , a n d o f co u rs e , a g re a t e r s p e e d o f wo rk. S e p a ra t in g a p ro g ra m fro m d a t a m e m o ry m a ke s it fu rt h e r p o s s ib le fo r in s t ru ct io n s n o t t o h a ve t o b e 8 - b it wo rd s . PIC1 6 F8 4 u s e s 1 4 b it s fo r in s t ru ct io n s wh ich a llo ws fo r a ll in s t ru ct io n s t o b e o n e wo rd in s t ru ct io n s . It is a ls o t yp ica l fo r Ha rva rd a rch it e ct u re t o h a ve fe we r in s t ru ct io n s t h a n vo n - Ne u m a n n 's , a n d t o h a ve in s t ru ct io n s u s u a lly e xe cu t e d in o n e cycle . Micro co n t ro lle rs wit h Ha rva rd a rch it e ct u re a re a ls o ca lle d "RIS C m icro co n t ro lle rs ". RIS C s t a n d s fo r Re d u ce d In s t ru ct io n S e t Co m p u t e r. Micro co n t ro lle rs wit h vo n - Ne u m a n n 's a rch it e ct u re a re ca lle d 'CIS C m icro co n t ro lle rs '. Tit le CIS C s t a n d s fo r Co m p le x In s t ru ct io n S e t Co m p u t e r. S in ce PIC1 6 F8 4 is a RIS C m icro co n t ro lle r, t h a t m e a n s t h a t it h a s a re d u ce d s e t o f in s t ru ct io n s ,
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_01Poglavlje.htm (2 of 5) [4/2/2003 16:17:37]
Chapter 2 - Microcontroller PIC16F84
m o re p re cis e ly 3 5 in s t ru ct io n s . ( e x. In t e l's a n d Mo t o ro la 's m icro co n t ro lle rs h a ve o ve r h u n d re d in s t ru ct io n s ) All o f t h e s e in s t ru ct io n s a re e xe cu t e d in o n e cycle e xce p t fo r ju m p a n d b ra n ch in s t ru ct io n s . Acco rd in g t o wh a t it s m a ke r s a ys , PIC1 6 F8 4 u s u a lly re a ch e s re s u lt s o f 2 : 1 in co d e co m p re s s io n a n d 4 : 1 in s p e e d in re la t io n t o o t h e r 8 - b it m icro co n t ro lle rs in it s cla s s .
Ap p lic a t io n s PIC1 6 F8 4 p e rfe ct ly fit s m a n y u s e s , fro m a u t o m o t ive in d u s t rie s a n d co n t ro llin g h o m e a p p lia n ce s t o in d u s t ria l in s t ru m e n t s , re m o t e s e n s o rs , e le ct rica l d o o rlo cks a n d s a fe t y d e vice s . It is a ls o id e a l fo r s m a rt ca rd s a s we ll a s fo r b a t t e ry s u p p lie d d e vice s b e ca u s e o f it s lo w co n s u m p t io n . EEPROM m e m o ry m a ke s it e a s ie r t o a p p ly m icro co n t ro lle rs t o d e vice s wh e re p e rm a n e n t s t o ra g e o f va rio u s p a ra m e t e rs is n e e d e d ( co d e s fo r t ra n s m it t e rs , m o t o r s p e e d , re ce ive r fre q u e n cie s , e t c. ) . Lo w co s t , lo w co n s u m p t io n , e a s y h a n d lin g a n d fle xib ilit y m a ke PIC1 6 F8 4 a p p lica b le e ve n in a re a s wh e re m icro co n t ro lle rs h a d n o t p re vio u s ly b e e n co n s id e re d ( e xa m p le : t im e r fu n ct io n s , in t e rfa ce re p la ce m e n t in la rg e r s ys t e m s , co p ro ce s s o r a p p lica t io n s , e t c. ) . In S ys t e m Pro g ra m m a b ilit y o f t h is ch ip ( a lo n g wit h u s in g o n ly t wo p in s in d a t a t ra n s fe r) m a ke s p o s s ib le t h e fle xib ilit y o f a p ro d u ct , a ft e r a s s e m b lin g a n d t e s t in g h a ve b e e n co m p le t e d . Th is ca p a b ilit y ca n b e u s e d t o cre a t e a s s e m b ly- lin e p ro d u ct io n , t o s t o re ca lib ra t io n d a t a a va ila b le o n ly a ft e r fin a l t e s t in g , o r it ca n b e u s e d t o im p ro ve p ro g ra m s o n fin is h e d p ro d u ct s .
Clo c k / in s t ru c t io n c y c le Clo ck is m icro co n t ro lle r's m a in s t a rt e r, a n d is o b t a in e d fro m a n e xt e rn a l co m p o n e n t ca lle d a n "o s cilla t o r". If we wa n t t o co m p a re a m icro co n t ro lle r wit h a t im e clo ck, o u r "clo ck" wo u ld t h e n b e a t ickin g s o u n d we h e a r fro m t h e t im e clo ck. In t h a t ca s e , o s cilla t o r co u ld b e co m p a re d t o a s p rin g t h a t is wo u n d s o t im e clo ck ca n ru n . Als o , fo rce u s e d t o win d t h e t im e clo ck ca n b e co m p a re d t o a n e le ct rica l s u p p ly. Clo ck fro m t h e o s cilla t o r e n t e rs a m icro co n t ro lle r via OS C1 p in wh e re in t e rn a l circu it o f a m icro co n t ro lle r d ivid e s t h e clo ck in t o fo u r e ve n clo cks Q1 , Q2 , Q3 , a n d Q4 wh ich d o n o t o ve rla p . Th e s e fo u r clo cks m a ke u p o n e in s t ru ct io n cycle ( a ls o ca lle d m a ch in e cycle ) d u rin g wh ich o n e in s t ru ct io n is e xe cu t e d . Exe cu t io n o f in s t ru ct io n s t a rt s b y ca llin g a n in s t ru ct io n t h a t is n e xt in s t rin g . In s t ru ct io n is ca lle d fro m p ro g ra m m e m o ry o n e ve ry Q1 a n d is writ t e n in in s t ru ct io n re g is t e r o n Q4 . De co d in g a n d e xe cu t io n o f in s t ru ct io n a re d o n e b e t we e n t h e n e xt Q1 a n d Q4 cycle s . On t h e fo llo win g d ia g ra m we ca n s e e t h e re la t io n s h ip b e t we e n in s t ru ct io n cycle a n d clo ck o f t h e o s cilla t o r ( OS C1 ) a s we ll a s t h a t o f in t e rn a l clo cks Q1 - Q4 . Pro g ra m co u n t e r ( PC) h o ld s in fo rm a t io n a b o u t t h e a d d re s s o f t h e n e xt in s t ru ct io n .
P ip e lin in g
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_01Poglavlje.htm (3 of 5) [4/2/2003 16:17:37]
Chapter 2 - Microcontroller PIC16F84
In s t ru ct io n cycle co n s is t s o f cycle s Q1 , Q2 , Q3 a n d Q4 . Cycle s o f ca llin g a n d e xe cu t in g in s t ru ct io n s a re co n n e ct e d in s u ch a wa y t h a t in o rd e r t o m a ke a ca ll, o n e in s t ru ct io n cycle is n e e d e d , a n d o n e m o re is n e e d e d fo r d e co d in g a n d e xe cu t io n . Ho we ve r, d u e t o p ip e lin in g , e a ch in s t ru ct io n is e ffe ct ive ly e xe cu t e d in o n e cycle . If in s t ru ct io n ca u s e s a ch a n g e o n p ro g ra m co u n t e r, a n d PC d o e s n 't p o in t t o t h e fo llo win g b u t t o s o m e o t h e r a d d re s s ( wh ich ca n b e t h e ca s e wit h ju m p s o r wit h ca llin g s u b p ro g ra m s ) , t wo cycle s a re n e e d e d fo r e xe cu t in g a n in s t ru ct io n . Th is is s o b e ca u s e in s t ru ct io n m u s t b e p ro ce s s e d a g a in , b u t t h is t im e fro m t h e rig h t a d d re s s . Cycle o f ca llin g b e g in s wit h Q1 clo ck, b y writ in g in t o in s t ru ct io n re g is t e r ( IR) . De co d in g a n d e xe cu t in g b e g in s wit h Q2 , Q3 a n d Q4 clo cks .
TCY0 re a d s in in s t ru ct io n MOVLW 5 5 h ( it d o e s n 't m a t t e r t o u s wh a t in s t ru ct io n wa s e xe cu t e d , b e ca u s e t h e re is n o re ct a n g le p ict u re d o n t h e b o t t o m ) . TCY1 e xe cu t e s in s t ru ct io n MOVLW 5 5 h a n d re a d s in MOVWF PORTB. TCY2 e xe cu t e s MOVWF PORTB a n d re a d s in CALL S UB_ 1 . TCY3 e xe cu t e s a ca ll o f a s u b p ro g ra m CALL S UB_ 1 , a n d re a d s in in s t ru ct io n BS F PORTA, BIT3 . As t h is in s t ru ct io n is n o t t h e o n e we n e e d , o r is n o t t h e firs t in s t ru ct io n o f a s u b p ro g ra m S UB_ 1 wh o s e e xe cu t io n is n e xt in o rd e r, in s t ru ct io n m u s t b e re a d in a g a in . Th is is a g o o d e xa m p le o f a n in s t ru ct io n n e e d in g m o re t h a n o n e cycle . TCY4 in s t ru ct io n cycle is t o t a lly u s e d u p fo r re a d in g in t h e firs t in s t ru ct io n fro m a s u b p ro g ra m a t a d d re s s S UB_ 1 . TCY5 e xe cu t e s t h e firs t in s t ru ct io n fro m a s u b p ro g ra m S UB_ 1 a n d re a d s in t h e n e xt o n e .
P in d e s c rip t io n PIC1 6 F8 4 h a s a t o t a l o f 1 8 p in s . It is m o s t fre q u e n t ly fo u n d in a DIP1 8 t yp e o f ca s e b u t ca n a ls o b e fo u n d in S MD ca s e wh ich is s m a lle r fro m a DIP. DIP is a n a b b re via t io n fo r Du a l In Pa cka g e . S MD is a n a b b re via t io n fo r S u rfa ce Mo u n t De vice s s u g g e s t in g t h a t h o le s fo r p in s t o g o t h ro u g h wh e n m o u n t in g , a re n 't n e ce s s a ry in s o ld e rin g t h is t yp e o f a co m p o n e n t .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_01Poglavlje.htm (4 of 5) [4/2/2003 16:17:37]
Chapter 2 - Microcontroller PIC16F84
Pin s o n PIC1 6 F8 4 m icro co n t ro lle r h a ve t h e fo llo win g m e a n in g : Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin
n o . 1 RA2 S e co n d p in o n p o rt A. Ha s n o a d d it io n a l fu n ct io n n o . 2 RA3 Th ird p in o n p o rt A. Ha s n o a d d it io n a l fu n ct io n . n o . 3 RA4 Fo u rt h p in o n p o rt A. TOCK1 wh ich fu n ct io n s a s a t im e r is a ls o fo u n d o n t h is p in n o . 4 MCLR Re s e t in p u t a n d Vp p p ro g ra m m in g vo lt a g e o f a m icro co n t ro lle r n o . 5 Vs s Gro u n d o f p o we r s u p p ly. n o . 6 RB0 Ze ro p in o n p o rt B. In t e rru p t in p u t is a n a d d it io n a l fu n ct io n . n o . 7 RB1 Firs t p in o n p o rt B. No a d d it io n a l fu n ct io n . n o . 8 RB2 S e co n d p in o n p o rt B. No a d d it io n a l fu n ct io n . n o . 9 RB3 Th ird p in o n p o rt B. No a d d it io n a l fu n ct io n . n o . 1 0 RB4 Fo u rt h p in o n p o rt B. No a d d it io n a l fu n ct io n . n o . 1 1 RB5 Fift h p in o n p o rt B. No a d d it io n a l fu n ct io n . n o . 1 2 RB6 S ixt h p in o n p o rt B. 'Clo ck' lin e in p ro g ra m m o d e . n o . 1 3 RB7 S e ve n t h p in o n p o rt B. 'Da t a ' lin e in p ro g ra m m o d e . n o . 1 4 Vd d Po s it ive p o we r s u p p ly p o le . n o . 1 5 OS C2 Pin a s s ig n e d fo r co n n e ct in g wit h a n o s cilla t o r n o . 1 6 OS C1 Pin a s s ig n e d fo r co n n e ct in g wit h a n o s cilla t o r n o . 1 7 RA2 S e co n d p in o n p o rt A. No a d d it io n a l fu n ct io n n o . 1 8 RA1 Firs t p in o n p o rt A. No a d d it io n a l fu n ct io n .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_01Poglavlje.htm (5 of 5) [4/2/2003 16:17:37]
Chapter 2 - Microcontroller PIC16F84
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
2 . 1 Clo c k g e n e ra t o r - o s c illa t o r Os cilla t o r circu it is u s e d fo r p ro vid in g a m icro co n t ro lle r wit h a clo ck. Clo ck is n e e d e d s o t h a t m icro co n t ro lle r co u ld e xe cu t e a p ro g ra m o r p ro g ra m in s t ru ct io n s .
Typ e s o f o s cilla t o rs PIC1 6 F8 4 ca n wo rk wit h fo u r d iffe re n t co n fig u ra t io n s o f a n o s cilla t o r. S in ce co n fig u ra t io n s wit h crys t a l o s cilla t o r a n d re s is t o r- ca p a cit o r ( RC) a re t h e o n e s t h a t a re u s e d m o s t fre q u e n t ly, t h e s e a re t h e o n ly o n e s we will m e n t io n h e re . Micro co n t ro lle r t yp e wit h a crys t a l o s cilla t o r h a s in it s d e s ig n a t io n XT, a n d a m icro co n t ro lle r wit h re s is t o r- ca p a cit o r p a ir h a s a d e s ig n a t io n RC. Th is is im p o rt a n t b e ca u s e yo u n e e d t o m e n t io n t h e t yp e o f o s cilla t o r wh e n b u yin g a m icro co n t ro lle r.
XT Os cilla t o r
Crys t a l o s cilla t o r is ke p t in m e t a l h o u s in g wit h t wo p in s wh e re yo u h a ve writ t e n d o wn t h e fre q u e n cy a t wh ich crys t a l o s cilla t e s . On e ce ra m ic ca p a cit o r o f 3 0 p F wh o s e o t h e r e n d is co n n e ct e d t o t h e g ro u n d n e e d s t o b e co n n e ct e d wit h e a ch p in . Os cilla t o r a n d ca p a cit o rs ca n b e p a cke d in jo in t ca s e wit h t h re e p in s . S u ch e le m e n t is ca lle d ce ra m ic re s o n a t o r a n d is re p re s e n t e d in ch a rt s like t h e o n e b e lo w. Ce n t e r p in s o f t h e e le m e n t is t h e g ro u n d , wh ile e n d p in s a re co n n e ct e d wit h OS C1 a n d OS C2 p in s o n t h e m icro co n t ro lle r. Wh e n d e s ig n in g a d e vice , t h e ru le is t o p la ce a n o s cilla t o r n e a re r a m icro co n t ro lle r, s o a s t o a vo id a n y in t e rfe re n ce o n lin e s o n wh ich m icro co n t ro lle r is re ce ivin g a clo ck.
RC Os cilla t o r In a p p lica t io n s wh e re g re a t t im e p re cis io n is n o t n e ce s s a ry, RC o s cilla t o r o ffe rs a d d it io n a l s a vin g s d u rin g p u rch a s e . Re s o n a n t fre q u e n cy o f RC o s cilla t o r d e p e n d s o n s u p p ly vo lt a g e ra t e , re s is t a n ce R, ca p a cit y C a n d wo rkin g t e m p e ra t u re . It s h o u ld b e m e n t io n e d h e re t h a t re s o n a n t fre q u e n cy is a ls o in flu e n ce d b y n o rm a l va ria t io n s in p ro ce s s p a ra m e t e rs , b y t o le ra n ce o f e xt e rn a l R a n d C co m p o n e n t s , e t c.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_02Poglavlje.htm (1 of 3) [4/2/2003 16:17:41]
Chapter 2 - Microcontroller PIC16F84
Ab o ve d ia g ra m s h o ws h o w RC o s cilla t o r is co n n e ct e d wit h PIC1 6 F8 4 . Wit h va lu e o f re s is t o r R b e in g b e lo w 2 . 2 k, o s cilla t o r ca n b e co m e u n s t a b le , o r it ca n e ve n s t o p t h e o s cilla t io n . Wit h ve ry h ig h va lu e o f R ( e x. 1 M) o s cilla t o r b e co m e s ve ry s e n s it ive t o n o is e a n d h u m id it y. It is re co m m e n d e d t h a t va lu e o f re s is t o r R s h o u ld b e b e t we e n 3 a n d 1 0 0 k. Eve n t h o u g h o s cilla t o r will wo rk wit h o u t a n e xt e rn a l ca p a cit o r( C= 0 p F) , ca p a cit o r a b o ve 2 0 p F s h o u ld s t ill b e u s e d fo r n o is e a n d s t a b ilit y. No m a t t e r wh ich o s cilla t o r is b e in g u s e d , in o rd e r t o g e t a clo ck t h a t m icro co n t ro lle r wo rks u p o n , a clo ck o f t h e o s cilla t o r m u s t b e d ivid e d b y 4 . Os cilla t o r clo ck d ivid e d b y 4 ca n a ls o b e o b t a in e d o n OS C2 / CLKOUT p in , a n d ca n b e u s e d fo r t e s t in g o r s yn ch ro n izin g o t h e r lo g ica l circu it s .
Fo llo win g a s u p p ly, o s cilla t o r s t a rt s o s cilla t in g . Os cilla t io n a t firs t h a s a n u n s t a b le p e rio d a n d a m p lit u d e , b u t a ft e r s o m e p e rio d o f t im e it b e co m e s s t a b ilize d .
To p re ve n t s u ch in a ccu ra t e clo ck fro m in flu e n cin g m icro co n t ro lle r's p e rfo rm a n ce , we n e e d t o ke e p t h e m icro co n t ro lle r in re s e t s t a t e d u rin g s t a b iliza t io n o f o s cilla t o r's clo ck. Ab o ve d ia g ra m s h o ws a t yp ica l s h a p e o f a s ig n a l wh ich m icro co n t ro lle r g e t s fro m t h e q u a rt z o s cilla t o r fo llo win g a s u p p ly.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_02Poglavlje.htm (2 of 3) [4/2/2003 16:17:41]
Chapter 2 - Microcontroller PIC16F84
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_02Poglavlje.htm (3 of 3) [4/2/2003 16:17:41]
Chapter 2 - Microcontroller PIC16F84
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
2 . 2 Re s e t Re s e t is u s e d fo r p u t t in g t h e m icro co n t ro lle r in t o a 'kn o wn ' co n d it io n . Th a t p ra ct ica lly m e a n s t h a t m icro co n t ro lle r ca n b e h a ve ra t h e r in a ccu ra t e ly u n d e r ce rt a in u n d e s ira b le co n d it io n s . In o rd e r t o co n t in u e it s p ro p e r fu n ct io n in g it h a s t o b e re s e t , m e a n in g a ll re g is t e rs wo u ld b e p la ce d in a s t a rt in g p o s it io n . Re s e t is n o t o n ly u s e d wh e n m icro co n t ro lle r d o e s n 't b e h a ve t h e wa y we wa n t it t o , b u t ca n a ls o b e u s e d wh e n t ryin g o u t a d e vice a s a n in t e rru p t in p ro g ra m e xe cu t io n , o r t o g e t a m icro co n t ro lle r re a d y wh e n re a d in g in a p ro g ra m .
In o rd e r t o p re ve n t fro m b rin g in g a lo g ica l ze ro t o MCLR p in a ccid e n t a lly ( lin e a b o ve it m e a n s t h a t re s e t is a ct iva t e d b y a lo g ica l ze ro ) , MCLR h a s t o b e co n n e ct e d via re s is t o r t o t h e p o s it ive s u p p ly p o le . Re s is t o r s h o u ld b e b e t we e n 5 a n d 1 0 K. Th is kin d o f re s is t o r wh o s e fu n ct io n is t o ke e p a ce rt a in lin e o n a lo g ica l o n e a s a p re ve n t ive , is ca lle d a p u ll u p .
Micro co n t ro lle r PIC1 6 F8 4 kn o ws s e ve ra l s o u rce s o f re s e t s : a) b) c) d) e)
Re s e t Re s e t Re s e t Re s e t Re s e t
d u rin g p o we r o n , POR ( Po we r- On Re s e t ) d u rin g re g u la r wo rk b y b rin g in g lo g ica l ze ro t o MCLR m icro co n t ro lle r's p in . d u rin g S LEEP re g im e a t wa t ch d o g t im e r ( WDT) o ve rflo w d u rin g a t WDT o ve rflo w d u rin g S LEEP wo rk re g im e .
Th e m o s t im p o rt a n t re s e t s o u rce s a re a ) a n d b ) . Th e firs t o n e o ccu rs e a ch t im e a p o we r s u p p ly is b ro u g h t t o t h e m icro co n t ro lle r a n d s e rve s t o b rin g a ll re g is t e rs t o a s t a rt in g p o s it io n in it ia l s t a t e . Th e s e co n d o n e is a p ro d u ct o f p u rp o s e fu l b rin g in g in o f a lo g ica l ze ro t o MCLR p in d u rin g n o rm a l o p e ra t io n o f t h e m icro co n t ro lle r. Th is s e co n d o n e is o ft e n u s e d in p ro g ra m d e ve lo p m e n t . Du rin g a re s e t , RAM m e m o ry lo ca t io n s a re n o t b e in g re s e t . Th e y a re u n kn o wn d u rin g a p o we r u p a n d a re n o t ch a n g e d a t a n y re s e t . Un like t h e s e , S FR re g is t e rs a re re s e t t o a s t a rt in g p o s it io n in it ia l s t a t e . On e o f t h e m o s t im p o rt a n t e ffe ct s o f a re s e t is s e t t in g a p ro g ra m co u n t e r ( PC) t o ze ro ( 0 0 0 0 h ) , wh ich e n a b le s t h e p ro g ra m t o s t a rt e xe cu t in g fro m t h e firs t writ t e n in s t ru ct io n .
Re s e t a t s u p p ly vo lt a g e d ro p b e lo w t h e p e rm is s ib le ( Bro wn - o u t Re s e t ) Im p u ls e fo r re s e t t in g d u rin g vo lt a g e vo lt a g e - u p is g e n e ra t e d b y m icro co n t ro lle r it s e lf wh e n it d e t e ct s a n in cre a s e in s u p p ly Vd d ( in a ra n g e fro m 1 . 2 V t o 1 . 8 V) . Th a t im p u ls e la s t s 7 2 m s wh ich is e n o u g h t im e fo r a n o s cilla t o r t o g e t s t a b ilize d . Th e s e 7 2 m s a re p ro vid e d b y a n in t e rn a l PWRT t im e r wh ich h a s it s o wn RC o s cilla t o r. Micro co n t ro lle r is in a re s e t m o d e a s lo n g a s PWRT is a ct ive . Ho we ve r, a s d e vice is wo rkin g , p ro b le m a ris e s wh e n s u p p ly d o e s n 't d ro p t o ze ro b u t fa lls b e lo w t h e lim it t h a t g u a ra n t e e s m icro co n t ro lle r's p ro p e r fu n ct io n in g . Th is is a like ly ca s e in p ra ct ice ,
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_03Poglavlje.htm (1 of 2) [4/2/2003 16:17:42]
Chapter 2 - Microcontroller PIC16F84
e s p e cia lly in in d u s t ria l e n viro n m e n t wh e re d is t u rb a n ce s a n d in s t a b ilit y o f s u p p ly a re a n e ve ryd a y o ccu rre n ce . To s o lve t h is p ro b le m we n e e d t o m a ke s u re t h a t m icro co n t ro lle r is in a re s e t s t a t e e a ch t im e s u p p ly fa lls b e lo w t h e a p p ro ve d lim it .
If, a cco rd in g t o e le ct rica l s p e cifica t io n , in t e rn a l re s e t circu it o f a m icro co n t ro lle r ca n n o t s a t is fy t h e n e e d s , s p e cia l e le ct ro n ic co m p o n e n t s ca n b e u s e d wh ich a re ca p a b le o f g e n e ra t in g t h e d e s ire d re s e t s ig n a l. Be s id e t h is fu n ct io n , t h e y ca n a ls o fu n ct io n in wa t ch in g o ve r s u p p ly vo lt a g e . If vo lt a g e d ro p s b e lo w s p e cifie d le ve l, a lo g ica l ze ro wo u ld a p p e a r o n MCLR p in wh ich h o ld s t h e m icro co n t ro lle r in re s e t s t a t e u n t il vo lt a g e is n o t wit h in lim it s t h a t g u a ra n t e e co rre ct fu n ct io n in g .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_03Poglavlje.htm (2 of 2) [4/2/2003 16:17:42]
Chapter 2 - Microcontroller PIC16F84
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
2 . 3 Ce n t ra l P ro c e s s in g Un it Ce n t ra l p ro ce s s in g u n it ( CPU) is t h e b ra in o f a m icro co n t ro lle r. Th a t p a rt is re s p o n s ib le fo r fin d in g a n d fe t ch in g t h e rig h t in s t ru ct io n wh ich n e e d s t o b e e xe cu t e d , fo r d e co d in g t h a t in s t ru ct io n , a n d fin a lly fo r it s e xe cu t io n .
Ce n t ra l p ro ce s s in g u n it co n n e ct s a ll p a rt s o f t h e m icro co n t ro lle r in t o o n e wh o le . S u re ly, it s m o s t im p o rt a n t fu n ct io n is t o d e co d e p ro g ra m in s t ru ct io n s . Wh e n p ro g ra m m e r writ e s a p ro g ra m , in s t ru ct io n s h a ve a cle a r fo rm like MOVLW 0 x2 0 . Ho we ve r, in o rd e r fo r a m icro co n t ro lle r t o u n d e rs t a n d t h a t , t h is 'le t t e r' fo rm o f a n in s t ru ct io n m u s t b e t ra n s la t e d in t o a s e rie s o f ze ro s a n d o n e s wh ich is ca lle d a n 'o p co d e '. Th is t ra n s it io n fro m a le t t e r t o b in a ry fo rm is d o n e b y t ra n s la t o rs s u ch a s a s s e m b le r t ra n s la t o r ( a ls o kn o wn a s a n a s s e m b le r) . In s t ru ct io n t h u s fe t ch e d fro m p ro g ra m m e m o ry m u s t b e d e co d e d b y a ce n t ra l p ro ce s s in g u n it . We ca n t h e n s e le ct fro m t h e t a b le o f a ll t h e in s t ru ct io n s a s e t o f a ct io n s wh ich e xe cu t e a a s s ig n e d t a s k d e fin e d b y in s t ru ct io n . As in s t ru ct io n s m a y wit h in t h e m s e lve s co n t a in a s s ig n m e n t s wh ich re q u ire d iffe re n t t ra n s fe rs o f d a t a fro m o n e m e m o ry in t o a n o t h e r, fro m m e m o ry o n t o p o rt s , o r s o m e o t h e r ca lcu la t io n s , CPU m u s t b e co n n e ct e d wit h a ll p a rt s o f t h e m icro co n t ro lle r. Th is is m a d e p o s s ib le t h ro u g h a d a t a b u s a n d a n a d d re s s b u s .
Arit h m e t ic Lo g ic Un it ( ALU) Arit h m e t ic lo g ic u n it is re s p o n s ib le fo r p e rfo rm in g o p e ra t io n s o f a d d in g , s u b t ra ct in g , m o vin g ( le ft o r rig h t wit h in a re g is t e r) a n d lo g ic o p e ra t io n s . Mo vin g d a t a in s id e a re g is t e r is a ls o kn o wn a s 's h ift in g '. PIC1 6 F8 4 co n t a in s a n 8 - b it a rit h m e t ic lo g ic u n it a n d 8 - b it wo rk re g is t e rs .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_04Poglavlje.htm (1 of 6) [4/2/2003 16:17:45]
Chapter 2 - Microcontroller PIC16F84
In in s t ru ct io n s wit h t wo o p e ra n d s , o rd in a rily o n e o p e ra n d is in wo rk re g is t e r ( W re g is t e r) , a n d t h e o t h e r is o n e o f t h e re g is t e rs o r a co n s t a n t . By o p e ra n d we m e a n t h e co n t e n t s o n wh ich s o m e o p e ra t io n is b e in g d o n e , a n d a re g is t e r is a n y o n e o f t h e GPR o r S FR re g is t e rs . GPR is a n a b re via t io n fo r 'Ge n e ra l Pu rp o s e s Re g is t e rs ', a n d S FR fo r 'S p e cia l Fu n ct io n Re g is t e rs '. In in s t ru ct io n s wit h o n e o p e ra n d , a n o p e ra n d is e it h e r W re g is t e r o r o n e o f t h e re g is t e rs . As a n a d d it io n in d o in g o p e ra t io n s in a rit h m e t ic a n d lo g ic, ALU co n t ro ls s t a t u s b it s ( b it s fo u n d in S TATUS re g is t e r) . Exe cu t io n o f s o m e in s t ru ct io n s a ffe ct s s t a t u s b it s , wh ich d e p e n d s o n t h e re s u lt it s e lf. De p e n d in g o n wh ich in s t ru ct io n is b e in g e xe cu t e d , ALU ca n a ffe ct va lu e s o f Ca rry ( C) , Dig it Ca rry ( DC) , a n d Ze ro ( Z) b it s in S TATUS re g is t e r.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_04Poglavlje.htm (2 of 6) [4/2/2003 16:17:45]
Chapter 2 - Microcontroller PIC16F84
S TATUS Re g is t e r
b it 0 C ( Ca rry) Tra n s fe r Bit t h a t is a ffe ct e d b y o p e ra t io n s o f a d d it io n , s u b t ra ct io n a n d s h ift in g . 1 = t ra n s fe r o ccu re d fro m t h e h ig h e s t re s u lt in g b it 0 = t ra n s fe r d id n o t o ccu r
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_04Poglavlje.htm (3 of 6) [4/2/2003 16:17:45]
Chapter 2 - Microcontroller PIC16F84
C b it is a ffe ct e d b y ADDWF, ADDLW, S UBLW, S UBWF in s t ru ct io n s . b it 1 D C ( Dig it Ca rry) DC Tra n s fe r Bit a ffe ct e d b y o p e ra t io n s o f a d d it io n , s u b t ra ct io n a n d s h ift in g . Un like C b it , t h is b it re p re s e n t s t ra n s fe r fro m t h e fo u rt h re s u lt in g p la ce . It is s e t b y a d d it io n wh e n o ccu rs ca rry fro m b it 3 t o b it 4 , o r b y s u b t ra ct io n wh e n o ccu rs b o rro w fro m b it 4 t o b it 3 , o r b y s h ift in g in b o t h d ire ct io n . 1 = t ra n s fe r o ccu re d o n t h e fo u rt h b it a cco rd in g t o t h e o rd e r o f t h e re s u lt 0 = t ra n s fe r d id n o t o ccu r DC b it is a ffe ct e d b y ADDWF, ADDLW, S UBLW, S UBWF in s t ru ct io n s . b it 2 Z ( Ze ro b it ) In d ica t io n o f a ze ro re s u lt Th is b it is s e t wh e n t h e re s u lt o f a n e xe cu t e d a rit h m e t ic o r lo g ic o p e ra t io n is ze ro . 1 = re s u lt e q u a ls ze ro 0 = re s u lt d o e s n o t e q u a l ze ro b it 3 P D ( Po we r- d o wn b it ) Bit wh ich is s e t wh e n e ve r p o we r s u p p ly is b ro u g h t t o a m icro co n t ro lle r a s it s t a rt s ru n n in g , a ft e r e a ch re g u la r re s e t a n d a ft e r e xe cu t io n o f in s t ru ct io n CLRWDT. In s t ru ct io n S LEEP re s e t s it wh e n m icro co n t ro lle r fa lls in t o lo w co n s u m p t io n / u s a g e re g im e . It s re p e a t e d s e t t in g is p o s s ib le via re s e t o r b y t u rn in g t h e s u p p ly o n , o r o ff . S e t t in g ca n b e t rig g e re d a ls o b y a s ig n a l o n RB0 / INT p in , ch a n g e o n RB p o rt , co m p le t io n o f writ in g in in t e rn a l DATA EEPROM, a n d b y a wa t ch d o g , t o o . 1 = a ft e r s u p p ly h a s b e e n t u rn e d o n 0 = e xe cu t in g S LEEP in s t ru ct io n b it 4 TO Tim e - o u t ; Wa t ch d o g o ve rflo w. Bit is s e t a ft e r t u rn in g o n t h e s u p p ly a n d e xe cu t io n o f CLRWDT a n d S LEEP in s t ru ct io n s . Bit is re s e t wh e n wa t ch d o g g e t s t o t h e e n d s ig n a lin g t h a t s o m e t h in g is n o t rig h t . 1 = o ve rflo w d id n o t o ccu r 0 = o ve rflo w d id o ccu r b it 6 : 5 RP 1 :RP 0 ( Re g is t e r Ba n k S e le ct b it s ) Th e s e t wo b it s a re u p p e r p a rt o f t h e a d d re s s fo r d ire ct a d d re s s in g . S in ce in s t ru ct io n s wh ich a d d re s s t h e m e m o ry d ire ct ly h a ve o n ly s e ve n b it s , t h e y n e e d o n e m o re b it in o rd e r t o a d d re s s a ll 2 5 6 b yt e s wh ich is h o w m a n y b yt e s PIC1 6 F8 4 h a s . RP1 b it is n o t u s e d , b u t is le ft fo r s o m e fu t u re e xp a n s io n s o f t h is m icro co n t ro lle r. 0 1 = firs t b a n k 0 0 = ze ro b a n k b it 7 I RP ( Re g is t e r Ba n k S e le ct b it ) Bit wh o s e ro le is t o b e a n e ig h t h b it fo r in d ire ct a d d re s s in g o f in t e rn a l RAM. 1= bank 2 and 3 0 = b a n k 0 a n d 1 ( fro m 0 0 h t o FFh ) S TATUS re g is t e r co n t a in s a rit h m e t ic s t a t u s ALU ( C, DC, Z) , RES ET s t a t u s ( TO, PD) a n d b it s fo r s e le ct in g o f m e m o ry b a n k ( IRP, RP1 , RP0 ) . Co n s id e rin g t h a t s e le ct io n o f m e m o ry b a n k is co n t ro lle d t h ro u g h t h is re g is t e r, it h a s t o b e p re s e n t in e a ch b a n k. Me m o ry b a n k will b e d is cu s s e d in m o re d e t a il in Me m o ry o rg a n iza t io n ch a p t e r. S TATUS re g is t e r ca n b e a d e s t in a t io n fo r a n y in s t ru ct io n , wit h a n y o t h e r re g is t e r. If S TATUS re g is t e r is a d e s t in a t io n fo r in s t ru ct io n s wh ich a ffe ct Z, DC o r C b it s , t h e n writ in g t o t h e s e t h re e b it s is n o t p o s s ib le .
OPTION re g is t e r
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_04Poglavlje.htm (4 of 6) [4/2/2003 16:17:45]
Chapter 2 - Microcontroller PIC16F84
b it 0 : 2 P S 0 , P S 1 , P S 2 ( Pre s ca le r Ra t e S e le ct b it ) Th e s e t h re e b it s d e fin e p re s ca le r ra t e s e le ct b it . Wh a t a p re s ca le r is a n d h o w t h e s e b it s ca n a ffe ct t h e wo rk o f a m icro co n t ro lle r will b e e xp la in e d in s e ct io n o n TMR0 .
b it 3 P S A ( Pre s ca le r As s ig n m e n t b it ) Bit wh ich a s s ig n s p re s ca le r b e t we e n TMR0 a n d wa t ch d o g . 1 = p re s ca le r is a s s ig n e d t o wa t ch d o g 0 = p re s ca le r is a s s ig n e d t o a fre e - ru n t im e r TMR0 b it 4 T0 S E ( TMR0 S o u rce Ed g e S e le ct b it ) If it is a llo we d t o t rig g e r TMR0 b y im p u ls e s fro m t h e p in RA4 / T0 CKI, t h is b it d e t e rm in e s wh e t h e r t h is will b e t o t h e fa llin g o r ris in g e d g e o f a s ig n a l. 1 = fa llin g e d g e 0 = ris in g e d g e b it 5 TOCS ( TMR0 Clo ck S o u rce S e le ct b it ) Th is p in e n a b le s fre e - ru n t im e r t o in cre m e n t it s s t a t e e it h e r fro m in t e rn a l o s cilla t o r o n e ve ry ¼ o f o s cilla t o r clo ck, o r t h ro u g h e xt e rn a l im p u ls e s o n RA4 / T0 CKI p in . 1 = e xt e rn a l im p u ls e s 0 = 1 / 4 in t e rn a l clo ck b it 6 IN TED G ( In t e rru p t Ed g e S e le ct b it ) If in t e rru p t is e n a b le d p o s s ib le t h is b it will d e t e rm in e t h e e d g e a t wh ich a n in t e rru p t will b e a ct iva t e d o n p in RB0 / INT. 1 = ris in g e d g e 0 = fa llin g e d g e b it 7 RBP U ( PORTB Pu ll- u p En a b le b it ) Th is b it t u rn s o n a n d o ff in t e rn a l 'p u ll- u p ' re s is t o rs o n p o rt B. 1 = "p u ll- u p " re s is t o rs t u rn e d o ff 0 = "p u ll- u p " re s is t o rs t u rn e d o n
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_04Poglavlje.htm (5 of 6) [4/2/2003 16:17:45]
Chapter 2 - Microcontroller PIC16F84 © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_04Poglavlje.htm (6 of 6) [4/2/2003 16:17:45]
Chapter 2 - Microcontroller PIC16F84
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
2 . 4 P o rt s Po rt re fe rs t o a g ro u p o f p in s o n a m icro co n t ro lle r wh ich ca n b e a cce s s e d s im u lt a n e o u s ly, o r o n wh ich we ca n s e t t h e d e s ire d co m b in a t io n o f ze ro s a n d o n e s , o r re a d fro m t h e m a n e xis t in g s t a t u s . Ph ys ica lly, p o rt is a re g is t e r in s id e a m icro co n t ro lle r wh ich is co n n e ct e d b y wire s t o t h e p in s o f a m icro co n t ro lle r. Po rt s re p re s e n t p h ys ica l co n n e ct io n o f Ce n t ra l Pro ce s s in g Un it wit h a n o u t s id e wo rld . Micro co n t ro lle r u s e s t h e m in o rd e r t o m o n it o r o r co n t ro l o t h e r co m p o n e n t s o r d e vice s . Du e t o fu n ct io n a lit y, s o m e p in s h a ve t wo fo ld ro le s like PA4 / TOCKI fo r in s t a n ce , wh ich is s im u lt a n e o u s ly t h e fo u rt h b it o f p o rt A a n d a n e xt e rn a l in p u t fo r fre e - ru n co u n t e r. S e le ct io n o f o n e o f t h e s e t wo p in fu n ct io n s is d o n e in o n e o f t h e co n fig u ra t io n a l re g is t e rs . An illu s t ra t io n o f t h is is t h e fift h b it T0 CS in OPTION re g is t e r. By s e le ct in g o n e o f t h e fu n ct io n s t h e o t h e r o n e is d is a b le d .
All p o rt p in s ca n b e d e fin e d a s in p u t o r o u t p u t , a cco rd in g t o t h e n e e d s o f a d e vice t h a t 's b e in g d e ve lo p e d . In o rd e r t o d e fin e a p in a s in p u t o r o u t p u t p in , t h e rig h t co m b in a t io n o f ze ro s a n d o n e s m u s t b e writ t e n in TRIS re g is t e r. If a t t h e a p p ro p ria t e p la ce in TRIS re g is t e r a lo g ica l "1 " is writ t e n , t h e n t h a t p in is a n in p u t p in , a n d if t h e o p p o s it e is t ru e , it 's a n o u t p u t p in . Eve ry p o rt h a s it s p ro p e r TRIS re g is t e r. Th u s , p o rt A h a s TRIS A a t a d d re s s 8 5 h , a n d p o rt B h a s TRIS B a t a d d re s s 86h.
PORTB PORTB h a s 8 p in s jo in e d t o it . Th e a p p ro p ria t e re g is t e r fo r d ire ct io n o f d a t a is TRIS B a t a d d re s s 8 6 h . S e t t in g a b it in TRIS B re g is t e r d e fin e s t h e co rre s p o n d in g p o rt p in a s a n in p u t p in , a n d re s e t t in g a b it in TRIS B re g is t e r d e fin e s t h e co rre s p o n d in g p o rt p in a s t h e o u t p u t p in . Ea ch p in o n PORTB h a s a we a k in t e rn a l p u ll- u p re s is t o r ( re s is t o r wh ich d e fin e s a lin e t o lo g ic o n e ) wh ich ca n b e a ct iva t e d b y re s e t t in g t h e s e ve n t h b it RBPU in OPTION re g is t e r. Th e s e 'p u ll- u p ' re s is t o rs a re a u t o m a t ica lly b e in g t u rn e d o ff wh e n p o rt p in is co n fig u re d a s a n o u t p u t . Wh e n a m icro co n t ro lle r is s t a rt e d , p u ll- u p 's a re d is a b le d . Fo u r p in s PORTB, RB7 : RB4 ca n ca u s e a n in t e rru p t wh ich o ccu rs wh e n t h e ir s t a t u s ch a n g e s fro m
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_05Poglavlje.htm (1 of 2) [4/2/2003 16:17:47]
Chapter 2 - Microcontroller PIC16F84
lo g ica l o n e in t o lo g ica l ze ro a n d o p p o s it e . On ly p in s co n fig u re d a s in p u t ca n ca u s e t h is in t e rru p t t o o ccu r ( if a n y RB7 : RB4 p in is co n fig u re d a s a n o u t p u t , a n in t e rru p t wo n 't b e g e n e ra t e d a t t h e ch a n g e o f s t a t u s . ) Th is in t e rru p t o p t io n a lo n g wit h in t e rn a l p u ll- u p re s is t o rs m a ke s it e a s ie r t o s o lve co m m o n p ro b le m s we fin d in p ra ct ice like fo r in s t a n ce t h a t o f m a t rix ke yb o a rd . If ro ws o n t h e ke yb o a rd a re co n n e ct e d t o t h e s e p in s , e a ch p u s h o n a ke y will t h e n ca u s e a n in t e rru p t . A m icro co n t ro lle r will d e t e rm in e wh ich ke y is a t h a n d wh ile p ro ce s s in g a n in t e rru p t It is n o t re co m m e n d e d t o re fe r t o p o rt B a t t h e s a m e t im e t h a t in t e rru p t is b e in g p ro ce s s e d .
Th e a b o ve e xa m p le s h o ws h o w p in s 0 , 1 , 2 , a n d 3 a re d e cla re d fo r in p u t , a n d p in s 4 , 5 , 6 , a n d 7 fo r o u t p u t .
PORTA PORTA h a s 5 p in s jo in e d t o it . Th e co rre s p o n d in g re g is t e r fo r d a t a d ire ct io n is TRIS A a t a d d re s s 8 5 h . Like wit h p o rt B, s e t t in g a b it in TRIS A re g is t e r d e fin e s a ls o t h e co rre s p o n d in g p o rt p in a s a n in p u t p in , a n d cle a rin g a b it in TRIS A re g is t e r d e fin e s t h e co rre s p o n d in g p o rt p in a s a n o u t p u t p in . Th e fift h p in o f p o rt A h a s d u a l fu n ct io n . On t h a t p in is a ls o s it u a t e d a n e xt e rn a l in p u t fo r t im e r TMR0 . On e o f t h e s e t wo o p t io n s is ch o s e n b y s e t t in g o r re s e t t in g t h e T0 CS b it ( TMR0 Clo ck S o u rce S e le ct b it ) . Th is p in e n a b le s t h e t im e r TMR0 t o in cre a s e it s s t a t u s e it h e r fro m in t e rn a l o s cilla t o r o r via e xt e rn a l im p u ls e s o n RA4 / T0 CKI p in .
Exa m p le s h o ws h o w p in s 0 , 1 , 2 , 3 , a n d 4 a re d e cla re d t o b e in p u t , a n d p in s 5 , 6 , a n d 7 t o b e o u t p u t p in s .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_05Poglavlje.htm (2 of 2) [4/2/2003 16:17:47]
Chapter 2 - Microcontroller PIC16F84
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
2 . 5 Me m o ry o rg a n iz a t io n PIC1 6 F8 4 h a s t wo s e p a ra t e m e m o ry b lo cks , o n e fo r d a t a a n d t h e o t h e r fo r p ro g ra m . EEPROM m e m o ry a n d GPR re g is t e rs in RAM m e m o ry m a ke u p a d a t a b lo ck, a n d FLAS H m e m o ry m a ke s u p a p ro g ra m b lo ck.
Pro g ra m m e m o ry Pro g ra m m e m o ry h a s b e e n re a lize d in FLAS H t e ch n o lo g y wh ich m a ke s it p o s s ib le t o p ro g ra m a m icro co n t ro lle r m a n y t im e s b e fo re it 's in s t a lle d in t o a d e vice , a n d e ve n a ft e r it s in s t a llm e n t if e ve n t u a l ch a n g e s in p ro g ra m o r p ro ce s s p a ra m e t e rs s h o u ld o ccu r. Th e s ize o f p ro g ra m m e m o ry is 1 0 2 4 lo ca t io n s wit h 1 4 b it s wid t h wh e re lo ca t io n s ze ro a n d fo u r a re re s e rve d fo r re s e t a n d in t e rru p t ve ct o r.
Da t a m e m o ry Da t a m e m o ry co n s is t s o f EEPROM a n d RAM m e m o rie s . EEPROM m e m o ry co n s is t s o f 6 4 e ig h t b it lo ca t io n s wh o s e co n t e n t s is n o t lo s t d u rin g lo o s in g o f p o we r s u p p ly. EEPROM is n o t d ire ct ly a d d re s s ib le , b u t is a cce s s e d in d ire ct ly t h ro u g h EEADR a n d EEDATA re g is t e rs . As EEPROM m e m o ry u s u a lly s e rve s fo r s t o rin g im p o rt a n t p a ra m e t e rs ( fo r e xa m p le , o f a g ive n t e m p e ra t u re in t e m p e ra t u re re g u la t o rs ) , t h e re is a s t rict p ro ce d u re fo r writ in g in EEPROM wh ich m u s t b e fo llo we d in o rd e r t o a vo id a ccid e n t a l writ in g . RAM m e m o ry fo r d a t a o ccu p ie s s p a ce o n a m e m o ry m a p fro m lo ca t io n 0 x0 C t o 0 x4 F wh ich co m e s t o 6 8 lo ca t io n s . Lo ca t io n s o f RAM m e m o ry a re a ls o ca lle d GPR re g is t e rs wh ich is a n a b b re via t io n fo r Ge n e ra l Pu rp o s e Re g is t e rs . GPR re g is t e rs ca n b e a cce s s e d re g a rd le s s o f wh ich b a n k is s e le ct e d a t t h e m o m e n t .
S FR re g is t e rs Re g is t e rs wh ich t a ke u p firs t 1 2 lo ca t io n s in b a n ks 0 a n d 1 a re re g is t e rs o f s p e cia lize d fu n ct io n a s s ig n e d wit h ce rt a in b lo cks o f t h e m icro co n t ro lle r. Th e s e a re ca lle d S p e cia l Fu n ct io n Re g is t e rs .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_06Poglavlje.htm (1 of 5) [4/2/2003 16:17:50]
Chapter 2 - Microcontroller PIC16F84
Me m o ry Ba n ks Be s id e t h is 'le n g t h ' d ivis io n t o S FR a n d GPR re g is t e rs , m e m o ry m a p is a ls o d ivid e d in 'wid t h ' ( s e e p re ce d in g m a p ) t o t wo a re a s ca lle d 'b a n ks '. S e le ct in g o n e o f t h e b a n ks is d o n e via RP0 a n d RP1 b it s in S TATUS re g is t e r. Ex a m p le : bcf STATUS, RP0 In s t ru ct io n BCF cle a rs b it RP0 ( RP0 = 0 ) in S TATUS re g is t e r a n d t h u s s e t s u p b a n k 0 .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_06Poglavlje.htm (2 of 5) [4/2/2003 16:17:50]
Chapter 2 - Microcontroller PIC16F84
bsf STATUS, RP0 In s t ru ct io n BS F s e t s t h e b it RP0 ( RP0 = 1 ) in S TATUS re g is t e r a n d t h u s s e t s u p b a n k1 . Us u a lly, g ro u p s o f in s t ru ct io n s t h a t a re o ft e n in u s e , a re co n n e ct e d in t o o n e u n it wh ich ca n e a s ily b e re ca lle d in a p ro g ra m , a n d wh o s e n a m e h a s a cle a r m e a n in g , s o ca lle d Ma cro s . Wit h t h e ir u s e , s e le ct io n b e t we e n t wo b a n ks b e co m e s m o re cle a r a n d t h e p ro g ra m it s e lf m o re le g ib le . BANK0 macro Bcf STATUS, RP0 Endm
;Select memory bank 0
BANK1 macro Bsf STATUS, RP0 Endm
;Select memory bank 1
Lo ca t io n s 0 Ch - 4 Fh a re g e n e ra l p u rp o s e re g is t e rs ( GPR) w h ich a re u s e d a s RAM m e m o ry . W h e n lo ca t io n s 8 Ch - CFh in Ba n k 1 a re a cce s s e d , w e a ct u a lly a cce s s t h e e x a ct s a m e lo ca t io n s in Ba n k 0 . In o t h e r w o rd s , w h e n e v e r y o u w is h t o a cce s s o n e o f t h e GPR re g is t e rs , t h e re is n o n e e d t o w o rry a b o u t w h ich b a n k w e a re in !
Pro g ra m Co u n t e r Pro g ra m co u n t e r ( PC) is a 1 3 b it re g is t e r t h a t co n t a in s t h e a d d re s s o f t h e in s t ru ct io n b e in g e xe cu t e d . By it s in cre m e n t in g o r ch a n g e ( e x. in ca s e o f ju m p s ) m icro co n t ro lle r e xe cu t e s p ro g ra m in s t ru ct io n s s t e p - b y- s t e p .
S t a ck PIC1 6 F8 4 h a s a 1 3 - b it s t a ck wit h 8 le ve ls , o r in o t h e r wo rd s , a g ro u p o f 8 m e m o ry lo ca t io n s o f 1 3 b it s wid t h wit h s p e cia l fu n ct io n . It s b a s ic ro le is t o ke e p t h e va lu e o f p ro g ra m co u n t e r a ft e r a ju m p fro m t h e m a in p ro g ra m t o a n a d d re s s o f a s u b p ro g ra m . In o rd e r fo r a p ro g ra m t o kn o w h o w t o g o b a ck t o t h e p o in t wh e re it s t a rt e d fro m , it h a s t o re t u rn t h e va lu e o f a p ro g ra m co u n t e r fro m a s t a ck. Wh e n m o vin g fro m a p ro g ra m t o a s u b p ro g ra m , p ro g ra m co u n t e r is b e in g p u s h e d o n t o a s t a ck ( e xa m p le o f t h is is CALL in s t ru ct io n ) . Wh e n e xe cu t in g in s t ru ct io n s s u ch a s RETURN, RETLW o r RETFIE wh ich we re e xe cu t e d a t t h e e n d o f a s u b p ro g ra m , p ro g ra m co u n t e r wa s t a ke n fro m a s t a ck s o t h a t p ro g ra m co u ld co n t in u e wh e re wa s s t o p p e d b e fo re it wa s in t e rru p t e d . Th e s e o p e ra t io n s o f p la cin g o n a n d t a kin g o ff fro m a p ro g ra m co u n t e r s t a ck a re ca lle d PUS H a n d POP, a n d a re n a m e d a cco rd in g s im ila r in s t ru ct io n s o n s o m e b ig g e r m icro co n t ro lle rs .
In S ys t e m Pro g ra m m in g In o rd e r t o p ro g ra m a p ro g ra m m e m o ry, m icro co n t ro lle r m u s t b e s e t t o s p e cia l wo rkin g m o d e b y b rin g in g u p MCLR p in t o 1 3 . 5 V, a n d s u p p ly vo lt a g e Vd d h a s t o b e s t a b ilize d b e t we e n 4 . 5 V t o 5 . 5 V. Pro g ra m m e m o ry ca n b e p ro g ra m m e d s e ria lly u s in g t wo 'd a t a / clo ck' p in s wh ich m u s t p re vio u s ly b e s e p a ra t e d fro m d e vice lin e s , s o t h a t e rro rs wo u ld n 't co m e u p d u rin g p ro g ra m m in g .
Ad d re s s in g m o d e s RAM m e m o ry lo ca t io n s ca n b e a cce s s e d d ire ct ly o r in d ire ct ly.
Dire ct Ad d re s s in g Dire ct Ad d re s s in g is d o n e t h ro u g h a 9 - b it a d d re s s . Th is a d d re s s is o b t a in e d b y co n n e ct in g 7 t h b it o f d ire ct a d d re s s o f a n in s t ru ct io n wit h t wo b it s ( RP1 , RP0 ) fro m S TATUS re g is t e r a s is s h o wn o n t h e fo llo win g p ict u re . An y a cce s s t o S FR re g is t e rs ca n b e a n e xa m p le o f d ire ct a d d re s s in g .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_06Poglavlje.htm (3 of 5) [4/2/2003 16:17:50]
Chapter 2 - Microcontroller PIC16F84
Bsf STATUS, RP0 ;Bankl movlw 0xFF ;w=0xFF movwf TRISA ;address of TRISA register is taken from ;instruction movwf
D ire c t a d d re s s in g
In d ire ct Ad d re s s in g In d ire ct u n like d ire ct a d d re s s in g d o e s n o t t a ke a n a d d re s s fro m a n in s t ru ct io n b u t m a ke s it wit h t h e h e lp o f IRP b it o f S TATUS a n d FS R re g is t e rs . Ad d re s s e d lo ca t io n is a cce s s e d via INDF re g is t e r wh ich in fa ct h o ld s t h e a d d re s s in d ica t e d b y a FS R. In o t h e r wo rd s , a n y in s t ru ct io n wh ich u s e s INDF a s it s re g is t e r in re a lit y a cce s s e s d a t a in d ica t e d b y a FS R re g is t e r. Le t 's s a y, fo r in s t a n ce , t h a t o n e g e n e ra l p u rp o s e re g is t e r ( GPR) a t a d d re s s 0 Fh co n t a in s a va lu e o f 2 0 . By writ in g a va lu e o f 0 Fh in FS R re g is t e r we will g e t a re g is t e r in d ica t o r a t a d d re s s 0 Fh , a n d b y re a d in g fro m INDF re g is t e r, we will g e t a va lu e o f 2 0 , wh ich m e a n s t h a t we h a ve re a d fro m t h e firs t re g is t e r it s va lu e wit h o u t a cce s s in g it d ire ct ly ( b u t via FS R a n d INDF) . It a p p e a rs t h a t t h is t yp e o f a d d re s s in g d o e s n o t h a ve a n y a d va n t a g e s o ve r d ire ct a d d re s s in g , b u t ce rt a in n e e d s d o e xis t d u rin g p ro g ra m m in g wh ich ca n b e s o lve d s m o o t h ly o n ly t h ro u g h in d ire ct a d d re s s in g .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_06Poglavlje.htm (4 of 5) [4/2/2003 16:17:50]
Chapter 2 - Microcontroller PIC16F84
An o f s u ch e xa m p le ca n b e s e n d in g a s e t o f d a t a via s e ria l co m m u n ica t io n , wo rkin g wit h b u ffe rs a n d in d ica t o rs ( wh ich will b e d is cu s s e d fu rt h e r in a ch a p t e r wit h e xa m p le s ) , o r e ra s in g a p a rt o f RAM m e m o ry ( 1 6 lo ca t io n s ) a s in t h e fo llo win g in s t a n ce .
Re a d in g d a t a fro m INDF re g is t e r wh e n t h e co n t e n t s o f FS R re g is t e r is e q u a l t o ze ro re t u rn s t h e va lu e o f ze ro , a n d writ in g t o it re s u lt s in NOP o p e ra t io n ( n o o p e ra t io n ) .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_06Poglavlje.htm (5 of 5) [4/2/2003 16:17:50]
Chapter 2 - Microcontroller PIC16F84
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
2 . 6 I n t e rru p t s In t e rru p t s a re a m e ch a n is m o f a m icro co n t ro lle r wh ich e n a b le s it t o re s p o n d t o s o m e e ve n t s a t t h e m o m e n t wh e n t h e y o ccu r, re g a rd le s s o f wh a t m icro co n t ro lle r is d o in g a t t h e t im e . Th is is a ve ry im p o rt a n t p a rt , b e ca u s e it p ro vid e s co n n e ct io n b e t we e n a m icro co n t ro lle r a n d e n viro n m e n t wh ich s u rro u n d s it . Ge n e ra lly, e a ch in t e rru p t ch a n g e s t h e p ro g ra m flo w, in t e rru p t s it a n d a ft e r e xe cu t in g a n in t e rru p t s u b p ro g ra m ( in t e rru p t ro u t in e ) it co n t in u e s fro m t h a t s a m e p o in t o n .
On e o f t h e p o s s ib le s o u rc e s o f a n in t e rru p t a n d h o w it a ffe c t s t h e m a in p ro g ra m Co n t ro l re g is t e r o f a n in t e rru p t is ca lle d INTCON a n d is fo u n d a t 0 Bh a d d re s s . It s ro le is t o a llo w o r d is a llo we d in t e rru p t s , a n d in ca s e t h e y a re n o t a llo we d , it re g is t e rs s in g le in t e rru p t re q u e s t s t h ro u g h it s o wn b it s .
INTCON Re g is t e r
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.htm (1 of 7) [4/2/2003 16:17:55]
Chapter 2 - Microcontroller PIC16F84
b it 0 RBI F ( RB Po rt Ch a n g e In t e rru p t Fla g b it ) Bit wh ich in fo rm s a b o u t ch a n g e s o n p in s 4 , 5 , 6 a n d 7 o f p o rt B. 1 = a t le a s t o n e p in h a s ch a n g e d it s s t a t u s 0 = n o ch a n g e o ccu re d o n a n y o f t h e p in s b it 1 I N TF ( INT Ext e rn a l In t e rru p t Fla g b it ) Ext e rn a l in t e rru p t o ccu re d . 1 = in t e rru p t o ccu re d 0 = in t e rru p t d id n o t o ccu r If a ris in g o r fa llin g e d g e wa s d e t e ct e d o n p in RB0 / INT, ( wh ich is d e fin e d wit h b it INTEDG in OPTION re g is t e r) , b it INTF is s e t . Bit m u s t b e cle a re d in in t e rru p t s u b p ro g ra m in o rd e r t o d e t e ct t h e n e xt in t e rru p t . b it 2 T0 I F ( TMR0 Ove rflo w In t e rru p t Fla g b it ) Ove rflo w o f co u n t e r TMR0 . 1 = co u n t e r ch a n g e d it s s t a t u s fro m FFh t o 0 0 h 0 = o ve rflo w d id n o t o ccu r Bit m u s t b e cle a re d in p ro g ra m in o rd e r fo r a n in t e rru p t t o b e d e t e ct e d . b it 3 RBI E ( RB p o rt ch a n g e In t e rru p t En a b le b it ) En a b le s in t e rru p t s t o o ccu r a t t h e ch a n g e o f s t a t u s o f p in s 4 , 5 , 6 , a n d 7 o f p o rt B. 1 = e n a b le s in t e rru p t s a t t h e ch a n g e o f s t a t u s 0 = in t e rru p t s d is a b le d a t t h e ch a n g e o f s t a t u s If RBIE a n d RBIF we re s im u lt a n e o u s ly s e t , a n in t e rru p t wo u ld o ccu r. b it 4 I N TE ( INT Ext e rn a l In t e rru p t En a b le b it ) Bit wh ich e n a b le s e xt e rn a l in t e rru p t fro m p in RB0 / INT. 1 = e xt e rn a l in t e rru p t e n a b le d 0 = e xt e rn a l in t e rru p t d is a b le d If INTE a n d INTF we re s e t s im u lt a n e o u s ly, a n in t e rru p t wo u ld o ccu r. b it 5 T0 I E ( TMR0 Ove rflo w In t e rru p t En a b le b it ) Bit wh ich e n a b le s in t e rru p t s d u rin g co u n t e r TMR0 o ve rflo w. 1 = in t e rru p t e n a b le d 0 = in t e rru p t d is a b le d If T0 IE a n d T0 IF we re s e t s im u lt a n e o u s ly, in t e rru p t wo u ld o ccu r. Bit 6 EEI E ( EEPROM Writ e Co m p le t e In t e rru p t En a b le b it ) Bit wh ich e n a b le s a n in t e rru p t a t t h e e n d o f a writ in g ro u t in e t o EEPROM 1 = in t e rru p t e n a b le d 0 = in t e rru p t d is a b le d If EEIE a n d EEIF ( wh ich is in EECON1 re g is t e r) we re s e t s im u lt a n e o u s ly , a n in t e rru p t wo u ld o ccu r. Bit 7 GI E ( Glo b a l In t e rru p t En a b le b it ) Bit wh ich e n a b le s o r d is a b le s a ll in t e rru p t s . 1 = a ll in t e rru p t s a re e n a b le d 0 = a ll in t e rru p t s a re d is a b le d PIC1 6 F8 4 h a s fo u r in t e rru p t s o u rce s : 1. 2. 3. 4.
Te rm in a t io n o f writ in g d a t a t o EEPROM TMR0 in t e rru p t ca u s e d b y t im e r o ve rflo w In t e rru p t d u rin g a lt e ra t io n o n RB4 , RB5 , RB6 a n d RB7 p in s o f p o rt B. Ext e rn a l in t e rru p t fro m RB0 / INT p in o f m icro co n t ro lle r
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.htm (2 of 7) [4/2/2003 16:17:55]
Chapter 2 - Microcontroller PIC16F84
Ge n e ra lly s p e a kin g , e a ch in t e rru p t s o u rce h a s t wo b it s jo in e d t o it . On e e n a b le s in t e rru p t s , a n d t h e o t h e r d e t e ct s wh e n in t e rru p t s o ccu r. Th e re is o n e co m m o n b it ca lle d GIE wh ich ca n b e u s e d t o d is a llo w o r e n a b le a ll in t e rru p t s s im u lt a n e o u s ly. Th is b it is ve ry u s e fu l wh e n writ in g a p ro g ra m b e ca u s e it a llo ws fo r a ll in t e rru p t s t o b e d is a b le d fo r a p e rio d o f t im e , s o t h a t e xe cu t io n o f s o m e im p o rt a n t p a rt o f a p ro g ra m wo u ld n o t b e in t e rru p t e d . Wh e n in s t ru ct io n wh ich re s e t s GIE b it wa s e xe cu t e d ( GIE= 0 , a ll in t e rru p t s d is a llo we d ) , a n y in t e rru p t t h a t re m a in e d u n s o lve d s h o u ld b e ig n o re d .
In t e rru p t s wh ich re m a in e d u n s o lve d a n d we re ig n o re d , a re p ro ce s s e d wh e n GIE b it ( GIE= 1 , a ll in t e rru p t s a llo we d ) wo u ld b e cle a re d . Wh e n in t e rru p t wa s a n s we re d , GIE b it wa s cle a re d s o t h a t a n y a d d it io n a l in t e rru p t s wo u ld b e d is a b le d , re t u rn a d d re s s wa s p u s h e d o n t o s t a ck a n d a d d re s s 0 0 0 4 h wa s writ t e n in p ro g ra m co u n t e r - o n ly a ft e r t h is d o e s re p lyin g t o a n in t e rru p t b e g in ! Aft e r in t e rru p t is p ro ce s s e d , b it wh o s e s e t t in g ca u s e d a n in t e rru p t m u s t b e cle a re d , o r in t e rru p t ro u t in e wo u ld a u t o m a t ica lly b e p ro ce s s e d o ve r a g a in d u rin g a re t u rn t o t h e m a in p ro g ra m .
Ke e p in g t h e co n t e n t s o f im p o rt a n t re g is t e rs On ly re t u rn va lu e o f p ro g ra m co u n t e r is s t o re d o n a s t a ck d u rin g a n in t e rru p t ( b y re t u rn va lu e o f p ro g ra m co u n t e r we m e a n t h e a d d re s s o f t h e in s t ru ct io n wh ich wa s t o b e e xe cu t e d , b u t wa s n 't b e ca u s e in t e rru p t o ccu re d ) . Ke e p in g o n ly t h e va lu e o f p ro g ra m co u n t e r is o ft e n n o t e n o u g h . S o m e re g is t e rs wh ich a re a lre a d y in u s e in t h e m a in p ro g ra m ca n a ls o b e in u s e in in t e rru p t ro u t in e . If t h e y we re n o t re t a in e d , m a in p ro g ra m wo u ld d u rin g a re t u rn fro m a n in t e rru p t ro u t in e g e t co m p le t e ly d iffe re n t va lu e s in t h o s e re g is t e rs , wh ich wo u ld ca u s e a n e rro r in t h e p ro g ra m . On e e xa m p le fo r s u ch a ca s e is co n t e n t s o f t h e wo rk re g is t e r W. If we s u p p o s e t h a t m a in p ro g ra m wa s u s in g wo rk re g is t e r W fo r s o m e o f it s o p e ra t io n s , a n d if it h a d s t o re d in it s o m e va lu e t h a t 's im p o rt a n t fo r t h e fo llo win g in s t ru ct io n , t h e n a n in t e rru p t wh ich o ccu rs b e fo re t h a t in s t ru ct io n wo u ld ch a n g e t h e va lu e o f wo rk re g is t e r W wh ich wo u ld d ire ct ly b e in flu e n ce d t h e m a in p ro g ra m . Pro ce d u re o f re co rd in g im p o rt a n t re g is t e rs b e fo re g o in g t o a n in t e rru p t ro u t in e is ca lle d PUS H, wh ile t h e p ro ce d u re wh ich b rin g s re co rd e d va lu e s b a ck, is ca lle d POP. PUS H a n d POP a re in s t ru ct io n s wit h s o m e o t h e r m icro co n t ro lle rs ( In t e l) , b u t a re s o wid e ly a cce p t e d t h a t a wh o le o p e ra t io n is n a m e d a ft e r t h e m . PIC1 6 F8 4 d o e s n o t h a ve in s t ru ct io n s like PUS H a n d POP, a n d t h e y h a ve t o b e p ro g ra m m e d .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.htm (3 of 7) [4/2/2003 16:17:55]
Chapter 2 - Microcontroller PIC16F84
On e o f t h e p o s s ib le c a s e s o f e rro rs if s a v in g w a s n o t d o n e w h e n g o in g t o a s u b p ro g ra m o f a n in t e rru p t Du e t o s im p licit y a n d fre q u e n t u s a g e , t h e s e p a rt s o f t h e p ro g ra m ca n b e m a d e a s m a cro s . Th e co n ce p t o f a Ma cro is e xp la in e d in "Pro g ra m a s s e m b ly la n g u a g e ". In t h e fo llo win g e xa m p le , co n t e n t s o f W a n d S TATUS re g is t e rs a re s t o re d in W_ TEMP a n d S TATUS _ TEMP va ria b le s p rio r t o in t e rru p t ro u t in e . At t h e b e g in n in g o f PUS H ro u t in e we n e e d t o ch e ck p re s e n t ly s e le ct e d b a n k b e ca u s e W_ TEMP a n d S TATUS _ TEMP a re fo u n d in b a n k 0 . Fo r e xch a n g e o f d a t a b e t we e n t h e s e re g is t e rs , S WAPF in s t ru ct io n is u s e d in s t e a d o f MOVF b e ca u s e it d o e s n o t a ffe ct t h e s t a t u s o f S TATUS re g is t e r b it s . Exa m p le is a p ro g ra m a s s e m b le r fo r fo llo win g s t e p s : 1. 2. 3. 4. 5. 6.
Te s t in g t h e cu rre n t b a n k S t o rin g W re g is t e r re g a rd le s s o f t h e cu rre n t b a n k S t o rin g S TATUS re g is t e r in b a n k 0 . Exe cu t in g in t e rru p t ro u t in e fo r in t e rru p t p ro ce s s in g ( IS R) Re s t o re s S TATUS re g is t e r Re s t o re s W re g is t e r
If t h e re a re s o m e m o re va ria b le s o r re g is t e rs t h a t n e e d t o b e s t o re d , t h e n t h e y n e e d t o b e ke p t a ft e r s t o rin g S TATUS re g is t e r ( s t e p 3 ) , a n d b ro u g h t b a ck b e fo re S TATUS re g is t e r is re s t o re d ( s t e p 5 ) .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.htm (4 of 7) [4/2/2003 16:17:55]
Chapter 2 - Microcontroller PIC16F84
Th e s a m e e xa m p le ca n b e re a lize d b y u s in g m a cro s , t h u s g e t t in g a m o re le g ib le p ro g ra m . Ma cro s t h a t a re a lre a d y d e fin e d ca n b e u s e d fo r writ in g n e w m a cro s . Ma cro s BANK1 a n d BANK0 wh ich a re e xp la in e d in "Me m o ry o rg a n iza t io n " ch a p t e r a re u s e d wit h m a cro s 'p u s h ' a n d 'p o p '.
Ext e rn a l in t e rru p t o n RB0 / INT p in o f m icro co n t ro lle r
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.htm (5 of 7) [4/2/2003 16:17:55]
Chapter 2 - Microcontroller PIC16F84
Ext e rn a l in t e rru p t o n RB0 / INT p in is t rig g e re d b y ris in g s ig n a l e d g e ( if b it INTEDG= 1 in OPTION< 6 > re g is t e r) , o r fa llin g e d g e ( if INTEDG= 0 ) . Wh e n co rre ct s ig n a l a p p e a rs o n INT p in , INTF b it is s e t in INTCON re g is t e r. INTF b it ( INTCON< 1 > ) m u s t b e re s e t in in t e rru p t ro u t in e , s o t h a t in t e rru p t wo u ld n 't o ccu r a g a in wh ile g o in g b a ck t o t h e m a in p ro g ra m . Th is is a n im p o rt a n t p a rt o f t h e p ro g ra m wh ich p ro g ra m m e r m u s t n o t fo rg e t , o r p ro g ra m will co n s t a n t ly g o in t o in t e rru p t ro u t in e . In t e rru p t ca n b e t u rn e d o ff b y re s e t t in g INTE co n t ro l b it ( INTCON< 4 > ) .
In t e rru p t d u rin g a TMR0 co u n t e r o ve rflo w Ove rflo w o f TMR0 co u n t e r ( fro m FFh t o 0 0 h ) will s e t T0 IF ( INTCON< 2 > ) b it . Th is is ve ry im p o rt a n t in t e rru p t b e ca u s e m a n y re a l p ro b le m s ca n b e s o lve d u s in g t h is in t e rru p t . On e o f t h e e xa m p le s is t im e m e a s u re m e n t . If we kn o w h o w m u ch t im e co u n t e r n e e d s in o rd e r t o co m p le t e o n e cycle fro m 0 0 h t o FFh , t h e n a n u m b e r o f in t e rru p t s m u lt ip lie d b y t h a t a m o u n t o f t im e will yie ld t h e t o t a l o f e la p s e d t im e . In in t e rru p t ro u t in e s o m e va ria b le wo u ld b e in cre m e n t e d in RAM m e m o ry, va lu e o f t h a t va ria b le m u lt ip lie d b y t h e a m o u n t o f t im e t h e co u n t e r n e e d s t o co u n t t h ro u g h a wh o le cycle , wo u ld yie ld t o t a l e la p s e d t im e . In t e rru p t ca n b e t u rn e d o n / o ff b y s e t t in g / re s e t t in g T0 IE ( INTCON< 5 > ) b it .
In t e rru p t d u rin g a ch a n g e o n p in s 4 , 5 , 6 a n d 7 o f p o rt B Ch a n g e o f in p u t s ig n a l o n PORTB < 7 : 4 > s e t s RBIF ( INTCON< 0 > ) b it . Fo u r p in s RB7 , RB6 , RB5 a n d RB4 o f p o rt B, ca n t rig g e r a n in t e rru p t wh ich o ccu rs wh e n s t a t u s o n t h e m ch a n g e s fro m lo g ic o n e t o lo g ic ze ro , o r vice ve rs a . Fo r p in s t o b e s e n s it ive t o t h is ch a n g e , t h e y m u s t b e d e fin e d a s in p u t . If a n y o n e o f t h e m is d e fin e d a s o u t p u t , in t e rru p t will n o t b e g e n e ra t e d a t t h e ch a n g e o f s t a t u s . If t h e y a re d e fin e d a s in p u t , t h e ir cu rre n t s t a t e is co m p a re d t o t h e o ld va lu e wh ich wa s s t o re d a t t h e la s t re a d in g fro m p o rt B. In t e rru p t ca n b e t u rn e d o n / o ff b y s e t t in g / re s e t t in g RBIE b it in INTCON re g is t e r.
In t e rru p t u p o n fin is h in g writ e - s u b ro u t in e t o EEPROM Th is in t e rru p t is o f p ra ct ica l n a t u re o n ly. S in ce writ in g t o o n e EEPROM lo ca t io n t a ke s a b o u t 1 0 m s ( wh ich is a lo n g t im e in t h e n o t io n o f a m icro co n t ro lle r) , it d o e s n 't p a y o ff t o a m icro co n t ro lle r t o wa it fo r writ in g t o e n d . Th u s in t e rru p t m e ch a n is m is a d d e d wh ich a llo ws t h e m icro co n t ro lle r t o co n t in u e e xe cu t in g t h e m a in p ro g ra m , wh ile writ in g in EEPROM is b e in g d o n e in t h e b a ckg ro u n d . Wh e n writ in g is co m p le t e d , in t e rru p t in fo rm s t h e m icro co n t ro lle r t h a t writ in g h a s e n d e d . EEIF b it , t h ro u g h wh ich t h is in fo rm in g is d o n e , is fo u n d in EECON1 re g is t e r. Occu rre n ce o f a n in t e rru p t ca n b e d is a b le d b y re s e t t in g t h e EEIE b it in INTCON re g is t e r.
In t e rru p t in it ia liza t io n In o rd e r t o u s e a n in t e rru p t m e ch a n is m o f a m icro co n t ro lle r, s o m e p re p a ra t o ry t a s ks n e e d t o b e p e rfo rm e d . Th e s e p ro ce d u re s a re in s h o rt ca lle d "in it ia liza t io n ". By in it ia liza t io n we d e fin e t o wh a t in t e rru p t s t h e m icro co n t ro lle r will re s p o n d , a n d wh ich o n e s it will ig n o re . If we d o n o t s e t t h e b it t h a t a llo ws a ce rt a in in t e rru p t , p ro g ra m will n o t e xe cu t e a n in t e rru p t s u b p ro g ra m . Th ro u g h t h is we ca n o b t a in co n t ro l o ve r in t e rru p t o ccu rre n ce , wh ich is ve ry u s e fu l.
Th e a b o ve e xa m p le s h o ws in it ia liza t io n o f e xt e rn a l in t e rru p t o n RB0 p in o f a m icro co n t ro lle r. Wh e re we s e e o n e b e in g s e t , t h a t m e a n s t h a t in t e rru p t is e n a b le d . Occu rre n ce o f o t h e r in t e rru p t s is n o t a llo we d , a n d a ll in t e rru p t s t o g e t h e r a re d is a llo we d u n t il GIE b it is ke e p in g t o o n e . Th e fo llo win g e xa m p le s h o ws a t yp ica l wa y o f h a n d lin g in t e rru p t s . PIC1 6 F8 4 h a s o n ly o n e lo ca t io n wh e re t h e a d d re s s o f a n in t e rru p t s u b p ro g ra m is s t o re d . Th is m e a n s t h a t firs t we n e e d t o d e t e ct wh ich in t e rru p t is a t h a n d ( if m o re t h a n o n e in t e rru p t s o u rce is a va ila b le ) , a n d t h e n we ca n e xe cu t e t h a t p a rt o f a p ro g ra m wh ich re fe rs t o t h a t in t e rru p t .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.htm (6 of 7) [4/2/2003 16:17:55]
Chapter 2 - Microcontroller PIC16F84
Re t u rn fro m in t e rru p t ro u t in e ca n b e a cco m p lis h e d w it h in s t ru ct io n s RETURN, RETLW a n d RETFIE. It is re co m m e n d e d t h a t in s t ru ct io n RETFIE b e u s e d b e ca u s e t h a t in s t ru ct io n is t h e o n ly o n e w h ich a u t o m a t ica lly s e t s t h e GIE b it w h ich a llo w s n e w in t e rru p t s t o o ccu r.
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.htm (7 of 7) [4/2/2003 16:17:55]
Chapter 2 - Microcontroller PIC16F84
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
2 . 7 Fre e - ru n t im e r TMR0 Tim e rs a re u s u a lly m o s t co m p lica t e d p a rt s o f a m icro co n t ro lle r, s o it is n e ce s s a ry t o s e t a s id e m o re t im e fo r t h e ir e xp la in in g . Wit h t h e ir a p p lica t io n it is p o s s ib le t o cre a t e re la t io n s b e t we e n a re a l d im e n s io n s u ch a s "t im e " a n d a va ria b le wh ich re p re s e n t s s t a t u s o f a t im e r wit h in a m icro co n t ro lle r. Ph ys ica lly, t im e r is a re g is t e r wh o s e va lu e is co n t in u a lly in cre a s in g t o 2 5 5 , a n d t h e n it s t a rt s a ll o ve r a g a in : 0 , 1 , 2 , 3 , 4 . . . 2 5 5 . . . . 0 , 1 , 2 , 3 . . . . . . e t c.
Th is in cre m e n t in g is d o n e in t h e b a ckg ro u n d o f e ve ryt h in g a m icro co n t ro lle r d o e s . It is u p t o p ro g ra m m e r t o "t h in k u p a wa y" h o w h e will t a ke a d va n t a g e o f t h is ch a ra ct e ris t ic fo r h is n e e d s . On e o f t h e wa ys is in cre a s in g s o m e va ria b le o n e a ch t im e r o ve rflo w. If we kn o w h o w m u ch t im e a t im e r n e e d s t o m a ke o n e co m p le t e ro u n d , t h e n m u lt ip lyin g t h e va lu e o f a va ria b le b y t h a t t im e will yie ld t h e t o t a l a m o u n t o f e la p s e d t im e . PIC1 6 F8 4 h a s a n 8 - b it t im e r. Nu m b e r o f b it s d e t e rm in e s wh a t va lu e t im e r co u n t s t o b e fo re s t a rt in g t o co u n t fro m ze ro a g a in . In t h e ca s e o f a n 8 - b it t im e r, t h a t n u m b e r is 2 5 6 . A s im p lifie d s ch e m e o f re la t io n b e t we e n a t im e r a n d a p re s ca le r is re p re s e n t e d o n t h e p re vio u s d ia g ra m . Pre s ca le r is a n a m e fo r t h e p a rt o f a m icro co n t ro lle r wh ich d ivid e s o s cilla t o r clo ck b e fo re it will re a ch lo g ic t h a t in cre a s e s t im e r s t a t u s . Nu m b e r wh ich d ivid e s a clo ck is d e fin e d t h ro u g h firs t t h re e b it s in OPTION re g is t e r. Th e h ig h e s t d ivis o r is 2 5 6 . Th is a ct u a lly m e a n s t h a t o n ly a t e ve ry 2 5 6 t h clo ck, t im e r va lu e wo u ld in cre a s e
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_08Poglavlje.htm (1 of 5) [4/2/2003 16:17:58]
Chapter 2 - Microcontroller PIC16F84
b y o n e . Th is p ro vid e s u s wit h t h e a b ilit y t o m e a s u re lo n g e r t im e r p e rio d s .
Aft e r e a ch co u n t u p t o 2 5 5 , t im e r re s e t s it s va lu e t o ze ro a n d s t a rt s wit h a n e w cycle o f co u n t in g t o 2 5 5 . Du rin g e a ch t ra n s it io n fro m 2 5 5 t o ze ro , T0 IF b it in INTCOM re g is t e r is s e t . If in t e rru p t s a re a llo we d t o o ccu r, t h is ca n b e t a ke n a d va n t a g e o f in g e n e ra t in g in t e rru p t s a n d in p ro ce s s in g in t e rru p t ro u t in e . It is u p t o p ro g ra m m e r t o re s e t T0 IF b it in in t e rru p t ro u t in e , s o t h a t n e w in t e rru p t , o r n e w o ve rflo w co u ld b e d e t e ct e d . Be s id e t h e in t e rn a l o s cilla t o r clo ck, t im e r s t a t u s ca n a ls o b e in cre a s e d b y t h e e xt e rn a l clo ck o n RA4 / TOCKI p in . Ch o o s in g o n e o f t h e s e t wo o p t io n s is d o n e in OPTION re g is t e r t h ro u g h T0 CS b it . If t h is o p t io n o f e xt e rn a l clo ck wa s s e le ct e d , it wo u ld b e p o s s ib le t o d e fin e t h e e d g e o f a s ig n a l ( ris in g o r fa llin g ) , o n wh ich t im e r wo u ld in cre a s e it s va lu e .
In p ra ct ice , o n e o f t h e t yp ica l e xa m p le t h a t is s o lve d via e xt e rn a l clo ck a n d a t im e r is co u n t in g fu ll t u rn s o f a n a xis o f s o m e p ro d u ct io n m a ch in e , like t ra n s fo rm e r win d e r fo r in s t a n ce . Le t 's win d fo u r m e t a l s cre ws o n t h e a xis o f a win d e r. Th e s e fo u r s cre ws will re p re s e n t m e t a l co n ve xit y. Le t 's p la ce
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_08Poglavlje.htm (2 of 5) [4/2/2003 16:17:58]
Chapter 2 - Microcontroller PIC16F84
n o w t h e in d u ct ive s e n s o r a t a d is t a n ce o f 5 m m fro m t h e h e a d o f a s cre w. In d u ct ive s e n s o r will g e n e ra t e t h e fa llin g s ig n a l e ve ry t im e t h e h e a d o f t h e s cre w is p a ra lle l wit h s e n s o r h e a d . Ea ch s ig n a l will re p re s e n t o n e fo u rt h o f a fu ll t u rn , a n d t h e s u m o f a ll fu ll t u rn s will b e fo u n d in TMR0 t im e r. Pro g ra m ca n e a s ily re a d t h is d a t a fro m t h e t im e r t h ro u g h a d a t a b u s . Th e fo llo win g e xa m p le illu s t ra t e s h o w t o in it ia lize t im e r t o s ig n a l fa llin g e d g e s fro m e xt e rn a l clo ck s o u rce wit h a p re s ca le r 1 : 4 . Tim e r wo rks in "p o lig " m o d e .
Th e s a m e e xa m p le ca n b e re a lize d t h ro u g h a n in t e rru p t in t h e fo llo win g wa y:
Pre s ca le r ca n b e a s s ig n e d e it h e r t im e r TMR0 o r a wa t ch d o g . Wa t ch d o g is a m e ch a n is m wh ich m icro co n t ro lle r u s e s t o d e fe n d it s e lf a g a in s t p ro g ra m s g e t t in g s t u ck. As wit h a n y o t h e r e le ct rica l circu it , s o wit h a m icro co n t ro lle r t o o ca n o ccu r fa ilu re , o r s o m e wo rk im p a irm e n t . Un fo rt u n a t e ly, m icro co n t ro lle r a ls o h a s p ro g ra m wh e re p ro b le m s ca n o ccu r a s we ll. Wh e n t h is h a p p e n s , m icro co n t ro lle r will s t o p wo rkin g a n d will re m a in in t h a t s t a t e u n t il s o m e o n e re s e t s it . Be ca u s e o f t h is , wa t ch d o g m e ch a n is m h a s b e e n in t ro d u ce d . Aft e r a ce rt a in p e rio d o f t im e , wa t ch d o g re s e t s t h e m icro co n t ro lle r ( m icro co n t ro lle r in fa ct re s e t s it s e lf) . Wa t ch d o g wo rks o n a s im p le p rin cip le : if t im e r o ve rflo w o ccu rs , m icro co n t ro lle r is re s e t , a n d it s t a rt s e xe cu t in g a p ro g ra m a ll o ve r a g a in . In t h is wa y, re s e t will o ccu r in ca s e o f b o t h co rre ct a n d in co rre ct fu n ct io n in g . Ne xt s t e p is p re ve n t in g re s e t in ca s e o f co rre ct fu n ct io n in g , wh ich is d o n e b y writ in g ze ro in WDT re g is t e r ( in s t ru ct io n CLRWDT) e ve ry t im e it n e a rs it s o ve rflo w. Th u s p ro g ra m will p re ve n t a re s e t a s lo n g a s it 's e xe cu t in g co rre ct ly. On ce it g e t s s t u ck, ze ro will n o t b e writ t e n , o ve rflo w o f WDT t im e r a n d a re s e t will o ccu r wh ich will b rin g t h e m icro co n t ro lle r b a ck t o co rre ct fu n ct io n in g a g a in .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_08Poglavlje.htm (3 of 5) [4/2/2003 16:17:58]
Chapter 2 - Microcontroller PIC16F84
Pre s ca le r is a cco rd e d t o t im e r TMR0 , o r t o wa t ch d o g t im e r t ro u g h PS A b it in OPTION re g is t e r. By cle a rin g PS A b it , p re s ca le r will b e a cco rd e d t o t im e r TMR0 . Wh e n p re s ca le r is a cco rd e d t o t im e r TMR0 , a ll in s t ru ct io n s o f writ in g t o TMR0 re g is t e r ( CLRF TMR0 , MOVWF TMR0 , BS F TMR0 , . . . ) will cle a r p re s ca le r. Wh e n p re s ca le r is a s s ig n e d t o a wa t ch d o g t im e r, o n ly CLRWDT in s t ru ct io n will cle a r a p re s ca le r a n d wa t ch d o g t im e r a t t h e s a m e t im e . Pre s ca le r ch a n g e is co m p le t e ly u n d e r p ro g ra m m e r's co n t ro l, a n d ca n b e ch a n g e d wh ile p ro g ra m is ru n n in g .
Th e re is o n ly o n e p re s ca le r a n d o n e t im e r. De p e n d in g o n t h e n e e d s , t h e y a re a s s ig n e d e it h e r t o t im e r TMR0 o r t o a w a t ch d o g .
OPTION Co n t ro l Re g is t e r
Bit 0 : 2 P S 0 , P S 1 , P S 2 ( Pre s ca le r Ra t e S e le ct b it ) Th e s u b je ct o f a p re s ca le r, a n d h o w t h e s e b it s a ffe ct t h e wo rk o f a m icro co n t ro lle r will b e co ve re d in s e ct io n o n TMR0 .
b it 3 P S A ( Pre s ca le r As s ig n m e n t b it ) Bit wh ich a s s ig n s p re s ca le r b e t we e n TMR0 a n d wa t ch d o g t im e r. 1 = p re s ca le r is a s s ig n e d t o wa t ch d o g t im e r. 0 = p re s ca le r is a s s ig n e d t o fre e t im e r TMR0 b it 4 T0 S E ( TMR0 S o u rce Ed g e S e le ct b it ) If t rig g e r TMR0 wa s e n a b le d wit h im p u ls e s fro m a RA4 / T0 CKI p in , t h is b it wo u ld d e t e rm in e wh e t h e r it wo u ld b e o n t h e ris in g o r fa llin g e d g e o f a s ig n a l. 1 = fa llin g e d g e 0 = ris in g e d g e b it 5 T0 CS ( TMR0 Clo ck S o u rce S e le ct b it ) Th is p in e n a b le s a fre e - ru n t im e r t o in cre m e n t it s va lu e e it h e r fro m a n in t e rn a l o s cilla t o r, i. e . e ve ry 1 / 4 o f o s cilla t o r clo ck, o r via e xt e rn a l im p u ls e s o n RA4 / T0 CKI p in . 1 = e xt e rn a l im p u ls e s 0 = 1 / 4 in t e rn a l clo ck b it 6 IN TED G ( In t e rru p t Ed g e S e le ct b it ) If o ccu rre n ce o f in t e rru p t s wa s e n a b le d , t h is b it wo u ld d e t e rm in e a t wh a t e d g e in t e rru p t o n RB0 / INT p in wo u ld o ccu r. 1 = ris in g e d g e 0 = fa llin g e d g e
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_08Poglavlje.htm (4 of 5) [4/2/2003 16:17:58]
Chapter 2 - Microcontroller PIC16F84
b it 7 RBP U ( PORTB Pu ll- u p En a b le b it ) Th is b it t u rn s in t e rn a l p u ll- u p re s is t o rs o n p o rt B o n o r o ff. 1 = 'p u ll- u p ' re s is t o rs t u rn e d o n 0 = 'p u ll- u p ' re s is t o rs t u rn e d o ff
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_08Poglavlje.htm (5 of 5) [4/2/2003 16:17:58]
Chapter 2 - Microcontroller PIC16F84
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
2 . 8 EEP ROM D a t a m e m o ry PIC1 6 F8 4 h a s 6 4 b yt e s o f EEPROM m e m o ry lo ca t io n s o n a d d re s s e s fro m 0 0 h t o 6 3 h t h o s e ca n b e writ t e n t o o r re a d fro m . Th e m o s t im p o rt a n t ch a ra ct e ris t ic o f t h is m e m o ry is t h a t it d o e s n o t lo o s e it s co n t e n t s d u rin g p o we r s u p p ly t u rn e d o ff. Th a t p ra ct ica lly m e a n s t h a t wh a t wa s writ t e n t o it will b e re m a in in g e ve n if m icro co n t ro lle r is t u rn e d o ff. Da t a ca n b e re t a in e d in EEPROM wit h o u t p o we r s u p p ly fo r u p t o 4 0 ye a rs ( a s m a n u fa ct u re r o f PIC1 6 F8 4 m icro co n t ro lle r s t a t e s ) , a n d u p t o 1 0 0 0 0 cycle s o f writ in g ca n b e e xe cu t e d . In p ra ct ice , EEPROM m e m o ry is u s e d fo r s t o rin g im p o rt a n t d a t a o r s o m e p ro ce s s p a ra m e t e rs . On e s u ch p a ra m e t e r is a g ive n t e m p e ra t u re , a s s ig n e d wh e n s e t t in g u p a t e m p e ra t u re re g u la t o r t o s o m e p ro ce s s . If t h a t d a t a wa s n 't re t a in e d , it wo u ld b e n e ce s s a ry t o a d ju s t a g ive n t e m p e ra t u re a ft e r e a ch lo s s o f s u p p ly. S in ce t h is is ve ry im p ra ct ica l ( a n d e ve n d a n g e ro u s ) , m a n u fa ct u re rs o f m icro co n t ro lle rs h a ve b e g a n in s t a llin g o n e s m a lle r t yp e o f EEPROM m e m o ry. EEPROM m e m o ry is p la ce d in a s p e cia l m e m o ry s p a ce a n d ca n b e a cce s s e d t h ro u g h s p e cia l re g is t e rs . Th e s e re g is t e rs a re : • EED ATA a t a d d re s s 0 8 h , wh ich h o ld s re a d d a t a o r t h a t t o b e writ t e n . • EEAD R a t a d d re s s 0 9 h , wh ich co n t a in s a n a d d re s s o f EEPROM lo ca t io n b e in g a cce s s e d . • EECON 1 a t a d d re s s 8 8 h , wh ich co n t a in s co n t ro l b it s . • EECON 2 a t a d d re s s 8 9 h . Th is re g is t e r d o e s n o t e xis t p h ys ica lly a n d s e rve s t o p ro t e ct EEPROM fro m a ccid e n t a l writ in g . EECON1 re g is t e r a t a d d re s s 8 8 h is a co n t ro l re g is t e r wit h five im p le m e n t e d b it s . Bit s 5 , 6 a n d 7 a re n o t u s e d , a n d b y re a d in g a lwa ys a re ze ro . In t e rp re t a t io n o f EECON1 re g is t e r b it s fo llo ws .
EECON1 Re g is t e r
b it 0 RD ( Re a d Co n t ro l b it ) S e t t in g t h is b it in it ia lize s t ra n s fe r o f d a t a fro m a d d re s s d e fin e d in EEADR t o EEDATA re g is t e r. S in ce t im e is n o t a s e s s e n t ia l in re a d in g d a t a a s in writ in g , d a t a fro m EEDATA ca n a lre a d y b e u s e d fu rt h e r in t h e n e xt in s t ru ct io n . 1 = in it ia lize s re a d in g 0 = d o e s n o t in it ia lize re a d in g b it 1 W R ( Writ e Co n t ro l b it ) S e t t in g o f t h is b it in it ia lize s writ in g d a t a fro m EEDATA re g is t e r t o t h e a d d re s s s p e cifie d t ro u g h EEADR re g is t e r. 1 = in it ia lize s writ in g 0 = d o e s n o t in it ia lize writ in g b it 2 W REN ( EEPROM Writ e En a b le b it ) En a b le s writ in g t o EEPROM If t h is b it wa s n o t s e t , m icro co n t ro lle r wo u ld n o t a llo w writ in g t o EEPROM. 1 = writ in g a llo we d
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_09Poglavlje.htm (1 of 3) [4/2/2003 16:18:00]
Chapter 2 - Microcontroller PIC16F84
0 = writ in g d is a llo we d b it 3 W RERR ( Writ e EEPROM Erro r Fla g ) Erro r d u rin g writ in g t o EEPROM Th is b it wa s s e t o n ly in ca s e s wh e n writ in g t o EEPROM h a d b e e n in t e rru p t e d b y a re s e t s ig n a l o r b y ru n n in g o u t o f t im e in wa t ch d o g t im e r ( if it 's a ct iva t e d ) . 1 = e rro r o ccu re d 0 = e rro r d id n o t o ccu r b it 4 EEI F ( EEPROM Writ e Op e ra t io n In t e rru p t Fla g b it ) Bit u s e d t o in fo rm t h a t writ in g d a t a t o EEPROM h a s e n d e d . Wh e n writ in g h a s t e rm in a t e d , t h is b it wo u ld b e s e t a u t o m a t ica lly. Pro g ra m m e r m u s t cle a r EEIF b it in h is p ro g ra m in o rd e r t o d e t e ct n e w t e rm in a t io n o f writ in g . 1 = writ in g t e rm in a t e d 0 = writ in g n o t t e rm in a t e d ye t , o r h a s n o t s t a rt e d
Re a d in g fro m EEPROM Me m o ry S e t t in g t h e RD b it in it ia lize s t ra n s fe r o f d a t a fro m a d d re s s fo u n d in EEADR re g is t e r t o EEDATA re g is t e r. As in re a d in g d a t a we d o n 't n e e d s o m u ch t im e a s in writ in g , d a t a t a ke n o ve r fro m EEDATA re g is t e r ca n a lre a d y b e u s e d fu rt h e r in t h e n e xt in s t ru ct io n . S a m p le o f t h e p a rt o f a p ro g ra m wh ich re a d s d a t a in EEPROM, co u ld lo o k s o m e t h in g like t h e fo llo win g :
Aft e r t h e la s t p ro g ra m in s t ru ct io n , co n t e n t s fro m a n EEPROM a d d re s s ze ro ca n b e fo u n d in wo rkin g re g is t e r w.
Writ in g t o EEPROM Me m o ry In o rd e r t o writ e d a t a t o EEPROM lo ca t io n , p ro g ra m m e r m u s t firs t writ e a d d re s s t o EEADR re g is t e r a n d d a t a t o EEDATA re g is t e r. On ly t h e n is it u s e fu l t o s e t WR b it wh ich s e t s t h e wh o le a ct io n in m o t io n . WR b it will b e re s e t , a n d EEIF b it s e t fo llo win g a writ in g wh a t m a y b e u s e d in p ro ce s s in g in t e rru p t s . Va lu e s 5 5 h a n d AAh a re t h e firs t a n d t h e s e co n d ke y wh o s e d is a llo w fo r a ccid e n t a l writ in g t o EEPROM t o o ccu r. Th e s e t wo va lu e s a re writ t e n t o EECON2 wh ich s e rve s o n ly t h a t p u rp o s e , t o re ce ive t h e s e t wo va lu e s a n d t h u s p re ve n t a n y a ccid e n t a l writ in g t o EEPROM m e m o ry. Pro g ra m lin e s m a rke d a s 1 , 2 , 3 , a n d 4 m u s t b e e xe cu t e d in t h a t o rd e r in e ve n t im e in t e rva ls . Th e re fo re , it is ve ry im p o rt a n t t o t u rn o ff in t e rru p t s wh ich co u ld ch a n g e t h e t im in g n e e d e d fo r e xe cu t in g in s t ru ct io n s . Aft e r writ in g , in t e rru p t s ca n b e e n a b le d a g a in . Exa m p le o f t h e p a rt o f a p ro g ra m wh ich writ e s d a t a 0 xEE t o firs t lo ca t io n in EEPROM m e m o ry co u ld lo o k s o m e t h in g like t h e fo llo win g :
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_09Poglavlje.htm (2 of 3) [4/2/2003 16:18:00]
Chapter 2 - Microcontroller PIC16F84
It is re co m m e n d e d t h a t W REN b e t u rn e d o ff t h e w h o le t im e e x ce p t w h e n w rit in g d a t a t o EEPROM, s o t h a t p o s s ib ilit y o f a ccid e n t a l w rit in g w o u ld b e m in im a l. All w rit in g t o EEPROM w ill a u t o m a t ica lly cle a r a lo ca t io n p rio r t o w rit in g a n e w !
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_09Poglavlje.htm (3 of 3) [4/2/2003 16:18:00]
Chapter 3 - Instruction Set
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
CHAP TER 3 I n s t ru c t io n S e t In t ro d u ct io n In s t ru ct io n s e t in PIC1 6 Cxx m icro co n t ro lle r fa m ily Da t a Tra n s fe r Arit h m e t ic a n d lo g ic Bit o p e ra t io n s Dire ct in g t h e p ro g ra m flo w In s t ru ct io n e xe cu t io n p e rio d Wo rd lis t
I n t ro d u c t io n We h a ve a lre a d y m e n t io n e d t h a t m icro co n t ro lle r is n o t like a n y o t h e r in t e g ra t e d circu it . Wh e n t h e y co m e o u t o f p ro d u ct io n m o s t in t e g ra t e d circu it s a re re a d y t o b e b u ilt in t o d e vice s wh ich is n o t t h e ca s e wit h m icro co n t ro lle rs . In o rd e r t o "m a ke " m icro co n t ro lle r p e rfo rm a t a s k, we h a ve t o t e ll it e xa ct ly wh a t t o d o , o r in o t h e r wo rd s we m u s t writ e t h e p ro g ra m m icro co n t ro lle r will e xe cu t e . We will d e s crib e in t h is ch a p t e r in s t ru ct io n s wh ich m a ke u p t h e a s s e m b le r, o r lo we r- le ve l p ro g ra m la n g u a g e fo r PIC m icro co n t ro lle rs .
I n s t ru c t io n S e t in P I C1 6 Cx x Mic ro c o n t ro lle r Fa m ily Co m p le t e s e t wh ich in clu d e s 3 5 in s t ru ct io n s is g ive n in t h e fo llo win g t a b le . A re a s o n fo r s u ch a s m a ll n u m b e r o f in s t ru ct io n s lie s p rim a rily in t h e fa ct t h a t we a re t a lkin g a b o u t a RIS C m icro co n t ro lle r wh o s e in s t ru ct io n s a re we ll o p t im ize d co n s id e rin g t h e s p e e d o f wo rk, a rch it e ct u ra l s im p licit y a n d co d e co m p a ct n e s s . Th e o n ly d ra wb a ck is t h a t p ro g ra m m e r is e xp e ct e d t o m a s t e r "u n co m fo rt a b le " t e ch n iq u e o f u s in g a re d u ce d t s e t o f 3 5 in s t ru ct io n s .
D a t a t ra n s fe r Tra n s fe r o f d a t a in a m icro co n t ro lle r is d o n e b e t we e n wo rk ( W) re g is t e r a n d a n 'f' re g is t e r t h a t re p re s e n t s a n y lo ca t io n in in t e rn a l RAM ( re g a rd le s s wh e t h e r t h o s e a re s p e cia l o r g e n e ra l p u rp o s e re g is t e rs ) . Firs t t h re e in s t ru ct io n s ( lo o k a t t h e fo llo win g t a b le ) p ro vid e fo r a co n s t a n t b e in g writ t e n in W re g is t e r ( MOVLW is s h o rt fo r MOVe Lit e ra l t o W) , a n d fo r d a t a t o b e co p ie d fro m W re g is t e r o n t o RAM a n d d a t a fro m RAM t o b e co p ie d o n t o W re g is t e r ( o r o n t h e s a m e RAM lo ca t io n , a t wh ich p o in t o n ly t h e s t a t u s o f Z fla g ch a n g e s ) . In s t ru ct io n CLRF writ e s co n s t a n t 0 in 'f ' re g is t e r, a n d CLRW writ e s co n s t a n t 0 in re g is t e r W. S WAPF in s t ru ct io n e xch a n g e s p la ce s o f t h e 4 - b it n ib b le s fie ld in s id e a re g is t e r.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/3_Poglavlje.htm (1 of 4) [4/2/2003 16:18:03]
Chapter 3 - Instruction Set
Arit h m e t ic a n d lo g ic Of a ll a rit h m e t ic o p e ra t io n s , PIC like m o s t m icro co n t ro lle rs s u p p o rt s o n ly s u b t ra ct io n a n d a d d it io n . Fla g s C, DC a n d Z a re s e t d e p e n d in g o n a re s u lt o f a d d it io n o r s u b t ra ct io n , b u t wit h o n e e xce p t io n : s in ce s u b t ra ct io n is p e rfo rm e d like a d d it io n o f a n e g a t ive va lu e , C fla g is in ve rs e fo llo win g a s u b t ra ct io n . In o t h e r wo rd s , it is s e t if o p e ra t io n is p o s s ib le , a n d re s e t if la rg e r n u m b e r wa s s u b t ra ct e d fro m a s m a lle r o n e . Lo g ic u n it o f PIC h a s ca p a b ilit y o f p e rfo rm in g o p e ra t io n s AND, OR, EX- OR, co m p le m e n t in g ( COMF) a n d ro t a t io n ( RLF a n d RRF) . In s t ru ct io n s wh ich ro t a t e t h e re g is t e r co n t e n t s m o ve b it s in s id e a re g is t e r t h ro u g h fla g C b y o n e s p a ce t o t h e le ft ( t o wa rd b it 7 ) , o r t o t h e rig h t ( t o wa rd b it 0 ) . Bit wh ich "co m e s o u t " o f a re g is t e r is writ t e n in fla g C, a n d va lu e o f C fla g is writ t e n in a b it o n t h e "o p p o s it e s id e " o f t h e re g is t e r.
Bit o p e ra t io n s In s t ru ct io n s BCF a n d BS F d o s e t t in g o r cle a n in g o f o n e b it a n ywh e re in t h e m e m o ry. Eve n t h o u g h t h is s e e m s like a s im p le o p e ra t io n , it is e xe cu t e d s o t h a t CPU firs t re a d s t h e wh o le b yt e , ch a n g e s o n e b it in it a n d t h e n writ e s in t h e e n t ire b yt e a t t h e s a m e p la ce .
D ire c t in g a p ro g ra m flo w In s t ru ct io n s GOTO, CALL a n d RETURN a re e xe cu t e d t h e s a m e wa y a s o n a ll o t h e r m icro co n t ro lle rs , o n ly s t a ck is in d e p e n d e n t o f in t e rn a l RAM a n d lim it e d t o e ig h t le ve ls . 'RETLW k' in s t ru ct io n is id e n t ica l wit h RETURN in s t ru ct io n , e xce p t t h a t b e fo re co m in g b a ck fro m a s u b p ro g ra m a co n s t a n t d e fin e d b y in s t ru ct io n o p e ra n d is writ t e n in W re g is t e r. Th is in s t ru ct io n e n a b le s u s t o d e s ig n e a s ily t h e Lo o k- u p t a b le s ( lis t s ) . Mo s t ly we u s e t h e m b y d e t e rm in in g d a t a p o s it io n o n o u r t a b le a d d in g it t o t h e a d d re s s a t wh ich t h e t a b le b e g in s , a n d t h e n we re a d d a t a fro m t h a t lo ca t io n ( wh ich is u s u a lly fo u n d in p ro g ra m m e m o ry) . Ta b le ca n b e fo rm e d a s a s u b p ro g ra m wh ich co n s is t s o f a s e rie s o f 'RETLW k' in s t ru ct io n s , wh e re 'k' co n s t a n t s a re m e m b e rs o f t h e t a b le .
We writ e t h e p o s it io n o f a m e m b e r o f o u r t a b le in W re g is t e r, a n d u s in g CALL in s t ru ct io n we ca ll a s u b p ro g ra m wh ich cre a t e s t h e t a b le . Firs t s u b p ro g ra m lin e ADDWF PCL, f a d d s t h e p o s it io n o f a W re g is t e r m e m b e r t o t h e s t a rt in g a d d re s s o f o u r t a b le , fo u n d in PCL re g is t e r, a n d s o we g e t t h e re a l d a t a a d d re s s in p ro g ra m m e m o ry. Wh e n re t u rn in g fro m a s u b p ro g ra m we will h a ve in W re g is t e r t h e co n t e n t s o f a n a d d re s s e d t a b le m e m b e r. In a p re vio u s e xa m p le , co n s t a n t 'k2 ' will b e in W re g is t e r fo llo win g a re t u rn fro m a s u b p ro g ra m . RETFIE ( RETu rn Fro m In t e rru p t - In t e rru p t En a b le ) is a re t u rn fro m in t e rru p t ro u t in e a n d d iffe rs fro m a RETURN o n ly in t h a t it a u t o m a t ica lly s e t s GIE ( Glo b a l In t e rru p t En a b le ) b it . Up o n a n in t e rru p t , t h is b it is a u t o m a t ica lly cle a re d . As in t e rru p t b e g in s , o n ly t h e va lu e o f p ro g ra m co u n t e r is p u t a t t h e t o p o f a s t a ck. No a u t o m a t ic s t o rin g o f re g is t e r va lu e s a n d s t a t u s is p ro vid e d . Co n d it io n a l ju m p s a re s yn t h e s ize d in t o t wo in s t ru ct io n s : BTFS C a n d BTFS S . De p e n d in g o n a b it s t a t u s in 'f' re g is t e r t h a t is b e in g t e s t e d , in s t ru ct io n s s kip o r d o n 't s kip o ve r t h e n e xt p ro g ra m
http://www.mikroelektronika.co.yu/english/product/books/PICbook/3_Poglavlje.htm (2 of 4) [4/2/2003 16:18:03]
Chapter 3 - Instruction Set
in s t ru ct io n .
I n s t ru c t io n Ex e c u t io n P e rio d All in s t ru ct io n s a re e xe cu t e d in o n e cycle e xce p t fo r co n d it io n a l b ra n ch in s t ru ct io n s if co n d it io n wa s t ru e , o r if t h e co n t e n t s o f p ro g ra m co u n t e r wa s ch a n g e d b y s o m e in s t ru ct io n . In t h a t ca s e , e xe cu t io n re q u ire s t wo in s t ru ct io n cycle s , a n d t h e s e co n d cycle is e xe cu t e d a s NOP ( No Op e ra t io n ) . Fo u r o s cilla t o r clo cks m a ke u p o n e in s t ru ct io n cycle . If we a re u s in g a n o s cilla t o r wit h 4 MHz fre q u e n cy, t h e n o rm a l t im e fo r e xe cu t in g a n in s t ru ct io n is 1 µ s , a n d in ca s e o f co n d it io n a l b ra n ch in g , e xe cu t io n p e rio d is 2 µ s .
W o rd lis t f W b d la b e l TOS []
a n y m e m o ry lo ca t io n in a m icro co n t ro lle r wo rk re g is t e r b it p o s it io n in 'f' re g is t e r d e s t in a t io n b it g ro u p o f e ig h t ch a ra ct e rs wh ich m a rks t h e b e g in n in g o f a p a rt o f t h e p ro g ra m t o p o f s t a ck o p t io n b it p o s it io n in s id e re g is t e r
http://www.mikroelektronika.co.yu/english/product/books/PICbook/3_Poglavlje.htm (3 of 4) [4/2/2003 16:18:03]
Chapter 3 - Instruction Set
* 1 If I/ O p o rt is s o u rce o p e ra n d , s t a t u s o n m icro co n t ro lle r p in s is re a d * 2 If t h is in s t ru ct io n is e xe cu t e d o n TMR re g is t e r a n d if d = 1 , p re s ca le r a s s ig n e d t o t h a t t im e r will a u t o m a t ica lly b e cle a re d * 3 If PC wa s m o d ifie d , o r t e s t re s u lt = 1 , in s t ru ct io n wa s e xe cu t e d in t wo cycle s .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/3_Poglavlje.htm (4 of 4) [4/2/2003 16:18:03]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
CHAP TER 4 As s e m b ly La n g u a g e P ro g ra m m in g In t ro d u ct io n An e xa m p le writ t in g p ro g ra m Co n t ro l d ire ct ive s ● ● ● ● ● ● ● ●
4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8
d e fin e in clu d e co n s t a n t va ria b le set equ o rg end
Co n d it io n a l in s t ru ct io n s ● ● ● ● ● ● ●
4 . 9 if 4 . 1 0 e ls e 4 . 1 1 e n d if 4 . 1 2 wh ile 4.13 e ndw 4 . 1 4 ifd e f 4 . 1 5 ifn d e f
Da t a d ire ct ive s ● ● ● ● ●
4.16 4.17 4.18 4.19 4.20
cb lo ck endc db de dt
Co n fig u ra t in g a d ire ct ive ● ●
4 . 2 1 _ CONFIG 4 . 2 2 Pro ce s s o r
As s e m b le r a rit h m e t ic o p e ra t o rs
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (1 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
File s cre a t e d a s a re s u lt o f p ro g ra m t ra n s la t io n Ma cro s
I n t ro d u c t io n Th e a b ilit y t o co m m u n ica t e is o f g re a t im p o rt a n ce in a n y fie ld . Ho we ve r, it is o n ly p o s s ib le if b o t h co m m u n ica t io n p a rt n e rs kn o w t h e s a m e la n g u a g e , i. e fo llo w t h e s a m e ru le s d u rin g co m m u n ica t io n . Us in g t h e s e p rin cip le s a s a s t a rt in g p o in t , we ca n a ls o d e fin e co m m u n ica t io n t h a t o ccu rs b e t we e n m icro co n t ro lle rs a n d m a n . La n g u a g e t h a t m icro co n t ro lle r a n d m a n u s e t o co m m u n ica t e is ca lle d "a s s e m b ly la n g u a g e ". Th e t it le it s e lf h a s n o d e e p e r m e a n in g , a n d is a n a lo g u e t o n a m e s o f o t h e r la n g u a g e s , e x. En g lis h o r Fre n ch . Mo re p re cis e ly, "a s s e m b ly la n g u a g e " is ju s t a p a s s in g s o lu t io n . Pro g ra m s writ t e n in a s s e m b ly la n g u a g e m u s t b e t ra n s la t e d in t o a "la n g u a g e o f ze ro s a n d o n e s " in o rd e r fo r a m icro co n t ro lle r t o u n d e rs t a n d it . "As s e m b ly la n g u a g e " a n d "a s s e m b le r" a re t wo d iffe re n t n o t io n s . Th e firs t re p re s e n t s a s e t o f ru le s u s e d in writ in g a p ro g ra m fo r a m icro co n t ro lle r, a n d t h e o t h e r is a p ro g ra m o n t h e p e rs o n a l co m p u t e r wh ich t ra n s la t e s a s s e m b ly la n g u a g e in t o a la n g u a g e o f ze ro s a n d o n e s . A p ro g ra m t h a t is t ra n s la t e d in t o "ze ro s " a n d "o n e s " is a ls o ca lle d "m a ch in e la n g u a g e ".
Th e p ro c e s s o f c o m m u n ic a t io n b e t w e e n a m a n a n d a m ic ro c o n t o lle r Ph ys ica lly, "P ro g ra m " re p re s e n t s a file o n t h e co m p u t e r d is c ( o r in t h e m e m o ry if it is re a d in a m icro co n t ro lle r) , a n d is writ t e n a cco rd in g t o t h e ru le s o f a s s e m b le r o r s o m e o t h e r la n g u a g e fo r m icro co n t ro lle r p ro g ra m m in g . Ma n ca n u n d e rs t a n d a s s e m b le r la n g u a g e a s it co n s is t s o f a lp h a b e t s ig n s a n d wo rd s . Wh e n writ in g a p ro g ra m , ce rt a in ru le s m u s t b e fo llo we d in o rd e r t o re a ch a d e s ire d e ffe ct . A Tra n s la t o r in t e rp re t s e a ch in s t ru ct io n writ t e n in a s s e m b ly la n g u a g e a s a s e rie s o f ze ro s a n d o n e s wh ich h a ve a m e a n in g fo r t h e in t e rn a l lo g ic o f t h e m icro co n t ro lle r. Le t s t a ke fo r in s t a n ce t h e in s t ru ct io n "RETURN" t h a t a m icro co n t ro lle r u s e s t o re t u rn fro m a s u b p ro g ra m . Wh e n t h e a s s e m b le r t ra n s la t e s it , we g e t a 1 4 - b it s e rie s o f ze ro s a n d o n e s wh ich t h e m icro co n t ro lle r kn o ws h o w t o in t e rp re t . Ex a m p le : RETURN 0 0 0 0 0 0 0 0 0 0 1 0 0 0 S im ila r t o t h e a b o ve in s t a n ce , e a ch a s s e m b le r in s t ru ct io n is in t e rp re t e d a s co rre s p o n d in g t o a s e rie s o f ze ro s a n d o n e s . Th e p la ce wh e re t h is t ra n s la t io n o f a s s e m b ly la n g u a g e is fo u n d , is ca lle d a n "e xe cu t io n " file . We will o ft e n m e e t t h e n a m e "HEX" file . Th is n a m e co m e s fro m a h e xa d e cim a l re p re s e n t a t io n o f t h a t file , a s we ll a s fro m t h e s u ffix "h e x" in t h e t it le , e x. "t e s t . h e x". On ce it is g e n e ra t e d , t h e e xe cu t io n file is re a d in a m icro co n t ro lle r t h ro u g h a p ro g ra m m e r. An As s e m b ly La n g u a g e p ro g ra m is writ t e n in a p ro g ra m fo r t e xt p ro ce s s in g ( e d it o r) a n d is
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (2 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
ca p a b le o f p ro d u cin g a n AS CII file o n t h e co m p u t e r d is c o r in s p e cia lize d s u rro u n d in g s s u ch a s MPLAB - t o b e e xp la in e d in t h e n e xt ch a p t e r.
As s e m b ly la n g u a g e Ba s ic e le m e n t s o f a s s e m b ly la n g u a g e a re : ● ● ● ● ●
La b e ls In s t ru ct io n s Op e ra n d s Dire ct ive s Co m m e n t s
La b e ls A La b e l is a t e xt u a l d e s ig n a t io n ( g e n e ra lly a n e a s y- t o - re a d wo rd ) fo r a lin e in a p ro g ra m , o r s e ct io n o f a p ro g ra m wh e re t h e m icro ca n ju m p t o - o r e ve n t h e b e g in n in g o f s e t o f lin e s o f a p ro g ra m . It ca n a ls o b e u s e d t o e xe cu t e p ro g ra m b ra n ch in g ( s u ch a s Go t o . . . . . . . ) a n d t h e p ro g ra m ca n e ve n h a ve a co n d it io n t h a t m u s t b e m e t fo r t h e Go t o in s t ru ct io n t o b e e xe cu t e d . It is im p o rt a n t fo r a la b e l t o s t a rt wit h a le t t e r o f t h e a lp h a b e t o r wit h a n u n d e rlin e "_ ". Th e le n g t h o f t h e la b e l ca n b e u p t o 3 2 ch a ra ct e rs . It is a ls o im p o rt a n t t h a t a la b e l s t a rt s in t h e firs t clu m n .
In s t ru ct io n s In s t ru ct io n s a re a lre a d y d e fin e d b y t h e u s e o f a s p e cific m icro co n t ro lle r, s o it o n ly re m a in s fo r u s t o fo llo w t h e in s t ru ct io n s fo r t h e ir u s e in a s s e m b ly la n g u a g e . Th e wa y we writ e a n in s t ru ct io n is a ls o ca lle d in s t ru ct io n "s yn t a x". In t h e fo llo win g e xa m p le , we ca n re co g n ize a m is t a ke in writ in g b e ca u s e in s t ru ct io n s m o vlp a n d g o t t o d o n o t e xis t fo r t h e PIC1 6 F8 4 m icro co n t ro lle r.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (3 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
Op e ra n d s Op e ra n d s a re t h e in s t ru ct io n e le m e n t s fo r t h e in s t ru ct io n is b e in g e xe cu t e d . Th e y a re u s u a lly re g is t e rs o r v a ria b le s o r c o n s t a n t s .
Co m m e n t s Co m m e n t is a s e rie s o f wo rd s t h a t a p ro g ra m m e r writ e s t o m a ke t h e p ro g ra m m o re cle a r a n d le g ib le . It is p la ce d a ft e r a n in s t ru ct io n , a n d m u s t s t a rt wit h a s e m ico lo n "; ".
Dire ct ive s A d ire c t iv e is s im ila r t o a n in s t ru ct io n , b u t u n like a n in s t ru ct io n it is in d e p e n d e n t o n t h e m icro co n t ro lle r m o d e l, a n d re p re s e n t s a ch a ra ct e ris t ic o f t h e a s s e m b ly la n g u a g e it s e lf. Dire ct ive s a re u s u a lly g ive n p u rp o s e fu l m e a n in g s via va ria b le s o r re g is t e rs . Fo r e xa m p le , LEVEL ca n b e a d e s ig n a t io n fo r a va ria b le in RAM m e m o ry a t a d d re s s 0 Dh . In t h is wa y, t h e va ria b le a t t h a t a d d re s s ca n b e a cce s s e d via LEVEL d e s ig n a t io n . Th is is fa r e a s ie r fo r a p ro g ra m m e r t o u n d e rs t a n d t h a n fo r h im t o t ry t o re m e m b e r a d d re s s 0 Dh co n t a in s in fo rm a t io n a b o u t LEVEL.
An e x a m p le o f a w rit t in g p ro g ra m Th e fo llo win g e xa m p le illu s t ra t e s a s im p le p ro g ra m writ t e n in a s s e m b ly la n g u a g e re s p e ct in g t h e b a s ic ru le s . Wh e n writ in g a p ro g ra m , b e s id e m a n d a t o ry ru le s , t h e re a re a ls o s o m e ru le s t h a t a re n o t writ t e n d o wn b u t n e e d t o b e fo llo we d . On e o f t h e m is t o writ e t h e n a m e o f t h e p ro g ra m a t t h e b e g in n in g , wh a t t h e p ro g ra m d o e s , it s ve rs io n , d a t e wh e n it wa s writ t e n , t yp e o f m icro co n t ro lle r it wa s writ t e n fo r, a n d t h e p ro g ra m m e r's n a m e .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (4 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
S in ce t h is d a t a is n 't im p o rt a n t fo r t h e a s s e m b ly t ra n s la t o r, it is writ t e n a s c o m m e n t s . It s h o u ld b e n o t e d t h a t a co m m e n t a lwa ys b e g in s wit h a s e m ico lo n a n d it ca n b e p la ce d in a n e w ro w o r it ca n fo llo w a n in s t ru ct io n . Aft e r t h e o p e n in g co m m e n t h a s b e e n writ t e n , t h e d ire c t iv e m u s t b e in clu d e d . Th is is s h o wn in t h e e xa m p le a b o ve . In o rd e r t o fu n ct io n p ro p e rly, we m u s t d e fin e s e ve ra l m icro co n t ro lle r p a ra m e t e rs s u ch a s : - t yp e o f o s cilla t o r, - wh e t h e r wa t ch d o g t im e r is t u rn e d o n , a n d - wh e t h e r in t e rn a l re s e t circu it is e n a b le d . All t h is is d e fin e d b y t h e fo llo win g d ire ct ive : _CONFIG _CP_OFF&_WDT_OFF&PWRTE_ON&XT_OSC Wh e n a ll t h e n e e d e d e le m e n t s h a ve b e e n d e fin e d , we ca n s t a rt writ in g a p ro g ra m . Firs t , it is n e ce s s a ry t o d e t e rm in e a n a d d re s s fro m wh ich t h e m icro co n t ro lle r s t a rt s , fo llo win g a p o we r s u p p ly s t a rt - u p . Th is is ( o rg 0 x0 0 ) . Th e a d d re s s fro m wh ich t h e p ro g ra m s t a rt s if a n in t e rru p t o ccu rs is ( o rg 0 x0 4 ) . S in ce t h is is a s im p le p ro g ra m , it will b e e n o u g h t o d ire ct t h e m icro co n t ro lle r t o t h e b e g in n in g o f a p ro g ra m wit h a "g o t o Ma in " in s t ru ct io n . Th e in s t ru ct io n s fo u n d in t h e Ma in s e le ct m e m o ry b a n k1 ( BANK1 ) in o rd e r t o a cce s s TRIS B re g is t e r, s o t h a t p o rt B ca n b e d e cla re d a s a n o u t p u t ( m o vlw 0 x0 0 , m o vwf TRIS B) . Th e n e xt s t e p is t o s e le ct m e m o ry b a n k 0 a n d p la ce s t a t u s o f lo g ic o n e o n p o rt B ( m o vlw 0 xFF, m o vwf PORTB) , a n d t h u s t h e m a in p ro g ra m is fin is h e d . We n e e d t o m a ke a n o t h e r lo o p wh e re t h e m icro will b e h e ld s o it d o e s n 't "wa n d e r" if a n e rro r
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (5 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
o ccu rs . Fo r t h a t p u rp o s e , o n e in fin it e lo o p is m a d e wh e re t h e m icro is re t a in e d wh ile p o we r is co n n e ct e d . Th e n e ce s s a ry "e n d " a t t h e e n d o f e a ch p ro g ra m in fo rm s t h e a s s e m b ly t ra n s la t o r t h a t n o m o re in s t ru ct io n s a re in t h e p ro g ra m .
Co n t ro l d ire c t iv e s 4 . 1 # DEFINE
Exch a n g e s o n e p a rt o f t e xt fo r a n o t h e r
Synta x : # d e fin e < t e xt > [ < a n o t h e r t e xt > ] D e s c rip t io n : Ea ch t im e < t e xt > a p p e a rs in t h e p ro g ra m , it will b e e xch a n g e d fo r < a n o t h e r t e xt > . Ex a m p le : #define turned_o n 1 # d e fin e t u rn e d _off 0 S im ila r d ire c t iv e s : # UNDEFINE, IFDEF, IFNDEF
4 . 2 INCLUDE
In clu d e a n a d d it io n a l file in a p ro g ra m
Synta x : # in clu d e < file _ n a m e > # in clu d e "file _ n a m e " D e s c rip t io n : An a p p lica t io n o f t h is d ire ct ive h a s t h e e ffe ct a s t h o u g h t h e e n t ire file wa s co p ie d t o a p la ce wh e re t h e "in clu d e " d ire ct ive wa s fo u n d . If t h e file n a m e is in t h e s q u a re b ra cke t s , we a re d e a lin g wit h a s ys t e m file , a n d if it is in s id e q u o t a t io n m a rks , we a re d e a lin g wit h a u s e r file . Th e d ire ct ive "in clu d e " co n t rib u t e s t o a b e t t e r la yo u t o f t h e m a in p ro g ra m . Ex a m p le : #include #include "subprog.asm"
4 . 3 CONS TANT d e s ig n a t io n
Give s a co n s t a n t n u m e ric va lu e t o t h e t e xt u a l
Synta x : Co n s t a n t < n a m e > = < va lu e > D e s c rip t io n : Ea ch t im e t h a t < n a m e > a p p e a rs in p ro g ra m , it will b e re p la ce d wit h < va lu e > . Ex a m p le : Constant MAXIMUM=100 Constant Length=30 S im ila r d ire c t iv e s : S ET, VARIABLE
4 . 4 VARIABLE d e s ig n a t io n
Give s a va ria b le n u m e ric va lu e t o t e xt u a l
Synta x : Va ria b le < n a m e > = < va lu e >
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (6 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
D e s c rip t io n : By u s in g t h is d ire ct ive , t e xt u a l d e s ig n a t io n ch a n g e s wit h p a rt icu la r va lu e . It d iffe rs fro m CONS TANT d ire ct ive in t h a t a ft e r a p p lyin g t h e d ire ct ive , t h e va lu e o f t e xt u a l d e s ig n a t io n ca n b e ch a n g e d . Ex a m p le : variable level=20 variable time=13 S im ila r d ire c t iv e s : S ET, CONS TANT
4 . 5 S ET
De fin in g a s s e m b le r va ria b le
Synta x : < n a m e _ va ria b le > s e t < va lu e > D e s c rip t io n : To t h e va ria b le < n a m e _ va ria b le > is a d d e d e xp re s s io n < va lu e > . S ET d ire ct ive is s im ila r t o EQU, b u t wit h S ET d ire ct ive n a m e o f t h e va ria b le ca n b e re d e fin e d fo llo win g a d e fin it io n . Ex a m p le : level set 0 length set 12 level set 45 S im ila r d ire c t iv e s : EQU, VARIABLE
4 . 6 EQU
De fin in g a s s e m b le r co n s t a n t
Synta x : < n a m e _ co n s t a n t > e q u < va lu e > D e s c rip t io n : To t h e n a m e o f a co n s t a n t < n a m e _ co n s t a n t > is a d d e d va lu e < va lu e > Ex a m p le : five equ 5 six equ 6 seven equ 7 S im ila r in s t ru c t io n s : S ET
4 . 7 ORG De fin e s a n a d d re s s fro m wh ich t h e p ro g ra m is s t o re d in m icro co n t ro lle r m e m o ry Synta x : < la b e l> o rg < va lu e > D e s c rip t io n : Th is is t h e m o s t fre q u e n t ly u s e d d ire ct ive . Wit h t h e h e lp o f t h is d ire ct ive we d e fin e wh e re s o m e p a rt o f a p ro g ra m will b e s t a rt in t h e p ro g ra m m e m o ry. Ex a m p le : Start org 0×00 movlw 0xFF movwf PORTB Th e firs t t wo in s t ru ct io n s fo llo win g t h e firs t 'o rg ' d ire ct ive a re s t o re d fro m a d d re s s 0 0 , a n d t h e o t h e r t wo fro m a d d re s s 1 0 .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (7 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
4 . 8 END
En d o f p ro g ra m
Synta x : end D e s c rip t io n : At t h e e n d o f e a ch p ro g ra m it is n e ce s s a ry t o p la ce 'e n d ' d ire ct ive s o t h a t a s s e m b ly t ra n s la t o r wo u ld kn o w t h a t t h e re a re n o m o re in s t ru ct io n s in t h e p ro g ra m . Ex a m p le : . . movlw 0xFF movwf PORTB end
Co n d it io n a l in s t ru c t io n s
4 . 9 IF
Co n d it io n a l p ro g ra m b ra n ch in g
Synta x : if< co n d it io n a l_ t e rm > D e s c rip t io n : If co n d it io n in < co n d it io n a l_ t e rm > wa s m e t , p a rt o f t h e p ro g ra m wh ich fo llo ws IF d ire ct ive wo u ld b e e xe cu t e d . An d if it wa s n 't , t h e n t h e p a rt fo llo win g ELS E o r ENDIF d ire ct ive wo u ld b e e xe cu t e d . Ex a m p le : if level=100 goto FILL else goto DISCHARGE endif S im ila r d ire c t iv e s : # ELS E, ENDIF
4 . 1 0 ELS E Th e a lt e rn a t ive t o 'IF' p ro g ra m b lo ck wit h co n d it io n a l t e rm s Synta x : Els e D e s c rip t io n : Us e d wit h IF d ire ct ive a s a n a lt e rn a t ive if co n d it io n a l t e rm is in co rre ct . Ex a m p le : If time< 50 goto SPEED UP else goto SLOW DOWN endif S im ila r in s t ru c t io n s : ENDIF, IF
4 . 1 1 ENDIF
En d o f co n d it io n a l p ro g ra m s e ct io n
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (8 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
Synta x : e n d if D e s c rip t io n : Dire ct ive is writ t e n a t t h e e n d o f a co n d it io n a l b lo ck t o in fo rm t h e a s s e m b ly t ra n s la t o r t h a t it is t h e e n d o f t h e co n d it io n a l b lo ck Ex a m p le : If level=100 goto LOADS else goto UNLOADS endif S im ila r d ire c t iv e s : ELS E, IF
4 . 1 2 WHILE Exe cu t io n o f p ro g ra m s e ct io n a s lo n g a s co n d it io n is m e t Synta x : wh ile < co n d it io n > . endw D e s c rip t io n : Pro g ra m lin e s b e t we e n WHILE a n d ENDW wo u ld b e e xe cu t e d a s lo n g a s co n d it io n wa s m e t . If a co n d it io n s t o p p e d b e in g va lid , p ro g ra m wo u ld co n t in u e e xe cu t in g in s t ru ct io n s fo llo win g ENDW lin e . Nu m b e r o f in s t ru ct io n s b e t we e n WHILE a n d ENDW ca n b e 1 0 0 a t t h e m o s t , a n d n u m b e r o f e xe cu t io n s 2 5 6 . Ex a m p le : While i wa s p re vio u s ly d e fin e d ( m o s t co m m o n ly b y # DEFINE in s t ru ct io n ) , in s t ru ct io n s wh ich fo llo w wo u ld b e e xe cu t e d u n t il ELS E o r ENDIF d ire ct ive s a re n o t wo u ld b e
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (9 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
re a ch e d . Ex a m p le : #define test . ifdef test ;how the test was defined ......; instructions from these lines would execute endif S im ila r d ire c t iv e s : # DEFINE, ELS E, ENDIF, IFNDEF, # UNDEFINE
4 . 1 5 IFNDEF wa s d e fin e d
Exe cu t io n o f a p a rt o f t h e p ro g ra m if s ym b o l
Synta x : ifn d e f< d e s ig n a t io n > D e s c rip t io n : If d e s ig n a t io n < d e s ig n a t io n > wa s n o t p re vio u s ly d e fin e d , o r if it s d e fin it io n wa s e ra s e d wit h d ire ct ive # UNDEFINE, in s t ru ct io n s wh ich fo llo w wo u ld b e e xe cu t e d u n t il ELS E o r ENDIF d ire ct ive s wo u ld b e re a ch e d . Ex a m p le : #define test .......... #undefine test .......... ifndef test ;how the test was undefined ..... .; instructions from these lines would execute endif S im ila r d ire c t iv e s : # DEFINE, ELS E, ENDIF, IFDEF, # UNDEFINE
D a t a D ire c t iv e s 4 . 1 6 CBLOCK
De fin in g a b lo ck fo r t h e n a m e d co n s t a n t s
Synta x : Cb lo ck [ < t e rm > ] < la b e l> [ : < in cre m e n t > ] , < la b e l> [ : < in cre m e n t > ] . . . . . . endc D e s c rip t io n : Dire ct ive is u s e d t o g ive va lu e s t o n a m e d co n s t a n t s . Ea ch fo llo win g t e rm re ce ive s a va lu e g re a t e r b y o n e t h a n it s p re cu rs o r. If < in cre m e n t > p a ra m e t e r is a ls o g ive n , t h e n va lu e g ive n in < in cre m e n t > p a ra m e t e r is a d d e d t o t h e fo llo win g co n s t a n t . Va lu e o f < t e rm > p a ra m e t e r is t h e s t a rt in g va lu e . If it is n o t g ive n , it is co n s id e re d t o b e ze ro . Ex a m p le : Cblock 0x02 First, second, third endc
;first=0x02, second=0x03, third=0x04
cblock 0x02 first : 4, second : 2, third endc
;first=0x06, second=0x08, third=0x09
S im ila r d ire c t iv e s : ENDC
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (10 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
4 . 1 7 ENDC
En d o f co n s t a n t b lo ck d e fin it io n
Synta x : endc D e s c rip t io n : Dire ct ive wa s u s e d a t t h e e n d o f a d e fin it io n o f a b lo ck o f co n s t a n t s s o a s s e m b ly t ra n s la t o r co u ld kn o w t h a t t h e re a re n o m o re co n s t a n t s . S im ila r d ire c t iv e s : CBLOCK
4 . 1 8 DB
De fin in g o n e b yt e d a t a
Synta x : [ < la b e l> ] d b < t e rm > [ , < t e rm > , . . . . . , < t e rm > ] D e s c rip t io n : Dire ct ive re s e rve s a b yt e in p ro g ra m m e m o ry. Wh e n t h e re a re m o re t e rm s wh ich n e e d t o b e a s s ig n e d a b yt e e a ch , t h e y will b e a s s ig n e d o n e a ft e r a n o t h e r. Ex a m p le : db 't', 0×0f, 'e', 's', 0×12 S im ila r in s t ru c t io n s : DE, DT
4 . 1 9 DE
De fin in g t h e EEPROM m e m o ry b yt e
Synta x : [ < t e rm > ] d e < t e rm > [ , < t e rm > , . . . . . , < t e rm > ] D e s c rip t io n : Dire ct ive is u s e d fo r d e fin in g EEPROM m e m o ry b yt e . Eve n t h o u g h it wa s firs t in t e n d e d o n ly fo r EEPROM m e m o ry, it co u ld b e u s e d fo r a n y o t h e r lo ca t io n in a n y m e m o ry. Ex a m p le : org H'2100' de "Version 1.0" , 0 S im ila r in s t ru c t io n s : DB, DT
4 . 2 0 DT
De fin in g t h e d a t a t a b le
Synta x : [ < la b e l> ] d t < t e rm > [ , < t e rm > , . . . . . . . . . , < t e rm > ] D e s c rip t io n : Dire ct ive g e n e ra t e s RETLW s e rie s o f in s t ru ct io n s , o n e in s t ru ct io n p e r e a ch t e rm . Ex a m p le : dt "Message", 0 dt first, second, third S im ila r d ire c t iv e s : DB, DE
Co n fig u ra t io n a l d ire c t iv e s 4 . 2 1 _ CONFIG
S e t t in g t h e co n fig u ra t io n a l b it s
Synta x :
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (11 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
_ _ co n fig < t e rm > o r_ _ co n fig < a d d re s s > , < t e rm > D e s c rip t io n : Os cilla t o r, wa t ch d o g t im e r a p p lica t io n a n d in t e rn a l re s e t circu it a re d e fin e d . Be fo re u s in g t h is d ire ct ive , t h e p ro ce s s o r m u s t b e d e fin e d u s in g PROCES S OR d ire ct ive . Ex a m p le : _CONFIG _CP_OFF&_WDT_OFF&_PWRTE_ON&_XT_OSC S im ila r d ire c t iv e s : _ IDLOCS , PROCES S OR
4 . 2 2 PROCES S OR
De fin in g m icro co n t ro lle r m o d e l
Synta x : Pro ce s s o r < m icro co n t ro lle r_ t yp e > D e s c rip t io n : In s t ru ct io n s e t s t h e t yp e o f m icro co n t ro lle r wh e re p ro g ra m m in g is d o n e . Ex a m p le : processor 16F84
As s e m b le r a rit h m e t ic o p e ra t o rs Op e ra t o r De s crip t io n Exa m p le
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (12 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
File s c re a t e d a s a re s u lt o f p ro g ra m t ra n s la t io n As a re s u lt o f t h e p ro ce s s o f t ra n s la t in g a p ro g ra m writ t e n in a s s e m b le r la n g u a g e we g e t file s like : ● ● ●
Exe cu t in g file ( Pro g ra m _ Na m e . HEX) Pro g ra m e rro rs file ( Pro g ra m _ Na m e . ERR) Lis t file ( Pro g ra m _ Na m e . LS T)
Th e firs t file co n t a in s t ra n s la t e d p ro g ra m wh ich wa s re a d in m icro co n t ro lle r b y p ro g ra m m in g . It s co n t e n t s ca n n o t g ive a n y in fo rm a t io n t o p ro g ra m m e r, s o it will n o t b e co n s id e re d a n y fu rt h e r. Th e s e co n d file co n t a in s p o s s ib le e rro rs t h a t we re m a d e in t h e p ro ce s s o f writ in g , a n d wh ich we re n o t ice d b y a s s e m b ly t ra n s la t o r d u rin g t ra n s la t io n p ro ce s s . Erro rs ca n b e d is co ve re d in a "lis t " file a s we ll. Th is file is m o re s u it a b le t h o u g h wh e n p ro g ra m is b ig a n d vie win g t h e 'lis t ' file t a ke s lo n g e r. Th e t h ird file is t h e m o s t u s e fu l t o p ro g ra m m e r. Mu ch in fo rm a t io n is co n t a in e d in it , like in fo rm a t io n a b o u t p o s it io n in g in s t ru ct io n s a n d va ria b le s in m e m o ry, o r e rro r s ig n a liza t io n . Exa m p le o f 'lis t ' file fo r t h e p ro g ra m in t h is ch a p t e r fo llo ws . At t h e t o p o f e a ch p a g e is s t a t e d in fo rm a t io n a b o u t t h e file n a m e , d a t e wh e n it wa s t ra n s la t e d , a n d p a g e n u m b e r. Firs t co lu m n co n t a in s a n a d d re s s in p ro g ra m m e m o ry wh e re a in s t ru ct io n fro m t h a t ro w is p la ce d . S e co n d co lu m n co n t a in s a va lu e o f a n y va ria b le d e fin e d b y o n e o f t h e d ire ct ive s : S ET, EQU, VARIABLE, CONS TANT o r CBLOCK. Th ird co lu m n is re s e rve d fo r t h e fo rm o f a t ra n s la t e d in s t ru ct io n wh ich PIC is e xe cu t in g . Th e fo u rt h co lu m n co n t a in s a s s e m b le r in s t ru ct io n s a n d p ro g ra m m e r's co m m e n t s . Po s s ib le e rro rs will a p p e a r b e t we e n ro ws fo llo win g a lin e in wh ich t h e e rro r o ccu re d .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (13 of 15) [4/2/2003 16:18:10]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (14 of 15) [4/2/2003 16:18:11]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm
At t h e e n d o f t h e "lis t " file t h e re is a t a b le o f s ym b o ls u s e d in a p ro g ra m . Us e fu l e le m e n t o f 'lis t ' file is a g ra p h o f m e m o ry u t iliza t io n . At t h e ve ry e n d , t h e re is a n e rro r s t a t is t ic a s we ll a s t h e a m o u n t o f re m a in in g p ro g ra m m e m o ry.
Ma c ro s Ma cro s a re a ve ry u s e fu l e le m e n t in a s s e m b ly la n g u a g e . Th e y co u ld b rie fly b e d e s crib e d a s "u s e r d e fin e d g ro u p o f in s t ru ct io n s wh ich will e n t e r a s s e m b le r p ro g ra m wh e re m a cro wa s ca lle d ". It is p o s s ib le t o writ e a p ro g ra m e ve n wit h o u t u s in g m a cro s . Bu t wit h t h e ir u s e writ t e n p ro g ra m is m u ch m o re re a d a b le , e s p e cia lly if m o re p ro g ra m m e rs a re wo rkin g o n t h e s a m e p ro g ra m t o g e t h e r. Ma cro s h a ve t h e s a m e p u rp o s e a s fu n ct io n s o f h ig h e r p ro g ra m la n g u a g e s . Ho w t o w rit e t h e m : < la b e l> m a cro [ < a rg u m e n t 1 > , < a rg u m e n t 2 > , . . . . . . < a rg u m e n t N> ] ........ ....... endm Fro m t h e wa y t h e y we re writ t e n , we co u ld b e s e e n t h a t m a cro s ca n a cce p t a rg u m e n t s , t o o wh ich is a ls o ve ry u s e fu l in p ro g ra m m in g . Wh e n e ve r a rg u m e n t a p p e a rs in t h e b o d y o f a m a cro , it will b e re p la ce d wit h t h e < a rg u m e n t N> va lu e . Ex a m p le :
Th e a b o ve e xa m p le s h o ws a m a cro wh o s e p u rp o s e is t o p la ce o n p o rt B t h e ARG1 a rg u m e n t t h a t wa s d e fin e d wh ile m a cro wa s ca lle d . It s u s e in t h e p ro g ra m wo u ld b e lim it e d t o writ in g o n e lin e : ON_ PORTB 0 xFF , a n d t h u s we wo u ld p la ce va lu e 0 xFF o n PORTB. In o rd e r t o u s e a m a cro in t h e p ro g ra m , it is n e ce s s a ry t o in clu d e m a cro file in t h e m a in p ro g ra m wit h in s t ru ct io n in clu d e "m a cro _ n a m e . in c". Co n t e n t s o f a m a cro is a u t o m a t ica lly co p ie d o n t o a p la ce wh e re t h is in s t ru ct io n wa s writ t e n . Th is ca n b e b e s t s e e n in a p re vio u s lis t file wh e re file wit h m a cro s "b a n k. in c" wa s co p ie d b e lo w t h e lin e # in clu d e "b a n k. in c"
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/4_Poglavlje.htm (15 of 15) [4/2/2003 16:18:11]
Chapter 5 - MPLAB
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
CHAP TER 5 MP LAB In t ro d u ct io n 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8
In s t a llin g t h e MPLAB p ro g ra m p a cka g e In t ro d u ct io n t o MPLAB Ch o o s in g t h e d e ve lo p m e n t m o d e De s ig n in g a p ro je ct De s ig n in g n e w a s s e m b le r file Writ in g a p ro g ra m MPS IM s im u la t o r To o lb a r
I n t ro d u c t io n MPLAB is a Win d o ws p ro g ra m p a cka g e t h a t m a ke s writ in g a n d d e ve lo p in g a p ro g ra m e a s ie r. It co u ld b e s t b e d e s crib e d a s d e ve lo p in g e n viro n m e n t fo r s o m e s t a n d a rd p ro g ra m la n g u a g e t h a t is in t e n d e d fo r p ro g ra m m in g a PC co m p u t e r. S o m e o p e ra t io n s wh ich we re d o n e fro m t h e in s t ru ct io n lin e wit h a la rg e n u m b e r o f p a ra m e t e rs u n t il t h e d is co ve ry o f IDE "In t e g ra t e d De ve lo p m e n t En viro n m e n t " a re n o w m a d e e a s ie r b y u s in g t h e MPLAB. S t ill, o u r t a s t e s d iffe r, s o e ve n t o d a y s o m e p ro g ra m m e rs p re fe r t h e s t a n d a rd e d it o rs a n d co m p ile rs fro m in s t ru ct io n lin e . In a n y ca s e , t h e writ t e n p ro g ra m is le g ib le , a n d we ll d o cu m e n t e d h e lp is a ls o a va ila b le .
5 . 1 I n s t a llin g t h e p ro g ra m - MP LAB
MPLAB co n s is t s o f s e ve ra l p a rt s : - Gro u p in g t h e p ro je ct s file s in t o o n e p ro je ct ( Pro je ct Ma n a g e r)
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.htm (1 of 10) [4/2/2003 16:18:18]
Chapter 5 - MPLAB
- Ge n e ra t in g a n d p ro ce s s in g a p ro g ra m ( Te xt Ed it o r) - S im u la t o r o f t h e writ t e n p ro g ra m u s e d fo r s im u la t in g p ro g ra m fu n ct io n o n t h e m icro co n t ro lle r. Be s id e s t h e s e , t h e re a re s u p p o rt s ys t e m s fo r Micro ch ip p ro d u ct s s u ch a s PICS t a rt Plu s a n d ICD ( In Circu it De b u g g e r) . As t h is b o o k d o e s n o t co ve r t h e s e , t h e y will b e m e n t io n e d o n ly a s o p t io n s . Min im a l co m p u t e r re q u ire m e n t s fo r s t a rin g t h e MPLAB a re : · · · · · ·
PC co m p a t ib le co m p u t e r 4 8 6 o r h ig h e r Micro s o ft Win d o ws 3 . 1 x o r Win d o ws 9 5 a n d n e w ve rs io n s o f t h e Win d o ws o p e ra t in g s ys t e m VGA g ra p h ic ca rd 8 MB m e m o ry ( 3 2 MB re co m m e n d e d ) 2 0 MB s p a ce o n h a rd d is c Mo u s e
In o rd e r t o s t a rt t h e MPLAB we n e e d t o in s t a ll it firs t . In s t a llin g is a p ro ce s s o f co p yin g MPLAB file s fro m t h e CD o n t o a h a rd d is c o f yo u r co m p u t e r. Th e re is a n o p t io n o n e a ch n e w win d o w wh ich h e lp s yo u re t u rn t o a p re vio u s o n e , s o e rro rs s h o u ld n o t p re s e n t a p ro b le m o r b e co m e a s t re s s fu l e xp e rie n ce . In s t a llm e n t it s e lf wo rks m u ch t h e s a m e a s in s t a llm e n t o f m o s t Win d o ws p ro g ra m s . Firs t yo u g e t t h e We lco m e s cre e n , t h e n yo u ca n ch o o s e t h e o p t io n s fo llo we d b y in s t a llm e n t it s e lf, a n d , a t t h e e n d , yo u g e t t h e m e s s a g e wh ich s a ys yo u r in s t a lle d p ro g ra m is re a d y t o s t a rt . S t e p s fo r in s t a llin g MPLAB: 1. 2. 3. 4. 5. 6. 7.
S t a rt - u p t h e Micro s o ft Win d o ws Pu t t h e Micro ch ip CD d is c in t o CD ROM Click o n S TART in t h e b o t t o m le ft co rn e r o f t h e s cre e n a n d ch o o s e t h e RUN o p t io n Click o n BROWS E a n d s e le ct CD ROM d rive o f yo u r co m p u t e r. Fin d d ire ct o ry ca lle d MPLAB o n yo u r CD ROM Click o n S ETUP. EXE a n d t h e n o n OK . Click a g a in o n OK in yo u r RUN win d o w
In s t a llin g b e g in s a ft e r t h e s e s e ve n s t e p s . Th e fo llo win g p ict u re s e xp la in t h e m e a n in g o f ce rt a in in s t a llm e n t s t e p s .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.htm (2 of 10) [4/2/2003 16:18:18]
Chapter 5 - MPLAB
W e lc o m e s c re e n a t t h e b e g in n in g o f MP LAB in s t a llm e n t At t h e ve ry b e g in n in g , it is n e ce s s a ry t o s e le ct t h o s e MPLAB co m p o n e n t s we will b e wo rkin g wit h . S in ce we d o n 't h a ve a n y o rig in a l Micro ch ip h a rd wa re co m p o n e n t s s u ch a s p ro g ra m m e rs o r e m u la t o rs , we will o n ly in s t a ll MPLAB e n viro n m e n t , As s e m b le r, S im u la t o r a n d t h e in s t ru ct io n s .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.htm (3 of 10) [4/2/2003 16:18:18]
Chapter 5 - MPLAB
S e le c t in g c o m p o n e n t s o f MP LAB d e v e lo p in g e n v iro n m e n t As it is a s s u m e d yo u will wo rk in Win d o ws 9 5 ( o r a n e we r o p e ra t in g s ys t e m ) , e ve ryt h in g in co n n e ct io n wit h DOS o p e ra t in g s ys t e m h a s b e e n t a ke n o u t d u rin g s e le ct io n o f a s s e m b le r la n g u a g e . Ho we ve r, if yo u s t ill wis h t o wo rk in DOS , yo u n e e d t o d e s e le ct a ll o p t io n s co n n e ct e d wit h Win d o ws , a n d ch o o s e t h e co m p o n e n t s a p p ro p ria t e fo r DOS .
S e le c t in g t h e a s s e m b le r a n d t h e o p e ra t in g s y s t e m
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.htm (4 of 10) [4/2/2003 16:18:18]
Chapter 5 - MPLAB
Like a n y o t h e r p ro g ra m , MPLAB s h o u ld b e in s t a lle d in t o s o m e d ire ct o ry. Th is o p t io n co u ld b e m o ve d in t o a n y d ire ct o ry o n a n y h a rd d is c o f yo u r co m p u t e r. If yo u d id n 't h a ve a m o re p re s s in g n e e d , it s h o u ld b e le ft a t s e le ct e d p la ce .
Ch o o s in g t h e d ire c t o ry w h e re MP LAB w ill b e in s t a lle d
Us e rs wh o h a ve a lre a d y h a d MPLAB ( o ld e r ve rs io n t h a n t h is o n e ) n e e d t h e fo llo win g o p t io n . Th e p u rp o s e o f t h is o p t io n is t o s a ve co p ie s o f a ll file s wh ich will b e m o d ifie d d u rin g a ch a n g e o ve r t o a n e w MPLAB ve rs io n . In o u r ca s e we s h o u ld le a ve s e le ct e d NO b e ca u s e o f p re s u m p t io n t h a t t h is is yo u r firs t in s t a llm e n t o f MPLAB o n yo u r co m p u t e r.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.htm (5 of 10) [4/2/2003 16:18:18]
Chapter 5 - MPLAB
Op t io n fo r u s e rs w h o a re in s t a llin g a n e w v e rs io n o v e r a n a lre a d y in s t a lle d MP LAB S t a rt m e n u is a g ro u p o f p ro g ra m p o in t e rs , a n d is s e le ct e d b y clickin g o n S TART o p t io n in t h e lo we r le ft co rn e r o f t h e s cre e n . S in ce MPLAB will b e s t a rt e d fro m h e re , we n e e d t o le a ve t h is o p t io n a s it is .
Ad d in g t h e MP LAB t o t h e s t a rt m e n u
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.htm (6 of 10) [4/2/2003 16:18:18]
Chapter 5 - MPLAB
Lo ca t io n t h a t will b e m e n t io n e d fro m h e re o n , h a s t o d o wit h a p a rt o f MPLAB wh o s e e xp la n a t io n we d o n 't n e e d t o g e t in t o . By s e le ct in g a s p e cia l d ire ct o ry , MPLAB will ke e p a ll file s in co n n e ct io n wit h t h e lin ke r in a s e p a ra t e d ire ct o ry.
D e t e rm in in g a d ire c t o ry fo r lin k e r file s Eve ry Win d o ws p ro g ra m h a s s ys t e m file s u s u a lly s t o re d in a d ire ct o ry co n t a in in g Win d o ws p ro g ra m . Aft e r a n u m b e r o f d iffe re n t in s t a llm e n t s , t h e Win d o ws d ire ct o ry b e co m e s o ve rcro wd e d a n d t o o b ig . Th u s , s o m e p ro g ra m s a llo w fo r t h e ir s ys t e m file s t o b e ke p t in s a m e d ire ct o rie s wit h p ro g ra m s . MPLAB is a n e xa m p le o f s u ch p ro g ra m , a n d t h e b o t t o m o p t io n s h o u ld b e s e le ct e d .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.htm (7 of 10) [4/2/2003 16:18:18]
Chapter 5 - MPLAB
S e le c t in g a d ire c t o ry fo r s y s t e m file s Aft e r a ll o f t h e a b o ve s t e p s , in s t a llm e n t b e g in s b y clickin g o n 'Ne xt '.
S c re e n p rio r t o in s t a llm e n t In s t a llm e n t d o e s n 't t a ke lo n g , a n d t h e p ro ce s s o f co p yin g t h e file s ca n b e vie we d o n a s m a ll
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.htm (8 of 10) [4/2/2003 16:18:18]
Chapter 5 - MPLAB
win d o w in t h e rig h t co rn e r o f t h e s cre e n .
I n s t a llm e n t flo w Aft e r in s t a llm e n t h a ve b e e n co m p le t e d , t h e re a re t wo d ia lo g s cre e n s , o n e fo r t h e la s t m in u t e in fo rm a t io n re g a rd in g p ro g ra m ve rs io n s a n d co rre ct io n s , a n d t h e o t h e r is a we lco m e s cre e n . If t e xt file s ( Re a d m e . t xt ) h a ve o p e n e d , t h e y wo u ld n e e d t o b e clo s e d .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.htm (9 of 10) [4/2/2003 16:18:18]
Chapter 5 - MPLAB
La s t m in u t e in fo rm a t io n re g a rd in g p ro g ra m v e rs io n s a n d c o rre c t io n s . By clickin g o n Fin is h , in s t a llm e n t o f MPLAB is fin is h e d .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_01Poglavlje.htm (10 of 10) [4/2/2003 16:18:18]
Chapter 5 - MPLAB
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
5 . 2 MP LAB Fo llo win g t h e in s t a llm e n t p ro ce d u re , yo u will g e t a s cre e n o f t h e p ro g ra m it s e lf. As yo u ca n s e e , MPLAB lo o ks like m o s t o f t h e Win d o ws p ro g ra m s . Ne a r wo rkin g a re a t h e re is a "m e n u " ( u p p e r b lu e co lo re d a re a wit h o p t io n s File , Ed it . . e t c. ) , "t o o lb a r" ( a n a re a wit h illu s t ra t io n s t h e s ize o f s m a ll s q u a re s ) , a n d s t a t u s lin e o n t h e b o t t o m o f t h e win d o w. Th e re is a ru le in Win d o ws o f t a kin g s o m e o f t h e m o s t fre q u e n t ly u s e d p ro g ra m o p t io n s a n d p la cin g t h e m b e lo w t h e m e n u , t o o . Th u s we ca n a cce s s t h e m e a s ie r a n d s p e e d u p t h e wo rk. In o t h e r wo rd s , wh a t yo u h a ve in t h e t o o lb a r yo u a ls o h a ve in t h e m e n u .
Th e s c re e n a ft e r s t a rt in g t h e MP LAB Th e p u rp o s e o f t h is ch a p t e r is fo r yo u t o b e co m e fa m ilia r wit h MPLAB d e ve lo p in g e n viro n m e n t a n d wit h b a s ic e le m e n t s o f MPLAB s u ch a s : Ch o o s in g a d e ve lo p in g m o d e De s ig n in g a p ro je ct De s ig n in g a file fo r t h e o rig in a l p ro g ra m Writ in g a n e le m e n t a ry p ro g ra m in a s s e m b le r p ro g ra m la n g u a g e Tra n s la t in g a p ro g ra m in t o e xe cu t ive co d e S t a rt in g t h e p ro g ra m
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_02Poglavlje.htm (1 of 2) [4/2/2003 16:18:20]
Chapter 5 - MPLAB
Op e n in g a n e w win d o w fo r a s im u la t o r Op e n in g a n e w win d o w fo r va ria b le s wh o s e va lu e s we wa t ch ( Wa t ch Win d o w) S a vin g a win d o w wit h va ria b le s wh o s e va lu e s we a re wa t ch in g S e t t in g t h e b re a k p o in t s in a s im u la t o r ( Bre a k p o in t ) Pre p a rin g a p ro g ra m t o b e re a d in a m icro co n t ro lle r ca n b o il d o wn t o s e ve ra l b a s ic s t e p s :
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_02Poglavlje.htm (2 of 2) [4/2/2003 16:18:20]
Chapter 5 - MPLAB
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
5 . 3 Ch o o s in g t h e d e v e lo p m e n t m o d e S e t t in g a d e ve lo p in g m o d e is n e ce s s a ry s o t h a t MPLAB ca n kn o w wh a t t o o ls will b e u s e d t o e xe cu t e t h e writ t e n p ro g ra m . In o u r ca s e , we n e e d t o s e t u p t h e s im u la t o r a s a t o o l t h a t 's b e in g u s e d . By clickin g o n OPTIONS - - - > DEVELOPMENT MODE, a n e w win d o w will a p p e a r a s in t h e p ict u re b e lo w:
S e t t in g a d e v e lo p in g m o d e We s h o u ld s e le ct t h e 'MPLAB- S IM S im u la t o r' o p t io n b e ca u s e t h a t is wh e re t h e p ro g ra m will b e t rie d o u t . Be s id e t h is o p t io n , t h e 'Ed it o r On ly' o p t io n is a ls o a va ila b le . Th is o p t io n is u s e d o n ly if we wa n t t o writ e a p ro g ra m a n d b y p ro g ra m m e r writ e ' h e x file ' in a m icro co n t ro lle r. S e le ct io n o f t h e m icro co n t ro lle r m o d e l is d o n e o n t h e rig h t h a n d s id e . S in ce t h is b o o k is b a s e d o n t h e PIC1 6 F8 4 , t h is m o d e l s h o u ld b e s e le ct e d . Us u a lly wh e n we s t a rt wo rkin g wit h m icro co n t ro lle rs , we u s e a s im u la t o r. As t h e le ve l o f kn o wle d g e will h a ve in cre a s e d , p ro g ra m will b e writ t e n in a m icro co n t ro lle r rig h t a ft e r t ra n s la t io n . Ou r a d vice is t h a t yo u a lwa ys u s e t h e s im u la t o r. Th o u g h p ro g ra m will s e e m t o d e ve lo p s lo we r, it will p a y o ff in t h e e n d .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_03Poglavlje.htm [4/2/2003 16:18:21]
Ne x t pa g e
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
5 . 4 D e s ig n in g a p ro je c t In o rd e r t o s t a rt writ in g a p ro g ra m yo u n e e d t o cre a t e a p ro je ct firs t . By clickin g o n PROJECT - - > NEW PROJECT yo u a re a b le t o n a m e yo u r p ro je ct a n d s t o re it in a d ire ct o ry o f yo u r ch o ice . In t h e p ict u re b e lo w, a p ro je ct n a m e d 't e s t . p jt ' is b e in g cre a t e d a n d s t o re d in c: \ PIC\ PROJEKTS \ d ire ct o ry. Th is d ire ct o ry is ch o s e n b e ca u s e a u t h o rs h a d s u ch d ire ct o ry s e t u p o f o n t h e ir co m p u t e r. Ge n e ra lly s p e a kin g , d ire ct o ry wit h file s is u s u a lly p la ce d in a la rg e r d ire ct o ry wh o s e n a m e is u n m is t a ka b ly a s s o cia t e d wit h it s co n t e n t s .
Op e n in g a n e w p ro je c t Aft e r n a m in g t h e p ro je ct , click o n OK. Ne w win d o w co m e s u p a s in t h e n e xt p ict u re .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm (1 of 4) [4/2/2003 16:18:24]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm
Ad ju s t in g p ro je c t e le m e n t s Us in g a m o u s e click o n "t e s t [ . h e x] " wh ich a ct iva t e s 'No d e p ro p e rt ie s ' o p t io n in t h e b o t t o m rig h t co rn e r o f a win d o w. By clickin g o n it yo u g e t t h e fo llo win g win d o w.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm (2 of 4) [4/2/2003 16:18:24]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm
D e fin in g p a ra m e t e rs o f MP AS M a s s e m b le r Fro m t h e p ict u re we s e e t h a t t h e re a re m a n y d iffe re n t p a ra m e t e rs . Ea ch kin d co rre s p o n d s t o o n e p a ra m e t e r in "Co m m a n d lin e " . As m e m o rizin g t h e s e p a ra m e t e rs is ve ry u n co m fo rt a b le , e ve n fo rb id d in g fo r b e g in n e rs , g ra p h ic a d ju s t m e n t h a s b e e n in t ro d u ce d . Fro m t h e p ict u re we s e e wh ich o p t io n s n e e d t o b e t u rn e d o n . By clickin g o n OK we g o b a ck t o p re vio u s win d o w wh e re "Ad d n o d e " is a n a ct ive o p t io n . By clickin g o n it we g e t t h e fo llo win g win d o w wh e re we n a m e o u r a s s e m b le r p ro g ra m . Le t 's n a m e it "Te s t . a s m " s in ce t h is is o u r firs t p ro g ra m in MPLAB.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm (3 of 4) [4/2/2003 16:18:24]
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm
Op e n in g a n e w p ro je c t By clickin g o n OK we g o b a ck t o t h e s t a rt in g win d o w wh e re we s e e a d d e d a n a s s e m b le r file .
As s e m b le r file a d d e d By clickin g o n OK we re t u rn t o MPLAB e n viro n m e n t .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_04Poglavlje.htm (4 of 4) [4/2/2003 16:18:24]
Chapter 5 - MPLAB
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
5 . 5 D e s ig n in g a n e w a s s e m b le r file ( w rit in g a n e w p ro g ra m ) Wh e n "p ro je ct " p a rt o f t h e wo rk is fin is h e d , we n e e d t o s t a rt writ in g a p ro g ra m . In o t h e r wo rd s , n e w file m u s t b e o p e n e d , a n d will b e n a m e d "t e s t . a s m ". In o u r ca s e , file h a s t o b e n a m e d "t e s t . a s m " b e ca u s e in p ro je ct s wh ich h a ve o n ly o n e file ( s u ch a s o u rs ) , n a m e o f t h e p ro je ct a n d n a m e o f t h e s o u rce file h a ve t o b e t h e s a m e . Ne w file is o p e n e d b y clickin g o n FILE> NEW. Th u s we g e t a t e xt win d o w in s id e MPLAB wo rk s p a ce .
N e w a s s e m b le r file o p e n e d Ne w win d o w re p re s e n t s a file wh e re p ro g ra m will b e writ t e n . S in ce o u r a s s e m b le r file h a s t o b e n a m e d "t e s t . a s m ", we will n a m e it s o . Na m in g is d o n e ( a s wit h a ll Win d o ws p ro g ra m s ) b y clickin g o n FILE> S AVE AS . Th e n we g e t a win d o w like t h e fo llo win g p ict u re .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_05Poglavlje.htm (1 of 2) [4/2/2003 16:18:26]
Chapter 5 - MPLAB
N a m in g a n d s a v in g a n e w a s s e m b le r file Wh e n we g e t t h is win d o w, we n e e d t o writ e 't e s t . a s m ' b e lo w 'File n a m e : ', a n d click o n OK. Aft e r t h a t , we will s e e 't e s t . a s m ' file n a m e a t t h e t o p o f o u r win d o w.
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_05Poglavlje.htm (2 of 2) [4/2/2003 16:18:26]
Chapter 5 - MPLAB
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
5 . 6 W rit in g a p ro g ra m On ly a ft e r a ll o f t h e p re ce d in g o p e ra t io n s h a ve b e e n co m p le t e d we a re a b le t o s t a rt writ in g a p ro g ra m . S in ce a s im p le p ro g ra m h a s a lre a d y b e e n writ t e n in "As s e m b ly La n g u a g e Pro g ra m m in g " s e ct io n o f t h e b o o k, s o we will u s e t h a t s a m e p ro g ra m h e re , t o o .
Pro g ra m h a s t o b e writ t e n t o a win d o w t h a t 's o p e n e d , o r co p ie d fro m a d is c, o r t a ke n fro m Mikro Ele kt ro n ika In t e rn e t p re s e n t a t io n u s in g o p t io n s co p y a n d p a s t e . Wh e n t h e p ro g ra m is co p ie d t o "t e s t . a s m " win d o w, we ca n u s e PROJECT - > BUILD ALL co m m a n d ( if t h e re we re n o e rro rs ) , a n d a n e w win d o w wo u ld a p p e a r a s in t h e n e xt p ict u re .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_06Poglavlje.htm (1 of 2) [4/2/2003 16:18:28]
Chapter 5 - MPLAB
W in d o w w it h m e s s a g e s fo llo w in g a t ra n s la t io n o f a s s e m b le r p ro g ra m We ca n s e e fro m t h e p ict u re t h a t we g e t "t e s t . h e x" file a s a re s u lt o f t ra n s la t io n p ro ce s s , t h a t MPAS MWIN p ro g ra m is u s e d fo r t ra n s la t io n , a n d t h a t t h e re is o n e m e s s a g e . In a ll t h a t in fo rm a t io n , t h e la s t s e n t e n ce in t h e win d o w is t h e m o s t im p o rt a n t o n e b e ca u s e it s h o ws wh e t h e r t ra n s la t io n wa s s u cce s s fu l o r n o t . 'Bu ild co m p le t e d s u cce s s fu lly' is a m e s s a g e s t a t in g t h a t t ra n s la t io n wa s s u cce s s fu l a n d t h a t t h e re we re n o e rro rs . In ca s e a n e rro r s h o ws u p , we n e e d t o d o u b le click o n e rro r m e s s a g e in 'Bu ild Re s u lt s ' win d o w. Th is wo u ld a u t o m a t ica lly t ra n s fe r yo u t o a s s e m b le r p ro g ra m a n d t o t h e lin e wh e re t h e e rro r wa s .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_06Poglavlje.htm (2 of 2) [4/2/2003 16:18:28]
Chapter 5 - MPLAB
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
5 . 7 MP S I M S im u la t o r S im u la t o r is p a rt o f MPLAB e n viro n m e n t wh ich p ro vid e s a b e t t e r in s ig h t in t o t h e wo rkin g s o f a m icro co n t ro lle r. Tro u g h a s im u la t o r, we ca n m o n it o r cu rre n t va ria b le va lu e s , re g is t e r va lu e s a n d s t a t u s o f p o rt p in s . Tru t h fu lly, s im u la t o r d o e s n o t h a ve t h e s a m e va lu e in a ll p ro g ra m s . If a p ro g ra m is s im p le ( like t h e o n e g ive n h e re a s a n e xa m p le ) , s im u la t io n is n o t o f g re a t im p o rt a n ce b e ca u s e s e t t in g p o rt B p in s t o lo g ic o n e is n o t a d ifficu lt t a s k. Ho we ve r, s im u la t o r ca n b e o f g re a t h e lp wit h m o re co m p lica t e d p ro g ra m s wh ich in clu d e t im e rs , d iffe re n t co n d it io n s wh e re s o m e t h in g h a p p e n s a n d o t h e r s im ila r re q u ire m e n t s ( e s p e cia lly wit h m a t h e m a t ica l o p e ra t io n s ) . S im u la t io n , a s t h e n a m e in d ica t e s "s im u la t e s t h e wo rk o f a m icro co n t ro lle r". As m icro co n t ro lle r e xe cu t e s in s t ru ct io n s o n e b y o n e , s im u la t o r is co n ce ive d - p ro g ra m m e r m o ve s t h ro u g h a p ro g ra m s t e p - b ys t e p ( lin e - b y- lin e ) a n d fo llo ws wh a t g o e s o n wit h d a t a wit h in a m icro co n t ro lle r. Wh e n writ in g is co m p le t e d , it is a g o o d t ra it t o , p ro g ra m m e r's firs t ch e ck h is p ro g ra m in a s im u la t o r, a n d t h e n ru n s it o u t in a re a l s it u a t io n . Un fo rt u n a t e ly, a s wit h m a n y o t h e r g o o d h a b it s , m a n o ve rflo ws t h is o n e t o o , m o re o r le s s . Re a s o n s fo r t h is a re p a rt ly p e rs o n a lit y, a n d p a rt ly la ck o f g o o d s im u la t o rs . Firs t t h in g we n e e d t o d o , a s in a re a l s it u a t io n , is t o re s e t a m icro co n t ro lle r wit h DEBUG > RUN > RES ET co m m a n d . Th is co m m a n d re s u lt s in b o ld lin e p o s it io n e d a t t h e b e g in n in g o f a p ro g ra m , a n d p ro g ra m co u n t e r is p o s it io n e d a t ze ro wh ich ca n b e s e e n in s t a t u s lin e ( p c: 0 x0 0 ) .
Be g in n in g o f p ro g ra m s im u la t io n , re s e t t in g a m ic ro c o n t ro lle r On e o f t h e m a in ch a ra ct e ris t ics o f a s im u la t o r is t h e a b ilit y t o vie w re g is t e r s t a t u s wit h in a m icro co n t ro lle r. Th e s e re g is t e rs a re a ls o ca lle d s p e cia l fu n ct io n re g is t e rs , o r S FR re g is t e rs . We ca n g e t a win d o w wit h S FR re g is t e rs b y clickin g o n WINDOW- > S PECIAL FUNCTION REGIS TERS , o r o n S FR ico n . Be s id e S FR re g is t e rs , it is u s e fu l t o h a ve a n in s ig h t in t o file re g is t e rs . Win d o w wit h file re g is t e rs ca n b e o p e n e d b y clickin g o n WINDOW- > FILE REGIS TERS .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_07Poglavlje.htm (1 of 2) [4/2/2003 16:18:32]
Chapter 5 - MPLAB
If t h e re a re va ria b le s in t h e p ro g ra m , it is g o o d t o wa t ch t h e m , t o o . To e a ch va ria b le is a s s ig n e d o n e win d o w ( Wa t ch Win d o ws ) b y clickin g o n WINDOW- > WATCH WINDOWS .
S im u la t o r w it h o p e n w in d o w s fo r S FR re g is t e rs , file re g is t e rs a n d v a ria b le s . Th e n e xt co m m a n d in a s im u la t o r is DEBUG> RUN> S TEP wh ich s t a rt s o u r s t e p in g t h ro u g h t h e p ro g ra m . Th e s a m e co m m a n d co u ld h a ve b e e n a s s ig n e d fro m a ke yb o a rd wit h < F7 > ke y ( g e n e ra lly s p e a kin g , a ll s ig n ifica n t co m m a n d s h a ve ke ys a s s ig n e d o n t h e ke yb o a rd ) . By u s in g t h e F7 ke y, p ro g ra m is e xe cu t e d s t e p - b y- s t e p . Wh e n we g e t t o a m a cro , file co n t a in in g a m a cro is o p e n e d ( Ba n k. in c) , a n d we p ro ce e d t o g o t h ro u g h a m a cro . In a S FR re g is t e rs win d o w we ca n o b s e rve h o w W re g is t e r re ce ive s va lu e 0 xFF a n d d e live rs it t o p o rt B. By clickin g o n F7 ke y a g a in , we d o n 't a ch ie ve a n yt h in g b e ca u s e p ro g ra m h a s a rrive d t o a n "in fin it e lo o p ". In fin it e lo o p is a t e rm we will m e e t o ft e n . It re p re s e n t s a lo o p fro m wh ich a m icro co n t ro lle r ca n n o t g e t o u t u n t il in t e rru p t o ccu rs ( if it is u s e d in a p ro g ra m ) , o r u n t il a m icro co n t ro lle r wo u ld b e re s e t .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_07Poglavlje.htm (2 of 2) [4/2/2003 16:18:32]
Chapter 5 - MPLAB
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
5 . 8 To o lb a r S in ce MPLAB h a s m o re t h a n o n e co m p o n e n t , e a ch o f t h e co m p o n e n t s h a s it s o wn t o o lb a r. Ho we ve r, t h e re is a t o o lb a r wh ich is s o m e co m p ila t io n o f a ll t o o lb a rs , a n d ca n s e rve a s a co m m o n ly u s e d t o o lb a r. Th is t o o lb a r is e n o u g h fo r o u r n e e d s , a n d it will b e e xp la in e d in m o re d e t a il. In t h e p ict u re b e lo w, we ca n s e e a t o o lb a r we n e e d wit h a b rie f e xp la n a t io n o f e a ch ico n . Be ca u s e o f t h e lim it e d fo rm a t o f t h is b o o k, t h is t o o lb a r is s h o wn a s a h a n g in g t o o lb a r. Ge n e ra lly, it is p la ce d h o rizo n t a lly b e lo w t h e m e n u , o ve r t h e e n t ire le n g t h o f t h e s cre e n .
Un iv e rs a l t o o lb a r w it h b rie f e x p la n a t io n s o f t h e ic o n s
To o lb a r ico n d e s crip t io n If t h e cu rre n t t o o lb a r fo r s o m e re a s o n d o e s n o t re s p o n d t o a click o n t h is ico n , t h e n e xt o n e a p p e a rs . Ch a n g e o ve r is re p e a t e d s o t h a t o n t h e fo u rt h click we will g e t t h e s a m e t o o lb a r a g a in . Ico n fo r o p e n in g a p ro je ct . Pro je ct o p e n e d in t h is wa y co n t a in s a ll s cre e n a d ju s t m e n t s a n d a d ju s t m e n t o f a ll e le m e n t s wh ich a re cru cia l t o t h e cu rre n t p ro je ct .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_08Poglavlje.htm (1 of 3) [4/2/2003 16:18:38]
Chapter 5 - MPLAB
Ico n fo r s a vin g a p ro je ct . S a ve d p ro je ct will ke e p a ll win d o w a d ju s t m e n t s a n d a ll p a ra m e t e r a d ju s t m e n t s . Wh e n we re a d in a p ro g ra m a g a in , e ve ryt h in g will re t u rn t o t h e s cre e n a s wh e n t h e p ro je ct wa s clo s e d . S e a rch in g fo r a p a rt o f t h e p ro g ra m , o r wo rd s is o p e ra t io n we n e e d wh e n s e a rch in g t h ro u g h b ig g e r a s s e m b le r o r o t h e r p ro g ra m s . By u s in g it , we ca n fin d q u ickly a p a rt o f t h e p ro g ra m , la b e l, m a cro , e t c. Cu t t in g a p a rt o f t h e t e xt o u t . Th is o n e a n d t h e fo llo win g t h re e ico n s a re s t a n d a rd in a ll p ro g ra m s t h a t d e a l wit h p ro ce s s in g t e xt u a l file s . S in ce e a ch p ro g ra m is a ct u a lly a co m m o n t e xt file , t h o s e o p e ra t io n s a re u s e fu l. Co p yin g a p a rt o f t h e t e xt . Th e re is a d iffe re n ce b e t we e n t h is o n e a n d t h e p re vio u s ico n . Wit h cu t o p e ra t io n , wh e n yo u cu t a p a rt o f t h e t e xt o u t , it d is a p p e a rs fro m t h e s cre e n ( a n d fro m a p ro g ra m ) a n d is co p ie d a ft e rwa rd s . Bu t wit h co p y o p e ra t io n , t e xt is co p ie d b u t n o t cu t o u t , a n d it re m a in s o n t h e s cre e n . Wh e n a p a rt o f t h e t e xt is co p ie d , it is m o ve d in t o a p a rt o f t h e m e m o ry wh ich s e rve s fo r t ra n s fe rrin g d a t a in Win d o ws o p e ra t io n a l s ys t e m . La t e r, b y clickin g o n t h is ico n it ca n b e 'p a s t e d ' in t h e t e xt wh e re t h e cu rs o r is . S a vin g a p ro g ra m ( a s s e m b le r file ) . S t a rt p ro g ra m e xe cu t io n in fu ll s p e e d . It is re co g n ize d b y a p p e a ra n ce o f a ye llo w s t a t u s lin e . Wit h t h is kin d o f p ro g ra m e xe cu t io n , s im u la t o r e xe cu t e s a p ro g ra m in fu ll s p e e d u n t il it is in t e rru p t e d b y clickin g o n t h e re d t ra ffic lig h t ico n . S t o p p ro g ra m e xe cu t io n in fu ll s p e e d . Aft e r clickin g o n t h is ico n , s t a t u s lin e b e co m e s g ra y a g a in , a n d p ro g ra m e xe cu t io n ca n co n t in u e s t e p b y s t e p . S t e p b y s t e p p ro g ra m e xe cu t io n . By clickin g o n t h is ico n , we b e g in e xe cu t in g a n in s t ru ct io n fro m t h e n e xt p ro g ra m lin e in re la t io n t o t h e cu rre n t o n e . S kip re q u ire m e n t s . S in ce s im u la t o r is s t ill a s o ft wa re s im u la t io n o f re a l wo rk, it is p o s s ib le t o s im p ly s kip o ve r s o m e p ro g ra m re q u ire m e n t s . Th is is e s p e cia lly h a n d y wit h in s t ru ct io n s wh ich a re wa it in g fo r s o m e re q u ire m e n t fo llo win g wh ich p ro g ra m ca n p ro ce e d fu rt h e r. Th a t p a rt o f t h e p ro g ra m wh ich fo llo ws a re q u ire m e n t is t h e p a rt t h a t 's in t e re s t in g t o a p ro g ra m m e r. Re s e t t in g a m icro co n t ro lle r. By clickin g o n t h is ico n , p ro g ra m co u n t e r is p o s it io n e d a t t h e b e g in n in g o f a p ro g ra m a n d s im u la t io n ca n s t a rt . By clickin g o n t h is ico n we g e t a win d o w wit h a p ro g ra m , b u t t h is t im e a s p ro g ra m m e m o ry wh e re we ca n s e e wh ich in s t ru ct io n is fo u n d a t wh ich a d d re s s . Wit h t h e h e lp o f t h is ico n we g e t a win d o w wit h t h e co n t e n t s o f RAM m e m o ry o f a m icro co n t ro lle r. By clickin g o n t h is ico n , win d o w wit h S FR re g is t e r a p p e a rs . S in ce S FR re g is t e rs a re u s e d in e ve ry p ro g ra m , it is re co m m e n d e d t h a t in s im u la t o r t h is win d o w is a lwa ys a ct ive . If a p ro g ra m co n t a in s va ria b le s wh o s e va lu e s we n e e d t o ke e p t ra ck o f ( e x. co u n t e r) , a win d o w n e e d s t o b e a d d e d fo r e a ch o f t h e m , wh ich is d o n e b y u s in g t h is ico n . Wh e n ce rt a in e rro rs in a p ro g ra m a re n o t ice d d u rin g s im u la t io n p ro ce s s , p ro g ra m h a s t o b e co rre ct e d . S in ce s im u la t o r u s e s HEX file a s it s in p u t , s o we n e e d t o t ra n s la t e a p ro g ra m a g a in s o t h a t a ll ch a n g e s wo u ld b e t ra n s fe rre d t o a s im u la t o r. By clickin g o n t h is ico n , e n t ire p ro je ct is t ra n s la t e d a g a in , a n d we g e t t h e n e we s t ve rs io n o f HEX file fo r t h e s im u la t o r.
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_08Poglavlje.htm (2 of 3) [4/2/2003 16:18:38]
Chapter 5 - MPLAB
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/5_08Poglavlje.htm (3 of 3) [4/2/2003 16:18:38]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
CHAP TER 6 Th e S a m p le s In t ro d u ct io n 6 . 1 S u p p lyin g t h e m icro co n t ro lle r 6 . 2 Ma cro s u s e d in p ro g ra m s ● ●
Ma cro s WAIT, WAITX Ma cro PRINT
6 . 3 S a m p le s ● ● ●
● ● ●
● ● ● ●
Lig h t - e m it t in g d io d e s - LEDs Ke yb o a rd Op t o co u p le r ❍ Op t o co u p le rin g t h e in p u t lin e s ❍ Op t o co u p le rin g t h e o u t p u t lin e s Re la ys Ge n e ra t in g a s o u n d S h ift re g is t e rs ❍ In p u t s h ift re g is t e r ❍ Ou t p u t s h ift re g is t e r 7 - s e g m e n t Dis p la ys ( m u lt ip le xin g ) LCD d is p la y 1 2 - b it AD co n ve rt e r S e ria l co m m u n ica t io n
I n t ro d u c t io n Exa m p le s g ive n in t h is ch a p t e r will s h o w yo u h o w t o co n n e ct t h e PIC m icro co n t ro lle r wit h o t h e r p e rip h e ra l co m p o n e n t s o r d e vice s wh e n d e ve lo p in g yo u r o wn m icro co n t ro lle r s ys t e m . Ea ch e xa m p le co n t a in s d e t a ile d d e s crip t io n o f t h e h a rd wa re p a rt wit h e le ct rica l o u t lin e a n d co m m e n t s a b o u t t h e p ro g ra m . All p ro g ra m s ca n b e t a ke n d ire ct ly fro m t h e fro m 'Mikro Ele kt ro n ika ' in t e rn e t p re s e n t a t io n .
S u p p ly in g t h e m ic ro c o n t ro lle r Ge n e ra lly s p e a kin g , t h e co rre ct vo lt a g e s u p p ly is o f u t m o s t im p o rt a n ce fo r t h e p ro p e r fu n ct io n in g o f t h e m icro co n t ro lle r s ys t e m . It ca n e a s ily b e co m p a re d t o a m a n b re a t h in g in t h e a ir. It is m o re
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_01Poglavlje.htm (1 of 2) [4/2/2003 16:18:40]
Chapter 6 - Samples
like ly t h a t a m a n wh o is b re a t h in g in fre s h a ir will live lo n g e r t h a n a m a n wh o 's livin g in a p o llu t e d e n viro n m e n t . Fo r a p ro p e r fu n ct io n o f a n y m icro co n t ro lle r, it is n e ce s s a ry t o p ro vid e a s t a b le s o u rce o f s u p p ly, a s u re re s e t wh e n yo u t u rn it o n a n d a n o s cilla t o r. Acco rd in g t o t e ch n ica l s p e cifica t io n s b y t h e m a n u fa ct u re r o f PIC m icro co n t ro lle r, s u p p ly vo lt a g e s h o u ld m o ve b e t we e n 2 . 0 V t o 6 . 0 V in a ll ve rs io n s . Th e s im p le s t s o lu t io n t o t h e s o u rce o f s u p p ly is u s in g t h e vo lt a g e s t a b ilize r LM7 8 0 5 wh ich g ive s s t a b le + 5 V o n it s o u t p u t . On e s u ch s o u rce is s h o wn in t h e p ict u re b e lo w.
In o rd e r t o fu n ct io n p ro p e rly, o r in o rd e r t o h a ve s t a b le 5 V a t t h e o u t p u t ( p in 3 ) , in p u t vo lt a g e o n p in 1 o f LM7 8 0 5 s h o u ld b e b e t we e n 7 V t h ro u g h 2 4 V. De p e n d in g o n cu rre n t co n s u m p t io n o f d e vice we will u s e t h e a p p ro p ria t e t yp e o f vo lt a g e s t a b ilize r LM7 8 0 5 . Th e re a re s e ve ra l ve rs io n s o f LM7 8 0 5 . Fo r cu rre n t co n s u m p t io n o f u p t o 1 A we s h o u ld u s e t h e ve rs io n in TO- 2 2 0 ca s e wit h t h e ca p a b ilit y o f a d d it io n a l co o lin g . If t h e t o t a l co n s u m p t io n is 5 0 m A, we ca n u s e 7 8 L0 5 ( s t a b ilize r ve rs io n in s m a ll TO - 9 2 p a cka g in g fo r cu rre n t o f u p t o 1 0 0 m A) .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_01Poglavlje.htm (2 of 2) [4/2/2003 16:18:40]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
Ma c ro s u s e d in p ro g ra m s Exa m p le s g ive n in t h e fo llo win g s e ct io n s o f t h is ch a p t e r o ft e n u s e m a cro s WAIT, WAITX a n d PRINT, s o t h e y will b e e xp la in e d in m o re d e t a il.
Ma cro s WAIT, WAITX File Wa it . in c co n t a in s t wo m a cro s WAIT a n d WAITX. Th ro u g h t h e s e m a cro s it is p o s s ib le t o a s s ig n t im e d e la ys in d iffe re n t in t e rva ls . Bo t h m a cro s u s e t h e o ve rflo w o f co u n t e r TMR0 a s a b a s ic in t e rva l. By ch a n g in g t h e p re s ca le r we ca n ch a n g e t h e le n g t h o f t h e o ve rflo w in t e rva l o f t h e co u n t e r TMR0 .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_02Poglavlje.htm (1 of 4) [4/2/2003 16:18:43]
Chapter 6 - Samples
If we u s e t h e o s cilla t o r ( re s o n a t o r) o f 4 MHz, fo r p re s ca le r va lu e s 0 , 1 , a n d 7 t h a t d ivid e t h e b a s ic clo ck o f t h e o s cilla t o r, t h e in t e rva l fo llo we d b y a n o ve rflo w o f t im e r TMR0 will b e 0 . 5 1 2 , 1 . 0 2 a n d 6 5 . 3 m S . Pra ct ica lly, t h a t m e a n s t h a t t h e b ig g e s t d e la y wo u ld b e 2 5 6 x6 5 . 3 m S wh ich is e q u a l t o 1 6 . 7 2 s e co n d s .
In o rd e r t o u s e m a cro s in t h e m a in p ro g ra m it is n e ce s s a ry d o d e cla re va ria b le s wcycle a n d p re s cWAIT a s will b e d o n e in e xa m p le s wh ich fo llo w in t h is ch a p t e r. Ma cro WAIT h a s o n e a rg u m e n t . Th e s t a n d a rd va lu e a s s ig n e d t o p re s ca le r o f t h is m a cro is 1 ( 1 . 0 2 m S ) , a n d it ca n n o t b e ch a n g e d . WAIT t im e co n s t _ 1 t im e c o n s t _ 1 is n u m b e r fro m 0 t o 2 5 5 . By m u lt ip lyin g t h a t n u m b e r wit h t h e o ve rflo w t im e p e rio d we g e t t h e t o t a l a m o u n t o f t h e d e la y: TIME= t im e co n s t _ 1 x 1 . 0 2 m S . Ex a m p le : WAIT . 1 0 0 Exa m p le s h o ws h o w t o m a ke a d e la y o f 1 0 0 x1 . 0 2 m S , o r t o t a l o f 1 0 2 m S . Un like m a cro WAIT, m a cro WAITX h a s o n e m o re a rg u m e n t t h a t ca n a s s ig n p re s ca le r va lu e . Ma cro WAITX h a s t wo a rg u m e n t s : Tim e c o n s t _ 2 is n u m b e r fro m 0 t o 2 5 5 . By m u lt ip lyin g t h a t n u m b e r wit h t h e o ve rflo w t im e p e rio d we g e t t h e t o t a l a m o u n t o f t h e d e la y: TIME= t im e co n s t _ 1 x 1 . 0 2 m S x PRES Ce xt P RES Ce x t is n u m b e r fro m 0 t o 7 wh ich s e t s u p t h e re la t io n s h ip b e t we e n a clo ck a n d t im e r TMR0 . Ex a m p le : WAITX . 1 0 0 , 7 Exa m p le s h o ws h o w t o m a ke a d e la y o f 1 0 0 x6 5 . 3 m S , o r t o t a l o f 6 5 3 m S .
Ma cro PRINT Ma cro PRINT is fo u n d in Prin t . in c file . It m a ke s it e a s y t o s h o w a s t rin g o f d a t a o n o n e o f t h e o u t p u t d e vice s s u ch a s : LCD, RS 2 3 2 , m a t rix p rin t e r. . . e t c. Th e e a s ie s t wa y t o fo rm a s e rie s is b y u s in g a d t ( d e fin e t a b le ) d ire ct ive . Th is in s t ru ct io n s t o re s a s e rie s o f d a t a in t o p ro g ra m m e m o ry a s a g ro u p o f re t lw in s t ru ct io n s wh o s e o p e ra n d is d a t a fro m t h e s t rin g .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_02Poglavlje.htm (2 of 4) [4/2/2003 16:18:43]
Chapter 6 - Samples
Ho w o n e s u ch s e q u e n ce is fo rm e d b y u s in g d t in s t ru ct io n is s h o wn in t h e fo llo win g e xa m p le : org 0x00 goto Main String movwf PCL String1 dt "this is 'ASCII' string" String2 dt "Second string" End Main movlw .5 call String : Firs t in s t ru ct io n a ft e r la b e l Ma in writ e s t h e p o s it io n o f a m e m b e r o f t h e s t rin g in w re g is t e r. We ju m p wit h in s t ru ct io n ca ll o n t o la b e l s t rin g wh e re p o s it io n o f a m e m b e r o f t h e s t rin g is a d d e d t o t h e va lu e o f t h e p ro g ra m co u n t e r: PCL= PCL+ W. Ne xt we will h a ve in t h e p ro g ra m co u n t e r a n a d d re s s o f re t lw in s t ru ct io n wit h t h e d e s ire d m e m b e r o f t h e s t rin g . Wh e n t h is in s t ru ct io n is e xe cu t e d , m e m b e r o f t h e s t rin g will b e in w re g is t e r, a n d a d d re s s o f t h e in s t ru ct io n t h a t e xe cu t e d a ft e r t h e ca ll in s t ru ct io n will b e in t h e p ro g ra m co u n t e r. En d la b e l is a n e le g a n t wa y t o m a rk t h e a d d re s s a t wh ich t h e s t rin g e n d s . Ma cro PRINT h a s five a rg u m e n t s : PRINT m a cro Ad d r, S t a rt , En d , Va r, Ou t Ad d r is a n a d d re s s wh e re o n e o r m o re s t rin g s ( wh ich fo llo w o n e b y o n e ) b e g in . S t a rt is a n a d d re s s o f t h e firs t m e m b e r o f t h e s t rin g En d is a n a d d re s s wh e re t h e s t rin g e n d s Va r is t h e va ria b le wh ich h a s a ro le o f s h o win g ( p o in t in g ) t h e m e m b e rs o f t h e s t rin g Ou t is a n a rg u m e n t we u s e t o s e n d t h e a d d re s s o f e xis t in g s u b p ro g ra m s a s s ig n e d t o o u t p u t d e vice s s u ch a s : LCD, RS - 2 3 2 , e t c.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_02Poglavlje.htm (3 of 4) [4/2/2003 16:18:43]
Chapter 6 - Samples
Ma cro PRINT writ e s o u t a s t rin g o f AS CII ca ra ct e rs fo r 'Mikro Ele kt ro n ika ' o n LCD d is p la y. Th e s t rin g t a ke s u p o n e p a rt o f p ro g ra m m e m o ry b e g in n in g a t a d d re s s 0 x0 3 .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_02Poglavlje.htm (4 of 4) [4/2/2003 16:18:43]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
S a m p le s Lig h t - Em it t in g Dio d e s - LEDs LEDs a re s u re ly o n e o f t h e m o s t co m m o n ly u s e d e le m e n t s in e le ct ro n ics . LED is a n a b b re via t io n fo r 'Lig h t Em it t in g Dio d e '. Wh e n ch o o s in g a LED, s e ve ra l p a ra m e t e rs s h o u ld b e lo o ke d a t : d ia m e t e r, wh ich is u s u a lly 3 o r 5 m m ( m illim e t e rs ) , wo rkin g cu rre n t wh ich is u s u a lly a b o u t 1 0 m A ( It ca n b e a s lo w a s 2 m A fo r LEDs wit h h ig h e fficie n cy - h ig h lig h t o u t p u t ) , a n d co lo r o f co u rs e , wh ich ca n b e re d o r g re e n t h o u g h t h e re a re a ls o o ra n g e , b lu e , ye llo w. . . . LEDs m u s t b e co n n e ct e d a ro u n d t h e co rre ct wa y, in o rd e r t o e m it lig h t a n d t h e cu rre n t - lim it in g re s is t o r m u s t b e t h e co rre ct va lu e s o t h a t t h e LED is n o t d a m a g e d o r b u rn o u t ( o ve rh e a t e d ) . Th e p o s it ive o f t h e s u p p ly is t a ke n t o t h e a n o d e , a n d t h e ca t h o d e g o e s t o t h e n e g a t ive o r g ro u n d o f t h e p ro je ct ( circu it ) . In o rd e r t o id e n t ify e a ch le a d , t h e ca t h o d e is t h e s h o rt e r le a d a n d t h e LED "b u lb " u s u a lly h a s a cu t o r "fla t " o n t h e ca t h o d e s id e . Dio d e s will e m it lig h t o n ly if cu rre n t is flo win g fro m a n o d e t o ca t h o d e . Ot h e rwis e , it s PN ju n ct io n is re ve rs e b ia s e d a n d cu rre n t wo n 't flo w. In o rd e r t o co n n e ct a LED co rre ct ly, a re s is t o r m u s t b e a d d e d in s e rie s t h a t t o lim it t h e a m o u n t o f cu rre n t t h ro u g h t h e d io d e , s o t h a t it d o e s n o t b u rn o u t . Th e va lu e o f t h e re s is t o r is d e t e rm in e d b y t h e a m o u n t o f cu rre n t yo u wa n t t o flo w t h ro u g h t h e LED. Ma xim u m cu rre n t flo w t ro u g h LED wa s d e fin e d b y m a n u fa ct u re r. Hig h - e fficie n cy LEDs ca n p ro d u ce a ve ry g o o d o u t p u t wit h a cu rre n t a s lo w a s 2 m A.
To d e t e rm in e t h e va lu e o f t h e d ro p p e r- re s is t o r, we n e e d t o kn o w t h e va lu e o f t h e s u p p ly vo lt a g e . Fro m t h is we s u b t ra ct t h e ch a ra ct e ris t ic vo lt a g e d ro p o f a LED. Th is va lu e will ra n g e fro m 1 . 2 v t o 1 . 6 v d e p e n d in g o n t h e co lo r o f t h e LED. Th e a n s we r is t h e va lu e o f Ur. Us in g t h is va lu e a n d t h e cu rre n t we wa n t t o flo w t h ro u g h t h e LED ( 0 . 0 0 2 A t o 0 . 0 1 A) we ca n wo rk o u t t h e va lu e o f t h e re s is t o r fro m t h e fo rm u la R= Ur/ I .
LEDs a re co n n e ct e d t o a m icro co n t ro lle r in t wo wa ys . On e is t o t u rn t h e m o n wit h lo g ic ze ro , a n d o t h e r t o t u rn t h e m o n wit h lo g ic o n e . Th e firs t is ca lle d NEGATIVE lo g ic a n d t h e o t h e r is ca lle d POS ITIVE lo g ic. Th e a b o ve d ia g ra m s h o ws h o w t h e y a re co n n e ct e d fo r POS ITIVE lo g ic. S in ce POS ITIVE lo g ic p ro vid e s a vo lt a g e o f + 5 V t o t h e d io d e a n d d ro p p e r re s is t o r, it will e m it lig h t e a ch t im e a p in o f p o rt B is p ro vid e d wit h a lo g ic 1 ( 1 = HIGH o u t p u t ) . NEGATIVE lo g ic re q u ire s t h e LED t o b e t u rn e d a ro u n d t h e o t h e r wa y a n d t h e a n o d e s co n n e ct e d t o g e t h e r t o t h e p o s it ive s u p p ly. Wh e n a LOW o u t p u t fro m t h e m icro co n t ro lle r is d e live re d t o t h e ca t h o d e a n d re s is t o r, t h e LED will illu m in a t e .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_03Poglavlje.htm (1 of 3) [4/2/2003 16:18:45]
Chapter 6 - Samples
Co n n e c t in g LED d io d e s t o P ORTB m ic ro c o n t ro lle r
Th e fo llo win g e xa m p le in it ia lize s p o rt B a s o u t p u t a n d s e t s lo g ic o n e t o e a ch p in o f p o rt B t o t u rn o n a ll LEDs .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_03Poglavlje.htm (2 of 3) [4/2/2003 16:18:45]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_03Poglavlje.htm (3 of 3) [4/2/2003 16:18:45]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
Ke yb o a rd Ke yb o a rd s a re m e ch a n ica l d e vice s u s e d t o e xe cu t e a b re a k o r m a ke co n n e ct io n b e t we e n t wo p o in t s . Th e y co m e in d iffe re n t s ize s a n d wit h d iffe re n t p u rp o s e s . Ke ys t h a t a re u s e d h e re a re a ls o ca lle d "d ip - ke ys ". Th e y a re s o ld e re d d ire ct ly o n t o a p rin t e d b o a rd a n d a re o ft e n fo u n d in e le ct ro n ics . Th e y h a ve fo u r p in s ( t wo fo r e a ch co n t a ct ) wh ich g ive t h e m m e ch a n ica l s t a b ilit y.
Ex a m p le o f c o n n e c t in g k e y s t o m ic ro c o n t ro lle r p in s .
Ke y fu n ct io n is s im p le . Wh e n we p re s s a ke y, t wo co n t a ct s a re jo in e d t o g e t h e r a n d co n n e ct io n is m a d e . S t ill, it is n 't a ll t h a t s im p le . Th e p ro b le m lie s in t h e n a t u re o f vo lt a g e a s a n e le ct rica l d im e n s io n , a n d in t h e im p e rfe ct io n o f m e ch a n ica l co n t a ct s . Th a t is t o s a y, b e fo re co n t a ct is m a d e o r cu t o ff, t h e re is a s h o rt t im e p e rio d wh e n vib ra t io n ( o s cilla t io n ) ca n o ccu r a s a re s u lt o f u n e ve n n e s s o f m e ch a n ica l co n t a ct s , o r a s a re s u lt o f t h e d iffe re n t s p e e d in p re s s in g a ke y ( t h is d e p e n d s o n p e rs o n wh o p re s s e s t h e ke y) . Th e t e rm g ive n t o t h is p h e n o m e n a is ca lle d S WITCH ( CONTACT) DEBOUNCE. If t h is is o ve rlo o ke d wh e n p ro g ra m is writ t e n , a n e rro r ca n o ccu r, o r t h e p ro g ra m ca n p ro d u ce m o re t h a n o n e o u t p u t p u ls e fo r a s in g le ke y p re s s . In o rd e r t o a vo id t h is , we ca n in t ro d u ce a s m a ll d e la y wh e n we d e t e ct t h e clo s in g o f a co n t a ct . Th is will e n s u re t h a t t h e p re s s o f a ke y is in t e rp re t e d a s a s in g le p u ls e . Th e d e b o u n ce d e la y is p ro d u ce d in s o ft wa re a n d t h e le n g t h o f t h e d e la y d e p e n d s o n t h e ke y, a n d t h e p u rp o s e o f t h e ke y. Th e p ro b le m ca n b e p a rt ia lly s o lve d b y a d d in g a ca p a cit o r a cro s s t h e ke y, b u t a we ll- d e s ig n e d p ro g ra m is a m u ch - b e t t e r a n s we r. Th e p ro g ra m ca n b e a d ju s t e d u n t il fa ls e d e t e ct io n is co m p le t e ly e lim in a t e d . In s o m e ca s e a s im p le d e la y will b e a d e q u a t e b u t if yo u wa n t t h e p ro g ra m t o b e a t t e n d in g t o a n u m b e r o f t h in g s a t t h e s a m e t im e , a s im p le d e la y will m e a n t h e p ro ce s s o r is "d o in g - n o t h in g " fo r a lo n g p e rio d o f t im e a n d m a y m is s o t h e r in p u t s o r b e t a ke n a wa y fro m o u t p u t t in g t o a d is p la y. Th e s o lu t io n is t o h a ve a p ro g ra m t h a t lo o ks fo r t h e p re s s o f a ke y a n d a ls o t h e re le a s e o f a ke y. Th e m a cro b e lo w ca n b e u s e d fo r ke yp re s s d e b o u n ce .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_04Poglavlje.htm (1 of 3) [4/2/2003 16:18:47]
Chapter 6 - Samples
Th e a b o ve m a cro h a s s e ve ra l a rg u m e n t s t h a t n e e d t o b e e xp la in e d : TES TER m a cro HiLo , Po rt , Bit , De la y, Ad d re s s HiLo ca n b e '0 ' o r '1 ' wh ich re p re s e n t s ris in g o r fa llin g e d g e wh e re s e rvice s u b p ro g ra m will b e e xe cu t e d wh e n yo u p re s s a ke y. P o rt is a m icro co n t ro lle r's p o rt t o wh ich a ke y is co n n e ct e d . In t h e ca s e o f a PIC1 6 F8 4 m icro co n t ro lle r, it ca n b e PORTA o r PORTB. Bit is p o rt 's p in t o wh ich t h e ke y is co n n e ct e d . D e la y is a n u m b e r fro m 0 t o 2 5 5 , u s e d t o a s s ig n t h e t im e n e e d e d fo r ke y d e b o u n ce d e t e ct io n co n t a ct o s cilla t io n - t o s t o p . It is ca lcu la t e d a s TIME = De la y x 1 m s . Ad d re s s is t h e a d d re s s wh e re t h e m icro g o e s a ft e r a ke y is d e t e ct e d . Th e s u b - ro u t in e a t t h e a d d re s s ca rrie s o u t t h e re q u ire d in s t ru ct io n fo r t h e ke yp re s s . Ex a m p le 1 : TES TER 0 , PORTA, 3 , . 1 0 0 , Te s t e r1 _ a b o ve Ke y- 1 is co n n e ct e d t o RA0 ( t h e firs t o u t p u t o f p o rt A) wit h a d e la y o f 1 0 0 m icro s e co n d s a n d a re a ct io n t o lo g ic ze ro . S u b p ro g ra m t h a t p ro ce s s e s ke y is fo u n d a t a d d re s s o f la b e l Te s t e r1 _ a b o ve . Ex a m p le 2 : TES TER 0 , PORTA, 2 , . 2 0 0 , Te s t e r2 _ b e lo w Ke y- 2 is co n n e ct e d t o RA1 ( t h e s e co n d o u t p u t o f p o rt A) wit h 2 0 0 m S d e la y a n d a re a ct io n t o lo g ic o n e . S u b p ro g ra m t h a t p ro ce s s e s ke y is fo u n d a t a d d re s s o f la b e l Te s t e r2 _ b e lo w. Th e n e xt e xa m p le s h o ws t h e u s e o f m a cro s in a p ro g ra m . TES TER. AS M t u rn s LED o n a n d o ff. Th e LED is co n n e ct e d t o t h e s e ve n t h o u t p u t o f p o rt B. Ke y- 1 is u s e d t o t u rn LED o n . Ke y- 2 t u rn s LED o ff.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_04Poglavlje.htm (2 of 3) [4/2/2003 16:18:47]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_04Poglavlje.htm (3 of 3) [4/2/2003 16:18:47]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
Op t o c o u p le r Op t o co u p le r co m b in e a LED a n d p h o t o - t ra n s is t o r in t h e s a m e ca s e . Th e p u rp o s e o f a n o p t o co u p le r is t o s e p a ra t e t wo p a rt s o f a circu it . Th is is d o n e fo r a n u m b e r o f re a s o n s : ●
●
●
I n t e rfe re n c e . On e p a rt o f a circu it m a y b e in a lo ca t io n wh e re it p icks u p a lo t o f in t e rfe re n ce ( s u ch a s fro m e le ct ric m o t o rs , we ld in g e q u ip m e n t , p e t ro l m o t o rs e t c. ) If t h e o u t p u t o f t h is circu it g o e s t h ro u g h a n o p t o co u p le r t o a n o t h e r circu it , o n ly t h e in t e n d e d s ig n a ls will p a s s t h ro u g h t h e o p t o co u p le r. Th e in t e rfe re n ce s ig n a ls will n o t h a ve e n o u g h "s t re n g t h " t o a ct iva t e t h e LED in t h e o p t o co u p le r a n d t h u s t h e y a re e lim in a t e d . To p ro t e ct a s e ct io n o f t h e d e vice . Typ ica l e xa m p le s a re in d u s t ria l u n it s wit h lo t s o f in t e rfe re n ce s wh ich a ffe ct s ig n a ls in t h e wire s . If t h e s e in t e rfe re n ce s a ffe ct t h e fu n ct io n o f co n t ro l s e ct io n , e rro rs will o ccu r a n d t h e u n it will s t o p wo rkin g . S im u lt a n e o u s s e p a ra t io n a n d in t e n s ific a t io n o f a s ig n a l. A s ig n a l a s lo w a s 3 v is a b le t o a ct iva t e a n o p t o co u p le r a n d t h e o u t p u t o f t h e o p t o co u p le r ca n b e co n n e ct e d t o a n in p u t lin e o f a m icro co n t ro lle r. Th e m icro co n t ro lle r re q u ire s a n in p u t s win g o f 5 v a n d in t h is ca s e t h e 3 v s ig n a l is a m p lifie d t o 5 v. It ca n a ls o b e u s e d t o a m p lify t h e cu rre n t o f a s ig n a l. S e e b e lo w fo r u s e o n t h e o u t p u t lin e o f a m icro co n t ro lle r fo r cu rre n t a m p lifica t io n . Hig h Vo lt a g e S e p a ra t io n . Op t o co u p le rs h a ve in h e re n t h ig h vo lt a g e s e p a ra t io n q u a lit ie s . S in ce t h e LED is co m p le t e ly s e p a ra t e fro m t h e p h o t o - t ra n s is t o r, o p t o co u p le rs ca n e xh ib it vo lt a g e is o la t io n o f 3 kv o r h ig h e r.
Op t o co u p le rs ca n b e u s e d a s in p u t o r o u t p u t d e vice . Th e y ca n h a ve a d d it io n a l fu n ct io n s s u ch a s S ch m it t t rig g e rin g ( t h e o u t p u t o f a S ch m it t t rig g e r is e it h e r 0 o r 1 - it ch a n g e s s lo w ris in g a n d fa llin g wa ve fo rm s in t o d e fin it e lo w o r h ig h va lu e s ) . Op t o co u p le rs a re p a cka g e d a s a s in g le u n it o r in g ro u p s o f t wo o r m o re in o n e h o u s in g . Th e y a re a ls o ca lle d PHOTO INTERRUPTERS wh e re a s p o ke d wh e e l is in s e rt e d in a s lo t b e t we e n t h e LED a n d p h o t o t ra n s is t o r a n d e a ch t im e t h e lig h t is in t e rru p t e d , t h e t ra n s is t o r p ro d u ce s a p u ls e . Ea ch o p t o co u p le r n e e d s t wo s u p p lie s in o rd e r t o fu n ct io n . Th e y ca n b e u s e d wit h o n e s u p p ly, b u t t h e vo lt a g e is o la t io n fe a t u re is lo s t .
Op t o c o u p le r o n a n in p u t lin e Th e wa y it wo rks is s im p le : wh e n a s ig n a l a rrive s , t h e LED wit h in t h e o p t o co u p le r is t u rn e d o n , a n d it s h in e s o n t h e b a s e o f a p h o t o - t ra n s is t o r wit h in t h e s a m e ca s e . Wh e n t h e t ra n s is t o r is a ct iva t e d , t h e vo lt a g e b e t we e n co lle ct o r a n d e m it t e r fa lls t o 0 . 5 V o r le s s a n d t h e m icro co n t ro lle r s e e s t h is a s a lo g ic ze ro o n it s RA4 p in . Th e e xa m p le b e lo w is a co u n t e r, u s e d fo r co u n t in g p ro d u ct s o n p ro d u ct io n lin e , d e t e rm in in g m o t o r s p e e d , co u n t in g t h e n u m b e r o f re vo lu t io n s o f a n a xis e t c. Le t t h e s e n s o r b e a m icro - s wit ch . Ea ch t im e t h e s wit ch is clo s e d , t h e LED is illu m in a t e d . Th e LED 't ra n s fe rs ' t h e s ig n a l t o t h e p h o t o t ra n s is t o r a n d t h e o p e ra t io n o f t h e p h o t o - t ra n s is t o r d e live rs a LOW t o in p u t RA4 o f a m icro co n t ro lle r. A p ro g ra m in t h e m icro co n t ro lle r will b e n e e d e d t o p re ve n t fa ls e co u n t in g a n d a n in d ica t o r co n n e ct e d t o a n y o f t h e o u t p u t s o f t h e m icro co n t ro lle r will s h o ws t h e cu rre n t s t a t e o f t h e co u n t e r.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_05Poglavlje.htm (1 of 3) [4/2/2003 16:18:52]
Chapter 6 - Samples
I n p u t lin e o p t o c o u p le r e x a m p le
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_05Poglavlje.htm (2 of 3) [4/2/2003 16:18:52]
Chapter 6 - Samples
Op t o c o u p le r o n a n o u t p u t lin e An Op t o co u p le r ca n b e u s e d t o s e p a ra t e t h e o u t p u t s ig n a l o f a m icro co n t ro lle r fro m a n o u t p u t d e vice . Th is m a y b e n e e d e d fo r h ig h vo lt a g e s e p a ra t io n o r cu rre n t a m p lifica t io n . Th e o u t p u t o f s o m e m icro co n t ro lle rs is lim it e d t o 2 5 m A. Th e o p t o co u p le r will t a ke t h e lo w- cu rre n t s ig n a l fro m t h e m icro co n t ro lle r a n d it 's o u t p u t t ra n s is t o r will d rive a LED o r re la y, a s s h o wn b e lo w:
Ou t p u t lin e o p t o c o u p le r e x a m p le Th e p ro g ra m fo r t h is e xa m p le is s im p le . By d e live rin g a lo g ic '1 ' t o t h e fo u rt h p in o f p o rt A, t h e LED will b e t u rn e d o n a n d t h e t ra n s is t o r will b e a ct iva t e d in t h e o p t o co u p le r. An y d e vice co n n e ct e d t o t h e o u t p u t o f t h e o p t o co u p le r will b e a ct iva t e d . Th e t ra n s is t o r cu rre n t - lim it is a b o u t 2 5 0 m A.
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_05Poglavlje.htm (3 of 3) [4/2/2003 16:18:52]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
Th e Re la y Th e re la y is a n e le ct ro m e ch a n ica l d e vice , wh ich t ra n s fo rm s a n e le ct rica l s ig n a l in t o m e ch a n ica l m o ve m e n t . It co n s is t s o f a co il o f in s u la t e d wire o n a m e t a l co re , a n d a m e t a l a rm a t u re wit h o n e o r m o re co n t a ct s . Wh e n a s u p p ly vo lt a g e wa s d e live re d t o t h e co il, cu rre n t wo u ld flo w a n d a m a g n e t ic fie ld wo u ld b e p ro d u ce d t h a t m o ve s t h e a rm a t u re t o clo s e o n e s e t o f co n t a ct s a n d / o r o p e n a n o t h e r s e t . Wh e n p o we r is re m o ve d fro m t h e re la y, t h e m a g n e t ic flu x in t h e co il co lla p s e s a n d p ro d u ce s a fa irly h ig h vo lt a g e in t h e o p p o s it e d ire ct io n . Th is vo lt a g e ca n d a m a g e t h e d rive r t ra n s is t o r a n d t h u s a re ve rs e b ia s e d d io d e is co n n e ct e d a cro s s t h e co il t o "s h o rt - o u t " t h e s p ike wh e n it o ccu rs .
Co n n e c t in g a re la y t o t h e m ic ro c o n t ro lle r v ia a t ra n s is t o r Ma n y m icro co n t ro lle rs ca n n o t d rive a re la y d ire ct ly a n d s o a d rive r t ra n s is t o r is re q u ire d . A HIGH o n t h e b a s e o f t h e t ra n s is t o r t u rn s t h e t ra n s is t o r ON a n d t h is a ct iva t e s t h e re la y. Th e re la y ca n b e co n n e ct e d t o a n y e le ct rica l d e vice via t h e co n t a ct s . Th e 1 0 k re s is t o r o n t h e b a s e o f t h e t ra n s is t o r lim it s t h e cu rre n t fro m t h e m icro co n t ro lle r t o t h a t re q u ire d b y t h e t ra n s is t o r. Th e 1 0 k b e t we e n b a s e a n d t h e n e g a t ive ra il p re ve n t s n o is e o n t h e b a s e fro m a ct iva t in g t h e re la y. Th u s o n ly a cle a r s ig n a l fro m t h e m icro co n t ro lle r will a ct iva t e t h e re la y.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_06Poglavlje.htm (1 of 4) [4/2/2003 16:18:54]
Chapter 6 - Samples
Co n n e c t in g t h e o p t o c o u p le r a n d re la y t o a m ic ro c o n t ro lle r A re la y ca n a ls o b e a ct iva t e d via a n o p t o co u p le r wh ich a t t h e s a m e t im e a m p lifie s t h e cu rre n t re la t e d t o t h e o u t p u t o f t h e m icro co n t ro lle r a n d p ro vid e s a h ig h d e g re e o f is o la t io n . Hig h cu rre n t o p t o co u p le rs u s u a lly co n t a in a 'd a rlin g t o n ' o u t p u t t ra n s is t o r t o p ro vid e h ig h o u t p u t cu rre n t . Co n n e ct in g via a n o p t o co u p le r is re co m m e n d e d e s p e cia lly fo r m icro co n t ro lle r a p p lica t io n s , wh e re m o t o rs a re a ct iva t e d a s t h e co m m u t a t o r n o is e fro m t h e m o t o r ca n g e t b a ck t o t h e m icro co n t ro lle r via t h e s u p p ly lin e s . Th e o p t o co u p le r d rive s a re la y a n d t h e re la y a ct iva t e s t h e m o t o r. Th e fig u re b e lo w s h o ws t h e p ro g ra m n e e d e d t o a ct iva t e t h e re la y, a n d in clu d e s s o m e o f t h e a lre a d y d is cu s s e d m a cro s .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_06Poglavlje.htm (2 of 4) [4/2/2003 16:18:54]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_06Poglavlje.htm (3 of 4) [4/2/2003 16:18:54]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_06Poglavlje.htm (4 of 4) [4/2/2003 16:18:54]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
Ge n e ra t in g a s o u n d A Pie zo d ia p h ra g m ca n b e a d d e d t o a n o u t p u t lin e o f a m icro co n t ro lle r t o d e live r a "s p e a ke r" t o n e s , b e e p s a n d s ig n a ls . It is im p o rt a n t t o kn o w t h e re a re t wo m a in t yp e s o f p ie zo s o u n d - e m it t in g d e vice s . On e h a s a ct ive co m p o n e n t s in s id e t h e ca s e a n d o n ly re q u ire s a DC s u p p ly fo r t h e "s p e a ke r" t o e m it a t o n e o r b e e p . Ge n e ra lly t h e t o n e s o r b e e p s e m it t e d b y t h e s e "s p e a ke r" o r "b e e p e rs " ca n n o t b e ch a n g e d t h e y a re fixe d b y t h e in t e rn a l circu it ry. Th is is n o t t h e t yp e we a re d is cu s s in g in t h is a rt icle . Th e o t h e r t yp e co n s is t s o f a p ie zo d ia p h ra g m a n d re q u ire s a s ig n a l t o b e d e live re d t o it fo r it t o fu n ct io n . De p e n d in g o n t h e fre q u e n cy o f t h e wa ve fo rm , t h e o u t p u t ca n b e a t o n e , t u n e , a la rm o r e ve n vo ice m e s s a g e s . In o rd e r fo r t h e m t o wo rk we m u s t d e live r a cycle co n s is t in g o f a HIGH a n d LOW. It is t h e ch a n g e fro m HIGH t o LOW o r LOW t o HIGH t h a t ca u s e s t h e d ia p h ra g m t o "d is h " ( m o ve ) t o p ro d u ce t h e ch a ra ct e ris t ic "t in n y" s o u n d . Th e wa ve fo rm ca n b e a s m o o t h ch a n g e fro m o n e va lu e t o t h e o t h e r ( ca lle d a s in e wa ve ) o r a fa s t ch a n g e ( ca lle d a S QUARE WAVE) . A co m p u t e r is id e a l fo r p ro d u cin g a s q u a re wa ve . Th e s q u a re wa ve d e live ry p ro d u ce s a s lig h t ly h a rs h e r o u t p u t . Co n n e ct in g a p ie zo d ia p h ra g m is ve ry s im p le . On e p in is co n n e ct e d t o t h e n e g a t ive ra il a n d t h e o t h e r t o a n o u t p u t o f a m icro co n t ro lle r, a s s h o wn in t h e d ia g ra m b e lo w. Th is will d e live r a 5 v wa ve fo rm t o t h e p ie zo d ia p h ra g m . To p ro d u ce a h ig h e r o u t p u t , t h e wa ve fo rm m u s t b e in cre a s e d a n d t h is re q u ire s a d rive r t ra n s is t o r a n d in d u ct o r.
Co n n e c t in g a p ie z o d ia p h ra g m t o a m ic ro c o n t ro lle r
As wit h a ke y, yo u ca n e m p lo y a m a cro t h a t will d e live r a BEEP ROUTINE in t o a p ro g ra m wh e n needed. BEEP m a c ro fre q , d u ra t io n : fre q : fre q u e n cy o f t h e s o u n d . Th e h ig h e r n u m b e r p ro d u ce s h ig h e r fre q u e n cy
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_07Poglavlje.htm (1 of 4) [4/2/2003 16:18:57]
Chapter 6 - Samples
d u ra t io n : s o u n d d u ra t io n . Th e h ig h e r t h e n u m b e r, t h e lo n g e r t h e s o u n d . Ex a m p le 1 : BEEP 0 xFF, 0 x0 2 Th e o u t p u t o f t h e p ie zo d ia p h ra g m h a s t h e h ig h e s t fre q u e n cy a n d d u ra t io n a t 2 cycle s p e r 6 5 . 3 m S wh ich g ive s 1 3 0 . 6 m S Ex a m p le 2 : BEEP 0 x9 0 , 0 x0 5 Th e o u t p u t o f t h e p ie zo d ia p h ra g m h a s a fre q u e n cy o f 0 x9 0 a n d d u ra t io n o f 5 cycle s p e r 6 5 . 3 m S . It is b e s t t o d e t e rm in e t h e s e m a cro a rg u m e n t s t h ro u g h e xp e rim e n t a t io n a n d s e le ct t h e s o u n d t h a t b e s t s u it s t h e a p p lica t io n . Th e fo llo win g is t h e BEEP Ma cro lis t in g :
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_07Poglavlje.htm (2 of 4) [4/2/2003 16:18:57]
Chapter 6 - Samples
Th e fo llo win g e xa m p le s h o ws t h e u s e o f a m a cro in a p ro g ra m . Th e p ro g ra m p ro d u ce s t wo m e lo d ie s wh ich a re o b t a in e d b y p re s s in g T1 o r T2 . S o m e o f t h e p re vio u s ly d is cu s s e d m a cro s a re in clu d e d in t h e p ro g ra m .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_07Poglavlje.htm (3 of 4) [4/2/2003 16:18:57]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_07Poglavlje.htm (4 of 4) [4/2/2003 16:18:57]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
S h ift re g is t e rs Th e re a re t wo t yp e s o f s h ift re g is t e rs : in p u t a n d o u t p u t . I n p u t s h ift re g is t e rs re ce ive d a t a in p a ra lle l, t h ro u g h 8 lin e s a n d t h e n s e n d it s e ria lly t h ro u g h t wo lin e s t o a m icro co n t ro lle r. Ou t p u t s h ift re g is t e rs wo rk in t h e o p p o s it e d ire ct io n ; t h e y re ce ive s e ria l d a t a a n d o n a "la t ch " lin e s ig n a l, t h e y t u rn it in t o p a ra lle l d a t a . S h ift re g is t e rs a re g e n e ra lly u s e d t o e xp a n d t h e n u m b e r o f in p u t - o u t p u t lin e s o f a m icro co n t ro lle r. Th e y a re n o t s o m u ch in u s e a n y m o re t h o u g h , b e ca u s e m o s t m o d e rn m icro co n t ro lle rs h a ve a la rg e n u m b e r o f in p u t / o u t p u t lin e s . Ho we ve r, t h e ir u s e wit h m icro co n t ro lle rs s u ch a s PIC1 6 F8 4 is ve ry im p o rt a n t .
I n p u t s h ift re g is t e r 7 4 HC5 9 7 In p u t s h ift re g is t e rs t ra n s fo rm p a ra lle l d a t a in t o s e ria l d a t a a n d t ra n s fe r it t o a m icro co n t ro lle r. Th e ir wo rkin g is q u it e s im p le . Th e re a re fo u r lin e s fo r t h e t ra n s fe r o f d a t a : c lo c k , la t c h , lo a d a n d d a t a . Da t a is firs t re a d fro m t h e in p u t p in s b y a n in t e rn a l re g is t e r t h ro u g h a 'la t ch ' s ig n a l. Th e n , wit h a 'lo a d ' s ig n a l, d a t a is t ra n s fe rre d fro m t h e in p u t la t ch re g is t e r t o t h e s h ift re g is t e r, a n d fro m t h e re it is s e ria lly t ra n s fe rre d t o a m icro co n t ro lle r via 'd a t a ' a n d 'clo ck' lin e s .
An o u t lin e o f t h e co n n e ct io n o f t h e s h ift re g is t e r 7 4 HC5 9 7 t o a m icro , is s h o wn b e lo w.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_08Poglavlje.htm (1 of 7) [4/2/2003 16:19:01]
Chapter 6 - Samples
Ho w t o c o n n e c t a n in p u t s h ift re g is t e r t o a m ic ro c o n t ro lle r In o rd e r t o s im p lify t h e m a in p ro g ra m , a m a cro ca n b e u s e d fo r t h e in p u t s h ift re g is t e r. Ma cro HC5 9 7 h a s t wo a rg u m e n t s : HC5 9 7 m a cro Va r, Va r1 Va r va ria b le wh e re d a t a fro m s h ift re g is t e r in p u t p in s is t ra n s fe rre d Va r1 lo o p co u n t e r Ex a m p le : HC5 9 7 d a t a , co u n t e r Da t a fro m t h e in p u t p in s o f t h e s h ift re g is t e r is s t o re d in d a t a va ria b le . Tim e r/ co u n t e r va ria b le is u s e d a s a lo o p co u n t e r. Ma cro lis t in g :
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_08Poglavlje.htm (2 of 7) [4/2/2003 16:19:01]
Chapter 6 - Samples
Exa m p le o f h o w t o u s e t h e HC5 9 7 m a cro is g ive n in t h e fo llo win g p ro g ra m . Pro g ra m re ce ive s d a t a fro m a p a ra lle l in p u t o f t h e s h ift re g is t e r a n d m o ve s it s e ria lly in t o t h e RX va ria b le o f t h e m icro co n t ro lle r. LEDs co n n e ct e d t o p o rt B will in d ica t e t h e re s u lt o f t h e d a t a in p u t .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_08Poglavlje.htm (3 of 7) [4/2/2003 16:19:01]
Chapter 6 - Samples
Ou t p u t s h ift re g is t e r Ou t p u t s h ift re g is t e rs t ra n s fo rm s e ria l d a t a in t o p a ra lle l d a t a . On e ve ry ris in g e d g e o f t h e clo ck, t h e s h ift re g is t e r re a d s t h e va lu e fro m d a t a lin e , s t o re s it in t e m p o ra ry re g is t e r, a n d t h e n re p e a t s t h is cycle 8 t im e s . On a s ig n a l fro m 'la t ch ' lin e , d a t a is co p ie d fro m t h e s h ift re g is t e r t o in p u t
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_08Poglavlje.htm (4 of 7) [4/2/2003 16:19:01]
Chapter 6 - Samples
re g is t e r, t h u s d a t a is t ra n s fo rm e d fro m s e ria l in t o p a ra lle l d a t a .
An o u t lin e o f t h e 7 4 HC5 9 5 s h ift re g is t e r co n n e ct io n s is s h o wn o n t h e d ia g ra m b e lo w:
Co n n e c t in g a n o u t p u t s h ift re g is t e r t o a m ic ro c o n t ro lle r
Ma cro u s e d in t h is e xa m p le is fo u n d in h c5 9 5 . in c file , a n d is ca lle d HC5 9 5 . Ma cro HC5 9 5 h a s t wo a rg u m e n t s : HC5 9 5 m a cro Va r, Va r1 Va r va ria b le wh o s e co n t e n t s is t ra n s fe rre d t o o u t p u t s o f s h ift re g is t e r. Va r1 lo o p co u n t e r Ex a m p le : HC5 9 5 Da t a , co u n t e r Th e d a t a we wa n t t o t ra n s fe r is s t o re d in d a t a va ria b le , a n d co u n t e r va ria b le is u s e d a s a lo o p co u n t e r.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_08Poglavlje.htm (5 of 7) [4/2/2003 16:19:01]
Chapter 6 - Samples
An e xa m p le o f h o w t o u s e t h e HC5 9 5 m a cro is g ive n in t h e fo llo win g p ro g ra m . Da t a fro m va ria b le TX is s e ria lly t ra n s fe rre d t o s h ift re g is t e r. LEDs co n n e ct e d t o t h e p a ra lle l o u t p u t o f t h e s h ift re g is t e r will in d ica t e t h e s t a t e o f t h e lin e s . In t h is e xa m p le va lu e 0 xCB ( 1 1 0 0 1 0 1 1 ) is s e n t s o t h a t t h e e ig h t h , s e ve n t h , fo u rt h , s e co n d a n d firs t LEDs a re illu m in a t e d .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_08Poglavlje.htm (6 of 7) [4/2/2003 16:19:01]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_08Poglavlje.htm (7 of 7) [4/2/2003 16:19:01]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
S e v e n - S e g m e n t D is p la y ( m u lt ip le x in g ) Th e s e g m e n t s in a 7 - s e g m e n t d is p la y a re a rra n g e d t o fo rm a s in g le d ig it fro m 0 t o F a s s h o wn in t h e a n im a t io n :
We ca n d is p la y a m u lt i- d ig it n u m b e r b y co n n e ct in g a d d it io n a l d is p la ys . Eve n t h o u g h LCD d is p la ys a re m o re co m fo rt a b le t o wo rk wit h , 7 - s e g m e n t d is p la ys a re s t ill s t a n d a rd in t h e in d u s t ry. Th is is d u e t o t h e ir t e m p e ra t u re ro b u s t n e s s , vis ib ilit y a n d wid e vie win g a n g le . S e g m e n t s a re m a rke d wit h n o n - ca p it a l le t t e rs : a , b , c, d , e , f, g a n d d p , wh e re d p is t h e d e cim a l p o in t . Th e 8 LEDs in s id e e a ch d is p la y ca n b e a rra n g e d wit h a co m m o n ca t h o d e o r co m m o n a n o d e . Wit h a co m m o n ca t h o d e d is p la y, t h e co m m o n ca t h o d e m u s t b e co n n e ct e d t o t h e 0 V ra il a n d t h e LEDs a re t u rn e d o n wit h a lo g ic o n e . Co m m o n a n o d e d is p la ys m u s t h a ve t h e co m m o n a n o d e co n n e ct e d t o t h e + 5 V ra il. Th e s e g m e n t s a re t u rn e d o n wit h a lo g ic ze ro . Th e s ize o f a d is p la y is m e a s u re d in m illim e t e rs , t h e h e ig h t o f t h e d ig it it s e lf ( n o t t h e h o u s in g , b u t t h e d ig it !) . Dis p la ys a re a va ila b le wit h a d ig it h e ig h t o f 7 , 1 0 , 1 3 . 5 , 2 0 , o r 2 5 m illim e t e rs . Th e y co m e in d iffe re n t co lo rs , in clu d in g : re d , o ra n g e , a n d g re e n . Th e s im p le s t wa y t o d rive a d is p la y is via a d is p la y d rive r. Th e s e a re a va ila b le fo r u p t o 4 d is p la ys . Alt e rn a t ive ly d is p la ys ca n b e d rive n b y a m icro co n t ro lle r a n d if m o re t h a n o n e d is p la y is re q u ire d , t h e m e t h o d o f d rivin g t h e m is ca lle d "m u lt ip le xin g . " Th e m a in d iffe re n ce b e t we e n t h e t wo m e t h o d s is t h e n u m b e r o f "d rive lin e s . " A s p e cia l d rive r m a y n e e d o n ly a s in g le "clo ck" lin e a n d t h e d rive r ch ip will a cce s s a ll t h e s e g m e n t s a n d in cre m e n t t h e d is p la y. If a s in g le d is p la y is t o b e d rive n fro m a m icro co n t ro lle r, 7 lin e s will b e n e e d e d p lu s o n e fo r t h e d e cim a l p o in t . Fo r e a ch a d d it io n a l d is p la y, o n ly o n e e xt ra lin e is n e e d e d . To p ro d u ce a 4 , 5 o r 6 d ig it d is p la y, a ll t h e 7 - s e g m e n t d is p la ys a re co n n e ct e d in p a ra lle l. Th e co m m o n lin e ( t h e co m m o n - ca t h o d e lin e ) is t a ke n o u t s e p a ra t e ly a n d t h is lin e is t a ke n lo w fo r a s h o rt p e rio d o f t im e t o t u rn o n t h e d is p la y. Ea ch d is p la y is t u rn e d o n a t a ra t e a b o ve 1 0 0 t im e s p e r s e co n d , a n d it will a p p e a r t h a t a ll t h e d is p la ys a re t u rn e d o n a t t h e s a m e t im e . As e a ch d is p la y is t u rn e d o n , t h e a p p ro p ria t e in fo rm a t io n m u s t b e d e live re d t o it s o t h a t it will g ive t h e co rre ct re a d in g . Up t o 6 d is p la ys ca n b e a cce s s e d like t h is wit h o u t t h e b rig h t n e s s o f e a ch d is p la y b e in g a ffe ct e d . Ea ch d is p la y is t u rn e d o n ve ry h a rd fo r o n e - s ixt h t h e t im e a n d t h e POV ( p e rs is t e n ce o f vis io n ) o f o u r e ye t h in ks t h e d is p la y is t u rn e d o n t h e wh o le t im e . All t h e t im in g s ig n a ls fo r t h e d is p la y a re p ro d u ce d b y t h e p ro g ra m , t h e a d va n t a g e o f a m icro co n t ro lle r d rivin g t h e d is p la y is fle xib ilit y. Th e d is p la y ca n b e co n fig u re d a s a n u p - co u n t e r, d o wn - co u n t e r, a n d ca n p ro d u ce a n u m b e r o f m e s s a g e s u s in g le t t e rs o f t h e a lp h a b e t t h a t ca n b e re a d ily d is p la ye d . Th e e xa m p le b e lo w s h o ws h o w t o d ive t wo d is p la ys .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_09Poglavlje.htm (1 of 5) [4/2/2003 16:19:03]
Chapter 6 - Samples
Co n n e c t in g a m ic ro c o n t ro lle r t o 7 - s e g m e n t d is p la y s in m u lt ip le x m o d e
File Le d . in c co n t a in s t wo m a cro s : LED_ In it a n d LED_ Dis p 2 . Th e firs t m a cro is u s e d fo r d is p la y in it ia liza t io n . Th a t is wh e re d is p la y re fre s h m e n t p e rio d is d e fin e d a s we ll a s m icro co n t ro lle r p in s u s e d fo r co n n e ct in g t h e d is p la ys . Th e s e co n d m a cro is u s e d fo r d is p la yin g n u m b e rs fro m 0 t o 9 9 o n t wo d is p la ys . Ma cro LED_ Dis p 2 h a s o n e a rg u m e n t : LED_ Dis p 2 firs t m a cro firs t is t h e n u m b e r fro m 0 t o 9 9 t o b e d is p la ye d o n Ms d a n d Ls d d ig it . Ex a m p le : LED_ Dis p 1 2 0 x3 4 Nu m b e r 3 4 will b e s h o wn o n t h e d is p la y Re a liza t io n o f a m a cro is g ive n in t h e fo llo win g lis t in g .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_09Poglavlje.htm (2 of 5) [4/2/2003 16:19:03]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_09Poglavlje.htm (3 of 5) [4/2/2003 16:19:03]
Chapter 6 - Samples
Th e fo llo win g e xa m p le s h o ws t h e u s e o f m a cro s in a p ro g ra m . Pro g ra m d is p la ys n u m b e r '2 1 ' in t wo 7 - s e g m e n t d ig it s .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_09Poglavlje.htm (4 of 5) [4/2/2003 16:19:03]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_09Poglavlje.htm (5 of 5) [4/2/2003 16:19:03]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
LCD D is p la y Mo re m icro co n t ro lle r d e vice s a re u s in g 's m a rt LCD' d is p la ys t o o u t p u t vis u a l in fo rm a t io n . Th e fo llo win g d is cu s s io n co ve rs t h e co n n e ct io n o f a Hit a c h i LCD d is p la y t o a PIC m icro co n t ro lle r. LCD d is p la ys d e s ig n e d a ro u n d Hit a ch i's LCD HD4 4 7 8 0 m o d u le , a re in e xp e n s ive , e a s y t o u s e , a n d it is e ve n p o s s ib le t o p ro d u ce a re a d o u t u s in g t h e 8 x 8 0 p ixe ls o f t h e d is p la y. Hit a ch i LCD d is p la ys h a ve a s t a n d a rd AS CII s e t o f ch a ra ct e rs p lu s Ja p a n e s e , Gre e k a n d m a t h e m a t ica l s ym b o ls .
A 1 6 x 2 lin e Hit a c h i HD 4 4 7 8 0 d is p la y
Ea ch o f t h e 6 4 0 p ixe ls o f t h e d is p la y m u s t b e a cce s s e d in d ivid u a lly a n d t h is is d o n e wit h a n u m b e r o f s u rfa ce - m o u n t d rive r/ co n t ro lle r ch ip s m o u n t e d o n t h e b a ck o f t h e d is p la y. Th is s a ve s a n e n o rm o u s a m o u n t o f wirin g a n d co n t ro llin g s o t h a t o n ly a fe w lin e s a re re q u ire d t o a cce s s t h e d is p la y t o t h e o u t s id e wo rld . We ca n co m m u n ica t e t o t h e d is p la y via a n 8 - b it d a t a b u s o r 4 - b it da ta bus. Fo r a 8 - b it d a t a b u s , t h e d is p la y re q u ire s a + 5 V s u p p ly p lu s 1 1 I/ O lin e s . Fo r a 4 - b it d a t a b u s it o n ly re q u ire s t h e s u p p ly lin e s p lu s s e ve n e xt ra lin e s . Wh e n t h e LCD d is p la y is n o t e n a b le d , d a t a lin e s a re t ri- s t a t e wh ich m e a n s t h e y a re in a s t a t e o f h ig h im p e d a n ce ( a s t h o u g h t h e y a re d is co n n e ct e d ) a n d t h is m e a n s t h e y d o n o t in t e rfe re wit h t h e o p e ra t io n o f t h e m icro co n t ro lle r wh e n t h e d is p la y is n o t b e in g a d d re s s e d . Th e LCD a ls o re q u ire s 3 "co n t ro l" lin e s fro m t h e m icro co n t ro lle r. Th e En a b le ( E) lin e a llo ws a cce s s t o t h e d is p la y t h ro u g h R/ W a n d RS lin e s . Wh e n t h is lin e is lo w, t h e LCD is d is a b le d a n d ig n o re s s ig n a ls fro m R/ W a n d RS . Wh e n ( E) lin e is h ig h , t h e LCD ch e cks t h e s t a t e o f t h e t wo co n t ro l lin e s a n d re s p o n d s a cco rd in g ly. Th e Re a d . W rit e ( R/ W) lin e d e t e rm in e s t h e d ire ct io n o f d a t a b e t we e n t h e LCD a n d m icro co n t ro lle r. Wh e n it is lo w, d a t a is writ t e n t o t h e LCD. Wh e n it is h ig h , d a t a is re a d fro m t h e LCD. Wit h t h e h e lp o f t h e Re g is t e r s e le c t ( RS ) lin e , t h e LCD in t e rp re t s t h e t yp e o f d a t a o n d a t a lin e s . Wh e n it is lo w, a n in s t ru ct io n is b e in g writ t e n t o t h e LCD. Wh e n it is h ig h , a ch a ra ct e r is b e in g writ t e n t o t h e LCD. Lo g ic s t a t u s o n co n t ro l lin e s : E
0 Acce s s t o LCD d is a b le d 1 Acce s s t o LCD e n a b le d
R/ W 0 Writ in g d a t a t o LCD 1 Re a d in g d a t a fro m LCD RS
0 In s t ru ct io n 1 Ch a ra ct e r
Writ in g d a t a t o t h e LCD is d o n e in s e ve ra l s t e p s : S e t R/ W b it t o lo w S e t RS b it t o lo g ic 0 o r 1 ( in s t ru ct io n o r ch a ra ct e r) S e t d a t a t o d a t a lin e s ( if it is writ in g ) S e t E lin e t o h ig h S e t E lin e t o lo w Re a d d a t a fro m d a t a lin e s ( if it is re a d in g )
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (1 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
Re a d in g d a t a fro m t h e LCD is d o n e in t h e s a m e wa y, b u t co n t ro l lin e R/ W h a s t o b e h ig h . Wh e n we s e n d a h ig h t o t h e LCD, it will re s e t a n d wa it fo r in s t ru ct io n s . Typ ica l in s t ru ct io n s s e n t t o LCD d is p la y a ft e r a re s e t a re : t u rn in g o n a d is p la y, t u rn in g o n a cu rs o r a n d writ in g ch a ra ct e rs fro m le ft t o rig h t . Wh e n t h e LCD is in it ia lize d , it is re a d y t o co n t in u e re ce ivin g d a t a o r in s t ru ct io n s . If it re ce ive s a ch a ra ct e r, it will writ e it o n t h e d is p la y a n d m o ve t h e cu rs o r o n e s p a ce t o t h e rig h t . Th e Cu rs o r m a rks t h e n e xt lo ca t io n wh e re a ch a ra ct e r will b e writ t e n . Wh e n we wa n t t o writ e a s t rin g o f ch a ra ct e rs , firs t we n e e d t o s e t u p t h e s t a rt in g a d d re s s , a n d t h e n s e n d o n e ch a ra ct e r a t a t im e . Ch a ra ct e rs t h a t ca n b e s h o wn o n t h e d is p la y a re s t o re d in d a t a d is p la y ( DD) RAM. Th e s ize o f DDRAM is 8 0 b yt e s .
Th e LCD d is p la y a ls o p o s s e s s e s 6 4 b yt e s o f Ch a ra ct e rGe n e ra t o r ( CG) RAM. Th is m e m o ry is u s e d fo r ch a ra ct e rs d e fin e d b y t h e u s e r. Da t a in CG RAM is re p re s e n t e d a s a n 8 b it ch a ra ct e r b it - m a p . Ea ch ch a ra ct e r t a ke s u p 8 b yt e s o f CG RAM, s o t h e t o t a l n u m b e r o f ch a ra ct e rs , wh ich t h e u s e r ca n d e fin e is e ig h t . In o rd e r t o re a d in t h e ch a ra ct e r b it - m a p t o t h e LCD d is p la y, we m u s t firs t s e t t h e CG RAM a d d re s s t o s t a rt in g p o in t ( u s u a lly 0 ) , a n d t h e n writ e d a t a t o t h e d is p la y. Th e d e fin it io n o f a 's p e cia l' ch a ra ct e r is g ive n in t h e p ict u re .
Be fo re we a cce s s DD RAM a ft e r d e fin in g a s p e cia l ch a ra ct e r, t h e p ro g ra m m u s t s e t t h e DD RAM a d d re s s . Writ in g a n d re a d in g d a t a fro m a n y LCD m e m o ry is d o n e fro m t h e la s t a d d re s s wh ich wa s s e t u p u s in g s e t - a d d re s s in s t ru ct io n . On ce t h e a d d re s s o f DD RAM is s e t , a n e w writ t e n ch a ra ct e r will b e d is p la ye d a t t h e a p p ro p ria t e p la ce o n t h e s cre e n . Un t il n o w we d is cu s s e d t h e o p e ra t io n o f writ in g a n d re a d in g t o a n LCD a s if it we re a n o rd in a ry m e m o ry. Bu t t h is is n o t s o . Th e LCD co n t ro lle r n e e d s 4 0 t o 1 2 0 m icro s e co n d s ( u S ) fo r writ in g a n d re a d in g . Ot h e r o p e ra t io n s ca n t a ke u p t o 5 m S . Du rin g t h a t t im e , t h e m icro co n t ro lle r ca n n o t a cce s s t h e LCD, s o a p ro g ra m n e e d s t o kn o w wh e n t h e LCD is b u s y. We ca n s o lve t h is in t wo wa ys .
On e wa y is t o ch e ck t h e BUS Y b it fo u n d o n d a t a lin e D7 . Th is is n o t t h e b e s t m e t h o d b e ca u s e
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (2 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
LCD's ca n g e t s t u ck, a n d p ro g ra m will t h e n s t a y fo re ve r in a lo o p ch e ckin g t h e BUS Y b it . Th e o t h e r wa y is t o in t ro d u ce a d e la y in t h e p ro g ra m . Th e d e la y h a s t o b e lo n g e n o u g h fo r t h e LCD t o fin is h t h e o p e ra t io n in p ro ce s s . In s t ru ct io n s fo r writ in g t o a n d re a d in g fro m a n LCD m e m o ry a re s h o wn in t h e p re vio u s t a b le . At t h e b e g in n in g we m e n t io n e d t h a t we n e e d e d 1 1 I/ O lin e s t o co m m u n ica t e wit h a n LCD. Ho we ve r, we ca n co m m u n ica t e wit h a n LCD t h ro u g h a 4 - b it d a t a b u s . Th u s we ca n re d u ce t h e t o t a l n u m b e r o f co m m u n ica t io n lin e s t o s e ve n . Th e wirin g fo r co n n e ct io n via a 4 - b it d a t a b u s is s h o wn in t h e d ia g ra m b e lo w. In t h is e xa m p le we u s e a n LCD d is p la y wit h 2 x1 6 ch a ra ct e rs , la b e lle d LM1 6 X2 1 2 b y Ja p a n e s e m a ke r S HARP. Th e m e s s a g e 'ch a ra ct e r' is writ t e n in t h e firs t ro w: a n d t wo s p e cia l ch a ra ct e rs '~ ' a n d '} ' a re d is p la ye d . In t h e s e co n d ro w we h a ve p ro d u ce d t h e wo rd 'm ikro Ele kt ro n ika '.
Co n n e c t in g a n LCD d is p la y t o a m ic ro c o n t ro lle r
File LCD . in c co n t a in s a g ro u p o f m a cro s fo r u s e wh e n wo rkin g wit h LCD d is p la ys .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (3 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (4 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (5 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (6 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
Ma c ro fo r LCD s u p p o rt LCD in it m a cro u s e d t o in it ia lize p o rt co n n e ct e d t o LCD. LCD is co n fig u re d t o wo rk in fo u r- b it m ode . Ex a m p le : LCDin it LCD c h a r LCD a rg Writ e AS CII ch a ra ct e r. Arg u m e n t is AS CII ca ra ct e r. Ex a m p le : LCDCh a r 'd ' LCD w Writ e ch a ra ct e r fo u n d in W re g is t e r. Ex a m p le : m o vlw 'p ' LCDw LCD c m d LCD c o m m a n d S e n d in g co m m a n d in s t ru ct io n s Ex a m p le : LCDcm d LCDCH LCD _ D D Ad r D D Ra m Ad d re s s S e t DD RAM a d d re s s . Ex a m p le : LCD_ DDAd r . 3 LCD lin e lin e _ n u m S e t cu rs o r t o t h e b e g in n in g o f 1 s t o r 2 n d ro w Ex a m p le : LCDlin e 2 Wh e n wo rkin g wit h a m icro co n t ro lle r t h e n u m b e rs a re p re s e n t e d in a b in a ry fo rm . As s u ch , t h e y ca n n o t b e d is p la ye d o n a d is p la y. Th a t 's wh y it is n e ce s s a ry t o ch a n g e t h e n u m b e rs fro m a b in a ry s ys t e m in t o a d e cim a l s ys t e m s o t h e y ca n b e e a s ily u n d e rs t o o d . Lis t in g s o f t wo m a cro s LCD v a l_ 0 8 a n d LCD v a l_ 1 6 a re g ive n b e lo w. Ma cro LCD v a l_ 0 8 co n ve rt s a n e ig h t - b it b in a ry n u m b e r in t o a d e cim a l n u m b e r fro m 0 t o 2 5 5 a n d d is p la ys it o n t h e LCD d is p la y. It is n e ce s s a ry t o d e cla re t h e fo llo win g va ria b le s in t h e m a in p ro g ra m : TEMP1 , TEMP2 , LO, LO_ TEMP, Bch e ck. An e ig h t - b it b in a ry n u m b e r is fo u n d in va ria b le LO. Wh e n a m a cro wa s e xe cu t e d , t h e d e cim a l e q u iva le n t o f it s n u m b e r wo u ld b e d is p la ye d o n t h e LCD d is p la y. Th e le a d in g ze ro s b e fo re t h e n u m b e r will n o t b e d is p la ye d .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (7 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
Ma cro LCD v a l_ 1 6 co n ve rt s 1 6 - b it b in a ry n u m b e r in t o d e cim a l n u m b e r fro m 0 t o 6 5 5 3 5 a n d d is p la ys it o n LCD d is p la y. Th e fo llo win g va ria b le s n e e d t o b e d e cla re d in t h e m a in p ro g ra m : TEMP1 , TEMP2 , TEMP3 , LO, HI, LO_ TEMP, HI_ TEMP, Bch e ck. A 1 6 - b it b in a ry n u m b e r is fo u n d in va ria b le s LO a n d HI. Wh e n a m a cro wa s e xe cu t e d , a d e cim a l e q u iva le n t o f t h is n u m b e r wo u ld b e d is p la ye d o n LCD d is p la y. Th e le a d in g ze ro s b e fo re t h e n u m b e r wo u ld n o t b e d is p la ye d .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (8 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (9 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
Th e m a in p ro g ra m is a d e m o n s t ra t io n o f u s in g t h e LCD d is p la y a n d g e n e ra t e n e w ch a ra ct e rs . At t h e b e g in n in g o f a p ro g ra m , we n e e d t o d e cla re va ria b le s LCD b u f a n d LCD t e m p u s e d b y s u b p ro g ra m s fo r t h e LCD a s we ll a s t h e m icro co n t ro lle r p o rt co n n e ct e d t o t h e LCD. Th e p ro g ra m writ e s t h e m e s s a g e 'ch a ra ct e rs : ' o n t h e firs t ro w a n d s h o ws t wo s p e cia l ch a ra ct e rs '~ ' a n d '} '. In t h e s e co n d ro w, 'm ikro Ele kt ro n ika ' is d is p la ye d .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (10 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_10Poglavlje.htm (11 of 11) [4/2/2003 16:19:08]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
1 2 - b it An a lo g t o D ig it a l c o n v e rt e r S in ce e ve ryt h in g in t h e m icro co n t ro lle r wo rld is re p re s e n t e d wit h "0 's " a n d "1 's ", h o w d o we ca t e r fo r a s ig n a l t h a t is 0 . 5 o r 0 . 7 7 ? Mo s t o f t h e wo rld o u t s id e a co m p u t e r co n s is t s o f a n a lo g u e s ig n a ls . Ap a rt fro m s p e e ch a n d m u s ic, t h e re a re m a n y q u a n t it ie s t h a t n e e d t o b e fe d in t o a co m p u t e r. Hu m id it y, t e m p e ra t u re , a ir p re s s u re , co lo u r, t u rb id it y, a n d m e t h a n e le ve ls , a re ju s t a fe w. Th e a n s we r is t o t a ke a n u m b e r o f d ig it a l lin e s a n d co m b in e t h e m s o t h e y ca n "re a d " a n a n a lo g u e va lu e . An a n a lo g u e va lu e is a n y va lu e b e t w e e n 0 a n d 1 . Yo u ca n a ls o ca ll it a "fra ct io n a l va lu e . " All t h e a b o ve q u a n t it ie s m u s t n o w b e co n ve rt e d t o a va lu e b e t we e n 0 a n d 1 s o t h e y ca n b e fe d in t o a co m p u t e r. Th is is t h e b ro a d co n ce p t . It b e co m e s a lit t le m o re co m p le x in a p p lica t io n . If we t a ke 8 lin e s a n d a rra n g e t h a n s o t h e y a cce p t b in a ry va lu e s , t h e t o t a l co u n t will b e 2 5 6 ( t h is is o b t a in e d b y a co u n t t o 2 5 5 p lu s t h e va lu e 0 ) . If we co n n e ct t h e s e 8 lin e s in t o a "b la ck b o x, " t h e y will b e ca lle d o u t p u t lin e s a n d s o we m u s t p ro vid e a s in g le in p u t lin e . Wit h t h is a rra n g e m e n t we ca n d e t e ct u p t o 2 5 5 in cre m e n t s b e t we e n "0 " a n d "1 . " Th is b la ck b o x is ca lle d a CONVERTER a n d s in ce we a re co n ve rt in g fro m An a lo g u e t o D ig it a l, t h e co n ve rt e r is ca lle d a n A- t o - D c o n v e rt e r o r AD C. AD co n ve rt e rs ca n b e cla s s ifie d a cco rd in g t o d iffe re n t p a ra m e t e rs . Th e m o s t im p o rt a n t p a ra m e t e rs a re p re c is io n a n d m o d e o f d a t a t ra n s fe r. As t o p re cis io n , t h e ra n g e is : 8 - b it , 1 0 b it , 1 2 - b it , 1 4 - b it , 1 6 - b it . S in ce 1 2 - b it co n ve rs io n is a n in d u s t ria l s t a n d a rd , t h e e xa m p le we h a ve p ro vid e d b e lo w wa s d o n e wit h a 1 2 - b it ADC. Th e o t h e r im p o rt a n t p a ra m e t e r is t h e wa y d a t a is t ra n s fe rre d t o a m icro co n t ro lle r. It ca n b e p a ra lle l o r s e ria l. Pa ra lle l t ra n s m is s io n is fa s t e r. Ho we ve r, t h e s e co n ve rt e rs a re u s u a lly m o re e xp e n s ive . S e ria l t ra n s m is s io n is s lo we r, b u t in t e rm s o f co s t a n d fe we r in p u t lin e s t o a m icro co n t ro lle r, it is t h e fa vo u rit e fo r m a n y a p p lica t io n s . An a lo g u e s ig n a ls ca n s o m e t im e s g o a b o ve t h e a llo we d in p u t lim it o f a n ADC. Th is m a y d a m a g e t h e co n ve rt e r. To p ro t e ct t h e in p u t , t wo d io d e s a re co n n e ct e d a s s h o wn in t h e d ia g ra m . Th is will p ro t e ct fro m vo lt a g e s a b o ve 5 V a n d b e lo w 0 V. In o u r e xa m p le we u s e d a LTC1 2 8 6 1 2 - b it ADC ( Lin e a r Te ch n o lo g y) . Th e co n ve rt e r is co n n e ct e d t o t h e m icro co n t ro lle r via t h re e lin e s : d a t a , clo ck a n d CS ( Ch ip S e le ct ) . Th e CS lin e is u s e d t o s e le ct a n in p u t d e vice a s it is p o s s ib le t o co n n e ct o t h e r in p u t d e vice s ( e g : in p u t s h ift re g is t e r, o u t p u t s h ift re g is t e r, s e ria l ADC) t o t h e s a m e lin e s o f t h e m icro co n t ro lle r. Th e circu it b e lo w s h o ws h o w t o co n n e ct a n ADC, re fe re n ce a n d LCD d is p la y t o a m icro . Th e LCD d is p la y h a s b e e n a d d e d t o s h o w t h e re s u lt o f t h e AD co n ve rs io n .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.htm (1 of 6) [4/2/2003 16:19:11]
Chapter 6 - Samples
Co n n e c t in g a n AD c o n v e rt e r w it h v o lt a g e re fe re n c e t o a m ic ro c o n t ro lle r
Th e Ma cro u s e d in t h is e xa m p le is LTC8 6 a n d is fo u n d in LTC1 2 8 6 . in c file .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.htm (2 of 6) [4/2/2003 16:19:11]
Chapter 6 - Samples
Th e LTC8 6 Ma cro h a s t h re e a rg u m e n t s : LTC8 6 m a cro Va r_ LO, Va r_ HI, Va r Va r_ LO va ria b le is wh e re t h e re s u lt o f lo w e r b y t e c o n v e rs io n is s t o re d Va r_ HI va ria b le is wh e re t h e re s u lt o f h ig h e r b y t e c o n v e rs io n is s t o re d Va r lo o p co u n t e r Ex a m p le : LTC8 6 LO, HI, Co u n t Th e fo u r b it s o f t h e h ig h e s t va lu e a re in va ria b le HI , a n d firs t e ig h t b it s o f co n ve rs io n re s u lt a re in va ria b le LO. Co u n t is a n a s s is t a n t va ria b le t o co u n t t h e p a s s e s t h ro u g h lo o p s . Th e fo llo win g e xa m p le s h o ws h o w m a cro s a re u s e d in t h e p ro g ra m . Th e p ro g ra m re a d s t h e va lu e fro m a n ADC a n d d is p la ys it o n t h e LCD d is p la y. Th e re s u lt is g ive n in q u a n t u m s . Eg : fo r 0 V t h e
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.htm (3 of 6) [4/2/2003 16:19:11]
Chapter 6 - Samples
re s u lt is 0 , a n d fo r 5 V it is 4 0 9 5 .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.htm (4 of 6) [4/2/2003 16:19:11]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.htm (5 of 6) [4/2/2003 16:19:11]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_11Poglavlje.htm (6 of 6) [4/2/2003 16:19:11]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
S e ria l Co m m u n ic a t io n S CI is a n a b b re via t io n fo r S e ria l Co m m u n ica t io n In t e rfa ce a n d , a s a s p e cia l s u b s ys t e m , it e xis t s o n m o s t m icro co n t ro lle rs . Wh e n it is n o t a va ila b le , a s is t h e ca s e wit h PIC1 6 F8 4 , it ca n b e cre a t e d in s o ft wa re .
As wit h h a rd wa re co m m u n ica t io n , we u s e s t a n d a rd NRZ ( No n Re t u rn t o Ze ro ) fo rm a t a ls o kn o wn a s 8 ( 9 ) - N- 1 , o r 8 o r 9 d a t a b it s , wit h o u t p a rit y b it a n d wit h o n e s t o p b it . Fre e lin e is d e fin e d a s t h e s t a t u s o f lo g ic o n e . S t a rt o f t ra n s m is s io n - S t a rt Bit , h a s t h e s t a t u s o f lo g ic z e ro . Th e d a t a b it s fo llo w t h e s t a rt b it ( t h e firs t b it is t h e lo w s ig n ifica n t b it ) , a n d a ft e r t h e b it s we p la ce t h e S t o p Bit o f lo g ic o n e . Th e d u ra t io n o f t h e s t o p b it 'T' d e p e n d s o n t h e s p e e d o f t ra n s m is s io n a n d is a d ju s t e d a cco rd in g t o t h e n e e d s o f t h e t ra n s m is s io n . Fo r t h e t ra n s m is s io n s p e e d o f 9 6 0 0 b a u d , T is 1 0 4 u S .
1. CD (Carrier Detect) 2. RXD (Receive Data) 3. TXD (Transmit Data) 4. DTR (Data terminal Ready) 5. GND (Ground) 6. DSR (Data Set Ready) 7. RTS (Request To Send) 8. CTS (Clear To Send) 9. RI (Ring Indicator)
P in d e s ig n a t io n s o n RS 2 3 2 c o n n e c t o r In o rd e r t o co n n e ct a m icro co n t ro lle r t o a s e ria l p o rt o n a PC co m p u t e r, we n e e d t o a d ju s t t h e le ve l o f t h e s ig n a ls s o co m m u n ica t in g ca n t a ke p la ce . Th e s ig n a l le ve l o n a PC is - 1 0 V fo r lo g ic ze ro , a n d + 1 0 V fo r lo g ic o n e . S in ce t h e s ig n a l le ve l o n t h e m icro co n t ro lle r is + 5 V fo r lo g ic o n e , a n d 0 V fo r lo g ic ze ro , we n e e d a n in t e rm e d ia ry s t a g e t h a t will co n ve rt t h e le ve ls . On e ch ip s p e cia lly d e s ig n e d fo r t h is t a s k is MAX2 3 2 . Th is ch ip re ce ive s s ig n a ls fro m - 1 0 t o + 1 0 V a n d co n ve rt s t h e m in t o 0 a n d 5 V. Th e circu it fo r t h is in t e rfa ce is s h o wn in t h e d ia g ra m b e lo w:
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_12Poglavlje.htm (1 of 6) [4/2/2003 16:19:14]
Chapter 6 - Samples
Co n n e c t in g a m ic ro c o n t ro lle r t o a P C v ia a MAX2 3 2 lin e in t e rfa c e c h ip .
File RS 2 3 2 . in c co n t a in s a g ro u p o f m a cro s u s e d fo r s e ria l co m m u n ica t io n .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_12Poglavlje.htm (2 of 6) [4/2/2003 16:19:14]
Chapter 6 - Samples
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_12Poglavlje.htm (3 of 6) [4/2/2003 16:19:14]
Chapter 6 - Samples
Us in g t h e m a cro : RS 2 3 2 in it Ma cro fo r in it ia lizin g RB0 p in a n d lin e fo r t ra n s m it t in g d a t a ( TX- p in ) . Ex a m p le : RS 2 3 2 in it S EN D S _ s t rin g S e n d in g AS CII ch a ra ct e r. Arg u m e n t is AS CII s ig n . Ex a m p le : S END 'g ' S EN D w S e n d in g d a t a fo u n d in W re g is t e r. Ex a m p le : m o vlw 't ' S ENDw RECEI VE m a cro in in t e rru p t ro u t in e re ce ive s d a t a fo r RS 2 3 2 a n d s t o re s it in RXD re g is t e r Ex a m p le :
At t h e b e g in n in g o f t h e m a in p ro g ra m , we n e e d t o d e cla re va ria b le s RS _ TEMP1 , RE_ TEMP2 , TXD, RXD a n d TX p in o n m icro co n t ro lle r. Aft e r re s e t t in g a m icro co n t ro lle r t h e p ro g ra m s e n d s a g re e t in g m e s s a g e t o PC co m p u t e r: $ P I V1 6 F8 4 o n lin e $ , a n d is re a d y t o re ce ive d a t a fro m RX lin e . We ca n s e n d a n d re ce ive d a t a fro m PC co m p u t e r fro m s o m e co m m u n ica t io n p ro g ra m . Wh e n m icro co n t ro lle r re ce ive s d a t a , it will s e n d a m e s s a g e : Ch a ra ct e r re ce ive d fro m PIC1 6 F8 4 : x, t h u s co n firm in g t h a t re ce p t io n wa s s u cce s s fu l.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_12Poglavlje.htm (4 of 6) [4/2/2003 16:19:14]
Chapter 6 - Samples
Ma in p ro g ra m :
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_12Poglavlje.htm (5 of 6) [4/2/2003 16:19:14]
Chapter 6 - Samples
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/6_12Poglavlje.htm (6 of 6) [4/2/2003 16:19:14]
Appendix A - Instruction Set
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
Ap p e n d ix A I n s t ru c t io n S e t
I n t ro d u c t io n Ap p e n d ix co n t a in s a ll in s t ru ct io n s p re s e n t e d s e p a ra t e ly wit h e xa m p le s fo r t h e ir u s e . S yn t a x, d e s crip t io n a n d it s e ffe ct s o n s t a t u s b it s a re g ive n fo r e a ch in s t ru ct io n . ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●
A. 1 MOVLW A. 2 MOVWF A. 3 MOVF A. 4 CLRW A. 5 CLRF A. 6 S WAPF A. 7 ADDLW A. 8 ADDWF A. 9 S UBLW A. 1 0 S UBWF A. 1 1 ANDLW A. 1 2 ANDWF A. 1 3 IORLW A. 1 4 IORWF A. 1 5 XORLW A. 1 6 XORWF A. 1 7 INCF A. 1 8 DECF A. 1 9 RLF A. 2 0 RRF A. 2 1 COMF A. 2 2 BCF A. 2 3 BS F A. 2 4 BTFS C A. 2 5 BTFS S A. 2 6 INCFS Z A. 2 7 DECFS Z A. 2 8 GOTO A. 2 9 CALL A. 3 0 RETURN A. 3 1 RETLW A. 3 2 RETFIE A. 3 3 NOP A. 3 4 CLRWDT
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (1 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set ●
A. 3 5 S LEEP
A. 1 MOVLW
A. 2 MOVWF
A. 3 MOVF
Writ e co n s t a n t in W re g is t e r
Co p y W t o f
Co p y f t o d
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (2 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 4 CLRW
Writ e 0 in W
A. 5 Writ e 0 in f
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (3 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 6 S WAPF
Co p y t h e n ib b le s fro m f t o d cro s s wis e
A. 7 ADDLW
Ad d W t o a co n s t a n t
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (4 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 8 ADDWF
Ad d W t o f
A. 9 S UBLW
S u b t ra ct W fro m a co n s t a n t
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (5 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 1 0 S UBWF
S u b t ra ct W fro m f
A. 1 1 ANDLW
Lo g ic AND W wit h co n s t a n t
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (6 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 1 2 ANDWF
A. 1 3 IORLW
Lo g ic AND W wit h f
Lo g ic OR W wit h co n s t a n t
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (7 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 1 4 IORWF
Lo g ic OR W wit h f
A. 1 5 XORLW
Lo g ic e xclu s ive OR W wit h co n s t a n t
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (8 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 1 6 XORWF
A. 1 7 INCF
Lo g ic e xclu s ive OR W wit h f
In cre m e n t f
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (9 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 1 8 DECF
De cre m e n t f
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (10 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 1 9 RLF
Ro t a t e f t o t h e le ft t h ro u g h CARRY
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (11 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 2 0 RRF
Ro t a t e f t o t h e rig h t t h ro u g h CARRY
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (12 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 2 1 COMF
Co m p le m e n t f
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (13 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 2 2 BCF
Re s e t b it b in f
A. 2 3 BS F
S e t b it b in f
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (14 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 2 4 BTFS C
Te s t b it b in f, s kip if it = 0
A. 2 5 BTFS S
Te s t b it b in f, s kip if = 1
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (15 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 2 6 INCFS Z
In cre m e n t f, s kip if= 0
A. 2 7 DECFS Z
De cre m e n t f, s kip if = 0
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (16 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 2 8 GOTO
A. 2 9 CALL
Ju m p t o a d d re s s
Ca ll a p ro g ra m
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (17 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 3 0 RETURN
Re t u rn fro m a s u b p ro g ra m
A. 3 1 RETLW Re t u rn fro m a s u b p ro g ra m wit h co n s t a n t in W
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (18 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 3 2 RETFIE
A. 3 3 NOP
Re t u rn fro m in t e rru p t ro u t in e
No o p e ra t io n
A. 3 4 CLRWDT
In it ia lize wa t ch d o g t im e r
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (19 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set
A. 3 5 S LEEP
P re v io u s p a g e
Sta nd by m ode
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (20 of 21) [4/2/2003 16:19:25]
Appendix A - Instruction Set © Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/A_Dodatak.htm (21 of 21) [4/2/2003 16:19:25]
Appendix B - Numeric Systems
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
Ap p e n d ix B N u m e ric S y s t e m s In t ro d u ct io n B. 1 De cim a l n u m e ric s ys t e m B. 2 Bin a ry n u m e ric s ys t e m B. 3 He xa d e cim a l n u m e ric s ys t e m Co n clu s io n
I n t ro d u c t io n It wa s a lwa ys d ifficu lt fo r p e o p le t o a cce p t t h e fa ct t h a t s o m e t h in g s d iffe r fro m t h e m o r t h e ir wa y o f t h in kin g . Th a t is p ro b a b ly o n e o f t h e re a s o n s wh y n u m e ric s ys t e m s wh ich d iffe r fro m a d e cim a l a re s t ill h a rd t o u n d e rs t a n d . S t ill, wh e t h e r we wa n t it o r n o t , re a lit y is d iffe re n t . De cim a l n u m e ric s ys t e m t h a t p e o p le u s e in e ve ryd a y life is s o fa r b e h in d t h e b in a ry s ys t e m u s e d b y m illio n s o f co m p u t e rs a ro u n d t h e wo rld . Ea ch n u m e ric s ys t e m a re b a s e d o n s o m e b a s is . Wit h a d e cim a l n u m e ric s ys t e m , t h a t b a s is is 1 0 , wit h b in a ry 2 , a n d wit h a h e xa d e cim a l s ys t e m 1 6 . Th e va lu e o f e a ch d e cim a l is d e t e rm in e d b y it s p o s it io n in re la t io n t o t h e wh o le n u m b e r re p re s e n t e d in t h e g ive n n u m e ric s ys t e m . Th e s u m o f va lu e s o f e a ch d e cim a l g ive s t h e va lu e o f t h e wh o le n u m b e r. Bin a ry a n d h e xa d e cim a l n u m e ric s ys t e m s a re e s p e cia lly in t e re s t in g fo r t h e s u b je ct o f t h is b o o k. Be s id e t h e s e , we will a ls o d is cu s s a d e cim a l s ys t e m , in o rd e r t o co m p a re it wit h t h e o t h e r t wo . Eve n t h o u g h a d e cim a l n u m e ric s ys t e m is a s u b je ct we a re we ll a cq u a in t e d wit h , we will d is cu s s it h e re b e ca u s e o f it s re la t e d n e s s t o o t h e r n u m e ric s ys t e m s .
B. 1 D e c im a l n u m e ric s y s t e m De cim a l n u m e ric s ys t e m is d e fin e d b y it s b a s is 1 0 a n d d e cim a l s p a ce t h a t is co u n t e d fro m rig h t t o le ft , a n d co n s is t s o f n u m b e rs 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 . Th a t m e a n s t h a t t h e e n d rig h t d ig it o f t h e t o t a l s u m is m u lt ip lie d b y 1 , n e xt o n e b y 1 0 , n e xt b y 1 0 0 , e t c. Ex a m p le :
http://www.mikroelektronika.co.yu/english/product/books/PICbook/B_Dodatak.htm (1 of 6) [4/2/2003 16:19:30]
Appendix B - Numeric Systems
Op e ra t io n s o f a d d it io n , s u b t ra ct io n , d ivis io n , a n d m u lt ip lica t io n in a d e cim a l n u m e ric s ys t e m a re u s e d in a wa y t h a t is a lre a d y kn o wn t o u s , s o we wo n 't d is cu s s it fu rt h e r.
B. 2 Bin a ry n u m e ric s y s t e m Bin a ry n u m e ric s ys t e m d iffe rs in m a n y a s p e ct s fro m t h e d e cim a l s ys t e m we a re u s e d t o in o u r e ve ryd a y live s . It s n u m e ric b a s is is 2 , a n d e a ch n u m b e r ca n h a ve o n ly t wo va lu e s , '1 ' o r '0 '. Bin a ry n u m e ric s ys t e m is u s e d in co m p u t e rs a n d m icro co n t ro lle rs b e ca u s e it is fa r m o re s u it a b le fo r p ro ce s s in g t h a n a d e cim a l s ys t e m . Us u a lly, b in a ry n u m b e r co n s is t s o f b in a ry d ig it s 8 , 1 6 o r 3 2 , a n d it is n o t im p o rt a n t in vie w o f t h e co n t e n t s o f o u r b o o k t o d is cu s s wh y. It will b e e n o u g h fo r n o w t o a d o p t t h is in fo rm a t io n . Ex a m p le : 1 0 0 1 1 0 1 1 b in a ry n u m b e r wit h 8 d ig it s In o rd e r t o u n d e rs t a n d t h e lo g ic o f b in a ry n u m b e rs , we will co n s id e r a n e xa m p le . Le t 's s a y t h a t we h a ve a s m a ll ch e s t wit h fo u r d ra we rs , a n d t h a t we n e e d t o t e ll s o m e o n e t o b rin g s o m e t h in g fro m o n e o f t h e d ra we rs t o u s . No t h in g is m o re s im p le , we will s a y le ft s id e , b o t t o m ( d ra we r) , a n d t h e d e s ire d d ra we r is cle a rly d e fin e d . Ho we ve r, if we h a d t o d o t h is wit h o u t t h e u s e o f in s t ru ct io n s like le ft , rig h t , b e n e a t h , a b o ve , e t c. , t h e n we wo u ld h a ve a p ro b le m . Th e re a re m a n y s o lu t io n t o t h is p ro b le m , b u t we s h o u ld lo o k fo r o n e t h a t is m o s t b e n e fice n t a n d p ra ct ica l! Le t s d e s ig n a t e ro ws wit h A, a n d t yp e s wit h B. If A= 1 , it re fe rs t o t h e u p p e r ro w o f d ra we rs , a n d fo r A= 0 , b o t t o m ro w. S im ila rly wit h co lu m n s , B= 1 re p re s e n t s t h e le ft co lu m n , a n d B= 0 , t h e rig h t ( n e xt p ict u re ) . No w it is a lre a d y e a s ie r t o e xp la in fro m wh ich d ra we r we n e e d s o m e t h in g . We s im p ly n e e d t o s t a t e o n e o f t h e fo u r co m b in a t io n s : 0 0 , 0 1 , 1 0 o r 1 1 . Th is ch a ra ct e ris t ic n a m in g o f e a ch d ra we r in d ivid u a lly is n o t h in g b u t b in a ry n u m e ric re p re s e n t a t io n , o r co n ve rs io n o f co m m o n n u m b e rs fro m a d e cim a l in t o b in a ry fo rm . In o t h e r wo rd s , re fe re n ce s like "firs t , s e co n d , t h ird a n d fo u rt h " a re e xch a n g e d wit h "0 0 , 0 1 , 1 0 a n d 1 1 ".
Wh a t re m a in s is fo r u s t o g e t a cq u a in t e d wit h lo g ic t h a t is u s e d wit h b in a ry n u m e ric s ys t e m , o r h o w t o g e t a n u m e ric va lu e fro m a s e rie s o f ze ro s a n d o n e s in a wa y we ca n u n d e rs t a n d , o f co u rs e . Th is p ro ce d u re is ca lle d co n ve rs io n fro m a b in a ry t o a d e cim a l n u m b e r. Ex a m p le :
http://www.mikroelektronika.co.yu/english/product/books/PICbook/B_Dodatak.htm (2 of 6) [4/2/2003 16:19:30]
Appendix B - Numeric Systems
As yo u ca n s e e , co n ve rt in g a b in a ry n u m b e r in t o a d e cim a l n u m b e r is d o n e b y ca lcu la t in g t h e e xp re s s io n o n t h e le ft s id e . De p e n d in g o n t h e p o s it io n in a b in a ry n u m b e r, d ig it s ca rry d iffe re n t va lu e s wh ich a re m u lt ip lie d b y t h e m s e lve s , a n d b y a d d in g t h e m we g e t a d e cim a l n u m b e r we ca n u n d e rs t a n d . Le t 's fu rt h e r s u p p o s e t h a t t h e re a re fe w m a rb le s in e a ch o f t h e d ra we rs : 2 in t h e firs t o n e , 4 in t h e s e co n d d ra we r, 7 in t h e t h ird a n d 3 in t h e fo u rt h d ra we r. Le t 's a ls o s a y t o t h e o n e wh o 's o p e n in g t h e d ra we rs t o u s e b in a ry re p re s e n t a t io n in a n s we r. Un d e r t h e s e co n d it io n s , q u e s t io n wo u ld b e a s fo llo ws : "Ho w m a n y m a rb le s a re t h e re in 0 1 ? ", a n d t h e a n s we r wo u ld b e : "Th e re a re 1 0 0 m a rb le s in 0 1 . " It s h o u ld b e n o t e d t h a t b o t h q u e s t io n a n d t h e a n s we r a re ve ry cle a r e ve n t h o u g h we d id n o t u s e t h e s t a n d a rd t e rm s . It s h o u ld fu rt h e r b e n o t e d t h a t fo r d e cim a l n u m b e rs fro m 0 t o 3 it is e n o u g h t o h a ve t wo b in a ry d ig it s , a n d t h a t fo r a ll va lu e s a b o ve t h a t we m u s t a d d n e w b in a ry d ig it s . S o , fo r n u m b e rs fro m 0 t o 7 it is e n o u g h t o h a ve t h re e d ig it s , fo r n u m b e rs fro m 0 t o 1 5 , fo u r, e t c. S im p ly s a id , t h e b ig g e s t n u m b e r t h a t ca n b e re p re s e n t e d b y a b in a ry d ig it is t h e o n e o b t a in e d wh e n b a s is 2 is g ra d e d o n t o a n u m b e r o f b in a ry d ig it s in a b in a ry n u m b e r a n d t h u s o b t a in e d n u m b e r is d e cre m e n t e d b y o n e . Ex a m p le :
Th is m e a n s t h a t it is p o s s ib le t o re p re s e n t d e cim a l n u m b e rs fro m 0 t o 1 5 wit h 4 b in a ry d ig it s , in clu d in g n u m b e rs '0 ' a n d '1 5 ', o r 1 6 d iffe re n t va lu e s . Op e ra t io n s wh ich e xis t in d e cim a l n u m e ric s ys t e m a ls o e xis t in a b in a ry s ys t e m . Fo r re a s o n s o f cla rit y a n d le g ib ilit y, we will re vie w a d d it io n a n d s u b t ra ct io n o n ly in t h is ch a p t e r. Ba s ic ru le s t h a t a p p ly t o b in a ry a d d it io n a re :
Ad d it io n is d o n e s o t h a t d ig it s in t h e s a m e n u m e ric p o s it io n s a re a d d e d , s im ila r t o t h e d e cim a l n u m e ric s ys t e m . If b o t h d ig it s b e in g a d d e d a re ze ro , t h e ir s u m re m a in s ze ro , a n d if t h e y a re '0 ' a n d '1 ', re s u lt is '1 '. Th e s u m o f t wo o n e s g ive s t wo , in b in a ry re p re s e n t a t io n it will b e a ze ro , b u t wit h t ra n s fe rrin g '1 ' t o a h ig h e r p o s it io n t h a t is a d d e d t o d ig it s fro m t h a t p o s it io n . Ex a m p le :
http://www.mikroelektronika.co.yu/english/product/books/PICbook/B_Dodatak.htm (3 of 6) [4/2/2003 16:19:30]
Appendix B - Numeric Systems
We ca n ch e ck wh e t h e r re s u lt is co rre ct b y t ra n s fe rrin g t h e s e n u m b e r t o d e cim a l n u m e ric s ys t e m a n d b y p e rfo rm in g a d d it io n in it . Wit h a t ra n s fe r we g e t a va lu e 1 0 a s t h e firs t n u m b e r, va lu e 9 a s t h e s e co n d , a n d va lu e 1 9 a s t h e s u m . Th u s we h a ve p ro ve n t h a t o p e ra t io n wa s d o n e co rre ct ly. Tro u b le co m e s wh e n s u m is g re a t e r t h a n wh a t ca n b e re p re s e n t e d b y a b in a ry n u m b e r wit h a g ive n n u m b e r o f b in a ry d ig it s . Diffe re n t s o lu t io n s ca n b e a p p lie d t h e n , o n e o f wh ich is e xp a n d in g t h e n u m b e r o f b in a ry d ig it s in t h e s u m a s in t h e p re vio u s e xa m p le . S u b t ra ct io n , like a d d it io n is d o n e o n t h e s a m e p rin cip le . Th e re s u lt o f s u b t ra ct io n b e t we e n t wo ze ro s , o r t wo o n e s re m a in s a ze ro . Wh e n s u b t ra ct in g o n e fro m ze ro , we h a ve t o b o rro w o n e fro m b in a ry d ig it wh ich h a s a h ig h e r va lu e in t h e b in a ry n u m b e r. Ex a m p le :
By ch e ckin g t h e re s u lt a s we d id wit h a d d it io n , wh e n we t ra n s la t e t h e s e b in a ry n u m b e rs we g e t d e cim a l n u m b e rs 1 0 a n d 9 . Th e ir d iffe re n ce co rre s p o n d s t o n u m b e r 1 wh ich is wh a t we g e t in s u b t ra ct io n .
B. 3 He x a d e c im a l n u m e ric s y s t e m He xa d e cim a l n u m e ric s ys t e m h a s a n u m b e r 1 6 a s it s b a s is . S in ce t h e b a s is o f a n u m e ric s ys t e m is 1 6 , t h e re a re 1 6 d iffe re n t d ig it s t h a t ca n b e fo u n d in a h e xa d e cim a l n u m b e r. Th o s e d ig it s a re "0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , A, B, C, D, E, F". Le t t e rs A, B, C, D, E a n d F a re n o t h in g b u t va lu e s 1 0 , 1 1 , 1 2 , 1 3 , 1 4 a n d 1 5 . Th e y a re in t ro d u ce d a s a re p la ce m e n t t o m a ke writ in g e a s ie r. As wit h a b in a ry s ys t e m , h e re t o o , we ca n d e t e rm in e wit h s a m e fo rm u la wh a t is t h e b ig g e s t d e cim a l n u m b e r we ca n re p re s e n t wit h a s p e cific n u m b e r o f h e xa d e cim a l d ig it s . Ex a m p le : W it h t w o h e x a d e c im a l d ig it s
Us u a lly, h e xa d e cim a l n u m b e r is writ t e n wit h a p re fix "$ " o r "0 x" , o r s u ffix"h " , t o e m p h a s ize t h e n u m e ric s ys t e m . Th u s , n u m b e r A3 7 E wo u ld b e writ t e n m o re co rre ct ly a s $ A3 7 E, 0 xA3 7 E, o r A3 7 Eh . In o rd e r t o t ra n s la t e a h e xa d e cim a l n u m b e r in t o a b in a ry n u m e ric s ys t e m it is n o t n e ce s s a ry t o p e rfo rm a n y ca lcu la t io n b u t s im p le e xch a n g e o f h e xa d e cim a l d ig it s wit h b in a ry d ig it s . S in ce t h e m a xim u m va lu e o f a h e xa d e cim a l n u m b e r is 1 5 , t h a t m e a n s t h a t it is e n o u g h t o u s e 4 b in a ry d ig it s fo r o n e h e xa d e cim a l d ig it . Ex a m p le :
By ch e ckin g , t h a t is t ra n s fe rrin g b o t h n u m b e rs in t o d e cim a l n u m e ric s ys t e m , we g e t a n u m b e r 2 2 8
http://www.mikroelektronika.co.yu/english/product/books/PICbook/B_Dodatak.htm (4 of 6) [4/2/2003 16:19:30]
Appendix B - Numeric Systems
wh ich p ro ve s t h e a ccu ra cy o f o u r a ct io n . In o rd e r t o g e t a d e cim a l e q u iva le n t o f a h e xa d e cim a l n u m b e r, we n e e d t o m u lt ip ly e a ch d ig it o f a n u m b e r wit h n u m b e r 1 6 wh ich is g ra d a t e d b y t h e p o s it io n o f t h a t d ig it in h e xa d e cim a l n u m b e r. Ex a m p le :
Ad d it io n is , like in t wo p re ce d in g e xa m p le s , p e rfo rm e d in a s im ila r m a n n e r. Ex a m p le :
We n e e d t o a d d co rre s p o n d in g n u m b e r d ig it s . If t h e ir s u m is e q u a l 1 6 , writ e 0 a n d t ra n s fe r o n e t o t h e n e xt h ig h e r p la ce . If t h e ir s u m is g re a t e r t h a n 1 6 , writ e va lu e a b o ve a n d t ra n s fe r 1 t o t h e n e xt h ig h e r d ig it . Eg . if s u m is 1 9 ( 1 9 = 1 6 + 3 ) writ e 3 a n d t ra n s fe r 1 t o t h e n e xt h ig h e r p la ce . By ch e ckin g , we g e t 1 4 8 9 1 a s t h e firs t n u m b e r, a n d s e co n d is 4 3 4 5 7 . Th e ir s u m is 5 8 3 4 8 , wh ich is a n u m b e r $ E3 EC wh e n it is t ra n s fe rre d in t o a d e cim a l n u m e ric s ys t e m . S u b t ra ct io n is a n id e n t ica l p ro ce s s t o p re vio u s t wo n u m e ric s ys t e m s . If t h e n u m b e r we a re s u b t ra ct in g is s m a lle r, we b o rro w fro m t h e n e xt p la ce o f h ig h e r va lu e . Ex a m p le :
By ch e ckin g t h is re s u lt , we g e t va lu e s 1 1 5 9 0 fo r t h e firs t n u m b e r a n d 5 9 7 0 fo r t h e s e co n d , wh e re t h e ir d iffe re n ce is 5 6 2 0 , wh ich co rre s p o n d s t o a n u m b e r $ 1 5 F4 a ft e r a t ra n s fe r in t o a d e cim a l n u m e ric s ys t e m .
Co n c lu s io n Bin a ry n u m e ric s ys t e m is s t ill t h e o n e t h a t is m o s t in u s e , d e cim a l t h e o n e t h a t 's e a s ie s t t o u n d e rs t a n d , a n d a h e xa d e cim a l is s o m e wh e re b e t we e n t h o s e t wo s ys t e m s . It s e a s y co n ve rs io n t o a b in a ry n u m e ric s ys t e m a n d e a s y m e m o riza t io n m a ke it , a lo n g wit h b in a ry a n d d e cim a l s ys t e m s , o n e o f t h e m o s t im p o rt a n t n u m e ric s ys t e m s .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/B_Dodatak.htm (5 of 6) [4/2/2003 16:19:30]
Appendix B - Numeric Systems
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/B_Dodatak.htm (6 of 6) [4/2/2003 16:19:30]
Ne x t pa g e
Appendix C - Glossary
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
Ne x t pa g e
Ap p e n d ix C Glo s s a ry In t ro d u ct io n ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●
Micro co n t ro lle r I/ O p in S o ft wa re Ha rd wa re S im u la t o r ICE EPROM Em u la t o r As s e m b le r HEX file Lis t file S o u rce File De b u g g in g ROM, EPROM, EEPROM, FLAS H, RAM Ad d re s s in g AS CII Ca rry Co d e Byt e , Kilo b yt e , Me g a b yt e Fla g In t e rru p t ve ct o r o r in t e rru p t s Pro g ra m m e r Pro d u ct
I n t ro d u c t io n S in ce a ll t h e fie ld s o f m a n 's a ct ivit y a re re g u la rly b a s e d o n a d e q u a t e a n d a lre a d y a d o p t e d t e rm s ( t h ro u g h wh ich o t h e r n o t io n s a n d d e fin it io n s b e co m e ) , s o in t h e fie ld o f m icro co n t ro lle rs we ca n s in g le o u t s o m e fre q u e n t ly u s e d t e rm s . Id e a s a re o ft e n co n n e ct e d s o t h a t co rre ct u n d e rs t a n d in g o f o n e n o t io n is n e e d e d in o rd e r t o g e t a cq u a in t e d wit h o n e o r m o re o f t h e o t h e r id e a s .
Micro co n t ro lle r Micro p ro ce s s o r wit h p e rip h e ra ls in o n e e le ct ro n ic co m p o n e n t .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/C_Dodatak.htm (1 of 4) [4/2/2003 16:19:32]
Appendix C - Glossary
I/ O p in Ext e rn a l m icro co n t ro lle r's co n n e ct o r p in wh ich ca n b e co n fig u re d a s in p u t o r o u t p u t . In m o s t ca s e s I/ O p in e n a b le s a m icro co n t ro lle r t o co m m u n ica t e , co n t ro l o r re a d in fo rm a t io n .
S o ft wa re In fo rm a t io n t h a t m icro co n t ro lle r n e e d s in o rd e r t o b e a b le t o fu n ct io n . S o ft wa re ca n n o t h a ve a n y e rro rs if we wa n t t h e p ro g ra m a n d a d e vice t o fu n ct io n p ro p e rly. S o ft wa re ca n b e writ t e n in d iffe re n t la n g u a g e s s u ch a s : Ba s ic, C, p a s ca l o r a s s e m b le r. Ph ys ica lly, t h a t is a file o n co m p u t e r d is c.
Ha rd wa re Micro co n t ro lle r, m e m o ry, s u p p ly, s ig n a l circu it s a n d a ll co m p o n e n t s co n n e ct e d wit h m icro co n t ro lle r. Th e o t h e r wa y o f vie win g t h is ( e s p e cia lly if it 's n o t wo rkin g ) is , t h a t , h a rd wa re is s o m e t h in g yo u ca n kick.
S im u la t o r S o ft wa re p a cka g e fo r PC wh ich s im u la t e s t h e in t e rn a l fu n ct io n o f m icro co n t ro lle r. It is id e a l fo r ch e ckin g s o ft wa re ro u t in e s a n d a ll t h e p a rt s o f t h e co d e wh ich d o n o t h a ve o ve r d e m a n d in g co n n e ct io n s wit h a n o u t s id e wo rld . Op t io n s a re in s t a lle d t o wa t ch t h e co d e , m o ve m e n t a ro u n d t h e p ro g ra m b a ck a n d fo rt h s t e p b y s t e p , a n d d e b u g g in g .
ICE ICE ( In Circu it Em u la t o r) , in t e rn a l e m u la t o r, ve ry u s e fu l p a rt o f t h e e q u ip m e n t wh ich co n n e ct s a PC in s t e a d o f m icro co n t ro lle r o n a d e vice t h a t is b e in g d e ve lo p e d . It e n a b le s s o ft wa re t o fu n ct io n o n t h e PC co m p u t e r, b u t t o a p p e a r a s if a re a l m icro co n t ro lle r e xis t s in t h e d e vice . ICE e n a b le s yo u t o m o ve t h ro u g h p ro g ra m in re a l t im e , t o s e e wh a t is g o in g o n in t h e m icro co n t ro lle r a n d h o w it co m m u n ica t e s wit h a n o u t s id e wo rld .
EPROM Em u la t o r EPROM Em u la t o r is a d e vice wh ich d o e s n o t e m u la t e t h e e n t ire m icro co n t ro lle r like ICE e m u la t o r, b u t it o n ly e m u la t e s it s m e m o ry. It is m o s t ly u s e d in m icro co n t ro lle rs t h a t h a ve e xt e rn a l m e m o ry. By u s in g it we a vo id co n s t a n t e ra s in g a n d writ in g o f EPROM m e m o ry.
As s e m b le r S o ft wa re p a cka g e wh ich t ra n s la t e s s o u rce co d e in t o a co d e wh ich m icro co n t ro lle r ca n u n d e rs t a n d . It co n t a in s a s e ct io n fo r d is co ve rin g e rro rs . Th is p a rt is u s e d wh e n we d e b u g a p ro g ra m fro m e rro rs m a d e wh e n p ro g ra m wa s writ t e n .
HEX file Th is is a file m a d e b y a s s e m b le r t ra n s la t o r wh e n t ra n s la t in g a s o u rce file , a n d h a s a fo rm "u n d e rs t o o d " b y m icro co n t ro lle rs . A co n t in u a t io n o f t h e file is u s u a lly File _ n a m e . HEX wh e re t h e n a m e HEX file co m e s fro m .
Lis t file Th is is a file m a d e b y a s s e m b le r t ra n s la t o r a n d it co n t a in s a ll in s t ru ct io n s fro m s o u rce file wit h a d d re s s e s a n d co m m e n t s p ro g ra m m e r h a s writ t e n . Th is is a ve ry u s e fu l file fo r ke e p in g t ra ck o f e rro rs in t h e p ro g ra m . File e xt e n s io n is LS T wh ich is wh e re it s n a m e co m e s fro m .
S o u rce File
http://www.mikroelektronika.co.yu/english/product/books/PICbook/C_Dodatak.htm (2 of 4) [4/2/2003 16:19:32]
Appendix C - Glossary
File writ t e n in t h e la n g u a g e u n d e rs t o o d b y m a n a n d a s s e m b le r t ra n s la t o r. By t ra n s la t in g t h e s o u rce file , we g e t HEX a n d LIS T file s .
De b u g g in g Erro r m a d e in writ in g a p ro g ra m , wh ich e rro r we a re n o t a wa re o f. Erro rs ca n b e q u it e s im p le s u ch a s t yp in g e rro rs , a n d q u it e co m p le x s u ch a s in co rre ct u s e o f p ro g ra m la n g u a g e . As s e m b le r will fin d m o s t o f t h e s e e rro rs a n d re p o rt t h e m t o '. LS T' file . Ot h e r e rro rs will n e e d t o b e s e a rch e d fo r b y t ryin g it o u t a n d wa t ch in g h o w d e vice fu n ct io n s .
ROM, EPROM, EEPROM, FLAS H, RAM Typ e s o f m e m o rie s we m e e t wit h m icro co n t ro lle r u s e . Firs t o n e ca n n o t b e e ra s e d , wh a t yo u writ e in it o n ce , s t a ys fo re ve r, a n d ca n n o t b e e ra s e d . Th e s e co n d is e ra s a b le wit h UV la m p . Th ird o n e ca n b e e ra s e d e le ct rica lly, u s in g vo lt a g e wh ich m icro co n t ro lle r o p e ra t e s o n . Fo u rt h o n e is e le ct rica lly e ra s a b le , b u t u n like EEPROM m e m o ry it d o e s n o t h a ve s u ch a g re a t n u m b e r o f cycle s o f writ in g a n d e ra s in g a t m e m o ry lo ca t io n s . Fift h o n e is fa s t , b u t it d o e s n o t h o ld b a ck t h e co n t e n t s a s t h e p re vio u s wh e n t h e re is s u p p ly s h o rt a g e . Th u s , p ro g ra m is n o t s t o re d in it , b u t it s e rve s fo r d iffe re n t va ria b le s a n d in t e r- re s u lt s .
Ad d re s s in g De t e rm in e s a n d d e s ig n a t e s ce rt a in m e m o ry lo ca t io n s .
AS CII S h o rt fo r "Am e rica n S t a n d a rd Co d e fo r In fo rm a t io n In t e rch a n g e ". It is wid e ly a cce p t e d t yp e o f co d in g wh e re e a ch n u m b e r a n d le t t e r h a ve t h e ir e ig h t - b it co d e .
Ca rry Tra n s fe r b it co n n e ct e d wit h a rit h m e t ic o p e ra t io n s
Co d e File , o r s e ct io n o f a file wh ich co n t a in s p ro g ra m in s t ru ct io n s .
Byt e , Kilo b yt e , Me g a b yt e Te rm s d e s ig n a t in g a m o u n t s o f in fo rm a t io n . Th e b a s ic u n it is a b yt e , a n d it h a s 8 b it s . Kilo b yt e h a s 1 0 2 4 b yt e s , a n d m e g a b yt e h a s 1 0 2 4 kilo b yt e s .
Fla g Bit s fro m a s t a t u s re g is t e r. By t h e ir a ct iva t io n , p ro g ra m m e r is in fo rm e d a b o u t ce rt a in a ct io n s . Pro g ra m a ct iva t e s it s re s p o n s e if n e ce s s a ry.
In t e rru p t ve ct o r o r in t e rru p t s Lo ca t io n in m icro co n t ro lle r m e m o ry. Micro co n t ro lle r t a ke s fro m t h is lo ca t io n in fo rm a t io n a b o u t a s e ct io n o f t h e p ro g ra m t h a t is t o b e e xe cu t e d a s a n a n s we r t o s o m e e ve n t o f in t e re s t t o p ro g ra m m e r a n d d e vice .
Pro g ra m m e r De vice wh ich m a ke s it p o s s ib le t o writ e s o ft wa re in m icro co n t ro lle r m e m o ry, t h u s e n a b lin g t h e m icro co n t ro lle r t o wo rk in d e p e n d e n t ly. It co n s is t s o f t h e h a rd wa re s e ct io n u s u a lly co n n e ct e d wit h o n e o f t h e p o rt s a n d s o ft wa re s e ct io n u s e d o n t h e co m p u t e r a s a p ro g ra m .
http://www.mikroelektronika.co.yu/english/product/books/PICbook/C_Dodatak.htm (3 of 4) [4/2/2003 16:19:32]
Appendix C - Glossary
Pro d u ct Pro d u ct d e ve lo p m e n t is a co m b in a t io n o f lu ck a n d e xp e rie n ce . S h o rt t e rm s , o r t im e - lim it s fo r p ro d u ct io n s h o u ld b e a vo id e d b e ca u s e e ve n wit h m o s t s im p le a s s ig n m e n t s , m u ch t im e is n e e d e d t o d e ve lo p a n d im p ro ve . Wh e n cre a t in g a p ro je ct , we n e e d t im e , q u ie t , lo g ica l m in d a n d m o s t im p o rt a n t ly, a t h o ro u g h u n d e rs t a n d in g o f co n s u m e r's n e e d s . Typ ica l co u rs e in cre a t in g a p ro d u ct wo u ld h a ve t h e fo llo win g a lg o rit h m .
P re v io u s p a g e
Ta b le o f c o n t e n t s
Ch a p t e r o v e rv ie w
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/product/books/PICbook/C_Dodatak.htm (4 of 4) [4/2/2003 16:19:32]
Ne x t pa g e