Author Topic: Interrupt in Pic microcontroller  (Read 3460 times)

0 Members and 1 Guest are viewing this topic.

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 145
  • Country: gl
Interrupt in Pic microcontroller
« on: October 15, 2018, 12:13:44 pm »
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: [Select]
#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;
   }
}

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 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;
//Enter your code here
}
}
« Last Edit: October 15, 2018, 12:53:29 pm by khatus »
 

Offline ggchab

  • Frequent Contributor
  • **
  • Posts: 276
  • Country: be
Re: Interrupt in Pic microcontroller
« Reply #1 on: October 15, 2018, 01:22:05 pm »
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.


 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3137
  • Country: ca
Re: Interrupt in Pic microcontroller
« Reply #2 on: October 15, 2018, 02:15:40 pm »
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.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9886
  • Country: us
Re: Interrupt in Pic microcontroller
« Reply #3 on: October 15, 2018, 02:47:58 pm »
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!
 

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 145
  • Country: gl
Re: Interrupt in Pic microcontroller
« Reply #4 on: October 15, 2018, 02:49:34 pm »
What is the meaning of MASKED by
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 2523
  • Country: us
Re: Interrupt in Pic microcontroller
« Reply #5 on: October 15, 2018, 02:50:25 pm »
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.
« Last Edit: October 15, 2018, 03:05:09 pm by MarkF »
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 822
Re: Interrupt in Pic microcontroller
« Reply #6 on: October 15, 2018, 03:19:52 pm »
Quote
PIC18s have PEIE, PIC16s don't.
...except when they do, things change.

Quote
What is the meaning of MASKED by
Its 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)
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3137
  • Country: ca
Re: Interrupt in Pic microcontroller
« Reply #7 on: October 15, 2018, 04:08:57 pm »
What is the meaning of MASKED by

The 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.

« Last Edit: October 15, 2018, 04:12:51 pm by NorthGuy »
 
The following users thanked this post: khatus

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 145
  • Country: gl
Re: Interrupt in Pic microcontroller
« Reply #8 on: October 16, 2018, 05:45:54 am »
What happened if i don't clear the TMR0IF bit??
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3452
  • Country: it
Re: Interrupt in Pic microcontroller
« Reply #9 on: October 16, 2018, 07:14:55 am »
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 cleared
when the interrupt routine has finished execution (RETIE instruction):
- Program counter is restored to the previous value
- the GIE bit is set

which means, if you don't clear the TMR0IF bits it will enter the interrupt routine again at the following cycle
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3137
  • Country: ca
Re: Interrupt in Pic microcontroller
« Reply #10 on: October 16, 2018, 02:39:41 pm »
What happened if i don't clear the TMR0IF bit??

In 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).
 

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 145
  • Country: gl
Re: Interrupt in Pic microcontroller
« Reply #11 on: November 09, 2018, 06:34:37 am »


Code: [Select]
#define Pulse LATB

void 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 here
    Pulse =~ 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)??
 

Offline jpanhalt

  • Super Contributor
  • ***
  • Posts: 3395
  • Country: us
Re: Interrupt in Pic microcontroller
« Reply #12 on: November 09, 2018, 09:58:13 am »
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.
« Last Edit: November 09, 2018, 10:00:02 am by jpanhalt »
 

Offline KL27x

  • Super Contributor
  • ***
  • Posts: 4099
  • Country: us
Re: Interrupt in Pic microcontroller
« Reply #13 on: November 14, 2018, 06:03:01 pm »
Quote
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)??
FYI, 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.
« Last Edit: November 14, 2018, 06:22:49 pm by KL27x »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf