Difference between Global and peripheral Interrupt in PIC microcontroller

khatus

Joined Jul 2, 2018
85
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;
}
}

AlbertHall

Joined Jun 4, 2014
10,050
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.

khatus

Joined Jul 2, 2018
85
I am using PIC 18f2550

khatus

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

C:
//Timer0
//Prescaler 1:1; TMR0 Preload = 15536; Actual Interrupt Time : 10 ms

//Place/Copy this part in declaration section
void InitTimer0()
{
T0CON    = 0x88;
TMR0H    = 0x3C;
TMR0L    = 0xB0;
GIE_bit    = 1;
TMR0IE_bit    = 1;
}

void Interrupt()
{
if (TMR0IF_bit){
TMR0IF_bit = 0;
TMR0H    = 0x3C;
TMR0L    = 0xB0;
}
}
Mod edit: code tags

Last edited by a moderator:

AlbertHall

Joined Jun 4, 2014
10,050
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.

khatus

Joined Jul 2, 2018
85
What is the meaning of MASKED by

jpanhalt

Joined Jan 18, 2008
9,742
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.

AlbertHall

Joined Jun 4, 2014
10,050
You may notice that you cannot "enable" the flags per se.
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.

atferrari

Joined Jan 6, 2004
4,016
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:

jpanhalt

Joined Jan 18, 2008
9,742
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.
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

Joined Jan 6, 2004
4,016
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.
Just priority to spice things up and not so complex to use. Got used to it quite quickly.

khatus

Joined Jul 2, 2018
85
What happened if i don't clear the TMR0IF bit??

jpanhalt

Joined Jan 18, 2008
9,742
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.
Source: Datasheet 9.2
Note: Interrupt flag bits are set when an interrupt
condition occurs regardless of the state of
its corresponding enable bit or the global
interrupt enable bit. User software should
ensure the appropriate interrupt flag bits
are clear prior to enabling an interrupt.
This feature allows for software polling.
And from the INTCON register:

Picbuster

Joined Dec 2, 2013
1,009
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.
PIC 18f2550 see page 87 INTCON bit7

Picbuster

Ian Rogers

Joined Dec 12, 2012
784
PIC 18f2550 see page 87 INTCON bit7

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