How can I check my counter is already zero ?

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
Guys,

How can I check if my counter is already zero on ledmgr.asm ?
Please see the code

thanks

timer.asm
Code:
;
;
;	File: timer.ASM
;

	list	p=16f628A
	#include p16f628A.inc
	#include globals.inc

	extern counter
	;extern counter1
	;extern sectimer
	
	

;------------------------------------------------------------------------------
BANK0	UDATA
;------------------------------------------------------------------------------
COUNT		RES	1	;COUNT OF INTERRUPTS
COUNT1		RES	1	;COUNT FOR  1 SECOND TIMER
COUNT2      RES 1
COUNT3		RES 1
;------------------------------------------------------------------------------
PROG0		CODE			; program code section
;------------------------------------------------------------------------------
TimerInt 	; Interrupt routine

		global TimerInt		; Define as global 

intsOff		bcf INTCON, T0IE	;Disable timer interrupts
			bcf INTCON, T0IF    ;clear interrupt flag
            bsf INTCON, GIE     ;Enable global interrupt
	
;-------------- Insert no new code above here ---------------------------------
	 
        	
		;*********************************************************************
		; 1 ms timer function calls
		;*********************************************************************
		bcf  STATUS, RP0		;Select Bank 0
		bcf  STATUS, RP1	

		;....... 1ms  timer counter here.....	
		;call sectimer		
		;call counter
           
	    ;test toggle LED here...
        ;bcf  	PORTB, POWERLED 		;Turn off LED 
        
         
		;....................................
	
		incf  COUNT, f
		movlw  .10		;Compare count to 10 decimal
		subwf COUNT, W		;Store the result back in 
		btfss STATUS, C		;Is the result 0, skip if no
    	goto  exittimer
		clrf  COUNT		;Clear count register
       
		;**********************************************************************
		; 10ms timer function calls
		;**********************************************************************
		;....... 10ms  timer counter here.....	
		;call counter
		;
        ;test toggle LED off here...
		;bcf  	PORTB, POWERLED 		;Turn off LED 
		
		;....................................

    	incf    COUNT1, f
		movlw   .10			    ;Compare count to 100 decimal
		subwf   COUNT1, W		;Store the result back in 
		btfss   STATUS, C		;Is the result 0, skip if no
		goto    exittimer
		clrf 	COUNT1
		;**********************************************************************
		; 100ms timer function calls
		;**********************************************************************
		;....... 100ms  timer counter here.....		
		;
		
		;call counter			
			
        
		;test toggle LED on here...
        ;bsf  	PORTB, POWERLED 		;Turn on LED 
		
		;....................................

        incf    COUNT2, f
		movlw   .10			    ;Compare count to 100 decimal
		subwf   COUNT2, W		;Store the result back in 
		btfss   STATUS, C		;Is the result 0, skip if no
		goto    exittimer
		clrf 	COUNT2
		;**********************************************************************
		; 1000ms timer function calls
		;**********************************************************************
		;....... 1000ms  timer counter here.....		
		;call counter		

		incf    COUNT3, f
		movlw   .30			    ;Compare count to 100 decimal
		subwf   COUNT3, W		;Store the result back in 
		btfss   STATUS, C		;Is the result 0, skip if no
		goto    exittimer
		clrf 	COUNT3
		;**********************************************************************
		; 30000ms timer function calls
		;**********************************************************************
		;....... 30000ms  timer counter here.....		
        call counter 	
        
exittimer       
		movlw 	.217		;count 14
		clrf TMR0 ;Clear TMR0 and prescaler
		banksel TMR0
		movwf TMR0
		
        
        
;-------------- Insert no new code below here ---------------------------------
		bcf INTCON, T0IF	;Clear timer overflow flag
		bsf INTCON, T0IE	;Enable interrupts again
		return
;----------------------------------------------------------------------------------
;**********************************************************************************
initTimer
 Global	 initTimer
 GLOBAL  COUNT
 	clrf	COUNT
	return
	end

and check the counter here :
ledmgr.asm
Code:
  LIST P=16F628A
  #include p16f628A.inc
  #include globals.inc
;------------------------------------------------------------------------------- 
 extern COUNT
;-------------------------------------------------------------------------------

;-------------------------------------------------------------------------------
BANK0 UDATA
;-------------------------------------------------------------------------------
;..... your variables here ....
LedState 	RES	1
LedTimer    RES 1
timersec 	RES 1

	Global 	LedTimer,LedState,timersec,counter
;-------------------------------------------------------------------------------
JUMPTAB0	CODE
;-------------------------------------------------------------------------------
LEDManager
 GLOBAL LEDManager
		movlw 	.10	            
		;movlw	.1       
		BANKSEL LedTimer		;Loading timer value
		movwf 	LedTimer		;to 30sec
	    movlw 	high LEDManager
		movwf	PCLATH
		
		;btfss INTCON,T0IF
		;goto	exit
		
		movf    LedState,w
		addwf   PCL,f	
        goto    led_state_1
	    goto    led_state_2 

