• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

2 different time delays, same register?

Status
Not open for further replies.

tempus

Member
Hey all;

I'm getting very close to completing my first PIC program. My design calls for 2 different time delays - 1 for debouncing (10ms) and 1 to control a mute function (2ms). The 2 never run at the same time (at least I don't think they do), so is it OK to use the same register for both counters?

Here's a sample of my code (16F737):

; Counter locations
dbcount equ 0x21h
dc1 equ 0x22h

; debounce switch down
db_down movlw 0x0d ;max count = 10ms/768us = 13 = 0x0d
movwf dbcount
clrf dc1 ;reset dc1
dn_dly incfsz dc1,f ;delay 256x3 = 768us
goto dn_dly
btfsc portb,7 ;if button up (RB7 hi),
goto db_down ; restart count
decfsz dbcount,f ;else repeat until max count is reached
goto dn_dly
bsf porta,6 ;turn mute on
movlw b'01101001'
movwf portc ;turn comp and delay on
bsf porta,5 ;output hi RA5 - vb off
nop
bsf porta,7 ;output hi RA7 - lead channel on
call mutedelay ;mute for 2ms
bcf porta,6 ;turn mute off
; debounce switch up
db_up movlw 0x0d ;max count = 10ms/768us = 13 = 0x0d
movwf dbcount
clrf dc1 ;reset dc1
up_dly incfsz dc1,f ;delay 256x3 = 768us
goto up_dly
btfss portb,7 ;if button down (RB7 lo),
goto db_up ; restart count
decfsz dbcount,f ;else repeat until max count is reached
goto up_dly
goto switch ;check switches again
;-----------------------------------------------------------------------------
; mutedelay - 2ms delay
;
mutedelay movlw 0x03 ;max count = 2ms/768us = 3
movwf dbcount
clrf dc1 ;reset dc1
incfsz dc1,f ;delay 256x3 = 768us
goto up_dly
decfsz dbcount,f ;else repeat until max count is reached
goto up_dly
return

Sorry everything's so jammed together, but i can't seem to get things spaced properly in the message window.
Thanks
 

ericgibbs

Well-Known Member
Most Helpful Member
hi
You can use a register counter for any count down routine, ensure that two routines dont use the reg at the same time.

When posting code, paste it to the window, select all of it and then click the hash '#' symbol in the upper rightside menu.

Code:
Here's	a	sample of my code (16F737):

; Counter locations  
dbcount	equ	0x21h
dc1	equ	0x22h

; debounce switch down  
db_down	movlw	0x0d		;max count = 10ms/768us = 13 = 0x0d
	movwf	dbcount
	clrf	dc1		;reset dc1
dn_dly	incfsz	dc1,f		;delay 256x3 = 768us
	goto	dn_dly
	btfsc	portb,7		;if button up (RB7 hi),
	goto	db_down		; restart count
	decfsz	dbcount,f	;else repeat until max count is reached
	goto	dn_dly
	bsf	porta,6		;turn mute on
	movlw	b'01101001'
	movwf	portc		;turn comp and delay on
	bsf	porta,5		;output hi RA5 - vb off
	nop
	bsf	porta,7		;output hi RA7 - lead channel on
	call	mutedelay	;mute for 2ms
	bcf	porta,6		;turn mute off
; debounce switch up  
db_up	movlw	0x0d		;max count = 10ms/768us = 13 = 0x0d
	movwf	dbcount
	clrf	dc1		;reset dc1
up_dly	incfsz	dc1,f		;delay 256x3 = 768us
	goto	up_dly
	btfss	portb,7		;if button down (RB7 lo),
	goto	db_up		; restart count
	decfsz	dbcount,f	;else repeat until max count is reached
	goto	up_dly
	goto	switch		;check switches again
;-----------------------------------------------------------------------------  
; mutedelay - 2ms delay  
;
mutedelay movlw	0x03		;max count = 2ms/768us = 3
	movwf	dbcount
	clrf	dc1		;reset dc1
	incfsz	dc1,f		;delay 256x3 = 768us
	goto	up_dly
	decfsz	dbcount,f	;else repeat until max count is reached
	goto	up_dly
	return
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top