• AVR Freaks

AnsweredHot!What happens if another interrupt occur when current one is executing

Page: 123 > Showing page 1 of 3
Author
Djsarkar
Starting Member
  • Total Posts : 56
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
2020/08/07 03:03:55 (permalink)
0

What happens if another interrupt occur when current one is executing

Hi
I have theorytical question, What happens if another interrupt occur when current one is executing like if external interrupt occur when timer interrupt is executing
#1
JPortici
Super Member
  • Total Posts : 1164
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/07 03:05:45 (permalink)
+1 (1)
Depends.
On the PIC and on how the interrupt controller is configured. (Check the datasheet, interrupt section)
 
#2
ric
Super Member
  • Total Posts : 28378
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/07 03:06:47 (permalink)
+6 (6)
The GIE bit is automatically cleared when your ISR starts running.
The new interrupt will set its matching xxIF flag.
If that flag is still set when the RETFIE instruction is executed at the end of the service (which sets GIE), another interrupt will be triggered immediately.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#3
JPortici
Super Member
  • Total Posts : 1164
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: Grappaland
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/07 03:09:26 (permalink)
+2 (2)
But ric, we don't know if he's talking about a PIC16, PIC18 or whatever else!
 
#4
ric
Super Member
  • Total Posts : 28378
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/07 03:41:02 (permalink)
+1 (1)
Good point.
What I have described is the simplest case of a PIC16F device.
We'll have to see if the OP cares to reveal what device they are actually using.
(In two weeks, they have never mentioned any PIC device at all, so I'm starting to suspect troll behaviour here.)
 
post edited by ric - 2020/08/07 03:44:51

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#5
Djsarkar
Starting Member
  • Total Posts : 56
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/07 04:35:44 (permalink)
0
ric
Good point.What I have described is the simplest case of a PIC16F device.We'll have to see if the OP cares to reveal what device they are actually using.(In two weeks, they have never mentioned any PIC device at all, so I'm starting to suspect troll behaviour here.) 

I am studying PIC12F1840 datasheet
I am new I don't know how to attach sketch in reply
I have made one sketch that I want to post
#6
ric
Super Member
  • Total Posts : 28378
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/07 04:43:03 (permalink)
+1 (1)
Djsarkar
I am studying PIC12F1840 datasheet

So why not mention that to start with?
There are many different types of PICs, and the answers to your questions change depending upon which family you are using.
The answer I gave is correct for a PIC12F1840.
 

I am new I don't know how to attach sketch in reply

"sketch" is an Arduino term. It doesn't apply to PIC programs.
 

I have made one sketch that I want to post

If you have some source code you want to post, just paste it into your post.
It will display better if you put
[CODE]
on a line before it, and
[/CODE]
afterwards, but change "CODE" to lower case "code". I had to use upper case here so you could see them.
 
post edited by ric - 2020/08/07 04:45:50

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#7
Djsarkar
Starting Member
  • Total Posts : 56
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/07 04:47:39 (permalink)
+1 (1)
ric
sketch" is an Arduino term. It doesn't apply to PIC programs

I have drawn diagram on paper to show the process and I want to post it
#8
ric
Super Member
  • Total Posts : 28378
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/07 05:02:57 (permalink)
+1 (3)
If you can scan or take a photo, you can post the image as an attachment by using the "Open Full Version" link above the Quick Reply window.
What is it a sketch of?
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#9
isoar
Starting Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2015/11/23 23:43:41
  • Location: Ottawa, Canada
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/08 14:34:59 (permalink)
+3 (3)
 
The PIC12F1840 is one of my favorite "go to" parts; I like to think of it as a 555 timer on steroids (has an analog comparator and a flip-flop as peripherals).  Seldom does a day go by when I don't have its datasheet currently open.  The following is based upon my understanding of that datasheet.  The same info likely applies to many other PICs with the same advanced mid-range architecture.
 
I invite others to correct me if the following is wrong!
 
Every Tcy the PIC does a 'fetch' phase for instruction 'N', and an 'execute' phase for the current instruction 'N-1'.  The pipeline can be stalled by some instructions, and effectively a NOP instruction is internally inserted into the pipeline.  There doesn't appear to be a pipeline flush either, rather just a NOP again is internally executed.
 
The instruction clock has 4 phases, Q1 Q2 Q3 Q4, and interrupts are only detected during the Q1 phase, and effect the 'fetch' phase for what would be instruction 'N+1'.  Keeping in mind that instruction 'N-1' has already started the 'execute' phase, and instruction 'N' has already started the 'fetch' phase in Q1.
 
