Connect with us

pic16f84 interrupt problem

Discussion in 'Misc Electronics' started by Ankur, Feb 15, 2005.

Scroll to continue with content
  1. Ankur

    Ankur Guest

    Hi all,
    Someone please help me out..
    Please see the program below which is working the way i don't want it
    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

    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


    #include ""

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

    org 0x00
    goto Main

    org 0x04
    goto ISR

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

    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!!

    goto loop ; wait state


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

    goto loop
  2. Mark VB

    Mark VB Guest

    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.
    will become:
    movf PORTB, W (or some other sort of read on PORTB)
    bcf INTCON, RBIF
    retfie (clear GIE & return)

    Mark Van Borm
  3. Guillaume

    Guillaume Guest

    Let's see...
    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

    You just put:


    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.
    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
  4. Michael

    Michael Guest

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

    I looked no further than that.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day