| 
	
	|  |  |  
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| Guest Guest
 
 
 
 
 
 
 
			
			
			
			
			
			
			
			
			
 
 | 
			
				| portb interrupt |  
				|  Posted: Sun Aug 28, 2005 7:51 pm |   |  
				| 
 |  
				| in the example EX_PBUTT.C, what is the purpose of the statement "last=current' in the detect_rb_change function? last is always set to 0 whenever an interrupt happens, is that right? 
 
  	  | Code: |  	  | void detect_rb_change() { int current;
 static int last=0;
 
 set_tris_b(0xF0);
 current=input_b();
 
 #if LOWTOHIGH
 if ((!bit_test(last,4))&&(bit_test(current,4))) {dbutton4=1;}
 if ((!bit_test(last,5))&&(bit_test(current,5))) {dbutton5=1;}
 if ((!bit_test(last,6))&&(bit_test(current,6))) {dbutton6=1;}
 if ((!bit_test(last,7))&&(bit_test(current,7))) {dbutton7=1;}
 #elif HIGHTOLOW
 if ((!bit_test(current,4))&&(bit_test(last,4))) {dbutton4=1;}
 if ((!bit_test(current,5))&&(bit_test(last,5))) {dbutton5=1;}
 if ((!bit_test(current,6))&&(bit_test(last,6))) {dbutton6=1;}
 if ((!bit_test(current,7))&&(bit_test(last,7))) {dbutton7=1;}
 #endif
 
 last=current;
 }
 | 
 
 regards
 |  |  
		|  |  
		| PCM programmer 
 
 
 Joined: 06 Sep 2003
 Posts: 21708
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Aug 28, 2005 7:57 pm |   |  
				| 
 |  
				|  	  | Quote: |  	  | last is always set to 0 whenever an interrupt happens, is that right? | 
 It's only set to zero initially.
 |  |  
		|  |  
		| Guest Guest
 
 
 
 
 
 
 
			
			
			
			
			
			
			
			
			
 
 | 
			
				|  |  
				|  Posted: Sun Aug 28, 2005 8:18 pm |   |  
				| 
 |  
				| isn't this function executes at each interrupt so that "last" will always be 0 as it is initialized at each interrupt? |  |  
		|  |  
		| PCM programmer 
 
 
 Joined: 06 Sep 2003
 Posts: 21708
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Aug 28, 2005 11:00 pm |   |  
				| 
 |  
				| It's not initialized at each interrupt.   It's only initialized when the program first starts running.
 
 I'll show you how you can check this.   First make a small test program:
 
  	  | Code: |  	  | #include <16F877.H>
 #fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
 #use delay(clock = 4000000)
 
 #int_rb
 void int_rb_isr(void)
 {
 int current;
 static int last = 0;
 
 last = current;
 }
 //=====================
 void main()
 {
 
 while(1);
 }
 
 | 
 
 Then compile it and look at the .LST file.
 Here's the symbol table:
 
  	  | Code: |  	  | int_rb_isr.last                   00000028
 int_rb_isr.current                00000029
 
 | 
 
 Below, notice that at address 0042, 'last' is cleared (set = 0).
 But notice that address 0042 is part of the main() code.
 If you follow the addresses in main(), they start at 0039
 and then they go down to 0040, and there's a gap.
 CCS puts the code for the isr inside the isr to make it
 easy to read the .LST file, but the "last = 0" code isn't
 executed inside the isr.   It's just done once, in main(),
 when the program starts.
 
 
  	  | Code: |  	  | 0000                 ... #int_rb
 0000                 ... void int_rb_isr(void)
 0000                 ... {
 0000                 ... int current;
 0000                 ... static int last = 0;
 0041 1283        BCF    03.5
 0042 01A8        CLRF   28
 0000                ...
 0000                ...
 0000                ... last = current;
 0033 0829        MOVF   29,W
 0034 00A8        MOVWF  28
 0000                ... }
 0000                ...
 0000                ...//=========================
 0035 100B        BCF    0B.0
 0036 118A        BCF    0A.3
 0037 120A        BCF    0A.4
 0038 281D        GOTO   01D
 0000                ... void main()
 0000                 ... {
 0039 0184        CLRF   04
 003A 301F        MOVLW  1F
 003B 0583        ANDWF  03,F
 003C 1683        BSF    03.5
 003D 141F        BSF    1F.0
 003E 149F        BSF    1F.1
 003F 151F        BSF    1F.2
 0040 119F        BCF    1F.3
 0000               ... while(1);
 0043 2843        GOTO   043
 0000               ... }
 0044 0063        SLEEP
 | 
 |  |  
		|  |  
		| asmallri 
 
 
 Joined: 12 Aug 2004
 Posts: 1660
 Location: Perth, Australia
 
 
			        
 
 | 
			
				|  |  
				|  Posted: Mon Aug 29, 2005 3:34 am |   |  
				| 
 |  
				|  	  | Guest wrote: |  	  | isn't this function executes at each interrupt so that "last" will always be 0 as it is initialized at each interrupt? | 
 
 No. The variable is declared as static and therefore retains its value between interrupts. It is initially set to zero on program startup.
 _________________
 Regards, Andrew
 
 http://www.brushelectronics.com/software
 Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
 |  |  
		|  |  
		|  |  
  
	| 
 
 | You cannot post new topics in this forum You cannot reply to topics in this forum
 You cannot edit your posts in this forum
 You cannot delete your posts in this forum
 You cannot vote in polls in this forum
 
 |  
 Powered by phpBB © 2001, 2005 phpBB Group
 
 |