This is important:  all instructions in the pipeline at the time of the interrupt will complete execution!  There is no instruction 'abandon and restart-later' mechanism.
 
When interrupt processing begins, the GIE flag is automatically cleared, preventing further interrupts from being detected.  However any pending interrupts ('xIF' flag and 'xIE' enable set) will cause another interrupt once the GIE flag is set again, either directly or via execution of the 'RETFIE' instruction.  It is up to the interrupt handler to choose to handle only a single interrupt source and clearing its 'xIF' flag, or handling all of them, before completing with the 'RETFIE' instruction which always sets the GIE flag.
 
You don't have to use interrupts.  Peripherals can be polled, but that does require more attention to timing.
post edited by isoar - 2020/08/08 15:20:07
#10
Hen
Senior Member
  • Total Posts : 92
  • Reward points : 0
  • Joined: 2018/10/24 04:01:44
  • Location: 0
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/08 21:04:36 (permalink)
0
isoar 
I invite others to correct me if the following is wrong!

Not to correct you but expand on the subtopic...
My go-to *enhanced 555* is the <AtTiny10> mainly because (the non-LLC) footie (SOT-23).
A SOT-363 would be even more hot...
#11
Djsarkar
Starting Member
  • Total Posts : 56
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/14 20:38:57 (permalink)
0
I have taken too much time to reply on the thread because I have spent my time to reading interrupt in last few days
 
My question What happen if external interrupt occur while timer interrupt is executing ?
 
Example :  flash LED at every 1000ms but when button pressed, LED should be stop flashing
 
consider following case
set timer interrupt to flash LED every 1000ms
set external interrupt on button to control LED
 

int main ()
{
      Button Input
      LED Output
 
      Set timer interrupt
      Set External interrupt
     return 0;
 }
 
ISR Flash ()
{
     LED ON
     Wait (1000ms)
     LED OFF
}
ISR Button ()
{
  LED OFF
}

 
Note : This is pseudo code will not compile. It's just example to demonstrate process
 
Will interrupt priority be use here ? I mean external interrupt will set at higher priority and timer will set at lower priority
post edited by Djsarkar - 2020/08/14 20:48:31
#12
ric
Super Member
  • Total Posts : 28378
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/14 21:13:03 (permalink) ☼ Best Answerby Djsarkar 2020/08/14 21:43:36
+3 (3)
A PIC12F1840 does not have interrupt priority, so your pseudo code does not represent what you can actually do in that PIC.
It only has a single hardware interrupt service, so what happens depends entirely upoin what code you put there.
Everything I said in post#3 applies if another interupt is triggered while the service code is running.
 
It would be better to try to write REAL code, then we could give you REAL answers.
n.b. You should NEVER put time delays inside interrupt services.
An interrupt service should do the minimum possible to clear the interrupt condition, and exit again ASAP.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#13
Djsarkar
Starting Member
  • Total Posts : 56
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/14 21:46:43 (permalink)
0
ric
n.b. You should NEVER put time delays inside interrupt services.
An interrupt service should do the minimum possible to clear the interrupt condition, and exit again ASAP.
 



Thank you I wish to know Why the Interrupt service routine should be short? What could be effect if LED is flashing in Interrupt service routine?
#14
ric
Super Member
  • Total Posts : 28378
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/14 22:42:25 (permalink)
+1 (1)
Djsarkar
Thank you I wish to know Why the Interrupt service routine should be short?

Because no other interrupt can be serviced while you are still inside the service routine.
 

What could be effect if LED is flashing in Interrupt service routine?

No other interrupt can be serviced.
 
In a real program, a much more effective tehnique would be to use a timer interrupt triggering once every millisecond.
Each service, you would check if it is time to turn the LED on or off, or just leave it alone.
You could also check the status of your button, and perform any software debouncing if required.
Apart from when trying to stop the processor to save power, it is rarely useful to have a pushbutton directly trigger an interrupt. The PIC is MUCH faster than the pushbutton.
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#15
Djsarkar
Starting Member
  • Total Posts : 56
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/15 02:20:34 (permalink)
0
ric
It would be better to try to write REAL code, then we could give you REAL answers.
n.b. You should NEVER put time delays inside interrupt services.
An interrupt service should do the minimum possible to clear the interrupt condition, and exit again ASAP.
 
