logo资料库

【51单片机学习资料】The_Final_Word_On_8051.pdf【英文书】

第1页 / 共256页
第2页 / 共256页
第3页 / 共256页
第4页 / 共256页
第5页 / 共256页
第6页 / 共256页
第7页 / 共256页
第8页 / 共256页
资料共256页,剩余部分请下载后查看
THE FINAL WORD ON THE 8051 - Introduction P a g e 1 T h i s i s a b o o k a b o u t t h e I n t e l 8 0 5 1 m i c r o c o n t r o l l e r a n d i t s l a r g e f a m i l y o f d e s c e n d a n t s . I t i s i n t e n d e d t o g i v e y o u , t h e r e a d e r , s o m e n e w t e c h n i q u e s f o r o p t i m i z i n g y o u r 8 0 5 1 p r o j e c t s a n d t h e d e v e l o p m e n t p r o c e s s y o u u s e f o r t h o s e p r o j e c t s . I t i s n o t t h e p u r p o s e o f t h i s b o o k t o p r o v i d e v a r i o u s r e c i p e s f o r d i f f e r e n t t y p e s o f e m b e d d e d p r o j e c t s . W h e r e v e r p o s s i b l e , I h a v e i n c l u d e d c o d e e x a m p l e s t o m a k e t h e d i s c u s s i o n c l e a r e r . T h e r e a r e p o i n t s i n t h e b o o k w h e r e p r o j e c t s a r e d i s c u s s e d a s a m e a n s o f i l l u s t r a t i n g t h e p o i n t o f t h e g i v e n c h a p t e r . M u c h o f t h i s c o d e i s a v a i l a b l e o n t h e c o m p a n i o n d i s k , t o u s e i t y o u w i l l n e e d t o b e f a m i l i a r w i t h C a n d 8 0 5 1 a s s e m b l e r s i n c e t h i s b o o k i s n o t i n t e n d e d t o b e a t u t o r i a l i n C o r 8 0 5 1 a s s e m b l e r . T h e r e a r e m a n y f i n e b o o k s y o u c a n b u y t o l e a r n a b o u t A N S I C . A s f o r 8 0 5 1 a s s e m b l e r , t h e b e s t s o u r c e i s t h e I n t e l d a t a b o o k w h i c h i s f r e e f r o m y o u r 8 0 5 1 v e n d o r o r t h e m a n u a l t h a t c o m e s w i t h y o u r p a r t i c u l a r a s s e m b l e r . T h e c o d e o n t h e c o m p a n i o n d i s k e t t e c o n t a i n s t h e c o d e I w r o t e a n d c o m p i l e d f o r t h e b o o k y o u h o l d i n y o u r h a n d s . I t i s f u l l y f u n c t i o n a l a n d h a s b e e n t e s t e d . T h i s i s n o t t o s a y t h a t t h a t t h e c o d e o n t h e d i s k e t t e i s r e a d y t o g o i n t o y o u r s y s t e m a n d b e d e l i v e r e d a s p a r t o f y o u r p r o j e c t s . S o m e o f i t w i l l r e q u i r e c h a n g e b e f o r e i t c a n b e i n t e g r a t e d i n t o y o u r s y s t e m . T h i s b o o k w i l l h e l p y o u l e a r n h o w t o m a k e t h e b e s t o u t o f t h e t o o l s y o u h a v e . I f y o u o n l y h a v e a n 8 0 5 1 a s s e m b l e r , y o u c a n s t i l l l e a r n f r o m t h i s b o o k a n d u s e t h e e x a m p l e s , b u t y o u w i l l h a v e t o d e c i d e f o r y o u r s e l f h o w t o i m p l e m e n t t h e C l a n g u a g e e x a m p l e s i n a s s e m b l e r . T h i s i s n o t a d i f f i c u l t t a s k f o r a n y o n e w h o u n d e r s t a n d s t h e b a s i c s o f C a n d t h e 8 0 5 1 a s s e m b l e r s e t . I f y o u h a v e a C c o m p i l e r f o r t h e 8 0 5 1 , t h e n I c o n g r a t u l a t e y o u . Y o u h a v e m a d e a n e x c e l l e n t d e c i s i o n i n y o u r u s e o f C . Y o u w i l l f i n d t h a t y o u r p r o j e c t d e v e l o p m e n t t i m e u s i n g C i s l o w e r a n d t h a t y o u r m a i n t e n a n c e t i m e u s i n g C i s a l s o l o w e r . I f y o u h a v e t h e K e i l C 5 1 p a c k a g e , t h e n y o u h a v e m a d e a n e x c e l l e n t d e c i s i o n i n 8 0 5 1 d e v e l o p m e n t t o o l s . I h a v e f o u n d t h a t t h e K e i l p a c k a g e f o r t h e 8 0 5 1 p r o v i d e s t h e b e s t s u p p o r t . T h e c o d e i n t h i s b o o k d i r e c t l y s u p p o r t s t h e K e i l C e x t e n s i o n s . I f y o u h a v e o n e o f t h e o t h e r d e v e l o p m e n t p a c k a g e s s u c h a s A r c h i m e d e s o r A v o c e t , y o u w i l l f i n d t h a t t h i s b o o k i s s t i l l o f g r e a t s e r v i c e t o y o u . T h e m a i n t h i n g t o b e a w a r e o f i s t h a t y o u m a y h a v e t o c h a n g e s o m e o f t h e K e i l s p e c i f i c d i r e c t i v e s t o t h e a p p r o p r i a t e o n e s f o r y o u r d e v e l o p m e n t t o o l s . I n m a n y p l a c e s i n t h i s b o o k a r e d i a g r a m s o f t h e h a r d w a r e o n w h i c h t h e e x a m p l e c o d e r u n s . T h e s e a r e n o t i n t e n d e d t o b e f u l l s c h e m a t i c s , b u t a r e m e r e l y b l o c k d i a g r a m s t h a t h a v e e n o u g h i n f o r m a t i o n t o a l l o w y o u t o u n d e r s t a n d h o w t h e s o f t w a r e m u s t i n t e r f a c e t o t h e h a r d w a r e . Y o u s h o u l d l o o k u p o n t h i s b o o k a s a l e a r n i n g t o o l r a t h e r t h a n a s o u r c e o f v a r i o u s s y s t e m d e s i g n s . T h i s i s n o t a n 8 0 5 1 c o o k b o o k , b u t r a t h e r a n e x p l o r a t i o n o f t h e c a p a b i l i t i e s o f t h e 8 0 5 1 g i v e n p r o p e r h a r d w a r e a n d s o f t w a r e d e s i g n . I p r e f e r t o t h i n k t h a t y o u w i l l u s e t h i s b o o k a s a s o u r c e o f i d e a s f r o m w h i c h y o u r d e s i g n s s p r i n g b o a r d a n d g r o w i n a m a r v e l o u s w o r l d o f s u n s h i n e a n d r o s e s ! S e r i o u s l y , t h o u g h , I t h i n k y o u w i l l g a i n u s e f u l k n o w l e d g e f r o m t h i s b o o k t h a t w i l l h e l p y o u g r e a t l y i m p r o v e y o u r d e s i g n s a n d m a k e y o u l o o k l i k e y o u r c o m p a n y ’ s 8 0 5 1 g u r u .
CHAPTER 2 - THE HARDWARE - The Hardware Overview • • • • • • • P a g e 2 T h e 8 0 5 1 f a m i l y o f m i c r o c o n t r o l l e r s i s b a s e d o n a n a r c h i t e c t u r e w h i c h i s h i g h l y o p t i m i z e d f o r e m b e d d e d c o n t r o l s y s t e m s . I t i s u s e d i n a w i d e v a r i e t y o f a p p l i c a t i o n s f r o m m i l i t a r y e q u i p m e n t t o a u t o m o b i l e s t o t h e k e y b o a r d o n y o u r P C . S e c o n d o n l y t o t h e M o t o r o l a 6 8 H C 1 1 i n e i g h t b i t p r o c e s s o r s s a l e s , t h e 8 0 5 1 f a m i l y o f m i c r o c o n t r o l l e r s i s a v a i l a b l e i n a w i d e a r r a y o f v a r i a t i o n s f r o m m a n u f a c t u r e r s s u c h a s I n t e l , P h i l i p s , a n d S i e m e n s . T h e s e m a n u f a c t u r e r s h a v e a d d e d n u m e r o u s f e a t u r e s a n d p e r i p h e r a l s t o t h e 8 0 5 1 s u c h a s I 2 C i n t e r f a c e s , a n a l o g t o d i g i t a l c o n v e r t e r s , w a t c h d o g t i m e r s , a n d p u l s e w i d t h m o d u l a t e d o u t p u t s . V a r i a t i o n s o f t h e 8 0 5 1 w i t h c l o c k s p e e d s u p t o 4 0 M H z a n d v o l t a g e r e q u i r e m e n t s d o w n t o 1 . 5 v o l t s a r e a v a i l a b l e . T h i s w i d e r a n g e o f p a r t s b a s e d o n o n e c o r e m a k e s t h e 8 0 5 1 f a m i l y a n e x c e l l e n t c h o i c e a s t h e b a s e a r c h i t e c t u r e f o r a c o m p a n y ' s e n t i r e l i n e o f p r o d u c t s s i n c e i t c a n p e r f o r m m a n y f u n c t i o n s a n d d e v e l o p e r s w i l l o n l y h a v e t o l e a r n t h i s o n e p l a t f o r m . T h e b a s i c a r c h i t e c t u r e c o n s i s t s o f t h e f o l l o w i n g f e a t u r e s : O n e 8 0 5 1 p r o c e s s o r c y c l e c o n s i s t s o f t w e l v e o s c i l l a t o r p e r i o d s . E a c h o f t h e t w e l v e o s c i l l a t o r p e r i o d s i s u s e d f o r a s p e c i a l f u n c t i o n b y t h e 8 0 5 1 c o r e s u c h a s o p c o d e f e t c h e s a n d s a m p l e s o f t h e i n t e r r u p t d a i s y c h a i n f o r p e n d i n g i n t e r r u p t s . T h e t i m e r e q u i r e d f o r a n y 8 0 5 1 i n s t r u c t i o n c a n b e c o m p u t e d b y d i v i d i n g t h e c l o c k f r e q u e n c y b y 1 2 , i n v e r t i n g t h a t r e s u l t a n d m u l t i p l y i n g i t b y t h e n u m b e r o f p r o c e s s o r c y c l e s r e q u i r e d b y t h e i n s t r u c t i o n i n q u e s t i o n . T h e r e f o r e , i f y o u h a v e a s y s t e m w h i c h i s u s i n g a n 1 1 . 0 5 9 M H z c l o c k , y o u c a n c o m p u t e t h e n u m b e r o f i n s t r u c t i o n s p e r s e c o n d b y d i v i d i n g t h i s v a l u e b y 1 2 . T h i s g i v e s a n i n s t r u c t i o n f r e q u e n c y o f 9 2 1 5 8 3 i n s t r u c t i o n s p e r s e c o n d . I n v e r t i n g t h i s w i l l p r o v i d e t h e a m o u n t o f t i m e t a k e n b y e a c h i n s t r u c t i o n c y c l e ( 1 . 0 8 5 m i c r o s e c o n d s ) . a n e i g h t b i t A L U 3 2 d e s c r e t e I / O p i n s ( 4 g r o u p s o f 8 ) w h i c h c a n b e i n d i v i d u a l l y a c c e s s e d t w o 1 6 b i t t i m e r / c o u n t e r s f u l l d u p l e x U A R T 6 i n t e r r u p t s o u r c e s w i t h 2 p r i o r i t y l e v e l s 1 2 8 b y t e s o f o n b o a r d R A M s e p a r a t e 6 4 K b y t e a d d r e s s s p a c e s f o r D A T A a n d C O D E m e m o r y
THE FINAL WORD ON THE 8051 Memory Organization The CODE Space P a g e 3 T h e 8 0 5 1 a r c h i t e c t u r e p r o v i d e s t h e u s e r w i t h t h r e e p h y s i c a l l y d i s t i n c t m e m o r y s p a c e s w h i c h c a n b e s e e n i n F i g u r e A - 1 . E a c h m e m o r y s p a c e c o n s i s t s o f c o n t i g u o u s a d d r e s s e s f r o m 0 t o t h e m a x i m u m s i z e , i n b y t e s , o f t h e m e m o r y s p a c e . A d d r e s s o v e r l a p s a r e r e s o l v e d b y u t i l i z i n g i n s t r u c t i o n s w h i c h r e f e r s p e c i f i c a l l y t o a g i v e n a d d r e s s s p a c e . T h e t h r e e m e m o r y s p a c e s f u n c t i o n a s d e s c r i b e d b e l o w . F i g u r e A - 1 - 8 0 5 1 M e m o r y A r c h i t e c t u r e T h e f i r s t m e m o r y s p a c e i s t h e C O D E s e g m e n t i n w h i c h t h e e x e c u t a b l e p r o g r a m r e s i d e s . T h i s s e g m e n t c a n b e u p t o 6 4 K ( s i n c e i t i s a d d r e s s e d b y 1 6 a d d r e s s l i n e s ) . T h e p r o c e s s o r t r e a t s t h i s s e g m e n t a s r e a d o n l y a n d w i l l g e n e r a t e s i g n a l s a p p r o p r i a t e t o a c c e s s a m e m o r y d e v i c e s u c h a s a n E P R O M . H o w e v e r , t h i s d o e s n o t m e a n t h a t t h e C O D E s e g m e n t m u s t b e i m p l e m e n t e d u s i n g a n E P R O M . M a n y e m b e d d e d s y s t e m s t h e s e d a y s a r e u s i n g E E P R O M w h i c h a l l o w s t h e m e m o r y t o b e o v e r w r i t t e n e i t h e r b y t h e 8 0 5 1 i t s e l f o r b y a n e x t e r n a l d e v i c e . T h i s m a k e s u p g r a d e s t o t h e p r o d u c t e a s y t o d o s i n c e n e w s o f t w a r e c a n b e d o w n l o a d e d i n t o t h e E E P R O M r a t h e r t h a n h a v i n g t o d i s a s s e m b l e i t a n d i n s t a l l a n e w E P R O M . A d d i t i o n a l l y , b a t t e r y b a c k e d S R A M s c a n b e u s e d i n p l a c e o f a n E P R O M . T h i s m e t h o d o f f e r s t h e s a m e c a p a b i l i t y t o u p l o a d n e w s o f t w a r e t o t h e u n i t a s d o e s a n E E P R O M , a n d d o e s n o t h a v e a n y s o r t o f r e a d / w r i t e c y c l e l i m i t a t i o n s s u c h a s a n E E P R O M h a s . H o w e v e r , w h e n t h e b a t t e r y s u p p l y i n g t h e R A M e v e n t u a l l y d i e s , s o d o e s t h e s o f t w a r e i n i t . U s i n g a n S R A M i n p l a c e o f a n E P R O M i n d e v e l o p m e n t s y s t e m s a l l o w s f o r r a p i d d o w n l o a d i n g o f n e w c o d e i n t o t h e t a r g e t s y s t e m . W h e n t h i s c a n b e d o n e , i t h e l p s a v o i d t h e c y c l e o f p r o g r a m m i n g / t e s t i n g / e r a s i n g w i t h E P R O M s , a n d c a n a l s o h e l p a v o i d h a s s l e s o v e r a n i n c i r c u i t e m u l a t o r w h i c h i s u s u a l l y a r a r e c o m m o d i t y . I n a d d i t i o n t o e x e c u t a b l e c o d e , i t i s c o m m o n p r a c t i c e w i t h t h e 8 0 5 1 t o s t o r e f i x e d l o o k u p t a b l e s i n t h e C O D E s e g m e n t . T o f a c i l i t a t e t h i s , t h e 8 0 5 1 p r o v i d e s i n s t r u c t i o n s w h i c h a l l o w r a p i d a c c e s s t o t a b l e s v i a t h e d a t a p o i n t e r ( D P T R ) o r t h e p r o g r a m c o u n t e r w i t h a n o f f s e t i n t o t h e t a b l e o p t i o n a l l y p r o v i d e d b y t h e a c c u m u l a t o r . T h i s m e a n s t h a t o f t e n t i m e s , a t a b l e ' s b a s e a d d r e s s c a n b e l o a d e d i n D P T R a n d t h e e l e m e n t o f t h e t a b l e t o a c c e s s c a n b e h e l d i n t h e a c c u m u l a t o r . T h e a d d i t i o n i s p e r f o r m e d b y t h e 8 0 5 1 d u r i n g t h e e x e c u t i o n o f t h e i n s t r u c t i o n w h i c h c a n s a v e m a n y c y c l e s d e p e n d i n g o n t h e s i t u a t i o n . A n e x a m p l e o f t h i s i s s h o w n l a t e r i n t h i s c h a p t e r i n
CHAPTER 2 - THE HARDWARE P a g e 4 L i s t i n g A - 5 .
THE FINAL WORD ON THE 8051 The DATA Space Special Function Registers + F8 F0 E8 E0 D8 D0 C8 C0 B8 B0 A8 A0 98 90 88 80 0 B ACC PSW T2CON IP P3 IE P2 SCON P1 TCON P0 1 2 3 4 5 6 7 RCAP2L RCAP2H TL2 TH2 SBUF TMOD SP TL0 DPL TL1 DPH TH0 TH1 PCON - Denotes bit addressable Special Function Registers in this table and all following diagrams P a g e 5 T h e s e c o n d m e m o r y s p a c e i s t h e 1 2 8 b y t e s o f i n t e r n a l R A M o n t h e 8 0 5 1 , o r t h e f i r s t 1 2 8 b y t e s o f i n t e r n a l R A M o n t h e 8 0 5 2 . T h i s s e g m e n t i s t y p i c a l l y r e f e r r e d t o a s t h e D A T A s e g m e n t . T h e R A M l o c a t i o n s i n t h i s s e g m e n t a r e a c c e s s e d i n o n e o r t w o c y c l e s d e p e n d i n g o n t h e i n s t r u c t i o n . T h i s a c c e s s t i m e i s m u c h q u i c k e r t h a n a c c e s s t o t h e X D A T A s e g m e n t b e c a u s e m e m o r y i s a d d r e s s e d d i r e c t l y r a t h e r t h a n v i a a m e m o r y p o i n t e r s u c h a s D P T R w h i c h m u s t f i r s t b e i n i t i a l i z e d . T h e r e f o r e , f r e q u e n t l y u s e d v a r i a b l e s a n d t e m p o r a r y s c r a t c h v a r i a b l e s a r e u s u a l l y a s s i g n e d t o t h e D A T A s e g m e n t . S u c h a l l o c a t i o n m u s t b e d o n e w i t h c a r e , h o w e v e r , d u e t o t h e l i m i t e d a m o u n t o f m e m o r y i n t h i s s e g m e n t . V a r i a b l e s s t o r e d i n t h e D A T A s e g m e n t c a n a l s o b e a c c e s s e d i n d i r e c t l y v i a R 0 o r R 1 . T h e r e g i s t e r b e i n g u s e d a s t h e m e m o r y p o i n t e r m u s t c o n t a i n t h e a d d r e s s o f t h e b y t e t o b e r e t r i e v e d o r a l t e r e d . T h e s e i n s t r u c t i o n s c a n t a k e o n e o r t w o p r o c e s s o r c y c l e s d e p e n d i n g o n t h e s o u r c e / d e s t i n a t i o n d a t a b y t e . T h e D A T A s e g m e n t c o n t a i n s t w o s m a l l e r s e g m e n t s o f i n t e r e s t . T h e f i r s t s u b s e g m e n t c o n s i s t s o f t h e f o u r s e t s o f r e g i s t e r b a n k s w h i c h c o m p o s e t h e f i r s t 3 2 b y t e s o f R A M . T h e 8 0 5 1 c a n u s e a n y o f t h e s e f o u r g r o u p s o f e i g h t b y t e s a s i t s d e f a u l t r e g i s t e r b a n k . T h e s e l e c t i o n o f r e g i s t e r b a n k s i s c h a n g e a b l e a t a n y t i m e v i a t h e R S 1 a n d t h e R S 0 b i t s i n t h e P r o c e s s o r S t a t u s W o r d ( P S W ) . T h e s e t w o b i t s c o m b i n e i n t o a n u m b e r f r o m 0 t o 3 ( w i t h R S 1 b e i n g t h e m o s t s i g n i f i c a n t b i t ) w h i c h i n d i c a t e s t h e r e g i s t e r b a n k t o b e u s e d . R e g i s t e r b a n k s w i t c h i n g a l l o w s n o t o n l y f o r q u i c k p a r a m e t e r p a s s i n g , b u t a l s o o p e n s t h e d o o r f o r s i m p l i f y i n g t a s k s w i t c h i n g o n t h e 8 0 5 1 . T h e s e c o n d s u b - s e g m e n t i n t h e D A T A s p a c e i s a b i t a d d r e s s a b l e s e g m e n t i n w h i c h e a c h b i t c a n b e i n d i v i d u a l l y a c c e s s e d . T h i s s e g m e n t i s r e f e r r e d t o a s t h e B D A T A s e g m e n t . T h e b i t a d d r e s s a b l e s e g m e n t c o n s i s t s o f 1 6 b y t e s ( 1 2 8 b i t s ) a b o v e t h e f o u r r e g i s t e r b a n k s i n m e m o r y . T h e 8 0 5 1 c o n t a i n s s e v e r a l s i n g l e b i t i n s t r u c t i o n s w h i c h a r e o f t e n v e r y u s e f u l i n c o n t r o l a p p l i c a t i o n s a n d a i d i n r e p l a c i n g e x t e r n a l c o m b i n a t o r i a l l o g i c w i t h s o f t w a r e i n t h e 8 0 5 1 t h u s r e d u c i n g p a r t s c o u n t o n t h e t a r g e t s y s t e m . I t s h o u l d b e n o t e d t h a t t h e s e 1 6 b y t e s c a n a l s o b e a c c e s s e d o n a " b y t e - w i d e " b a s i s j u s t l i k e a n y o t h e r b y t e i n t h e D A T A s p a c e . C o n t r o l r e g i s t e r s f o r t h e i n t e r r u p t s y s t e m a n d t h e p e r i p h e r a l s o n t h e 8 0 5 1 a r e c o n t a i n e d i n i n t e r n a l R A M a t l o c a t i o n s 8 0 h e x a n d a b o v e . T h e s e r e g i s t e r s a r e r e f e r r e d t o a s s p e c i a l f u n c t i o n r e g i s t e r s ( o r S F R s f o r s h o r t ) . M a n y o f t h e m a r e b i t a d d r e s s a b l e . T h e b i t s i n t h e b i t a d d r e s s a b l e S F R s c a n e i t h e r b e a c c e s s e d b y n a m e , i n d e x o r b i t a d d r e s s . T h u s , y o u c a n r e f e r t o t h e E A b i t o f t h e I n t e r r u p t E n a b l e S F R a s E A , I E . 7 , o r 0 A F H . T h e S F R s c o n t r o l t h i n g s s u c h a s t h e f u n c t i o n o f t h e t i m e r / c o u n t e r s , t h e U A R T , a n d t h e T a b l e A - 1
CHAPTER 2 - THE HARDWARE The IDATA Space The XDATA Space P a g e 6 i n t e r r u p t s o u r c e s a s w e l l a s t h e i r p r i o r i t i e s . T h e s e r e g i s t e r s a r e a c c e s s e d b y t h e s a m e s e t o f i n s t r u c t i o n s a s t h e b y t e s a n d b i t s i n t h e D A T A s e g m e n t . A m e m o r y m a p o f t h e S F R s i n d i c a t i n g t h e r e g i s t e r s w h i c h a r e b i t a d d r e s s a b l e i s s h o w n i n T a b l e A - 1 . C e r t a i n 8 0 5 1 f a m i l y m e m b e r s s u c h a s t h e 8 0 5 2 c o n t a i n a n a d d i t i o n a l 1 2 8 b y t e s o f i n t e r n a l R A M w h i c h r e s i d e a t R A M l o c a t i o n s 8 0 h e x a n d a b o v e . T h i s s e g m e n t o f R A M i s t y p i c a l l y r e f e r r e d t o a s t h e I D A T A s e g m e n t . B e c a u s e t h e I D A T A a d d r e s s e s a n d t h e S F R a d d r e s s e s o v e r l a p , a d d r e s s c o n f l i c t s b e t w e e n I D A T A R A M a n d t h e S F R s a r e r e s o l v e d b y t h e t y p e o f m e m o r y a c c e s s b e i n g p e r f o r m e d , s i n c e t h e I D A T A s e g m e n t c a n o n l y b e a c c e s s e d v i a i n d i r e c t a d d r e s s i n g m o d e s . T h e f i n a l 8 0 5 1 m e m o r y s p a c e i s 6 4 K i n l e n g t h a n d i s a d d r e s s e d b y t h e s a m e 1 6 a d d r e s s l i n e s a s t h e C O D E s e g m e n t . T h i s s p a c e i s t y p i c a l l y r e f e r r e d t o a s t h e e x t e r n a l d a t a m e m o r y s p a c e ( o r t h e X D A T A s e g m e n t f o r s h o r t ) . T h i s s e g m e n t u s u a l l y c o n s i s t s o f s o m e s o r t o f R A M ( u s u a l l y a n S R A M ) a n d t h e I / O d e v i c e s o r e x t e r n a l p e r i p h e r a l s t o w h i c h t h e 8 0 5 1 m u s t i n t e r f a c e v i a i t s b u s . R e a d o r w r i t e o p e r a t i o n s t o t h i s s e g m e n t t a k e a m i n i m u m o f t w o p r o c e s s o r c y c l e s a n d a r e p e r f o r m e d u s i n g e i t h e r D P T R , R 0 , o r R 1 . I n t h e c a s e o f D P T R , i t u s u a l l y t a k e s t w o p r o c e s s o r c y c l e s o r m o r e t o l o a d t h e d e s i r e d a d d r e s s i n a d d i t i o n t o t h e t w o c y c l e s r e q u i r e d t o p e r f o r m t h e r e a d o r w r i t e o p e r a t i o n . S i m i l a r l y , l o a d i n g R 0 o r R 1 w i l l t a k e m i n i m u m o f o n e c y c l e i n a d d i t i o n t o t h e t w o c y c l e s i m p o s e d b y t h e m e m o r y a c c e s s i t s e l f . T h e r e f o r e , i t i s e a s y t o s e e t h a t a t y p i c a l o p e r a t i o n w i t h t h e X D A T A s e g m e n t w i l l , i n g e n e r a l , t a k e a m i n i m u m o f t h r e e p r o c e s s o r c y c l e s . B e c a u s e o f t h i s , t h e D A T A s e g m e n t i s a v e r y a t t r a c t i v e p l a c e t o s t o r e a n y f r e q u e n t l y u s e d v a r i a b l e s . I t i s p o s s i b l e t o f i l l t h i s s e g m e n t e n t i r e l y w i t h 6 4 K o f R A M i f t h e 8 0 5 1 d o e s n o t n e e d t o p e r f o r m a n y I / O w i t h d e v i c e s i n i t s b u s o r i f t h e d e s i g n e r w i s h e s t o c y c l e t h e R A M o n a n d o f f w h e n I / O d e v i c e s a r e b e i n g a c c e s s e d v i a t h e b u s . M e t h o d s f o r p e r f o r m i n g t h i s t e c h n i q u e w i l l b e d i s c u s s e d i n c h a p t e r s l a t e r i n t h i s b o o k .
THE FINAL WORD ON THE 8051 Bit processing and Boolean logic P a g e 7 T h e 8 0 5 1 c o n t a i n s a s i n g l e b i t B o o l e a n p r o c e s s o r w h i c h c a n b e u s e d t o p e r f o r m l o g i c a l o p e r a t i o n s o n a n y o f t h e 1 2 8 a d d r e s s a b l e b i t s i n t h e B I T s e g m e n t , t h e 1 2 8 a d d r e s s a b l e b i t s i n t h e S F R s , a n d a n y o f t h e 3 2 I / O l i n e s ( p o r t 0 t h r o u g h p o r t 3 ) . T h e 8 0 5 1 c a n p e r f o r m O R , A N D , X O R , c o m p l e m e n t , s e t , a n d c l e a r o p e r a t i o n s o n b i t s a s w e l l a s m o v i n g b i t v a l u e s a s o n e w o u l d n o r m a l l y m o v e b y t e v a l u e s . L i s t i n g A - 1 M O V C , 2 2 H ; m o v e t h e b i t v a l u e a t a d d r e s s ; 2 2 H t o t h e c a r r y b i t O R L C , 2 3 H ; o r t h e b i t v a l u e a t a d d r e s s ; 2 3 H i n t o t h e c a r r y b i t A N L 2 4 H , C ; a n d t h e c a r r y b i t i n t o b i t ; a d d r e s s 2 4 H T h e r e a r e a l s o c o n d i t i o n a l b r a n c h e s w h i c h u s e a d d r e s s e d b i t s a s t h e c o n d i t i o n . O n e s u c h b r a n c h w h i c h i s e s p e c i a l l y u s e f u l i s t h e “ j u m p i f b i t i s s e t a n d c l e a r b i t ” i n s t r u c t i o n . T h i s " b r a n c h a n d c l e a r " c a n b e p e r f o r m e d i n t w o p r o c e s s o r c y c l e s a n d s a v e s a c y c l e o r t w o o v e r s p l i t t i n g t h e j u m p a n d t h e c l e a r i n t o t w o s e p a r a t e o p c o d e s . A s a n e x a m p l e , s u p p o s e t h a t y o u h a d t o w r i t e a r o u t i n e w h i c h w a i t e d f o r p i n P 0 . 0 t o s e t , b u t c o u l d n o t w a i t i n d e f i n i t e l y . T h i s r o u t i n e w o u l d h a v e t o d e c r e m e n t a t i m e o u t v a l u e a n d e x i t t h e p o l l i n g l o o p w h e n t h i s t i m e o u t i s e x c e e d e d . W h e n p i n P 0 . 0 s e t s , t h e p r o c e s s o r m u s t f o r c e i t b a c k t o 0 a n d e x i t t h e p o l l i n g l o o p . W i t h n o r m a l l o g i c f l o w , t h e r o u t i n e w o u l d l o o k l i k e t h e f o l l o w i n g . L i s t i n g A - 2 M O V t i m e o u t , # T O _ V A L ; s e t t h e t i m e o u t v a l u e L 2 : J B P 0 . 0 , L 1 ; c h e c k t h e b i t D J N Z t i m e o u t , L 2 ; d e c r e m e n t t h e t i m e o u t c o u n t e r ; a n d s a m p l e a g a i n L 1 : C L R P 0 . 0 ; f o r c e P 0 . 0 t o l o g i c l e v e l 0 R E T ; e x i t t h e r o u t i n e U s i n g t h e J B C i n s t r u c t i o n , t h e s a m e r o u t i n e w o u l d b e c o d e d a s f o l l o w s . L i s t i n g A - 3 M O V t i m e o u t , # T O _ V A L ; s e t t h e t i m e o u t v a l u e L 2 : J B C P 0 . 0 , L 1 ; c h e c k t h e b i t a n d f o r c e P 0 . 0 ; t o l o g i c l e v e l 0 i f s e t D J N Z t i m e o u t , L 2 ; d e c r e m e n t t h e t i m e o u t c o u n t e r L 1 : R E T ; e x i t t h e r o u t i n e W h i l e t h e s e c o n d r o u t i n e m a y n o t o f f e r a h u g e a m o u n t o f s a v i n g s i n t h e c o d e , i t d o e s m a k e t h e c o d e a l i t t l e s i m p l e r a n d m o r e e l e g a n t . T h e r e w i l l b e m a n y s i t u a t i o n s i n y o u r u s e o f a s s e m b l y c o d e o n t h e 8 0 5 1 c o n t r o l l e r w h e r e t h i s i n s t r u c t i o n w i l l c o m e i n h a n d y .
CHAPTER 2 - THE HARDWARE Addressing Modes P a g e 8 T h e 8 0 5 1 i s c a p a b l e o f p e r f o r m i n g d i r e c t a n d i n d i r e c t m e m o r y a c c e s s e s o n i t s v a r i o u s m e m o r y s p a c e s . T h e s e a r e t h e t y p i c a l m e t h o d s t h r o u g h w h i c h p r o c e s s o r s y s t e m s a c c e s s m e m o r y . D i r e c t a c c e s s e s a r e c h a r a c t e r i z e d b y p r e s e n c e o f t h e a d d r e s s o f t h e a c c e s s e d v a r i a b l e i n t h e i n s t r u c t i o n i t s e l f . T h e s e a c c e s s e s c a n o n l y b e p e r f o r m e d o n t h e D A T A s e g m e n t a n d t h e S F R s . E x a m p l e s o f d i r e c t m e m o r y a c c e s s e s a r e s h o w n b e l o w . L i s t i n g A - 4 M O V A , 0 3 H ; m o v e t h e v a l u e a t a d d r e s s 0 3 H t o ; t h e a c c u m u l a t o r M O V 4 3 H , 2 2 H ; m o v e t h e v a l u e a t a d d r e s s 2 2 H t o ; a d d r e s s 4 3 H M O V 0 2 H , C ; m o v e t h e v a l u e o f t h e c a r r y b i t t o ; b i t a d d r e s s 0 2 H M O V 4 2 H , # 1 8 ; l o a d a d d r e s s 4 2 H w i t h t h e v a l u e 1 8 M O V 0 9 H , S B U F ; l o a d t h e v a l u e i n S B U F i n t o ; a d d r e s s 0 9 H I n d i r e c t a c c e s s e s i n v o l v e a n o t h e r r e g i s t e r ( D P T R , P C , R 0 , o r R 1 o n t h e 8 0 5 1 ) w h i c h c o n t a i n s t h e a d d r e s s o f t h e v a r i a b l e t o b e a c c e s s e d . T h e i n s t r u c t i o n t h e n r e f e r s t o t h e p o i n t i n g r e g i s t e r r a t h e r t h a n t h e a d d r e s s i t s e l f . T h i s i s h o w a l l a c c e s s e s t o C O D E , I D A T A , a n d X D A T A s e g m e n t s a r e p e r f o r m e d . T h e D A T A s e g m e n t m a y a l s o b e a c c e s s e d i n t h i s m a n n e r . B i t s i n t h e B D A T A s e g m e n t c a n o n l y b e a c c e s s e d d i r e c t l y . I n d i r e c t m e m o r y a c c e s s e s a r e q u i t e u s e f u l w h e n a b l o c k o f d a t a m u s t b e m o v e d , a l t e r e d o r o p e r a t e d o n w i t h a m i n i m u m a m o u n t o f c o d e s i n c e t h e p o i n t e r c a n b e i n c r e m e n t e d t h r o u g h t h e m e m o r y a r e a v i a a l o o p i n g m e c h a n i s m . I n d i r e c t a c c e s s e s t o t h e C O D E s e g m e n t c a n h a v e a b a s e a d d r e s s i n e i t h e r t h e D P T R o r t h e P C r e g i s t e r a n d a n o f f s e t i n t h e a c c u m u l a t o r . T h i s i s u s e f u l f o r o p e r a t i o n s i n v o l v i n g l o o k u p t a b l e s . E x a m p l e s o f i n d i r e c t m e m o r y a c c e s s e s a r e s h o w n b e l o w .
分享到:
收藏