• AVR Freaks

Hot!Need example code for PIC18 interrupts with xc8

Author
KungFuAndy
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2012/01/05 10:24:00
  • Location: Colombia
  • Status: offline
2019/07/10 09:20:08 (permalink)
0

Need example code for PIC18 interrupts with xc8

I'm trying to build a project for a PIC18F25k83 using xc8 v2.05 and I'm having a lot of trouble with interrupts. I've left the interrupt configuration as default, I believe this means it doesn't use the vector table and the interrupts should behave in the same was as a PIC16.
 
My starting point for this was the example code for PIC18, but this uses the C18 compiler not the xc8 and it doesn't compile.
 
Can someone put an example of how to implement an isr using xc8, please?
Thanks
Andy
post edited by KungFuAndy - 2019/07/10 09:21:09
#1

7 Replies Related Threads

    Jim Nickerson
    User 452
    • Total Posts : 6124
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: Need example code for PIC18 interrupts with xc8 2019/07/10 09:29:46 (permalink)
    +1 (1)
    KungFuAndy
    I'm trying to build a project for a PIC18F25k83 using xc8 v2.05 and I'm having a lot of trouble with interrupts. I've left the interrupt configuration as default, I believe this means it doesn't use the vector table and the interrupts should behave in the same was as a PIC16.
     
    My starting point for this was the example code for PIC18, but this uses the C18 compiler not the xc8 and it doesn't compile.
     
    Can someone put an example of how to implement an isr using xc8, please?
    Thanks
    Andy


    Maybe you could try MCC
    #2
    NKurzman
    A Guy on the Net
    • Total Posts : 17613
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Need example code for PIC18 interrupts with xc8 2019/07/10 09:45:16 (permalink)
    +1 (1)
    C90 Mode, Or C99 Mode?  ( If your answer if I do not know, C99 is the Default.)
     
    Both are in the Manual.
    #3
    katela
    Super Member
    • Total Posts : 1330
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: Need example code for PIC18 interrupts with xc8 2019/07/10 10:02:27 (permalink)
    +1 (1)
    As explained above it's probably you are using the wrong interrupts syntax. Try to use MCC instead of referring to the old C18. You can get more info about the new interrupts syntax from this microchip video: https://youtu.be/srUf-BkmS4k

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #4
    KungFuAndy
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2012/01/05 10:24:00
    • Location: Colombia
    • Status: offline
    Re: Need example code for PIC18 interrupts with xc8 2019/07/10 14:05:55 (permalink)
    0
    I guessed I was using the wrong syntax, that really was the point of the question, I couldn't find anywhere that showed the right syntax. Even the xc8 user manual doesn't show this format:
    void __interrupt(high_priority) high_isr(void).
     
    That video was very useful. Thank you for that. However, I now have another problem. The errors I'm getting now suggest it is trying to populate a vector table, but the default for INTCON0 (IPEN) is that it uses just two interrupts. How can I see how this is set?
    The errors:
    interrupts.c:27:: error: (2016) interrupt function "high_isr" does not service any interrupt sources
    interrupts.c:71:: error: (2016) interrupt function "low_isr" does not service any interrupt sources
    Sensors.c:154:: warning: (520) function "_ReadIATSensor" is never called
    system.c:24:: warning: (520) function "_ConfigureOscillator" is never called
    Sensors.c:158:: warning: (1257) local variable "_Temp" is used but never given a value
    Non line specific message::: warning: (2020) IRQ 0 (SWINT) in vector table @ 0x8 is unassigned, will be programmed with the address of a RESET instruction
    Non line specific message::: warning: (2020) IRQ 1 (HLVD) in vector table @ 0x8 is unassigned, will be programmed with the address of a RESET instruction
    Non line specific message::: warning: (2020) IRQ 2 (OSF) in vector table @ 0x8 is unassigned, will be programmed with the address of a RESET instruction


    #5
    mbrowning
    Just a Member
    • Total Posts : 1460
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: Need example code for PIC18 interrupts with xc8 2019/07/10 14:12:18 (permalink)
    +2 (2)
    KungFuAndy
    I couldn't find anywhere that showed the right syntax. Even the xc8 user manual doesn't show this format:
    void __interrupt(high_priority) high_isr(void).

    AEUHHH???
    You're looking in the wrong manual then. XC8 User Guide for PIC section 4.9.1
    An example of an interrupt function written for code not using the IVT is shown below.
    Notice that the interrupt function checks for the source of the interrupt by looking at the
    interrupt enable bit (e.g., TMR0IE) and the interrupt flag bit (e.g., TMR0IF). Checking
    the interrupt enable flag is required since interrupt flags associated with a peripheral
    can be asserted even if the peripheral is not configured to generate an interrupt.
    int tick_count;
    void __interrupt(high_priority) tcInt(void)
    {
    if (TMR0IE && TMR0IF) { // any timer 0 interrupts?
    TMR0IF=0;
    ++tick_count;
    }
    if (TMR1IE && TMR1IF) { // any timer 1 interrupts?
    TMR1IF=0;
    tick_count += 100;
    }
    // process other interrupt sources here, if required
    return;
    }

    Here is the same function code, split and modified for a device using vector tables. Note
    that since only one source is associated each ISR, the interrupt code does not need to
    determine the source of the interrupt and is therefor faster.
    void __interrupt(irq(TMR0),high_priority) tc0Int(void)
    {
    TMR0IF=0;
    ++tick_count;
    return;
    }
    void __interrupt(irq(TMR1),high_priority) tc1Int(void)
    {
    TMR1IF=0;
    tick_count += 100;
    return;
    }


    If the IVT is available you should use it. It is so much easier and faster than the old way.

    Oh well - there's always next year
    #6
    KungFuAndy
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2012/01/05 10:24:00
    • Location: Colombia
    • Status: offline
    Re: Need example code for PIC18 interrupts with xc8 2019/07/10 14:23:34 (permalink)
    0
    Ah, you're right. I missed it in the manual.
    This system will, initially, only have one interrupt. That's why I chose to use the non IVT way.
    Am I right in saying that the following errors mean that it is trying to instantiate a vector table?
     
    Non line specific message::: warning: (2020) IRQ 0 (SWINT) in vector table @ 0x8 is unassigned, will be programmed with the address of a RESET instruction
    Non line specific message::: warning: (2020) IRQ 1 (HLVD) in vector table @ 0x8 is unassigned, will be programmed with the address of a RESET instruction
    Non line specific message::: warning: (2020) IRQ 2 (OSF) in vector table @ 0x8 is unassigned, will be programmed with the address of a RESET instruction


    The answer is YES! The line that needs to be added is #pragma config MVECEN = OFF. Which can be generated by going to Production->Set configuration bits, then click generate source code. Adding this line means the code now compiles.
    post edited by KungFuAndy - 2019/07/10 16:25:21
    #7
    jswanson
    Super Member
    • Total Posts : 355
    • Reward points : 0
    • Joined: 2003/11/07 12:44:05
    • Status: offline
    Re: Need example code for PIC18 interrupts with xc8 2019/07/31 02:47:14 (permalink)
    0
    Create a simple project in MCC.  It gives you the option to use a vector table or not, and will create all the ISR routines for you.
    #8
    Jump to:
    © 2019 APG vNext Commercial Version 4.5