Continue to Site

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.

  • 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.

RB0 External Interrupt Problem

Status
Not open for further replies.

Suraj143

Active Member
Hi all I’m doing a small led project. It has only three patterns. The patterns are changing through RB0 external switch.

Inside the Delay loop I’m checking the INTF flag bit. The patterns are not changing very well. Mostly it’s skipping the patterns & changing in a messy way.

Help me to solve this problem.

Thanks


Code:
	processor	PIC16F84A
	include		<P16F84A.inc>
	__config	3ffbh
	errorlevel	-302
		
	cblock	20h
	d1,d2,Select
	endc

	org 	0000h
Main	bsf 	STATUS,RP0
	movlw	b'00000001'
	movwf	TRISB
	bsf	INTCON,GIE	;enable global int enable
	bsf	INTCON,INTE	;enable ext interrupt
	bcf 	STATUS,RP0
	clrf	PORTB
	clrf	Select
	movlw	01h		;load 1 to Select reg
	movwf	Select
	goto	PAT1

************************************
Delay & detect which PATTERN to loop
************************************		

DEL0.1	btfsc	INTCON,INTF	;has external Int occur?
	goto	Choose		;YES,then goto choose pattern
	movlw	0x1F		;NO,then carry on the delay
	movwf	d1
	movlw	0x4F
	movwf	d2
DEL0.1A
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	DEL0.1A
	goto	$+1	
	return	
Choose	bcf	INTCON,INTF	;turn off ext interrupt
	incf	Select,F	;increment Select value by one
	movf	Select,W
	bcf	STATUS,Z
	xorlw	.2		;check the Select reg value 2 or not?
	btfsc	STATUS,Z
	goto	PAT2
	xorlw	.3		;check the Select reg value 3 or not?	
	btfsc	STATUS,Z
	goto	PAT3
	xorlw	.4		;check the Select reg value is 4?	
	btfsc	STATUS,Z
	goto	$+1		
	clrf	Select
	goto	PAT1

************
LED patterns
************

PAT1	movlw 	01H		;pattern 1
	movwf 	PORTB
	call 	DEL0.1	
	movlw 	02H
	movwf 	PORTB
	call 	DEL0.1
	movlw 	04H
	movwf 	PORTB
	call 	DEL0.1
	movlw 	0AH
	movwf 	PORTB
	call 	DEL0.1
	movlw 	14H
	movwf 	PORTB
	call 	DEL0.1
	movlw 	2AH
	movwf 	PORTB
	call 	DEL0.1
	movlw 	54H
	movwf 	PORTB
	call 	DEL0.1
	movlw 	28H
	movwf 	06H
	call 	DEL0.1
	movlw 	50H
	movwf 	PORTB
	call 	DEL0.1
	movlw 	20H
	movwf 	PORTB
	call 	DEL0.1
	movlw 	40H
	movwf 	PORTB
	call 	DEL0.1
	movlw 	00H
	movwf 	PORTB
	call 	DEL0.1		
	goto 	PAT1
		
	
PAT2	movlw 	02H		;pattern 2
	movwf 	PORTB
PAT2A	call 	DEL0.1
	bcf 	STATUS,C
	rlf 	PORTB,F	
	btfss 	PORTB,6
	goto 	$-4
PAT2B	call	DEL0.1
	rrf 	PORTB,F	
	btfss 	PORTB,1
	goto 	PAT2B		
	goto 	PAT2A
		
	
PAT3	movlw 	00H		;pattern 3
	movwf 	PORTB
	call 	DEL0.1	
	movlw 	02H
	movwf 	PORTB
	call 	DEL0.1
	movlw 	06H
	movwf 	PORTB
	call 	DEL0.1
	movlw 	0EH
	movwf 	PORTB
	call 	DEL0.1
	movlw 	1EH
	movwf 	PORTB
	call 	DEL0.1
	movlw 	3EH
	movwf 	PORTB
	call 	DEL0.1
	movlw 	7EH
	movwf 	PORTB
	call 	DEL0.1
	goto 	PAT3
	
	end
 
Hi,
I haven't used external interrupt, just timer interrupt :)
Just quick read the application note, there is no 'retfie (return from interrupt)' in your code right? It should be placed at the last line of the interrupt routine.
 
Ok Thank you guys I tried your way but still the problem is there.I put a new thread which my new routine is there.
 
Assuming you actually fixed your come (what you posted is waaaay off the mark) then I'd bet you are generating lots of interrupts every time you press the switch. Is the switch you hooked to rb0 debounced? I doubt it. It's generally a bad idea to hook an undebounced switch to an interrupt pin.

You need to read up on interrupts. it's not hard but you clearly don't understand them.

debouncing - when you close a switch, the contacts don't make an instaneous connection. since it's a mostly inelastic material, they tend to bounce around a bit. In the process they make and break the connection several times. Connected to an interrupt pin, you will see a number of interrupts in a short period of time. You need to figure out a way to supress the extra bounces. I would use a timer interrupt to read the state of the switch every few milliseconds and when the pin has had the same value for 20 mS or so, then I'd use the value of the pin. look here to learn more about debouncing http://www.ganssle.com/debouncing.pdf
 
Hi Philba you have read my mind. I was thinking one time to add the debouncing coding. For a normal I/O pin I’m using debouncing codings. But for RB0 like external interrupt pin I thought no need to debounce. But now it’s the other way I need to debounce RB0 as well.

Your PDF is excellent thank you very much for that. I was searching such a thing from a long time but today I got.

Thank you very much for your explanation.
 
Adding this line after org 0004h will disable the external interrupt enable bit.but make sure to enable back it again before returning from ISR.
Code:
	org	0004h
	bcf	INTCON,INTE
When the RB0 button press first spike will make to go to ISR, after clearing INTE bit the other poor ugly bouncing pulses will be ignored isn’t it?
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top