In a real program, a much more effective tehnique would be to use a timer interrupt triggering once every millisecond.
Each service, you would check if it is time to turn the LED on or off, or just leave it alone.
You could also check the status of your button, and perform any software debouncing if required.
Apart from when trying to stop the processor to save power, it is rarely useful to have a pushbutton directly trigger an interrupt. The PIC is MUCH faster than the pushbutton.
 

Program to generate interrupt at every 1ms (PIC16f877a). how to flash LED at every 1000ms but when button pressed, LED should be stop flashing

 
// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
 
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
 

#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include <xc.h>
 

void  initialize (void)
{
   //Make all PORTD pins low
  
   PORTD = 0;
 
   //Configured Port pin as input
   TRISB = 0b00000001;  //RB0 as input PIN for button
   
// Configured PORT pins as Output
  
   TRISC = 0b00000000;  //RC7 pin as out for LED
 
}
 
void main(void)
{
    //initialize port pins
       initialize ();  
 
    // Timer0 with external instruction clock  and 32 as prescalar
     OPTION_REG = 0b00000100;
   
    TMR0 = 100;    // Load the time value
 
    TMR0IE = 1;      //Enable timer interrupt bit
    INTE = 1;       //Enable RB0 as external Interrupt pin
  
    PEIE=1;         //Enable the Peripheral Interrupt
 
    GIE = 1;        //Enable Global Interrupt
  
    while(1)
    {
 
    }
    return;
}
 
void interrupt timer_isr()
{
    if(TMR0IF==1)
    {
       
        TMR0IF=0;       // Clear timer interrupt flag
   
   
    } //TMR0IF
}// interrupt
 

 
 
post edited by Djsarkar - 2020/08/15 02:22:09
#16
ric
Super Member
  • Total Posts : 28378
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/15 03:18:34 (permalink)
+1 (1)
Are you sure you want to select the "HS" oscillator?
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
That requires an external crystal.
It's a lot easier to use the internal oscillator when starting out.
 
Be aware, this:
    TMR0 = 100;    // Load the time value

only sets the count for the first cycle.
So you will get a count of 156 the first time, but then it will count a full 256 for all subsequent counts.
To get the period you want, you will have to reload the TMR0 value in each interrupt service.
 
You need to confirm you really are getting your one millisecond interrupt rate before proceeding.
 
 
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#17
Djsarkar
Starting Member
  • Total Posts : 56
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/15 04:38:46 (permalink)
0
ric
No other interrupt can be serviced.
 
In a real program, a much more effective tehnique would be to use a timer interrupt triggering once every millisecond..
 

As you said  to use timer interrupt triggering once every millisecond..  I shown you example to know what happen when external interrupt occur while timer interrupt is executing

 
int main ()
 
{
 
   count = 0;
 
}
 
void interrupt timer_isr()
{
    if(TMR0IF==1)
    {  
 
       LED = ON;  
       TMR0IF=0;       // Clear timer interrupt flag
       TMR0 = 100;    // Load the time value
 
     if (count = 1000)
 
    {
 
      LED = OFF   
      count = 0;
    }
}// interrupt

I assuming internal oscillator
 
How to write complete routine ?
post edited by Djsarkar - 2020/08/15 06:45:01
#18
upand_at_them
Super Member
  • Total Posts : 649
  • Reward points : 0
  • Joined: 2005/05/16 07:02:38
  • Location: Pennsylvania
  • Status: online
Re: What happens if another interrupt occur when current one is executing 2020/08/15 07:13:30 (permalink)
0
If you use Timer2 you don't have to reload.
#19
hexreader
Super Member
  • Total Posts : 1078
  • Reward points : 0
  • Joined: 2008/05/04 03:06:55
  • Location: England
  • Status: offline
Re: What happens if another interrupt occur when current one is executing 2020/08/15 07:58:08 (permalink)
+1 (1)
I don't think PIC16F877A has internal oscillator option...
 
I gather from other forum that Djsarkar does not have any hardware available. I could not learn that way, but maybe others can.
 
PIC12F1840 does have internal oscillator, so let's go back to that PIC.
 
Attached is my attempt at using a state machine to flash LED until external interrupt stops it. I do not like it and would not do this task this way, but you are free to take it or leave it....
 
Curious as to why the line with three question marks gives (harmless) warning?
 
 
post edited by hexreader - 2020/08/15 08:00:16

Experienced Hobbyist
#20
Page: 123 > Showing page 1 of 3
Jump to:
© 2020 APG vNext Commercial Version 4.5