Counting timer0 overflow for flipping LED ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Visit site
Activity points
9,442
Hi Guys,

Please correct me if I'm wrong,
I tried to count the overflow for creating a delay....
Are they correct ?

Thanks

Code:
  	LedTimer EQU .100 ;100 decimal (for content of the counter to get 100ms)

	movf LedTimer,W ; load value to W , Status register bit Zero is altered according to value

	      ;count for how many interrupts for the next state	
		  counting	 btfss  INTCON,T0IF         ;is timer0 interrupt flag true ?
					 bcf 	INTCON,T0IF		    ;if T0IF=false clear timer0 interrupt flag
				 	 decf	LedTimer,1          ;if T0IF=true reduce the counter
                     ;check if LedTimer = 0 ?
					 btfsc 	STATUS, Z 			;Z bit is set to 1 if W was 00 - skip next instruction if not 00
		     	     ;LedTimer=0
					 goto   next_state      	;from btfsc if it's 1, go to next state
                     ;LedTimer!=0
		 			 goto   counting			;from btfsc if it's 0 keep counting interrupt 
		
		next_state	incf    LedState,1			; if 00 increase the state, for the next state               
				   
					return
 

Hi,

Your logic is basically correct, but the execution is not.

The first BTFSS instruction, if the Flag is Set to 1, then the next instruction is skipped.

You are saying if the flag is set to 1, skip and decrement LedTimer - ok
but if the flag is 0, no not skip, but clear the flag, THEN the next instruction is performed which is Dec your counter.

You should be doing,

Code:
btfss INTCO,T0IF
goto  flag_not_set         ;if, T0IF=not true  go out to ...
 bcf 	INTCON,T0I         ;if T0IF=true clear timer0 interrupt flag
 decf	LedTimer,1          ;if T0IF=true reduce the counter
 

or just ?
Code:
btfss INTCO,T0IF
nop
 bcf 	INTCON,T0I         ;if T0IF=true clear timer0 interrupt flag
 decf	LedTimer,1          ;if T0IF=true reduce the counter

- - - Updated - - -

or like this :
Code:
counting	 btfss  INTCON,T0IF         ;is timer0 interrupt flag true ?
					 goto   counting
					 bcf 	INTCON,T0IF		    ;if T0IF=false clear timer0 interrupt flag
				 	 decf	LedTimer,1          ;if T0IF=true reduce the counter
                     ;check if LedTimer = 0 ?
					 btfsc 	STATUS, Z 			;Z bit is set to 1 if W was 00 - skip next instruction if not 00
		     	     ;LedTimer=0
					 goto   next_state      	;from btfsc if it's 1, go to next state
                     ;LedTimer!=0
		 			 goto   counting			;from btfsc if it's 0 keep counting interrupt 
		
		next_state	incf    LedState,1			; if 00 increase the state, for the next state               
				   	return
 

Hi,

The first example has no point if you use a NOP as one of the instructions.

The second one looks ok.

You are not showing your full program code, so do not know how much other code you are using, but think if you use MPLABs Debugger, SIM. you can single step though your code as see exactly what the registers and program flow is doing.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…