pic16f84 interrupt problem
Electronics Forum Index Electronics
Circuits, theory, electrons and discussions.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web ElectronicsHelp.net
pic16f84 interrupt problem

 
Post new topic   Reply to topic    Electronics Forum Index -> Electronics General
Author Message
Ankur
Guest





Posted: Tue Feb 15, 2005 3:17 pm    Post subject: pic16f84 interrupt problem Reply with quote

Hi all,
Someone please help me out..
Please see the program below which is working the way i don't want it
to.
I've interfaced 5leds to PORTA( i'v used a pull-up in PA4 pin) and am
trying to work out a binary counting on the 5leds.The trigger to count
is being given by RB<7:4> pin state change. So, whenever i change the
state of any of RB<7:4>, i should see a count UP on the leds(say it
was 00001 earlier, i should see 00010 on changing the state of
RB<7:4>pins)

But the problem i am facing is that the program comes back into the
Main Loop inspite of a "Wait Loop" provided. I don't know but after
the first interrupt, the program runs back into Main.
b'bye and thnx


******************************************PROGRAM*******************************

#include "p16f84a.inc"

count equ 0Ch ;First counter for our
delay loops
bank0 macro
bcf STATUS,RP0
endm
bank1 macro
bsf STATUS,RP0
endm


org 0x00
goto Main

org 0x04
goto ISR

org 0x10
Main
bank1
clrf TRISA ; portA configured as OUTPUT
movwf 0xff
movwf TRISB; portB configured as INPUT
bank0
movlw 0xff
movwf PORTA
movlw 0x04
movwf count ; count=4

bank1
movlw b'00000111'
movwf OPTION_REG ; for timer prescalar 1:256
;
; ;configure INTCON
;
bsf INTCON,RBIE ;RB port change Interrupt Enable bit
bsf INTCON,GIE ; Enable interrupts!!

loop
nop
goto loop ; wait state




ISR
bank0

;we inc. 'count' every time ISR is executed & send it on PORTA to
verify.
incf count,f ;when i used "incf PORTA"
********************************************
movf count,w ;instead of these 3
commands*****************************************
movwf PORTA ;it did not
work.****************************************************
;
bcf INTCON,RBIF ; IMPORTANT - clear back the interrupt flag
BSF INTCON,GIE ; IMPORTANT! - re-enable the interrupts
return

goto loop
end

Back to top
Mark VB
Guest





Posted: Tue Feb 15, 2005 10:14 pm    Post subject: Re: pic16f84 interrupt problem Reply with quote

Ankur wrote:
Quote:
Hi all,
Someone please help me out..
Please see the program below which is working the way i don't want it
to.
I've interfaced 5leds to PORTA( i'v used a pull-up in PA4 pin) and am
trying to work out a binary counting on the 5leds.The trigger to count
is being given by RB<7:4> pin state change. So, whenever i change the
state of any of RB<7:4>, i should see a count UP on the leds(say it
was 00001 earlier, i should see 00010 on changing the state of
RB<7:4>pins)

But the problem i am facing is that the program comes back into the
Main Loop inspite of a "Wait Loop" provided. I don't know but after
the first interrupt, the program runs back into Main.
b'bye and thnx


******************************************PROGRAM*******************************

#include "p16f84a.inc"

count equ 0Ch ;First counter for our
delay loops
bank0 macro
bcf STATUS,RP0
endm
bank1 macro
bsf STATUS,RP0
endm


org 0x00
goto Main

org 0x04
goto ISR

org 0x10
Main
bank1
clrf TRISA ; portA configured as OUTPUT
movwf 0xff
movwf TRISB; portB configured as INPUT
bank0
movlw 0xff
movwf PORTA
movlw 0x04
movwf count ; count=4

bank1
movlw b'00000111'
movwf OPTION_REG ; for timer prescalar 1:256
;
; ;configure INTCON
;
bsf INTCON,RBIE ;RB port change Interrupt Enable bit
bsf INTCON,GIE ; Enable interrupts!!

loop
nop
goto loop ; wait state




ISR
bank0

;we inc. 'count' every time ISR is executed & send it on PORTA to
verify.
incf count,f ;when i used "incf PORTA"
********************************************
movf count,w ;instead of these 3
commands*****************************************
movwf PORTA ;it did not
work.****************************************************
;
bcf INTCON,RBIF ; IMPORTANT - clear back the interrupt flag
BSF INTCON,GIE ; IMPORTANT! - re-enable the interrupts
return

goto loop
end

