I am attempting to create an external interrupt on GPIO.2 and I can get the interrupt fine, but I am not able to return from it when I use the wait command inside of the interrupt as follows. It will return after several minutes even though i just have a wait of 1 second.
'Chip model
#chip 12F683, 8
#config INTOSCIO
'Set the pin directions
dir GPIO.0 out
dir GPIO.1 out
dir GPIO.2 in
dir GPIO.3 in
dir GPIO.4 in
dir GPIO.5 in
That's a good workaround, but there is one problem - directly altering INTCON.GIE isn't the best way to enable/disable the interrupt in GCBASIC.
Consider this code:
sub Outer
Set INTCON.GIE Off
Wait 1 sec
Inner
Wait 1 sec
Set INTCON.GIE On
end sub
sub Inner
Set INTCON.GIE Off
Set PORTB.0 on
Wait 1 sec
Set PORTB.0 off
Set INTCON.GIE On
end sub
The first Wait in "Outer" will be fine, but then "Inner" is called and the interrupt is re-enabled at the end of "Inner". Thus, the second Wait in "Outer" will be run with interrupts on.
A better way is to use "IntOn" and "IntOff". These are intended for just this purpose, and use a counter to keep track of whether interrupts should be on or not. Each call to IntOff clears INTCON.GIE if the counter is 0, then increments the counter. Each call to IntOn decrements the counter, and sets INTCON.GIE if the counter is 0. GCBASIC will also remove IntOn and IntOff from the program if no interrupts are used.
Also, you don't need IntOn at the start of a program - GCBASIC automatically enables interrupts if needed.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am attempting to create an external interrupt on GPIO.2 and I can get the interrupt fine, but I am not able to return from it when I use the wait command inside of the interrupt as follows. It will return after several minutes even though i just have a wait of 1 second.
'Chip model
#chip 12F683, 8
#config INTOSCIO
'Set the pin directions
dir GPIO.0 out
dir GPIO.1 out
dir GPIO.2 in
dir GPIO.3 in
dir GPIO.4 in
dir GPIO.5 in
IntOn
'ExtInt0
On Interrupt ExtInt0 Call Interrupt_test
asm bsf IOC, IOC2
DataPosition = 12
WriteData = 1
if GPIO.2 = 1 then
EPWrite(DataPosition, WriteData)
'SET GPIO.1 ON
end if
Start:
SET GPIO.0 ON
wait 1 sec
SET GPIO.0 OFF
wait 1 sec
'Jump back to the start of the program
goto Start
sub Interrupt_test
SET GPIO.1 ON
wait 1 sec
SET GPIO.1 OFF
wait 1 sec
end sub
I found a workaround for this, if you insert
asm bcf INTCON, GIE
wait 1 sec
asm bsf INTCON, GIE
in the non-interrupt code, you are able to use the wait command in your interrupt sub.
That's a good workaround, but there is one problem - directly altering INTCON.GIE isn't the best way to enable/disable the interrupt in GCBASIC.
Consider this code:
sub Outer
Set INTCON.GIE Off
Wait 1 sec
Inner
Wait 1 sec
Set INTCON.GIE On
end sub
sub Inner
Set INTCON.GIE Off
Set PORTB.0 on
Wait 1 sec
Set PORTB.0 off
Set INTCON.GIE On
end sub
The first Wait in "Outer" will be fine, but then "Inner" is called and the interrupt is re-enabled at the end of "Inner". Thus, the second Wait in "Outer" will be run with interrupts on.
A better way is to use "IntOn" and "IntOff". These are intended for just this purpose, and use a counter to keep track of whether interrupts should be on or not. Each call to IntOff clears INTCON.GIE if the counter is 0, then increments the counter. Each call to IntOn decrements the counter, and sets INTCON.GIE if the counter is 0. GCBASIC will also remove IntOn and IntOff from the program if no interrupts are used.
Also, you don't need IntOn at the start of a program - GCBASIC automatically enables interrupts if needed.