Difference between Global and peripheral Interrupt in PIC microcontroller

Discussion in 'Embedded Systems and Microcontrollers' started by khatus, Oct 15, 2018.

  1. khatus

    Thread Starter Member

    Jul 2, 2018
    68
    1
    What is the difference between peripheral interrupt and global Interrupt in PIC micro controller??

    This is the code for MPLAB which i have found in the internet.Both peripheral(PEIE) interrupt and global(GIE) interrupt are used in this code. My question is what is the function of this two interrupt??

    #include <htc.h>


    //Chip Settings
    __CONFIG(1,0x0200);
    __CONFIG(2,0X1E1F);
    __CONFIG(3,0X8100);
    __CONFIG(4,0X00C1);
    __CONFIG(5,0XC00F);


    unsigned char counter=0;//Overflow counter

    void main()
    {
    //Setup Timer0
    T0PS0=1; //Prescaler is divide by 256

    T0PS1=1;
    T0PS2=1;

    PSA=0; //Timer Clock Source is from Prescaler

    T0CS=0; //Prescaler gets clock from FCPU (5MHz)

    T08BIT=1; //8 BIT MODE

    TMR0IE=1; //Enable TIMER0 Interrupt
    PEIE=1; //Enable Peripheral Interrupt

    GIE=1; //Enable INTs globally

    TMR0ON=1; //Now start the timer!

    //Set RB1 as output because we have LED on it

    TRISB&=0B11111101;

    while(1); //Sit Idle Timer will do every thing!
    }

    //Main Interrupt Service Routine (ISR)
    void interrupt ISR()
    {
    //Check if it is TMR0 Overflow ISR

    if(TMR0IE && TMR0IF)
    {
    //TMR0 Overflow ISR
    counter++; //Increment Over Flow Counter

    if(counter==76)
    {
    //Toggle RB1 (LED)

    if(RB1==0)
    RB1=1;
    else
    RB1=0;

    counter=0; //Reset Counter

    }

    //Clear Flag
    TMR0IF=0;
    }
    }
     
  2. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    8,060
    1,991
    You don't say which PIC you are using, however each interrupt has its own enable/disable. Some of these are grouped together as peripheral interrupts.

    Now if the global interrupt control is disabled no interrupts will happen.
    If the global interrupt is enabled but peripheral interrupts are disabled then only those interrupts not in the peripheral group will be enabled.
    If both global and peripheral interrupts are enabled then they can all generate interrupts.
    Below is the diagram from the PIC16F1527 darasheet. You should consult the darasheet for the PIC you re actually using.
    upload_2018-10-15_13-42-53.png
     
  3. khatus

    Thread Starter Member

    Jul 2, 2018
    68
    1
    I am using PIC 18f2550
     
    atferrari likes this.
  4. khatus

    Thread Starter Member

    Jul 2, 2018
    68
    1
    This is the code generated by mikroC timer calculator for 10 ms delay using timer0.but only GIE bit of INTCON register is used

    Code (C):
    1. //Timer0
    2. //Prescaler 1:1; TMR0 Preload = 15536; Actual Interrupt Time : 10 ms
    3.  
    4. //Place/Copy this part in declaration section
    5. void InitTimer0()
    6. {
    7.   T0CON    = 0x88;
    8.   TMR0H    = 0x3C;
    9.   TMR0L    = 0xB0;
    10.   GIE_bit    = 1;
    11.   TMR0IE_bit    = 1;
    12. }
    13.  
    14. void Interrupt()
    15. {
    16.   if (TMR0IF_bit){
    17.     TMR0IF_bit = 0;
    18.     TMR0H    = 0x3C;
    19.     TMR0L    = 0xB0;
    20.     //Enter your code here
    21.   }
    22. }
    Mod edit: code tags
     
    Last edited by a moderator: Oct 15, 2018
  5. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    8,060
    1,991
    Add more confusion and stir...
    The PIC18F2550 doesn't have GIE and PIEI bits as these chips have interrupt priority bits. Interrupts can be set as low or high priority which jump to two different address locations. There are separate bits to enable/disable the two grous of interrupts, GIEH and GIEL (high and low priority respectively). These two bits are in the same location as the 'old' GIE and PIEI.

    I repeat, consult the datasheet, 'interrupts' section.
     
    atferrari and khatus like this.
  6. khatus

    Thread Starter Member

    Jul 2, 2018
    68
    1
    What is the meaning of MASKED by
    [​IMG]
     
  7. jpanhalt

    Expert

    Jan 18, 2008
    7,533
    1,808
    In that context, it means that the TMR0 interrupt flag will be set when the timer rolls over, but it won't cause an interrupt. In other words, if TMR0IE is not set (enabled), the interrupt is masked. Microchip uses that term quite often. Be sure to clear the TMR0IF, if you plan to enable the interrupt (unmask it).

    Why have the flag and interrupt enable separate? Sometimes, you want to poll the flag without causing an interrupt. You may notice that you cannot "enable" the flags per se.
     
    atferrari likes this.
  8. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    8,060
    1,991
    You can set the flags if you want to. If the interrupt is enabled this will trigger the interrupt. This can be useful, but not often.
     
  9. atferrari

    AAC Fanatic!

    Jan 6, 2004
    3,305
    1,291
    The three qualified post above should be enough for you to understand.

    Anyway, when I started using interrupts, and much less painfully than I feared, everything was crystal clear when I realized that the Global enabling was what enabled whatever interrupts you had set, including those from peripherals. It is simply the main gate which controls the output.

    The logic diagram shows it clearly. Spend some time to verify that.
     
    Last edited: Oct 16, 2018
  10. jpanhalt

    Expert

    Jan 18, 2008
    7,533
    1,808
    I disagree. At least on the 16F devices you cannot enable the flags per se. While TMR0 is an R/W register, meaning you may be able to set the flag (I have never tried to do that) and can clear it on instruction, you cannot enable whether the flag gets set. It just happens if TMR0 is on, which is why Microchip cautions one to always clear the flag before enabling the interrupt. Some flags are not directly writable (i.e., clear-able), BTW.

    I have no actual experience with 18F devices, but in responding to various posts, I have looked at the datasheets occasionally and believe 18F's treatment of flags is analogous, which makes sense to me.
     
    atferrari likes this.
  11. atferrari

    AAC Fanatic!

    Jan 6, 2004
    3,305
    1,291
    Just priority to spice things up and not so complex to use. Got used to it quite quickly.
     
  12. khatus

    Thread Starter Member

    Jul 2, 2018
    68
    1
    What happened if i don't clear the TMR0IF bit??
     
  13. jpanhalt

    Expert

    Jan 18, 2008
    7,533
    1,808
    If you don't clear the interrupt flag, then then interrupt will keep recurring. Such flag bits can be cleared in different ways. The datasheet will tell you what is required. TMR0IF can be cleared directly by software.
    And from the INTCON register:
    upload_2018-10-16_2-2-51.png
     
  14. Picbuster

    Well-Known Member

    Dec 2, 2013
    959
    124
    PIC 18f2550 see page 87 INTCON bit7

    Picbuster
     
  15. Ian Rogers

    Well-Known Member

    Dec 12, 2012
    636
    174
    The interrupt logic on the pic18f's can be both!

    The IPEN bit selects whether GIE / PEIE or GIEH / GIEL are being used....

    The IPEN bit is in the RCON register.... So you are both correct.

    As you are using TMR0, you will only need GIE as the interrupt flag and enable bits are in the same register!
     
Loading...