Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to check the content if it's zero ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
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 the content of LedTimer equal to zero and jump to somewhere ?
Thanks


Code:
movlw LedTimer       	     ;load the content of LedTimer 
                 movwf COUNT_TIMER           ;load the LedTimer into file register
 

Hi,

Use -

movf COUNT_TIMER,W ; load value to W , Status register bit Zero is altered according to value
btfsc STATUS, Z ; Z bit is set to 1 if W was 00 - skip next instruction if not 00
call routineA ; if 00 call this routine
any instruction ; if not 00 carry on here
 

like this :
Code:
movf COUNT_TIMER,W ; load value to W , Status register bit Zero is altered according to value
btfsc STATUS, Z ; Z bit is set to 1 
if W was 00 - skip next instruction 
if not 00 call routineA ; 
if 00 call this routine
any instruction ; 
if not 00 carry on here

- - - Updated - - -

for example I have COUNT_TIMER = 100, how can I reduce it with a tick of background interrupt ?
thanks
 

like this :
Code:
movf COUNT_TIMER,W ; load value to W , Status register bit Zero is altered according to value
btfsc STATUS, Z ; Z bit is set to 1 
if W was 00 - skip next instruction 
if not 00 call routineA ; 
if 00 call this routine
any instruction ; 
if not 00 carry on here

- - - Updated - - -

for example I have COUNT_TIMER = 100, how can I reduce it with a tick of background interrupt ?
thanks

Hi,

To prove you code logic works ok us MPLABs, Debugger, MPlab SIM and single step though the code, watching W, and the registers being updated.

how can I reduce it with a tick of background interrupt ?

Not sure what you mean there - the simple way to reduce a value is by the DECF instruction.
Using an Interrupt is something else - you will have to say where the interrupt comes from; an internal source like a Timer or some external event ?
 

Not sure what you mean there - the simple way to reduce a value is by the DECF instruction.
Using an Interrupt is something else - you will have to say where the interrupt comes from; an internal source like a Timer or some external event ?

I see, the interrupt comes from Timer0, how can I reduce my value on COUNTER_TIMER, depends on tick from Timer0 ?
Timer0 is in the background and I need its overflow to reduce COUNTER_TIMER, is it related with T0IF, how can I manage it ?

thanks
 

When the interrupt occurs, check if T0IF caused it, if it did, clear T0IF then reload your timer value if necessary then decrement COUNTER_TIMER then leave the interrupt.

Brian.

can I see the example in code ?
thank you
 

I can't find an exact example and you haven't told us what kind of PIC you are using but this should give you an idea.

Code:
ISR
	ORG     0x004             	;interrupt vector location
	movwf   w_temp            	;save off current W register contents
	movf	STATUS,w          	;move status register into W register
	movwf	status_temp       	;save off contents of STATUS register

CheckUSART
	banksel	PIR1
	btfss	PIR1,RCIF		;see if UART caused interrupt
	goto	CheckTMR1
	banksel	RCREG
	movf	RCREG,W			;get the received byte & clear RCIF
	movwf	BusRx
	bsf	Events,BusBytePending
	goto	ISREnd
	
CheckTMR1
	btfss	PIR1,TMR1IF
	goto	ISREnd
	bcf	PIR1,TMR1IF		;clear the interrupt flag
	movf	RelockTimer,f		;set Z if zero
	btfss	STATUS,Z
	decf	RelockTimer,f
	movf	BeepTimer,f
	btfss	STATUS,Z
	decf	BeepTimer,f
	movf	BellLockoutTimer,f
	btfss	STATUS,Z
	decf	BellLockoutTimer,f
	banksel TMR1H
	movlw	0x1F			;reload the timer
	movwf	TMR1H
	clrf	TMR1L
ISREnd
	movf    status_temp,w     	;retrieve copy of STATUS register
	movwf	STATUS            	;restore pre-isr STATUS register contents
	swapf   w_temp,f
	swapf   w_temp,w          	;restore pre-isr W register contents
	retfie                    	;return from interrupt

This is for a 16F628A and it uses TMR1 rather than TMR0 but the principle is the same. In my example, there are two interrupts, one is the USART and the other is TMR1. You will see it first checks to see if the USART caused the interrupt and if it was, it clears the interrupt flag and picks up the character received by the USART. If it wasn't the USART, it checks TMR1 and if if that caused it, the TMR1IF is cleared and several counters are decremented to zero if they are not already there. The code is based on the template provided by Microchip in MPLAB/MPLABX.

Brian.
 

how about this one, I want the counter is depending on timer0 overflow and after the counter = 0 jump to another state,
any ideas ?

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

counting	 btfss  INTCON,T0IF         ;is timer0 interrupt flag true ?
					 goto zero_status
					 bcf 	INTCON,T0IF		    ;if T0IF=false clear timer0 interrupt flag
				 	 decf	LedTimer,1          ;if T0IF=true reduce the counter
                     ;check if LedTimer = 0 ?
		zero_status	 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
 

That should work but be careful where you call it from and where you place the 'counting' label. Remember that you are in an interrupt routine so before reaching your code, if you are using a mid-range PIC, you should have saved at least the W and STATUS registers and you should restore them before going back to your main program with a 'retfie' instruction. Jumping out of an interrupt routine is asking for trouble unless you are very careful about how you return. I suggest you copy the template code for your PIC as the starting point of your program then add your own code to it. The template already holds the basic structure of a program, including the ISR and return code.

Be careful how you define LedTimer, don't confuse it's value with it's address.

Brian.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top