For RB7:4, the pins value in input mode is XOR'ed with the old value
latched on the last read of PORTB. The result of these are OR'ed
together to generate the RBIF interrupt. This means you can't clear the
RBIF without reading PORTB first.

Quote:
bcf INTCON,RBIF ; IMPORTANT - clear back the interrupt flag
BSF INTCON,GIE ; IMPORTANT! - re-enable the interrupts
return

will become:
movf PORTB, W (or some other sort of read on PORTB)
bcf INTCON, RBIF
retfie (clear GIE & return)


HTH,
Mark Van Borm
Back to top
Guillaume
Guest





Posted: Tue Feb 15, 2005 10:49 pm    Post subject: Re: pic16f84 interrupt problem Reply with quote

Let's see...

Quote:
ISR
bank0

;we inc. 'count' every time ISR is executed & send it on PORTA to
verify.
incf count,f ;when i used "incf PORTA"
********************************************
movf count,w ;instead of these 3
commands*****************************************
movwf PORTA ;it did not
work.****************************************************
;
bcf INTCON,RBIF ; IMPORTANT - clear back the interrupt flag
BSF INTCON,GIE ; IMPORTANT! - re-enable the interrupts
return

This is not the correct way of returning from an ISR on the PIC.
Don't use 'return'! Use 'retfie' instead. 'retfie' re-enables the
interrupts, so you don't need to do this explicitly. So:

Instead of:

BSF INTCON,GIE ; IMPORTANT! - re-enable the interrupts
return

You just put:

retfie

You'd think it does the same thing (only saving an instruction)?
Well, it doesn't! By re-enabling the interrupts before calling
'return', you shoot yourself in the foot because your ISR might
get called again before it gets a chance to return. There you
go, a bad interrupt-reentrancy problem. Basically, your PIC
has every chance to get stuck after that.

That's what 'retfie' is for. It ensure no such thing can happen.

Quote:
goto loop
end

This last goto is totally useless. Your code won't ever reach
that part.

I suggest you read up a little more on using interrupts on
PICs.

Back to top
Michael
Guest





Posted: Wed Feb 16, 2005 6:10 am    Post subject: Re: pic16f84 interrupt problem Reply with quote

Two lines after Main ... I think you want the instr. to be
MOVLW 0xFF
not
movwf 0xff

I looked no further than that.

Ankur wrote:
Quote:

Hi all,
Someone please help me out..
Please see the program below which is working the way i don't want it
to.
I've interfaced 5leds to PORTA( i'v used a pull-up in PA4 pin) and am
trying to work out a binary counting on the 5leds.The trigger to count
is being given by RB<7:4> pin state change. So, whenever i change the
state of any of RB<7:4>, i should see a count UP on the leds(say it
was 00001 earlier, i should see 00010 on changing the state of
RB<7:4>pins)

But the problem i am facing is that the program comes back into the
Main Loop inspite of a "Wait Loop" provided. I don't know but after
the first interrupt, the program runs back into Main.
b'bye and thnx


******************************************PROGRAM*******************************

#include "p16f84a.inc"

count equ 0Ch ;First counter for our
delay loops
bank0 macro
bcf STATUS,RP0
endm
bank1 macro
bsf STATUS,RP0
endm

org 0x00
goto Main

org 0x04
goto ISR

org 0x10
Main
bank1
clrf TRISA ; portA configured as OUTPUT
movwf 0xff
movwf TRISB; portB configured as INPUT
bank0
movlw 0xff
movwf PORTA
movlw 0x04
movwf count ; count=4

bank1
movlw b'00000111'
movwf OPTION_REG ; for timer prescalar 1:256
;
; ;configure INTCON
;
bsf INTCON,RBIE ;RB port change Interrupt Enable bit
bsf INTCON,GIE ; Enable interrupts!!

loop
nop
goto loop ; wait state


ISR
bank0

;we inc. 'count' every time ISR is executed & send it on PORTA to
verify.
incf count,f ;when i used "incf PORTA"
********************************************
movf count,w ;instead of these 3
commands*****************************************
movwf PORTA ;it did not
work.****************************************************
;
bcf INTCON,RBIF ; IMPORTANT - clear back the interrupt flag
BSF INTCON,GIE ; IMPORTANT! - re-enable the interrupts
return

goto loop
end


--
---
NOTE: My addy is munged to foil SPAM.
Please reply via this NewsGroup.
Back to top
 
Post new topic   Reply to topic    Electronics Forum Index -> Electronics General All times are GMT
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



Home & Living New Topics
Contact Us
Powered by phpBB