PDA

View Full Version : Simple Interrupt Program



eoasap
- 13th October 2005, 23:00
hi everyone. I wrote a program that displays text to an LCD screen (lets say print the screen, wait a second, repeat about 10 times).

On portB.0 (int) on PIC16F84 i have a pushbutton that counts the # of button presses (continued from a different question i had earlier). The hardware is setup properly because i have used it to count & display the results in the MAIN code.

What i'm trying to do is setup an interrupt where when a button is pressed, i'll go to my interrupt code, increment the counter (# of button presses), display the # of button presses to the LCD and then resume the rest of the program.

Here is mthe relevant code so far.


__________________________________________________ ____________
'Definitions
symbol LED = PORTB.1
PushButton var PORTB.0

OPTION_REG = $7f 'enable PORTB pullups
INTCON = %10010000 'enable RB0 interrupt
TRISB=%00000001
CounterA=0 'set count value to zero
on interrupt goto myint 'define interrupt handle

{
main code
}


'interrupt handling
disable 'disable interrupts while in interrupt
myint: 'counts & displays a message if there's a button pressed

CounterA=CounterA+1
Lcdout $fe, 1, "Button Presses =" ' Clear LCD screen
Lcdout $FE, $c0, #CounterA
While Pushbutton=0:Wend

INTCON.1 = 0 'clear interrupt flag
resume 'return to main program
Enable 'enables the interrupt after handler
__________________________________________________ ___________

As of right now this is what happens. if i leave the pushbutton alone, it'll run the program normally. if i press the button, it just restarts the entire sequence of code. kind of like it acknowledges the interrupt, but then doesn't do my interrupt code (or even return to the proper place where i was in the code before the interrupt). I even put END instead of RESUME at the end of the interrupt but all that happens is it starts the program over when i hit the pushbutton.

Any ideas what i'm doing wrong?
Glenn

mister_e
- 15th October 2005, 14:43
OPTION_REG = $7f 'enable PORTB pullups

OK you enable the internal Pull-ups, so your interrupt should happen on a Falling Edge right?

what about


OPTION_REG=%00111111


maybe also safe to add a PAUSE 20-50 after the WHILE:WEND to debounce the PB contact.

eoasap
- 15th October 2005, 16:21
ok, got it working! Thanks Steve. also added the pause and added a capacitor across the pushbutton to create an RC time constant and deal with the debounce.

You guys are so helpful on here!

mister_e
- 15th October 2005, 16:41
you're welcome.

Actually you don't need the capacitor, the debounce is done by software.

Usually i choose 50mS as debounce delay.

arthurok
- 28th October 2005, 10:44
if you are scanning switches the capacitor dosent slow down your program "pauses and "button" command debouncing does.

some engineer told me many years ago the computer does as its told you are its master and superior

mister_e
- 28th October 2005, 16:22
sure! but we talk about <50msec... even <20mSec. it's also depend what else your program is doing and how your program is done. Interrupt can happen really fast on some software.. slower on other. Always case by case.

PushButtons aren't 0-1 signal... still present some odd result when releasing them.

By software: We just check and insert a safe delay prior to return to the main program.

By using capacitor: it just smooth the signal when releasing the switch... the old CMOS-TTL-...-.... to do it. I prefer the software method in many case. No additional parts and even if you have the most crapy switch in the world, you just need to modify 1 code line OR constant variable. Handy with some tact switches brand...