/ / I C C - A V R   a p p l i c a t i o n   b u i l d e r   :   2 0 0 6 - 0 2 - 1 2   1 4 : 0 0 : 0 0 
 / /   T a r g e t   :   A T m e g a 4 8 
 / /   C r y s t a l :   8 . 0 0 0 M h z 
 / /   A u t h o r :     j a c k y a n 
 / /   O l e d   T y p e   :   w h i t e 
 / / # d e f i n e   f o s c   8 0 0 0 0 0 0 
 / / # d e f i n e   b a u d   9 6 0 0 
 # i n c l u d e   " i o m 4 8 v . h " 
 # i n c l u d e   " m a c r o s . h " 
 # i n c l u d e   " d e f s . h " 
 / * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
       G l o b a l   D e f i n e s 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * / 
 u n s i g n e d   c h a r       k e y _ d e b u g ; 
 u n s i g n e d   c h a r   	 B u f f e r [ ] = { 
 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 
 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 
 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 
 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 0 X 0 0 , 
 } ; 
 # d e f i n e   T X _ A D R _ W I D T H         5       / /   5   b y t e s   T X ( R X )   a d d r e s s   w i d t h 
 # d e f i n e   T X _ P L O A D _ W I D T H     2 0     / /   1 6   b y t e s   T X   p a y l o a d 
 u n s i g n e d   c h a r   T X _ A D D R E S S [ T X _ A D R _ W I D T H ]     =   { 0 x 3 4 , 0 x 4 3 , 0 x 1 0 , 0 x 1 0 , 0 x 0 1 } ;   / /   D e f i n e   a   s t a t i c   T X   a d d r e s s 
 v o i d   i n i t _ C P U   ( v o i d ) ; 
 v o i d   d e l a y m s ( u n s i g n e d   s h o r t 	 d l y ) ; / / d l y = 1 e00000o01 m s 04 M H z 4lvfzv/chV
 v o i d   I N I T _ i o ( v o i d ) ; 
 v o i d   R X _ M o d e ( v o i d ) ; 
 v o i d   T X _ M o d e ( v o i d ) ; 
 v o i d   d e l a y ( v o i d ) ; 
 u n s i g n e d   c h a r   S P I _ W r i t e _ B u f ( u n s i g n e d   c h a r   r e g ,   u n s i g n e d   c h a r   * p B u f ,   u n s i g n e d   c h a r   b y t e s ) ; 
 u n s i g n e d   c h a r   S P I _ R e a d _ B u f ( u n s i g n e d   c h a r   r e g ,   u n s i g n e d   c h a r   * p B u f ,   u n s i g n e d   c h a r   b y t e s ) ; 
 u n s i g n e d   c h a r   S P I _ R W _ R e g ( u n s i g n e d   c h a r   r e g ,   u n s i g n e d   c h a r   v a l u e ) ; 
 u n s i g n e d   c h a r   S P I _ R e a d ( u n s i g n e d   c h a r   r e g ) ; 
 v o i d   c l e a r _ b u f ( u n s i g n e d   c h a r 	 * p t r , u n s i g n e d   c h a r   n u m b e r ) ; 
 u n s i g n e d   c h a r   a c c e p t _ f l a g = 0 ; 
 u n s i g n e d   i n t   a c c e p t _ t i m e = 0 ; 
 / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / 
 / /   S P I ( n R F 2 4 L 0 1 )   c o m m a n d s 
 # d e f i n e   R E A D _ R E G                 0 x 0 0     / /   D e f i n e   r e a d   c o m m a n d   t o   r e g i s t e r 
 # d e f i n e   W R I T E _ R E G               0 x 2 0     / /   D e f i n e   w r i t e   c o m m a n d   t o   r e g i s t e r 
 # d e f i n e   R D _ R X _ P L O A D           0 x 6 1     / /   D e f i n e   R X   p a y l o a d   r e g i s t e r   a d d r e s s 
 # d e f i n e   W R _ T X _ P L O A D           0 x A 0     / /   D e f i n e   T X   p a y l o a d   r e g i s t e r   a d d r e s s 
 # d e f i n e   F L U S H _ T X                 0 x E 1     / /   D e f i n e   f l u s h   T X   r e g i s t e r   c o m m a n d 
 # d e f i n e   F L U S H _ R X                 0 x E 2     / /   D e f i n e   f l u s h   R X   r e g i s t e r   c o m m a n d 
 # d e f i n e   R E U S E _ T X _ P L           0 x E 3     / /   D e f i n e   r e u s e   T X   p a y l o a d   r e g i s t e r   c o m m a n d 
 / / # d e f i n e   N O P                           0 x F F     / /   D e f i n e   N o   O p e r a t i o n ,   m i g h t   b e   u s e d   t o   r e a d   s t a t u s   r e g i s t e r 
 / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / 
 / /   S P I ( n R F 2 4 L 0 1 )   r e g i s t e r s ( a d d r e s s e s ) 
 # d e f i n e   C O N F I G                     0 x 0 0     / /   ' C o n f i g '   r e g i s t e r   a d d r e s s 
 # d e f i n e   E N _ A A                       0 x 0 1     / /   ' E n a b l e   A u t o   A c k n o w l e d g m e n t '   r e g i s t e r   a d d r e s s 
 # d e f i n e   E N _ R X A D D R               0 x 0 2     / /   ' E n a b l e d   R X   a d d r e s s e s '   r e g i s t e r   a d d r e s s 
 # d e f i n e   S E T U P _ A W                 0 x 0 3     / /   ' S e t u p   a d d r e s s   w i d t h '   r e g i s t e r   a d d r e s s 
 # d e f i n e   S E T U P _ R E T R             0 x 0 4     / /   ' S e t u p   A u t o .   R e t r a n s '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R F _ C H                       0 x 0 5     / /   ' R F   c h a n n e l '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R F _ S E T U P                 0 x 0 6     / /   ' R F   s e t u p '   r e g i s t e r   a d d r e s s 
 # d e f i n e   S T A T U S                     0 x 0 7     / /   ' S t a t u s '   r e g i s t e r   a d d r e s s 
 # d e f i n e   O B S E R V E _ T X             0 x 0 8     / /   ' O b s e r v e   T X '   r e g i s t e r   a d d r e s s 
 # d e f i n e   C D                             0 x 0 9     / /   ' C a r r i e r   D e t e c t '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ A D D R _ P 0             0 x 0 A     / /   ' R X   a d d r e s s   p i p e 0 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ A D D R _ P 1             0 x 0 B     / /   ' R X   a d d r e s s   p i p e 1 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ A D D R _ P 2             0 x 0 C     / /   ' R X   a d d r e s s   p i p e 2 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ A D D R _ P 3             0 x 0 D     / /   ' R X   a d d r e s s   p i p e 3 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ A D D R _ P 4             0 x 0 E     / /   ' R X   a d d r e s s   p i p e 4 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ A D D R _ P 5             0 x 0 F     / /   ' R X   a d d r e s s   p i p e 5 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   T X _ A D D R                   0 x 1 0     / /   ' T X   a d d r e s s '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ P W _ P 0                 0 x 1 1     / /   ' R X   p a y l o a d   w i d t h ,   p i p e 0 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ P W _ P 1                 0 x 1 2     / /   ' R X   p a y l o a d   w i d t h ,   p i p e 1 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ P W _ P 2                 0 x 1 3     / /   ' R X   p a y l o a d   w i d t h ,   p i p e 2 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ P W _ P 3                 0 x 1 4     / /   ' R X   p a y l o a d   w i d t h ,   p i p e 3 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ P W _ P 4                 0 x 1 5     / /   ' R X   p a y l o a d   w i d t h ,   p i p e 4 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   R X _ P W _ P 5                 0 x 1 6     / /   ' R X   p a y l o a d   w i d t h ,   p i p e 5 '   r e g i s t e r   a d d r e s s 
 # d e f i n e   F I F O _ S T A T U S           0 x 1 7     / /   ' F I F O   S t a t u s   R e g i s t e r '   r e g i s t e r   a d d r e s s 
 # d e f i n e   M A X _ R T     	 0 x 1 0     / /   M a x   # o f   T X   r e t r a n s   i n t e r r u p t 
 # d e f i n e   T X _ D S       	 0 x 2 0     / /   T X   d a t a   s e n t   i n t e r r u p t 
 # d e f i n e   R X _ D R       	 0 x 4 0     / /   R X   d a t a   r e c e i v e d 
 / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 # d e f i n e   B I T ( x ) 	                       ( 1   < <   ( x ) ) 
 # d e f i n e   S E T B I T ( x ,   y )       	       ( x   | =   y ) 
 # d e f i n e   C L E A R B I T ( x ,   y )   	       ( x   & =   ~ y ) 
 # d e f i n e   C H E C K B I T ( x ,   y )   	       ( x   &   y ) 
 # d e f i n e   B I T 7   0 x 8 0 
 # d e f i n e   B I T 6   0 x 4 0 
 # d e f i n e   B I T 5   0 x 2 0 
 # d e f i n e   B I T 4   0 x 1 0 
 # d e f i n e   B I T 3   0 x 0 8 
 # d e f i n e   B I T 2   0 x 0 4 
 # d e f i n e   B I T 1   0 x 0 2 
 # d e f i n e   B I T 0   0 x 0 1 
 / * # d e f i n e   n R F 2 4 L 0 1 _ C S N                 B I T 4 
 # d e f i n e   n R F 2 4 L 0 1 _ S C K                 B I T 5 
 # d e f i n e   n R F 2 4 L 0 1 _ M O S I               B I T 6 
 # d e f i n e   n R F 2 4 L 0 1 _ C E                   B I T 3 
 # d e f i n e   n R F 2 4 L 0 1 _ M I S O   	         B I T 7 
 # d e f i n e   n R F 2 4 L 0 1 _ I R Q     	         B I T 0 
 # d e f i n e   n R F 2 4 L 0 1 _ C S N H               S E T B I T ( P O R T D ,   B I T 4 )           
 # d e f i n e   n R F 2 4 L 0 1 _ C S N L               C L E A R B I T ( P O R T D ,   B I T 4 )       
 
 # d e f i n e   n R F 2 4 L 0 1 _ S C K H               S E T B I T ( P O R T D , B I T 5 )             
 # d e f i n e   n R F 2 4 L 0 1 _ S C K L               C L E A R B I T ( P O R T D , B I T 5 )         
 
 # d e f i n e   n R F 2 4 L 0 1 _ M O S I H             S E T B I T ( P O R T D , B I T 6 )           
 # d e f i n e   n R F 2 4 L 0 1 _ M O S I L 	         C L E A R B I T ( P O R T D , B I T 6 )       
 
 # d e f i n e   n R F 2 4 L 0 1 _ C E H                 S E T B I T ( P O R T D , B I T 3 )           
 # d e f i n e   n R F 2 4 L 0 1 _ C E L                 C L E A R B I T ( P O R T D , B I T 3 )   * /   
 / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 u n s i g n e d   c h a r   t 2 0 m s ; 
 # d e f i n e   B I T ( x ) 	                       ( 1   < <   ( x ) ) 
 # d e f i n e   S E T B I T ( x ,   y )       	       ( x   | =   y ) 
 # d e f i n e   C L E A R B I T ( x ,   y )   	       ( x   & =   ~ y ) 
 # d e f i n e   C H E C K B I T ( x ,   y )   	       ( x   &   y ) 
 # d e f i n e   B I T 7   0 x 8 0 
 # d e f i n e   B I T 6   0 x 4 0 
 # d e f i n e   B I T 5   0 x 2 0 
 # d e f i n e   B I T 4   0 x 1 0 
 # d e f i n e   B I T 3   0 x 0 8 
 # d e f i n e   B I T 2   0 x 0 4 
 # d e f i n e   B I T 1   0 x 0 2 
 # d e f i n e   B I T 0   0 x 0 1 
 # d e f i n e   n R F 2 4 L 0 1 _ C S N                 B I T 6 
 # d e f i n e   n R F 2 4 L 0 1 _ S C K                 B I T 7 
 # d e f i n e   n R F 2 4 L 0 1 _ M O S I               B I T 0 
 # d e f i n e   n R F 2 4 L 0 1 _ C E                   B I T 5       
 # d e f i n e   n R F 2 4 L 0 1 _ M I S O   	         B I T 1 
 # d e f i n e   n R F 2 4 L 0 1 _ I R Q     	         B I T 2 
 # d e f i n e   n R F 2 4 L 0 1 _ C S N H               S E T B I T ( P O R T D ,   n R F 2 4 L 0 1 _ C S N )           
 # d e f i n e   n R F 2 4 L 0 1 _ C S N L               C L E A R B I T ( P O R T D ,   n R F 2 4 L 0 1 _ C S N )       
 
 # d e f i n e   n R F 2 4 L 0 1 _ C S N _ D I R         S E T B I T ( D D R D ,   n R F 2 4 L 0 1 _ C S N )     / / O U T P U T 
 
 # d e f i n e   n R F 2 4 L 0 1 _ S C K H               S E T B I T ( P O R T D , n R F 2 4 L 0 1 _ S C K )             
 # d e f i n e   n R F 2 4 L 0 1 _ S C K L               C L E A R B I T ( P O R T D , n R F 2 4 L 0 1 _ S C K )         
 # d e f i n e   n R F 2 4 L 0 1 _ S C K _ D I R       S E T B I T ( D D R D , n R F 2 4 L 0 1 _ S C K )     / / O U T P U T 
   
 # d e f i n e   n R F 2 4 L 0 1 _ M O S I H             S E T B I T ( P O R T B , n R F 2 4 L 0 1 _ M O S I )           
 # d e f i n e   n R F 2 4 L 0 1 _ M O S I L 	         C L E A R B I T ( P O R T B , n R F 2 4 L 0 1 _ M O S I )       
 # d e f i n e   n R F 2 4 L 0 1 _ M O S I _ D I R       S E T B I T ( D D R B , n R F 2 4 L 0 1 _ M O S I ) 
 
 # d e f i n e   n R F 2 4 L 0 1 _ C E H                 S E T B I T ( P O R T D , n R F 2 4 L 0 1 _ C E )     / / O U T P U T       
 # d e f i n e   n R F 2 4 L 0 1 _ C E L                 C L E A R B I T ( P O R T D , n R F 2 4 L 0 1 _ C E ) 
 # d e f i n e   n R F 2 4 L 0 1 _ C E _ D I R           S E T B I T ( D D R D , n R F 2 4 L 0 1 _ C E ) 
 
 # d e f i n e   n R F 2 4 L 0 1 _ I R Q _ D I R           C L E A R B I T ( D D R B , n R F 2 4 L 0 1 _ I R Q )   / / I N P U T 
 
 # d e f i n e   n R F 2 4 L 0 1 _ M I S O _ D I R         C L E A R B I T ( D D R B , n R F 2 4 L 0 1 _ M I S O )   / / I N P U T 
 / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 v o i d   m a i n ( v o i d ) 
 { 
   	 u n s i g n e d   c h a r   G e t _ S O = 0 ; 
         C L I ( ) ;                                       / *   g l o b a l   i n t e r r u p t   d i s a b l e   * / 
         i n i t _ C P U   ( ) ; 
         d e l a y m s ( 1 0 0 ) ; 	 / / E^1 0 m s * 1 0 = 1 0 0 m s 
         d e l a y m s ( 1 0 0 ) ; 	 / / E^1 0 m s * 1 0 = 1 0 0 m s 
 	 R X _ M o d e ( ) ; 
 	 w h i l e ( 1 ) 
 	 { 	 	 
 	 / / 	 i f ( ! ( P I N B &   0 x 0 1 ) ) 
 	 	 i f ( ! ( P I N B &   0 x 0 4 ) ) 
 	 	 { / / n R F 2 4 L 0 1   000SO  
 	 	 	 k e y _ d e b u g = S P I _ R e a d ( S T A T U S ) ; 	 	 	 / /   r e a d   r e g i s t e r   S T A T U S ' s   v a l u e 
 	 	 	 i f ( k e y _ d e b u g & R X _ D R ) 	 	 	 	 	 / /   i f   r e n R F 2 4 L 0 1 _ C E i v e   d a t a   r e a d y   ( R X _ D R )   i n t e r r u p t 
 	 	 	 	 S P I _ R e a d _ B u f ( R D _ R X _ P L O A D , B u f f e r , T X _ P L O A D _ W I D T H ) ; / /   r e a d   r e n R F 2 4 L 0 1 _ C E i v e   p a y l o a d   f r o m   R X _ F I F O   b u f f e r 
 	 	 	 i f ( k e y _ d e b u g & M A X _ R T )   S P I _ R W _ R e g ( F L U S H _ T X , 0 ) ; 
 	 	 	 S P I _ R W _ R e g ( W R I T E _ R E G + S T A T U S , 0 x f f ) ; / /   c l e a r   R X _ D R   o r   T X _ D S   o r   M A X _ R T   i n t e r r u p t   f l a g 
 	 	 	 R X _ M o d e ( ) ; 
 	 	 	 i f ( ( B u f f e r [ 0 ] = = 8 0 ) & & ( B u f f e r [ 1 ] = = 0 2 ) ) / / d a t a   a c c e p t 
 	 	 	 { 
 	 	 	   	 a c c e p t _ f l a g = 1 ; 
 	 	 	   	 P O R T B   =   0 x 8 0 ; 
 	 	 	 	 d e l a y m s ( 5 0 0 0 ) ; 	 / / ^e1 0 m s * 1 0 = 1 0 0 m s 
 	 	 	 	 P O R T B   =   0 x 0 0 ; 
 	 	 	 } 
 	 	 } 
 	 	 i f ( a c c e p t _ f l a g = = 0 ) / / d a t a   s e n d 
 	 	 { 
 	 	   	 	 a c c e p t _ f l a g = 1 ; 
 	 	 	 	 a c c e p t _ t i m e = 0 ; 
 	 	   	 	 B u f f e r [ 0 ] = 8 0 ; 
 	 	 	 	 B u f f e r [ 1 ] = 0 2 ; 
 	 	 	 	 T X _ M o d e ( ) ; 	 	 	 / /   s e t   T X   M o d e   a n d   t r a n s m i t t i n g 
 	 	 	 	 B u f f e r [ 0 ] = 8 0 ; 
 	 	 	 	 B u f f e r [ 1 ] = 0 2 ; 
 	 	 	 	 T X _ M o d e ( ) ; 	 	 	 / /   s e t   T X   M o d e   a n d   t r a n s m i t t i n g 
 	 	 	 	 d e l a y m s ( 1 0 0 ) ; 
 	 / / 	 	 	 R X _ M o d e ( ) ; 
 	 / / 	 	 	 B u f f e r [ 0 ] = 0 0 ; 
 	 / / 	 	 	 B u f f e r [ 1 ] = 0 0 ; 
 	 	 } 
 	 	 i f ( a c c e p t _ f l a g = = 1 ) 
 	 	 { 
 	 	       a c c e p t _ t i m e + + ; 
 	 	       d e l a y m s ( 1 0 0 ) ; 
 	 	       i f ( a c c e p t _ t i m e > 1 0 0 0 0 ) 
 	 	       { 
 	 	       	   a c c e p t _ f l a g = 0 ; 
 	 	 	   a c c e p t _ t i m e = 0 ; 
 	 	       } 
 
 / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 	 
 / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 	 	 } 	 
 	 } 
 
 
 } 
 / * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
       M o d u l e :       i n i t _ C P U 
       F u n c t i o n :   I n i t i a l i z a t i o n   o f   C P U 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * / 
 v o i d   i n i t _ C P U   ( v o i d ) 
 { 
   	 M C U C R   =   0 x 0 0 ; 	 / /   
 	 E I C R A   =   0 x 0 0 ;   / / e x t e n d e d   e x t   i n t s 
         E I M S K   =   0 x 0 0 ; 
 	   
 	 T I M S K 0   =   0 x 0 1 ;   / / t i m e r   0   i n t e r r u p t   s o u r c e s 
 	 T I M S K 1   =   0 x 0 0 ;   / / t i m e r   1   i n t e r r u p t   s o u r c e s 
 	 T I M S K 2   =   0 x 0 0 ;   / / t i m e r   2   i n t e r r u p t   s o u r c e s 
 	   C L I ( ) ;   / / d i s a b l e   a l l   i n t e r r u p t s 
 	   N O P ( ) ; 
 	 P O R T B   =   0 x 0 1 ; 
   	 D D R B     =   0 x f 3 ; 
 	 P O R T D   =   0 x 8 0 ; 
 	 D D R D     =   0 x f f ; 
 	 P O R T C   =   0 x 8 0 ; 
 	 D D R C     =   0 x 7 8 ; 
         n R F 2 4 L 0 1 _ C E L ; 
 	 d e l a y ( ) ; 
 	 n R F 2 4 L 0 1 _ C S N H ; 	 	 	 / /   S p i   d i s a b l e 
 	 d e l a y ( ) ; 
 	 n R F 2 4 L 0 1 _ S C K L ; 	 
 	 d e l a y ( ) ; 
 } 
 v o i d   d e l a y ( v o i d ) / / 
 { 
 	 N O P ( ) ; 
 	 N O P ( ) ; 
 	 N O P ( ) ; 
 	 N O P ( ) ; 
 	 N O P ( ) ; 
 	 N O P ( ) ; 
 } 
 v o i d   d e l a y m s ( u n s i g n e d   s h o r t 	 d l y ) / / 
 { 
 	 f o r ( ; d l y > 0 ; d l y - - )   ; 
 } 
 
 v o i d   c l e a r _ b u f ( u n s i g n e d   c h a r 	 * p t r , u n s i g n e d   c h a r   n u m b e r ) / / b u f f e r 0000000pe0c[
 { 
 	 f o r   ( ; n u m b e r > 0 ; n u m b e r - - )   * p t r + + = 0 ; 
 } 
 / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 F u n c t i o n :   S P I _ R W ( ) ; 
 
 D e s c r i p t i o n : 
     W r i t e s   o n e   b y t e   t o   n R F 2 4 L 0 1 ,   a n d   r e t u r n   t h e   b y t e   r e a d 
     f r o m   n R F 2 4 L 0 1   d u r i n g   w r i t e ,   a c c o r d i n g   t o   S P I   p r o t o c o l     * / 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / 
 u n s i g n e d   c h a r   S P I _ R W ( u n s i g n e d   c h a r   b y t e ) 
 { 
 	 u n s i g n e d   c h a r     b i t _ c t r ; 	 
       	 f o r ( b i t _ c t r = 0 ; b i t _ c t r < 8 ; b i t _ c t r + + )       	 / /   o u t p u t   8 - b i t 
       	 { 
       	 	 i f ( b y t e   &   0 x 8 0 )   
       	 	 	 n R F 2 4 L 0 1 _ M O S I H ; 
       	 	 e l s e   
       	 	 	 n R F 2 4 L 0 1 _ M O S I L ; 
 	 	 d e l a y ( ) ; 
       	 	 b y t e   =   ( b y t e   < <   1 ) ;                       	 	 / /   s h i f t   n e x t   b i t   i n t o   M S B . . 
       	 	 n R F 2 4 L 0 1 _ S C K H ; 
 	 	 d e l a y ( ) ; 
   	 	 i f ( P I N B & 0 x 0 8 )   b y t e   | =   1 ; 
       	 	 n R F 2 4 L 0 1 _ S C K L ; 
 	 	 d e l a y ( ) ; 
       	 } 
         r e t u r n ( b y t e ) ;                       	 	     	 	 / /   r e t u r n   r e a d   b y t e 
 } 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 F u n c t i o n :   S P I _ R W _ R e g ( ) ; 
 
 D e s c r i p t i o n : 
     W r i t e s   v a l u e   ' v a l u e '   t o   r e g i s t e r   ' r e g '   * / 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / 
 u n s i g n e d   c h a r   S P I _ R W _ R e g ( u n s i g n e d   c h a r   r e g ,   u n s i g n e d   c h a r   v a l u e ) 
 { 
 	 u n s i g n e d   c h a r   s t a t u s ; 
 	 n R F 2 4 L 0 1 _ C S N L ; 
 	 d e l a y ( ) ; 
 	 s t a t u s   =   S P I _ R W ( r e g ) ;             / /   s e l e c t   r e g i s t e r 
     	 S P I _ R W ( v a l u e ) ;                           / /   . . a n d   w r i t e   v a l u e   t o   i t . . 
 	 n R F 2 4 L 0 1 _ C S N H ; 
 	 d e l a y ( ) ; 
     	 r e t u r n ( s t a t u s ) ;                         / /   r e t u r n   n R F 2 4 L 0 1   s t a t u s   b y t e 
 } 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 F u n c t i o n :   S P I _ R e a d ( ) ; 
 
 D e s c r i p t i o n : 
     R e a d   o n e   b y t e   f r o m   n R F 2 4 L 0 1   r e g i s t e r ,   ' r e g '     * / 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / 
 u n s i g n e d   c h a r   S P I _ R e a d ( u n s i g n e d   c h a r   r e g ) 
 { 
 	 u n s i g n e d   c h a r   r e g _ v a l ; 
 	 n R F 2 4 L 0 1 _ C S N L ; 
 	 d e l a y ( ) ; 
     	 S P I _ R W ( r e g ) ;                         / /   S e l e c t   r e g i s t e r   t o   r e a d   f r o m . . 
     	 r e g _ v a l   =   S P I _ R W ( 0 ) ;         / /   . . t h e n   r e a d   r e g i s t e r v a l u e 
 	 n R F 2 4 L 0 1 _ C S N H ; 
 	 d e l a y ( ) ; 
     	 r e t u r n ( r e g _ v a l ) ;                 / /   r e t u r n   r e g i s t e r   v a l u e 
 } 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 F u n c t i o n :   S P I _ R e a d _ B u f ( ) ; 
 
 D e s c r i p t i o n : 
     R e a d s   ' b y t e s '   # o f   b y t e s   f r o m   r e g i s t e r   ' r e g ' 
     T y p i c a l l y   u s e d   t o   r e a d   R X   p a y l o a d ,   R x / T x   a d d r e s s   * / 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / 
 u n s i g n e d   c h a r   S P I _ R e a d _ B u f ( u n s i g n e d   c h a r   r e g ,   u n s i g n e d   c h a r   * p B u f ,   u n s i g n e d   c h a r   b y t e s ) 
 { 
 	 u n s i g n e d   c h a r   s t a t u s , b y t e _ c t r ; 
 	 n R F 2 4 L 0 1 _ C S N L ; 
 	 d e l a y ( ) ; 
     	 s t a t u s   =   S P I _ R W ( r e g ) ;               	 	 / /   S e l e c t   r e g i s t e r   t o   w r i t e   t o   a n d   r e a d   s t a t u s   b y t e 
 	 f o r ( b y t e _ c t r = 0 ; b y t e _ c t r < b y t e s ; b y t e _ c t r + + ) 
         	 p B u f [ b y t e _ c t r ]   =   S P I _ R W ( 0 ) ;         / /   P e r f o r m   S P I _ R W   t o   r e a d   b y t e   f r o m   n R F 2 4 L 0 1 
 	 n R F 2 4 L 0 1 _ C S N H ; 
 	 d e l a y ( ) ; 
     	 r e t u r n ( s t a t u s ) ;                                         / /   r e t u r n   n R F 2 4 L 0 1   s t a t u s   b y t e 
 } 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 F u n c t i o n :   S P I _ W r i t e _ B u f ( ) ; 
 
 D e s c r i p t i o n : 
     W r i t e s   c o n t e n t s   o f   b u f f e r   ' * p B u f '   t o   n R F 2 4 L 0 1 
     T y p i c a l l y   u s e d   t o   w r i t e   T X   p a y l o a d ,   R x / T x   a d d r e s s   * / 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / 
 u n s i g n e d   c h a r   S P I _ W r i t e _ B u f ( u n s i g n e d   c h a r   r e g ,   u n s i g n e d   c h a r   * p B u f ,   u n s i g n e d   c h a r   b y t e s ) 
 { 
 	 u n s i g n e d   c h a r   s t a t u s , b y t e _ c t r ; 
 	 n R F 2 4 L 0 1 _ C S N L ;                                       	 	 / /   S e t   n R F 2 4 L 0 1 _ C S N   l o w ,   i n i t   S P I   t r a n a c t i o n 
     	 d e l a y ( ) ; 
 	 s t a t u s   =   S P I _ R W ( r e g ) ;         	 	 	 	 	 / /   S e l e c t   r e g i s t e r   t o   w r i t e   t o   a n d   r e a d   s t a t u s   b y t e 
     	 f o r ( b y t e _ c t r = 0 ;   b y t e _ c t r < b y t e s ;   b y t e _ c t r + + )   / /   t h e n   w r i t e   a l l   b y t e   i n   b u f f e r ( * p B u f ) 
         	 s t a t u s   =   S P I _ R W ( * p B u f + + ) ;                         
 	 n R F 2 4 L 0 1 _ C S N H ;   	 	 	 	 	 	 	 	 / /   S e t   n R F 2 4 L 0 1 _ C S N   h i g h   a g a i n 
 	 d e l a y ( ) ; 
     	 r e t u r n ( s t a t u s ) ;                     	 	 	 	 	 / /   r e t u r n   n R F 2 4 L 0 1   s t a t u s   b y t e 
 } 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 F u n c t i o n :   R X _ M o d e ( ) ; 
 
 D e s c r i p t i o n : 
     T h i s   f u n c t i o n   i n i t i a l i z e s   o n e   n R F 2 4 L 0 1   d e v i c e   t o 
     R X   M o d e ,   s e t   R X   a d d r e s s ,   w r i t e s   R X   p a y l o a d   w i d t h , 
     s e l e c t   R F   c h a n n e l ,   d a t a r a t e   &   L N A   H C U R R . 
     A f t e r   i n i t ,   C E   i s   t o g g l e d   h i g h ,   w h i c h   m e a n s   t h a t 
     t h i s   d e v i c e   i s   n o w   r e a d y   t o   r e c e i v e   a   d a t a p a c k e t .   * / 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / 
 v o i d   R X _ M o d e ( v o i d ) 
 { 
 	 n R F 2 4 L 0 1 _ C E L ; 
 	 d e l a y ( ) ; 
     	 S P I _ W r i t e _ B u f ( W R I T E _ R E G   +   R X _ A D D R _ P 0 ,   T X _ A D D R E S S ,   T X _ A D R _ W I D T H ) ;   / /   U s e   t h e   s a m e   a d d r e s s   o n   t h e   R X   d e v i c e   a s   t h e   T X   d e v i c e 
 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   E N _ A A ,   0 x 0 1 ) ;             / /   E n a b l e   A u t o . A c k : P i p e 0 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   E N _ R X A D D R ,   0 x 0 1 ) ;     / /   E n a b l e   P i p e 0 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   R F _ C H ,   4 0 ) ;                 / /   S e l e c t   R F   c h a n n e l   4 0 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   R X _ P W _ P 0 ,   T X _ P L O A D _ W I D T H ) ;   / /   S e l e c t   s a m e   R X   p a y l o a d   w i d t h   a s   T X   P a y l o a d   w i d t h 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   R F _ S E T U P ,   0 x 0 7 ) ;       / /   T X _ P W R : 0 d B m ,   D a t a r a t e : 2 M b p s ,   L N A : H C U R R 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   C O N F I G ,   0 x 0 f ) ;           / /   S e t   P W R _ U P   b i t ,   e n a b l e   C R C ( 2   b y t e s )   &   P r i m : R X .   R X _ D R   e n a b l e d . . 
     	 n R F 2 4 L 0 1 _ C E H ; 
 	 d e l a y ( ) ; 
 	 / /     T h i s   d e v i c e   i s   n o w   r e a d y   t o   r e c e i v e   o n e   p a c k e t   o f   1 6   b y t e s   p a y l o a d   f r o m   a   T X   d e v i c e   s e n d i n g   t o   a d d r e s s 
     / /     ' 3 4 4 3 1 0 1 0 0 1 ' ,   w i t h   a u t o   a c k n o w l e d g m e n t ,   r e t r a n s m i t   c o u n t   o f   1 0 ,   R F   c h a n n e l   4 0   a n d   d a t a r a t e   =   2 M b p s . 
 
 } 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / 
 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 F u n c t i o n :   T X _ M o d e ( ) ; 
 
 D e s c r i p t i o n : 
     T h i s   f u n c t i o n   i n i t i a l i z e s   o n e   n R F 2 4 L 0 1   d e v i c e   t o 
     T X   m o d e ,   s e t   T X   a d d r e s s ,   s e t   R X   a d d r e s s   f o r   a u t o . a c k , 
     f i l l   T X   p a y l o a d ,   s e l e c t   R F   c h a n n e l ,   d a t a r a t e   &   T X   p w r . 
     P W R _ U P   i s   s e t ,   C R C ( 2   b y t e s )   i s   e n a b l e d ,   &   P R I M : T X . 
 
     T o D o :   O n e   h i g h   p u l s e ( > 1 0 u s )   o n   C E   w i l l   n o w   s e n d   t h i s 
     p a c k e t   a n d   e x p e x t   a n   a c k n o w l e d g m e n t   f r o m   t h e   R X   d e v i c e .   * / 
 / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / 
 v o i d   T X _ M o d e ( v o i d ) 
 { 
 	 n R F 2 4 L 0 1 _ C E L ; 
 	 d e l a y ( ) ; 
     	 S P I _ W r i t e _ B u f ( W R I T E _ R E G   +   T X _ A D D R ,   T X _ A D D R E S S ,   T X _ A D R _ W I D T H ) ;         / /   W r i t e s   T X _ A d d r e s s   t o   n R F 2 4 L 0 1 
     	 S P I _ W r i t e _ B u f ( W R I T E _ R E G   +   R X _ A D D R _ P 0 ,   T X _ A D D R E S S ,   T X _ A D R _ W I D T H ) ;   / /   R X _ A d d r 0   s a m e   a s   T X _ A d r   f o r   A u t o . A c k 
     	 S P I _ W r i t e _ B u f ( W R _ T X _ P L O A D ,   B u f f e r ,   T X _ P L O A D _ W I D T H ) ;   / /   W r i t e s   d a t a   t o   T X   p a y l o a d 
 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   E N _ A A ,   0 x 0 1 ) ;             / /   E n a b l e   A u t o . A c k : P i p e 0 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   E N _ R X A D D R ,   0 x 0 1 ) ;     / /   E n a b l e   P i p e 0 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   S E T U P _ R E T R ,   0 x 1 a ) ;   / /   5 0 0 u s   +   8 6 u s ,   1 0   r e t r a n s . . . 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   R F _ C H ,   4 0 ) ;                 / /   S e l e c t   R F   c h a n n e l   4 0 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   R F _ S E T U P ,   0 x 0 7 ) ;       / /   T X _ P W R : 0 d B m ,   D a t a r a t e : 2 M b p s ,   L N A : H C U R R 
     	 S P I _ R W _ R e g ( W R I T E _ R E G   +   C O N F I G ,   0 x 0 e ) ;           / /   S e t   P W R _ U P   b i t ,   e n a b l e   C R C ( 2   b y t e s )   &   P r i m : T X .   M A X _ R T   &   T X _ D S   e n a b l e d . . 
 	 n R F 2 4 L 0 1 _ C E H ; 
 	 d e l a y ( ) ; 
 	 
 } 
 / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 