;-------------------------------------------------------------------------------
PROG0	CODE
;-------------------------------------------------------------------------------


;...... your state machine here ......
;led_state_1 begin
led_state_1	
				;call 	counter
	
				bsf  	PORTB, POWERLED ;Turn on LED
			    ;wait if there's interrupt				
				;btfss	STATUS,Z
				
				
				incf     LedState,1		 ;increase the state, for the next state
				
				return
;led_state_1 end
;-------------------------------------------------------------------------------
;led_state_2 begin		
led_state_2
				
                			
				bcf		PORTB, POWERLED ;Turn off LED
                ;btfss	STATUS,Z
				;goto	exit
	                        ;how can I check if my counter already zero here ?			
				;jump to previous state if there's interrupt
				
				decf    LedState,1		;increase the state, for the next state          
				return
;led_state_2 end

;-------------------------------------------------------------------------------
;--------------- Called in Initialisation ----------------------
initState
	Global 	initState
	movlw 	0x00
	BANKSEL	LedState		;Initialising
	movwf 	LedState		;State Value to '0'

	movlw 	.10	
	;movlw	.1				;test for 1s
	BANKSEL LedTimer		;Loading seconds timer
	movwf 	LedTimer		;to 10 
	return
;--------------------------------------------------------
;---------------------- Called in States ------------------
;counter	
;		Global	counter	
;		BANKSEL LedTimer		
;		movf 	LedTimer,w		;Checking timervalue
;		btfsc 	STATUS,Z		;reaches 0 or not?
;		return					;Yes-Return
;		decf 	LedTimer,f		;No-decrement value
;		return 					;and return

;------------------Call in Timer Module --------------------
counter
	Global 	counter			;Decrementing Sec's  
	BANKSEL LedTimer		;timer value
    movf 	LedTimer,w		;for every 10 counts
    btfss 	STATUS,Z
    decf 	LedTimer,f
   
	return
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;test
;------------------Call in Timer Module --------------------
;counter
;	Global 	counter			;Decrementing Sec's  
;	BANKSEL LedTimer		;timer value
;    movlw 	.10				;for every 10 counts
;    movwf	LedTimer
;check decfsz LedTimer
;      goto	 check
   
;	return
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	
 exit
 return ;LEDManager
 
		END
 

Do you want to test if counter is 0?

DECFSZ f,d
Decrement f. Skip the following instruction if clear.
f-1 -> d skip if Z = 1

decfsz counter

if counter is 0 then the next instruction is skipped. If counter is 0 zero flag (Z) is set to 1.
 

so it would be like this :
Code:
decfsz COUNTER
bsf  	PORTB, POWERLED ;Turn on LED
incf     LedState,1		 ;increase the state, for the next state

thank you
 

The easiest check to see if a variable is zero is to load it to itself "movf <variable name>,f" which does not use or change any variables but does set the Z flag in the STATUS register if <variable name> contains zero.

Brian.
 

similar like this, if the variable of the counter is COUNT ?
Code:
				;call 	counter
	            movf    COUNT
				 ; Now you can check the Z flag:
				btfsc  STATUS,Z
				goto   next  ; Counter is zero...
				nop          ; Counter is non-zero...
				bsf  	PORTB, POWERLED ;Turn on LED
			    ;wait if there's interrupt				
				;btfss	STATUS,Z
				
				
	next		incf     LedState,1		 ;increase the state, for the next state
 

Almost!

The code will work but only because the compiler is making an assumption about where the 'movf' puts the result. You should really code the line as "movf COUNT,f" (add comma f) so the compiler is sure you don't want to put the result in W instead. Try to use 'f' when the destination is the source register and 'w' when the destination is the W register, it makes it easier to read than 1 or 0.

Brian.
 

so it would be ?
Code:
		      ;call 	counter
	                        movf    COUNT,f
				 ; Now you can check the Z flag:
				btfsc  STATUS,Z
				goto   next  ; Counter is zero...
				nop          ; Counter is non-zero...
				bsf  	PORTB, POWERLED ;Turn on LED
			    ;wait if there's interrupt				
				;btfss	STATUS,Z
				
				
	next		incf     LedState,1		 ;increase the state, for the next state

- - - Updated - - -

it doesn't work properly yet.....first time it waits but after that it's blinking without waiting for the counter anymore...
I gave you the complete code so you can have a look...
thanks

- - - Updated - - -

complete files, please have a look ,thank you
 

Attachments

  • test.rar
    29.9 KB · Views: 52

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