Interrupt in Pic microcontroller - Page 1
Forum section: electronics / general

author: khatus
author_rank: Regular Contributor
timestamp: October 15, 2018, 12:13:44 pm
content: 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??Code:#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 countervoid main(){//Setup Timer0T0PS0=1; //Prescaler is divide by 256T0PS1=1;T0PS2=1;PSA=0; //Timer Clock Source is from PrescalerT0CS=0; //Prescaler gets clock from FCPU (5MHz)T08BIT=1; //8 BIT MODETMR0IE=1; //Enable TIMER0 InterruptPEIE=1; //Enable Peripheral InterruptGIE=1; //Enable INTs globallyTMR0ON=1; //Now start the timer!//Set RB1 as output because we have LED on itTRISB&=0B11111101;while(1); //Sit Idle Timer will do every thing!}//Main Interrupt Service Routine (ISR)void interrupt ISR(){//Check if it is TMR0 Overflow ISRif(TMR0IE && TMR0IF){//TMR0 Overflow ISRcounter++; //Increment Over Flow Counterif(counter==76){//Toggle RB1 (LED)if(RB1==0)RB1=1;elseRB1=0;counter=0; //Reset Counter}//Clear FlagTMR0IF=0;}}look at the above code they used both GIE as well as PEIE bit.Now this is the code generated by mikroC timer calculator for 10 ms delay using timer0.but only GIE bit of INTCON register is used here//Timer0//Prescaler 1:1; TMR0 Preload = 15536; Actual Interrupt Time : 10 ms//Place/Copy this part in declaration sectionvoid 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;//Enter your code here}}

author: ggchab
author_rank: Frequent Contributor
timestamp: October 15, 2018, 01:22:05 pm
content: PEIE must be set to 1 to enable interrupts from peripherals (UART, ADC, ...). Not all interrupts are disabled when this bit is set to 0.On some PIC (like PIC18F), when priorities are enabled, GIE / PEIE become GIEH / GIEL to control high / low priority interrupts.

author: NorthGuy
author_rank: Super Contributor
timestamp: October 15, 2018, 02:15:40 pm
content: There are hundreds of different PICs which are all different. You need to look at the specific PIC you use and read the datasheet.PIC18s have PEIE, PIC16s don't.

author: rstofer
author_rank: Super Contributor
timestamp: October 15, 2018, 02:47:58 pm
content: Definitely read the datasheet. In general, GIE is used to enable or disable all interrupts from every source. PEIE is used to disable only peripheral interrupts and if GIE is 1 other interrupts, like timer interrupts, are still enabled.The PIC may not be consistent across devices so definitely read the datasheet. Look through the 'family reference manual' as well. Between them, they will total just around 900 pages (eg 16F877A + Mid-Range Family) - a little light reading!

author: khatus
author_rank: Regular Contributor
timestamp: October 15, 2018, 02:49:34 pm
content: What is the meaning of MASKED by

author: MarkF
author_rank: Super Contributor
timestamp: October 15, 2018, 02:50:25 pm
content: A picture is worth a thousand words here:GIE == Enables all interrupts (Red gate)PEIE == Enables peripherals to interrupt (Blue gate)RBIE == Enables Register B pins to interrupt (Green gate)T0IE == Enable Timer 0 interrupt (Purple gate)Edit: Added Timer 0. Taken from PIC16F886 datasheet.

author: cv007
author_rank: Frequent Contributor
timestamp: October 15, 2018, 03:19:52 pm
content: Quote...except when they do, things change.QuoteIts like 4 light switches that have to be on before the light goes on. Turn off any one of those switches and the light stays off. You can debate whether the term 'masked' applies to every switch, but the light doesn't care what you call them.In this case they are calling the TMR0IE ('switch' #2) a mask as it will hide the state of TMR0IF ('switch' #1). With switch #2 off, it matters not what state switch #1 is in. You may have the peripheral set to where 'switch #1' is turning on (like a timer running and overflowing causing the 'switch'/flag to turn on), but you do not want to fire an interrupt so you turn switch #2 off. Now switch #1 cannot fire an interrupt no matter the state of any switch after switch # 2.This may be a bad analogy, but its close.TMR0IF TMR0IE PEIE GIE- 4 light switches. (assuming you have a light switch named peie)

