PDA

View Full Version : how to use INTERRUPT?



keithv
- 7th August 2014, 20:02
The code below blinks an LED on and off at two different rates. It switches between the two rates when a button is pressed. The button is connected to GPIO.1 of a PIC12F609.

The code works fine, or I should say, it works exactly as I expect it to. The problem is that I would like the program to jump to the "buttonpress" subroutine as soon as the as the button makes GPIO.1 go low. As it is now, it needs to wait for the LED to finish its blink loop before it will recognize the button.

Is an ON INTERRUPT statement what I want to use here? I've tried using it, but I don't think I'm setting it up correctly.


length var word
LED var GPIO.0
pushbutton var GPIO.1
ANSEL = 0 ' Set all digital
CMCON0 = 0 ' Analog comparators off
TRISIO = %00000010
length = 100


main:
do
LED = 1
pause length
LED = 0
pause length

if pushbutton = 0 then
goto buttonpress
endif
loop
goto main
end


buttonpress:

pause 10 'debounce switch press
if length = 100 then
length = 500
elseif length = 500 then
length = 100
endif

do while pushbutton = 0 'wait for switch to be released
pause 5
loop

pause 10 'debounce switch release

goto main

andywpg
- 7th August 2014, 23:49
This will answer all - works great!

http://darreltaylor.com/DT_INTS-14/intro.html

keithv
- 8th August 2014, 01:02
Excellent! Thank you.

keithv
- 8th August 2014, 01:30
I'm doing something wrong. I'm trying the first "hello world" program. I'm guessing PBP3 doesn't like that I've selected the 12F609 as my device. I want to change to a "falling edge". And I want to change the INT input pin. Where/how do I do that? I've installed the .bas files and they come up in my tabbed browser as defines, but I don't really know what I'm looking at.

richard
- 8th August 2014, 02:19
your not giving us much to work on , but from the data sheet and if you are using INT/int on gp2 then


12.4.1 GP2/INT INTERRUPT
The external interrupt on the GP2/INT pin is edgetriggered;
either on the rising edge if the INTEDG bit of
the OPTION register is set, or the falling edge, if the
INTEDG bit is clear. When a valid edge appears on the
GP2/INT pin, the INTF bit of the INTCON register is set.
This interrupt can be disabled by clearing the INTE
control bit of the INTCON register. The INTF bit must
be cleared by software in the Interrupt Service Routine
before re-enabling this interrupt. The GP2/INT interrupt
can wake-up the processor from Sleep, if the INTE bit
was set prior to going into Sleep. See Section 12.7
“Power-Down Mode (Sleep)” for details on Sleep and
Figure 12-9 for timing of wake-up from Sleep through
GP2/INT interrupt.

if you want more help post your code and a sketch/schematic of your setup

EarlyBird2
- 8th August 2014, 06:05
The code below blinks an LED on and off at two different rates. It switches between the two rates when a button is pressed. The button is connected to GPIO.1 of a PIC12F609.

The code works fine, or I should say, it works exactly as I expect it to. The problem is that I would like the program to jump to the "buttonpress" subroutine as soon as the as the button makes GPIO.1 go low. As it is now, it needs to wait for the LED to finish its blink loop before it will recognize the button.

Is an ON INTERRUPT statement what I want to use here? I've tried using it, but I don't think I'm setting it up correctly.


ON INTERRUPT waits for the pause to complete before executing the interrupt routine. To get over this issue use short pauses in a loop. Here is a non interrupt option.



I var word
length var word
LED var GPIO.0
pushbutton var GPIO.1
ANSEL = 0 ' Set all digital
CMCON0 = 0 ' Analog comparators off
TRISIO = %00000010
length = 100

main:

TOGGLE LED
for I = 1 to length
pause 1
if pushbutton = 0 then gosub buttonpress
next
goto main
end


buttonpress:

pause 10 'debounce switch press
if length = 100 then length = 500
if length = 500 then length = 100

do while pushbutton = 0 'wait for switch to be released
pause 5
loop

pause 10 'debounce switch release

return

keithv
- 8th August 2014, 17:58
your not giving us much to work on , but from the data sheet and if you are using INT/int on gp2 then



if you want more help post your code and a sketch/schematic of your setup

Ah....so the interrupt pin must to be GP2 (pin 5)? I can't use any other pin? And "OPTIONREG.6 = 0" would need to be added to my code, not changed in the .bas define files? I shouldn't have to modify anything in the .bas files right?

My code thus far looks exactly likes exactly like the example, but I changed the LED1 var to GPIO.0. I'm following the same schematic as well (going to different I/Os), but I haven't made it that far because PBP won't let me compile.

keithv
- 8th August 2014, 18:07
ON INTERRUPT waits for the pause to complete before executing the interrupt routine. To get over this issue use short pauses in a loop. Here is a non interrupt option.


Thanks! That does solve my immediate problem, but I'm planning on doing more complex stuff with the "length" var. Ultimately, I want the LED to continue blinking uninterrupted while the button is being pressed. I'm doing it one piece at a time, but ultimately the blinking LED will be in sync with a PWM, and the button will be a tap tempo to control the frequency of the PWM. The DT Instant Interrupts looks to be exactly what I need for this project. It's got the button. It's got the LED. ANd it's got the timer.