• AVR Freaks

Hot!Help using interrupt on a PIC18f4331

Author
keiga0
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/06/23 13:35:12
  • Location: 0
  • Status: offline
2019/06/30 15:38:17 (permalink)
0

Help using interrupt on a PIC18f4331

I have been working on using a button as an interrupt on a PIC18F4331 and have been hitting my head against the wall for a while now.  I am trying to use the RB3 with the weak pull-up and pull it to low for the interrupt.  Right now I am just trying to turn on an LED on port RD2.  Any help would be greatly appreciated.
 
 
#1

13 Replies Related Threads

    ric
    Super Member
    • Total Posts : 24544
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Help using interrupt on a PIC18f4331 2019/07/01 16:00:22 (permalink)
    +1 (1)
    Don't set GIE until AFTER you have completed all your initialisation.
    Do NOT twiddle GIE inside the interrupt service. The hardware does that for you. Setting GIE before the end of the ISR will cause problems later.
    Don't try to set RB3 inside the ISR, it is an input.
    However, you do need to do a dummy read of PORTB before you can clear the RBIF bit.
    This is documented in the datasheet under "11.2 PORTB, TRISB and LATB Registers"
     
    You have enabled interrupt priorities, but have not set the RBIP bit. It should default high, but I never trust "default" settings.
     
    And finally, unless you're trying to implement wakeup from a very low power mode, it's much easier to poll pushbuttons from a regular timer interrupt rather than deal the vagaries of the "on change" detection, especially if you have to implement debouncing for the button.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    ric
    Super Member
    • Total Posts : 24544
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Help using interrupt on a PIC18f4331 2019/07/01 19:14:13 (permalink)
    +1 (1)
    I just noticed, you are also enabling interrupts on INT0 (RC3), but your interrupt service doesn't handle them.
    That means if an INT0 does occur, your PIC will effectively "crash" because it will be continually trying to service INT0 interrupts that are never cleared.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    keiga0
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/06/23 13:35:12
    • Location: 0
    • Status: offline
    Re: Help using interrupt on a PIC18f4331 2019/07/02 12:21:27 (permalink)
    0
    ric,
    Thank you so much for this.  I will work on it as soon as I can get back home tomorrow and update. 
    #4
    keiga0
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/06/23 13:35:12
    • Location: 0
    • Status: offline
    Re: Help using interrupt on a PIC18f4331 2019/07/07 12:36:19 (permalink)
    0
    ric,
     
    I do not have a lot of experience using PICs just to preface that I have more questions.
    I changed the GIE until after all of the installation instructions. 
    I removed the twiddling of the GIE inside the ISR.
    I removed the PORTBbits.RB3 = 1 inside the ISR.
    I added a read inside the ISR for the PORTBbits.RB3.  I am not sure if i did this correctly.
    I have added the INTCON2bits.RBIP = 1; to the setup setting the port change interrupt to high.
    I am trying to wakeup from very low power mode, but if the is a better way is to poll pushbuttons from a regular timer interrupt I would do that.  I just don't know how to do that either.
    Finally I have removed the INTCONbits.INT0IE = 1; .  I thought that this just enabled the external interrupt.
     
    And with the changes I still cant get it to work.  I have a LED on RD1 and sometimes when I move my hand close it goes off, but is on when I upload the code it comes on (most of the time but not all of the time).  I also have a LED on RB3 and when I push the button pulling it to ground the LED on RB3 goes off, but i do not get any change on the RD1 LED.  I purchased new chips for fear I had burnt the other ones and have placed this on 2 different bread boards with different components (voltage regulator and caps) with the same results.  Does the code look right now?
     
    #5
    mbrowning
    USNA79
    • Total Posts : 1564
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: Help using interrupt on a PIC18f4331 2019/07/07 12:57:54 (permalink)
    0
    From the datasheet section 10.8
    10.8 PORTB Interrupt-on-Change
    An input change on PORTB<7:4> sets flag bit, RBIF
    (INTCON<0>).

    and from 11.2
    Four of the PORTB pins (RB<7:4>) have an interrupt-on-change feature.
    So why are you expecting an interrupt from changes on RB3?

    Go Navy! Beat Army!
    #6
    pcbbc
    Super Member
    • Total Posts : 1381
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Help using interrupt on a PIC18f4331 2019/07/07 12:58:59 (permalink)
    0
    If it’s the high priority interrupt, doesn’t the ISR need a flag to say that...
    __interrupt(high_priority)

    See XC8 Manual 2.5.10 Interrupt Functions

    I’ve no idea what the default priority is if you don’t specify one, but I wouldn’t count on it being correct.
    #7
    mbrowning
    USNA79
    • Total Posts : 1564
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: Help using interrupt on a PIC18f4331 2019/07/07 13:08:17 (permalink)
    0
    pcbbc
    If it’s the high priority interrupt, doesn’t the ISR need a flag to say that...
    __interrupt(high_priority)

    See XC8 Manual 2.5.10 Interrupt Functions

    I’ve no idea what the default priority is if you don’t specify one, but I wouldn’t count on it being correct.

    I thought it defaulted to low priority (I've always explicity specified it), but in fact the 2.00 compiler manual (the latest release I have immediate access to) specifies default is high priority. Although explicit priority is recommended. (xc8 2.00 manual 5.9.1 (legacy) or 4.9.1 (C99))

    Go Navy! Beat Army!
    #8
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11421
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Help using interrupt on a PIC18f4331 2019/07/07 13:22:08 (permalink)
    +2 (2)
    It's very rare that an application needs interrupt priorities, and you don't need the complication of interrupt priorities at this point.  The PIC defaults to a single interrupt vector without priorities.  No code is needed to set this up. 
    #9
    keiga0
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/06/23 13:35:12
    • Location: 0
    • Status: offline
    Re: Help using interrupt on a PIC18f4331 2019/07/07 14:31:55 (permalink)
    0
    What I am trying to accomplish here is to press a button and reset a counter on wire length measurement device that has an encoder on it.  This is my first project using a PIC.  I tried to use an Arduino, but it will not read the encoder fast enough.  I see that the PIC is a much better tool and want to use this in all of my future projects, but the learning curve seems to be a little more steep.  The reason that I have written the code the way I did was because that was the way I interpenetrated the data sheet.  If I should be polling the button with a timer that would be OK for me too.  At this point I would like to understand what I am doing wrong first before trying to get that to work.
    #10
    mbrowning
    USNA79
    • Total Posts : 1564
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: Help using interrupt on a PIC18f4331 2019/07/07 14:32:41 (permalink)
    0
    read my post 6 again.

    Go Navy! Beat Army!
    #11
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11421
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Help using interrupt on a PIC18f4331 2019/07/07 15:47:43 (permalink)
    0
    If I should be polling the button with a timer that would be OK for me too.

     
    Buttons are typically polled, because they don't need the real-time response of an interrupt.
     
    #12
    ric
    Super Member
    • Total Posts : 24544
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Help using interrupt on a PIC18f4331 2019/07/07 16:10:07 (permalink)
    0
    + polling a button makes it easy to "debounce" the button operation in software.
    If you don't take measures to debounce the button in hardware, you can often see hundreds of very short transitions as the button opens or closes and the contacts "bounce".
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #13
    keiga0
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/06/23 13:35:12
    • Location: 0
    • Status: offline
    Re: Help using interrupt on a PIC18f4331 2019/07/08 07:42:00 (permalink)
    +1 (1)
    Thank you guys for all of your help.  It looks like that PORTB only has interrupt on pins 4-7.  Since I have already made my board using the RB3 pin I will have to go with polling the button.  Which is good news because that is the way I should do it anyways.  I will search on here for examples of doing this.  If there is any other feedback you might have I would greatly appreciate it.  Thanks again.
     
     
    #14
    Jump to:
    © 2019 APG vNext Commercial Version 4.5