author: NorthGuy
author_rank: Super Contributor
timestamp: October 15, 2018, 04:08:57 pm
content: Quote from: khatus on October 15, 2018, 02:49:34 pmThe meaning is that the system doesn't see the corresponding IF flag. The IF flag is set when the event linked to the interrupt happens, but when IE is off, the system behaves as if the IF flag was cleared.GIE is a global flag - it controls whether an interrupt routine happens or not. Even if GIE is off, the system may still use the IF flag - for example the IF flag can be used to wake the CPU from sleep regardless of the GIE setting.The IE bit disables the IF flag and the system behaves as if the IF flag is zero. You, in your code, can still look at the IF flag to find out if the event has happened. This way you may be aware of the interrupt condition without enabling interrupts.If both IE and GIE are set then the interrupt routine (ISR) gets called, and will be called to the rest of eternity until you clear the IF flag. In this case, if the non-ISR code reads the IF flag, it always be cleared, because if the IF was set the non-ISR code would never execute.PEIE is just an additional flag which works as GIE, but only affects some of the interrupts.

author: khatus
author_rank: Regular Contributor
timestamp: October 16, 2018, 05:45:54 am
content: What happened if i don't clear the TMR0IF bit??

author: JPortici
author_rank: Super Contributor
timestamp: October 16, 2018, 07:14:55 am
content: more or less, when an interrupt occours:- Current program counter is saved in the call stack- Program counter is loaded with the address of the interrupt vector- the GIE bit is clearedwhen the interrupt routine has finished execution (RETIE instruction):- Program counter is restored to the previous value- the GIE bit is setwhich means, if you don't clear the TMR0IF bits it will enter the interrupt routine again at the following cycle

author: NorthGuy
author_rank: Super Contributor
timestamp: October 16, 2018, 02:39:41 pm
content: Quote from: khatus on October 16, 2018, 05:45:54 amIn this case, once you return from the ISR, you will go right back to the beginning of the ISR again (provided IE and PEIE are still set).

author: khatus
author_rank: Regular Contributor
timestamp: November 09, 2018, 06:34:37 am
content: Code:#define Pulse LATBvoid Timer0_start();void main(){OSCCON = 0x70; /* Configure oscillator frequency to 8MHz(0b01110000) */TRISB = 0; /* Set as output Port */Pulse = 0xff; /* Send high on PortB */Timer0_start();while(1);}/***************Interrupt Service Routine for Timer1******************/void timer0_interrupt() iv 0x000008 ics ICS_OFF{if (TMR0IF_bit){TMR0H = 0xF8;TMR0L = 0x30;TMR0IF_bit =0; /* Make Timer0 Overflow Flag to '0' *///Enter your code herePulse =~ Pulse; /* Toggle PortB at of 500 Hz */}}void Timer0_start(){/* Enable 16-bit TMR0 register,no pre-scaler,internal clock, timer OFF */T0CON = 0x88;TMR0H = 0xF8; /* Load Count for generating delay of 1ms */TMR0L = 0x30;//GIE_bit =1; /* Enable Global Interrupt */PEIE_bit =1; /* Enable Peripheral Interrupt */TMR0IE_bit =1; /* Enable Timer0 Overflow Interrupt */TMR0IF_bit = 0;TMR0ON_bit = 1; /* Turn ON Timer0 */}this is the code for generating 1 ms delay using timer0 (PIc 18f2550)my question is will i have to enable Global as well as peripheral interrupt bit if i want to generate 1ms delay using timer0?? or only Global interrupt bit is required(i am not using any external interrupt)??

author: jpanhalt
author_rank: Super Contributor
timestamp: November 09, 2018, 09:58:13 am
content: A good place to start with such questions is the datasheet. What does the datasheet say about Timer 0? Is it a peripheral? Is it unmasked using a PIE register or INTCON?Here's a relevant section from a similar chip:TMR0 is enabled directly in INTCON. You need to look at that register in the datasheet for your device.

author: KL27x
author_rank: Super Contributor
timestamp: November 14, 2018, 06:03:01 pm
content: QuoteFYI, the PIC datasheets have in index at the end. In this index you can find "Timer0>associated registers." This will show all the relevant registers. In this case, it's on page 129, and you will see that PIE register is not listed. If you were to look up any of the other timers' associated registers, you will see their IE bits are located in a PIEx register (and their IF's are located in the PIRx register). Timer0 is the only timer, in this case, that is not gated by the PEIE bit.In general, timer0 is usually not classified as a peripheral. But AFAIK, there's not necessarily a rhyme or reason to what is or isn't a subset of PEIE, so it's something you ought to look up... but Microchip is very consistent in the way it organizes the datasheets. You may not need to read the entire thing like a book; and even if you did read the entire section on Timer0, you might have a hard time digesting. You will often find the datasheet useful as a quick reference when you learn how it is organized and indexed. If you just read through the entire section on Timer0, you will get a lot of redundant information posted from different angles, and it is difficult to digest. The "associated registers" page is one of these angles and is the most relevant to the info you want.