PDA

View Full Version : interrupt on a port



nicolasronan
- 14th August 2006, 15:56
HI
i use 16F877A
how use interrupt on a port B (other that rb0) with 3 button.
for interrupt on RB4 to RB7 i must use intcon=00001000?
and intcon.0=0 to reset flag?

when i test this code . it dont work!

code:
' On Interrupt - Interrupts in BASIC
' Turn LED off on RA1 if Interrupt on PORTB.4
'
led var PORTA.1
trisa=1
LED=1 'just to see if port RA.1 is ok
pause 1000 '

OPTION_REG = $7f ' Enable PORTB pullups

On Interrupt Goto myint ' Define interrupt handler
INTCON =%00001000
loop: High led ' Turn LED on
Goto loop

' Interrupt handler
Disable ' No interrupts past this point
myint:
INTCON.0 = 0 ' Clear interrupt flag
low led ' If we get here, turn LED off
Pause 2000 ' Wait .5 seconds
resume ' Return to main program
Enable

with this code : led low and an action on rb4 nop
sample prog to use a port (other that RB0) and how use choice of different button?
please thank

Darrel Taylor
- 14th August 2006, 18:01
Hi Nick,

In the Interrupt handler, you need to read PORTB to "end the mismatch".
Temp = PORTB

Otherwise, as soon as you RESUME, it will interrupt again.
<br>

nicolasronan
- 16th August 2006, 17:33
HI
THANKS for your help

this is my final code
it s ok and works

'***************************************
' On Interrupt RB4 to RB7 - Interrupts in BASIC
' Turn LED1 off on RA1 if Interrupt on PORTB.4
' Turn LED2 off on RA2 if Interrupt on PORTB.5
'***************************************
OPTION_REG = %1000000 ' Enable PORTB pull-ups
ADCON1=6 ' port A = digital
TRISA=%00000000
TRISB=%11111111
PORTA=%11111111
PORTB=%11111111

On Interrupt Goto myint ' Define interrupt handler
INTCON =%00001000

loop:
@nop
Goto loop

'***************************************
'interrupt handler
myint:
Disable ' No interrupts past this point
IF PORTB.4=0 THEN goto LEDRTN
IF PORTB.5=0 THEN GOTO LED1RTN
resume ' Return to main program

LEDRTN: ' port B.4=0
PORTA.1=0 ' turn LED1 off
PAUSE 500 ' pause
PORTA.1=1 ' turn LED1 on
INTCON.0=0 ' Clear the RB port change flag bit
resume ' Return to main program

LED1RTN: ' port B.5=0
PORTA.2=0 ' turn LED2 off
PAUSE 4000 ' pause
PORTA.2=1 ' turn LED2 on
INTCON.0=0 ' Clear the RB port change flag bit
resume ' Return to main program
Enable