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.

Interrupt on GP Change problem

Status
Not open for further replies.

Dan East

New Member
I've been working with this for a couple days, and can't get it to behave properly. This minimal code should use interrupts to simply set the output state based on the input state. I've tried this without interrupts (basically just using the interrupt handler as the main loop) and it works fine, so I know my inputs and outputs are right.

Any insight as to why this doesn't work will be greatly appreciated!

Code:
#define BUTTON_BIT	2
#define OUTPUT_BIT	4

	LIST	p=12F629
	include "P12F629.inc"
	__config b'10000110000100'
	
	org	0x0000
	goto Setup
	
	org	0x0004
	goto Interrupt

Setup
	;turn comparators off					
	movlw	0x07
	movwf	CMCON

	bsf 	STATUS,		RP0	;Bank 1
	
	;Specify input ports
	clrf	TRISIO
	bsf		TRISIO, BUTTON_BIT

	;Enable pull up on input
	clrf	WPU	
	bsf		WPU, BUTTON_BIT

	;Set interrupt on change for input
	clrf	IOC
	bsf		IOC, BUTTON_BIT
	
	;Callibrate internal oscillator
	call	3FFh 
	movwf	OSCCAL
	
	;Set options (enable global weak pull ups)
	movlw	b'01111111'
	movwf	OPTION_REG
		
	bcf		STATUS, RP0 ;Bank 0

	;Read input state
	movf	GPIO,w
	
	;Enable global interrupts and GPIO change interrupt	
	bsf		INTCON, GPIE		
	bsf		INTCON, GIE

	;infinite loop
	goto $
	
Interrupt
	;Clear interrupt flag
	bcf		INTCON,GPIF
	
	;Set output based on input state
	btfss	GPIO,BUTTON_BIT
	goto	$+3
	bsf		GPIO,OUTPUT_BIT
	goto	$+2
	bcf		GPIO,OUTPUT_BIT
	
	retfie

	end
 
I did some more testing and I think I have a better handle on the problem. It seems that an interrupt is only being generated when the input goes high, but not when it goes low.

I thought an interrupt would be generated in both cases. Am I misinterpreting the datasheet?

Dan East
 
Okay, I've gotten it to work after modifying my circuit, however I'm not exactly sure why it wouldn't work properly before.

I'm using the internal pull up, and pulling the pin low externally using a 560 ohm resistor. Now as I said, if I read the pin state in a loop without using interrupts it behaves as it should - the GPIO state matches the physical switch. So I tried removing the 560 ohm resistor, and that correct the problem - the interrupts now work properly. So I can only surmise that the input tolerances required to trigger an interrupt are somehow different than those required to set the pin state. My guess is there is some logic used to properly interpret analog input so it will correctly trigger interrupts on a digital input, and since I wasn't pulling it all the way down to 0 volts it wasn't resetting that logic.

Maybe this will save someone else some trouble in the future. :)

Dan East
 
Status
Not open for further replies.

Latest threads

Back
Top