PDA

View Full Version : 16F872 and interrupt handling



Robson
- 26th June 2007, 23:13
Hi there,
i wrote my first program and i donīt understand, how to define the interrupts.
I īve set RB7 and RB6 as inputs and theyīre normally high "1".
If one of these inputs change the state, then i need to go to the interrupt routine. Can somebody help me how to define interrupts in PicBasic and which options needed for my example ?

Thanks a lot
Robson


ADCON1 = %111
DEFINE OSC 4

TRISA = %00110000
TRISB = %11000000 ' RB7 & RB6 as inputs
TRISC = %00000000

ENABLE
ON INTERRUPT GOTO myint
INTCON = ??? ' Donīt know what comes here
OPTION_REG = ' ???
main:
...
...
GOTO main

DISABLE
myint:
LED = 1
RESUME
ENABLE

Darrel Taylor
- 27th June 2007, 01:05
Hi Robson,

This might fill in a few of the blanks...
ADCON1 = %111
DEFINE OSC 4

LED1 VAR PORTB.0
LED2 VAR PORTB.1
RBCpins VAR BYTE
LastPins VAR BYTE

TRISA = %00110000
TRISB = %11000000 ' RB7 & RB6 as inputs
TRISC = %00000000


OPTION_REG.7 = 0 ; If you want Internal PORTB Pull-ups
PAUSE 10 ; Let weak pull-ups stabilize
LastPins = PORTB ; save current pin state,
; and end any mismatch conditions

INTCON.0 = 0 ; Clear RB Port change Int Flag
INTCON.3 = 1 ; Enable RB Port Change Interrupts

ON INTERRUPT GOTO myint

main:
'...
'...
GOTO main

DISABLE
myint:
RBCpins = PORTB ; read PORTB and end the mismatch
INTCON.0 = 0 ; reset RB Port change INT flag
if RBCpins.7 != LastPins.7 then ; Pin 7 changed
LastPins.7 = RBCpins.7
Toggle LED1
endif

if RBCpins.6 != LastPins.6 then ; Pin 6 changed
LastPins.6 = RBCpins.6
Toggle LED2
endif
RESUME
ENABLE

Robson
- 27th June 2007, 10:15
Thanks Daylor.
I`ll try to insert your program into my.

Robson
- 27th June 2007, 11:10
Hello Darrel,

now the interrupt works fine, but after interrupt the pic hangs. I need to reset it. Can you tell me where is the failure ?

code:
ADCON1 = %111

define OSC 4

TRISA = %00111000 ' RA0-2 OUT, RA3-5 INPUT
TRISB = %11000000 ' OUTPUT
TRISC = %00000000 ' OUTPUT

col1 var PORTA.0
col2 var PORTA.1
col3 var PORTA.2

row1 var PORTA.3
row2 var PORTA.4
row3 var PORTA.5

id1 var PORTB.7 ' Input1 Rotary
id2 var PORTB.6 ' Input2 Rotary

click var PORTB.5
tp var PORTB.4
tm var PORTB.3

vm var PORTC.0
vp var PORTC.1
unt var PORTC.2

srcl var PORTC.3
srcr var PORTC.4

dp4 var PORTC.5
dp3 var PORTC.6
dp2 var PORTC.7

dpo1 var PORTB.2 ' Simulation OUT 1
dpo2 var PORTB.1 ' Simulation OUT 2
summer var PORTB.0 ' Beep active

lp var word
drehpos var byte
savepos var byte
cnt var byte
ums var bit
delay var word
startclk var word

RBCPins var Byte
LastPins var Byte

Lastpins = PORTB

INTCON.0 = 0
INTCON.3 = 1

on interrupt goto myint


main:
...
GOTO main

switchd:
dpo2 = 0
for lp = 0 to 20
pause 1 ' Time for Simulation 20ms
next lp
dpo1 = 0
for lp = 0 to 40
pause 1 ' 40ms Pause
next lp
dpo2 = 1 : dpo1 = 1
return

switchh:
dpo1 = 0
for lp = 0 to 20
pause 1 ' Time for Simulation 20ms
next lp
dpo2 = 0
for lp = 0 to 40
pause 1 ' 40ms Pause
next lp
dpo1 = 1 : dpo2 = 1
return



DISABLE
myint:
RBCpins = PORTB
INTCON.0 = 0
if RBCpins.7 <> Lastpins.7 then
Lastpins.7 = rbcpins.7
gosub switchh
endif

if RBCpins.6 <> Lastpins.6 then
lastpins.6 = rbcpins.6
gosub switchd
resume
endif
enable

Darrel Taylor
- 27th June 2007, 11:23
Somehow, the RESUME got moved inside the IF statement.
The only way it will resume is if PORTB.6 changes.

Otherwise it falls off into never never land.
<br>

Robson
- 27th June 2007, 14:16
Ok Darrel iīve found the error.
I changed the program like this and itīs working perfect.

DISABLE
myint:
RBCpins = PORTB
INTCON.0 = 0

if rbcpins.7 <> Lastpins.7 then
dpo1 = 0
for lp = 0 to 20
pause 1
next lp
dpo2 = 0
for lp = 0 to 40
pause 1
next lp
dpo1 = 1
dpo2 = dpo1
endif

if rbcpins.6 <> lastpins.6 then
dpo2 = 0
for lp = 0 to 20
pause 1
next lp
dpo1 = 0
for lp = 0 to 40
pause 1
next lp
dpo2 = 1
dpo1 = dpo2
endif
RESUME
ENABLE

Maybe the error comes from the subroutine. I include the subroutine directly and it works fine.

Thanks a lot Darrel

PS: How you insert a code in a window like you posted it before ?

Darrel Taylor
- 27th June 2007, 22:47
Using
tags puts it in a window.

At the bottom of the page, there's a box that says "Posting Rules". Inside that there's a link to vB code. Check it out. You can do all kinds of formatting.

hth,

mister_e
- 28th June 2007, 01:28
:eek: Darrel and ON INTERRUPT in the same thread ??? :eek: ;)

Darrel Taylor
- 28th June 2007, 02:15
Well, I figure you can't appreciate "Instant Interrupts" until you've had to deal with ON INTERRUPT a few times.

Got to get them started. :)
<br>

mister_e
- 29th June 2007, 00:29
I wish Melabs would be willing to replace ON INTERRUPT with DT-INT... man you have to talk to them ;)

Another compiler use something around that... you know which one ;)

erice1984
- 6th July 2007, 05:12
is it possible to use a momentary switch (operating on 5v) as a digital input? so I can use interrupts.