Hi Henrik, thanks for helping. I have changed to the code to do as you said: goto's instead of gosub's & made the ISR set flag only (i think).
It all seems to work better now, however I still have a problem with the debounce as I think it is actually 'the problem' now instead of actually making the code work as you stated. The isr sees the release of the button as an interrupt & sets a flag again which skips modes. So how do I go about adding the debounce without slowing things down?
Isnt there a way to only interrupt or tell the ISR to flag only if the button is depressed & not when released? Here is the updated code -
Code:
'PIC 12F683
#CONFIG
__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
#ENDCONFIG
'DEFINE OCS 4 '4mhz ocsillator
DEFINE OSC 4 '4mhz ocsillator
ANSEL = 0 'all I/O digital
CMCON0 = 7 'comparator off
LED VAR GPIO.0 'LED pin 7
loop1 var word ' Loop 1 Counter (word for 0 to 65535 increments)
ModeFlag var bit ' Bit to determine what state of IOC.3 is
'*****Initlaze Vars*****
loop1 = 0
ModeFlag = 0
'***********************
ON INTERRUPT GOTO modeselect 'interrupt handler is modeselect
INTCON = %10001000 'enable GIE and GPIE; clear GPIF
IOC = %00001000 ' enable IOC3 (GPIO3 Interrupt on change)
ENABLE
mode1:
do
for loop1 = 1 to 1000
HIGH LED 'led on
'PAUSE 1000 'delay 1 second
pause 1 '1 ms
if ModeFlag = 1 then
ModeFlag = 0
goTO mode2
ENDIF
next loop1
for loop1 = 1 to 1000
LOW LED 'led off
'PAUSE 1000 'delay 1 second
pause 1 '1 ms
if ModeFlag = 1 then
Modeflag = 0
goTO mode2
endif
next loop1
'GOTO mode1 'repeat
loop
ENABLE
mode2:
do
for loop1 = 1 to 500
HIGH LED 'led on
'PAUSE 500 'delay 0.5 second
pause 1 '1 ms
if ModeFlag = 1 then
modeflag = 0
goTO mode3
endif
next loop1
for loop1 = 1 to 500
LOW LED 'led off
'PAUSE 500 'delay 0.5 second
pause 1 '1 ms
if ModeFlag = 1 then
modeflag = 0
goTO mode3
endif
next loop1
'GOTO mode2 'repeat
loop
ENABLE
mode3:
DO
for loop1 = 1 to 50
HIGH LED 'led on
'PAUSE 50 'delay 0.05 second
pause 1 '1 ms
if ModeFlag = 1 then
modeflag = 0
goTO mode1
endif
next loop1
for loop1 = 1 to 50
LOW LED 'led off
'PAUSE 50 'delay 0.05 second
pause 1 '1 ms
if ModeFlag = 1 then
modeflag = 0
goTO mode1
endif
next loop1
'GOTO mode3 'repeat
loop
DISABLE 'disable interrupts in handler
modeselect:
if INTCON.0 = 1 then 'one of the GPIO<5:0> pins changed state (must be cleared in software)
'next mode
'Set the ModeFlag
ModeFlag = 1
ENDIF
INTCON = %10001000 'enable GIE and GPIE; clear GPIF
RESUME 'return to where left off
ENABLE 'enable interrupts
end 'got here by mistake
Bookmarks