Hello
In my program, I must activate the interrupt and deactivate.
If the interrupt was triggered I have to deactivate it to activate the interrupt again at the end of the procedure.
So far I have always performed these actions as follows.
1. at the start of the program I activated the interrupt.
2. if it was triggered I called the Commponent interrupt and set the flag "deactivate interrupt".
3. at the end of the interrupt macro I called the commponent interrupt again and set the flag "activate interrupt".
I find this procedure somewhat dubious. Therefore my question is whether I can solve this problem a bit more intelegently.
Many thanks beforehand for suggestions for solutions.
interrupt activate and deactivate
Moderator: Benj
-
- Posts: 89
- Joined: Wed Aug 31, 2016 4:48 pm
- Been thanked: 4 times
- Contact:
-
- Valued Contributor
- Posts: 1191
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
- Contact:
Re: interrupt activate and deactivate
Give us some more detail - MCU etc.
You would not normally need to disable the interrupt - the MCU should not allow another interrupt to occur during the interrupt routine. The interrupt handler must of course be as short as possible (for example set a flag or increment a counter) and not call any routines that may not be re-entrant (for example it is a mistake to call an output routine in an interrupt handler - the main code may also be using it and things may not end well!). If an interrupt handler is taking so long that the same interrupt is occurring whilst it is running the program logic needs correcting.
Some code requires the interrupt flag to be cleared.
You may need to disable interrupts for time critical code (outputting to WS2812 LEDs for example)
Martin
You would not normally need to disable the interrupt - the MCU should not allow another interrupt to occur during the interrupt routine. The interrupt handler must of course be as short as possible (for example set a flag or increment a counter) and not call any routines that may not be re-entrant (for example it is a mistake to call an output routine in an interrupt handler - the main code may also be using it and things may not end well!). If an interrupt handler is taking so long that the same interrupt is occurring whilst it is running the program logic needs correcting.
Some code requires the interrupt flag to be cleared.
You may need to disable interrupts for time critical code (outputting to WS2812 LEDs for example)
Martin
-
- Posts: 89
- Joined: Wed Aug 31, 2016 4:48 pm
- Been thanked: 4 times
- Contact:
Re: interrupt activate and deactivate
Hello, Martin,
thank you very much for your detailed answer. Of course I deactivate the interrupt for Microchips MCUs with time-critical interrupts. This meant that the code to deactivate the interrupt has to be introduced into the program as C code. Since the program should run both on a PIC and on an AVR MCU, the C code is only conditionally helpful. I would have therefore, if it is possible, a Flowcode solution.
PS: As a security-conscious programmer I secure my interrupts during the processing of the interrupt by deactivating the interrupt.
thank you very much for your detailed answer. Of course I deactivate the interrupt for Microchips MCUs with time-critical interrupts. This meant that the code to deactivate the interrupt has to be introduced into the program as C code. Since the program should run both on a PIC and on an AVR MCU, the C code is only conditionally helpful. I would have therefore, if it is possible, a Flowcode solution.
PS: As a security-conscious programmer I secure my interrupts during the processing of the interrupt by deactivating the interrupt.
-
- Valued Contributor
- Posts: 1191
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
- Contact:
Re: interrupt activate and deactivate
Interrupts are automatically disabled whilst in an ISR at least on AVR MCUs (and I'll guess most processors are the same in this regard)
Can you post some code showing what you are trying to achieve...
Martin
Can you post some code showing what you are trying to achieve...
Martin
-
- Posts: 89
- Joined: Wed Aug 31, 2016 4:48 pm
- Been thanked: 4 times
- Contact:
Re: interrupt activate and deactivate
Hello Martin,
my problems are described by the unprofessional program in the appendix.
1. MCU: 18F26J50
2. any button
3. description of the example:
- the interrupt for the key is deactivated when the event occurs and
activated after about 1000ms
- A square wave signal with > 5Hz is applied to the interrupt.
- the counter "Taste" is incremented each time the interrupt is triggered by
One increased and after approx. 1000ms read out and deleted
4. result of the counter always 1 event
5. if the interrupt is not deactivated, the interrupt is deactivated during the waiting time.
is triggered and the counter shows a result greater than 1.
PS: I am an advocate of "robust" code
my problems are described by the unprofessional program in the appendix.
1. MCU: 18F26J50
2. any button
3. description of the example:
- the interrupt for the key is deactivated when the event occurs and
activated after about 1000ms
- A square wave signal with > 5Hz is applied to the interrupt.
- the counter "Taste" is incremented each time the interrupt is triggered by
One increased and after approx. 1000ms read out and deleted
4. result of the counter always 1 event
5. if the interrupt is not deactivated, the interrupt is deactivated during the waiting time.
is triggered and the counter shows a result greater than 1.
PS: I am an advocate of "robust" code
- Attachments
-
- BeispInterrupt.fcfx
- (14.45 KiB) Downloaded 216 times
-
- Valued Contributor
- Posts: 1191
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
- Contact:
Re: interrupt activate and deactivate
Takt200 is outputting data to USB
This is not a good idea - it will cause problems (if it even works - USB output might use interrupts for timing - it will break something in the future!)
Move the output into the main loop and check Takt's value there....
I did a simple frequency counter here viewtopic.php?f=26&t=20970&hilit=frequency that does something similar to your code - although I used an external source for a 1s interrupt.. I don't in the posted code - but at high frequencies (250k Hz on an Arduino) it struggled to output the data - I should have disabled the interrupts whilst displaying the data (I used an i2c oLED) - this would necessitate disabling both the frequency counter interrupt and restarting the counter on a 1s interrupt 'boundary'
Martin
This is not a good idea - it will cause problems (if it even works - USB output might use interrupts for timing - it will break something in the future!)
Move the output into the main loop and check Takt's value there....
I did a simple frequency counter here viewtopic.php?f=26&t=20970&hilit=frequency that does something similar to your code - although I used an external source for a 1s interrupt.. I don't in the posted code - but at high frequencies (250k Hz on an Arduino) it struggled to output the data - I should have disabled the interrupts whilst displaying the data (I used an i2c oLED) - this would necessitate disabling both the frequency counter interrupt and restarting the counter on a 1s interrupt 'boundary'
Martin
-
- Posts: 502
- Joined: Wed Nov 07, 2007 6:51 pm
- Location: Antwerp Belgium
- Has thanked: 121 times
- Been thanked: 108 times
- Contact:
Re: interrupt activate and deactivate
I don't want to steal this topic but I was wondering about the same thing lately.
What I need to do is detect which of two racetracks finishes first.
I have two lasers that shine across two racetracks (70cm) and my idea was to have an interrupt detect which of these laserbeams are broken first.
This would be with an Arduino Nano that looks for an interrupt. (Nano because I have limited space)
But, to rule out false signals (for instance due to rain), my idea was to wait in the interrupt routine if the beam stays interrupted for a while.
If during this time the signal stays low, the track is the one that won the race.
But during this time it also needs to check the other track if (in case of a false low at the first track), the second track would be monitored too.
I know it's a good practice to, not have much in a interrupt call, but in this case I think it's a good way to do this??
I didn't start this project yet, but because this topic was launched, my question rose up.
Any idea's on how to achieve this?
What I need to do is detect which of two racetracks finishes first.
I have two lasers that shine across two racetracks (70cm) and my idea was to have an interrupt detect which of these laserbeams are broken first.
This would be with an Arduino Nano that looks for an interrupt. (Nano because I have limited space)
But, to rule out false signals (for instance due to rain), my idea was to wait in the interrupt routine if the beam stays interrupted for a while.
If during this time the signal stays low, the track is the one that won the race.
But during this time it also needs to check the other track if (in case of a false low at the first track), the second track would be monitored too.
I know it's a good practice to, not have much in a interrupt call, but in this case I think it's a good way to do this??
I didn't start this project yet, but because this topic was launched, my question rose up.
Any idea's on how to achieve this?
-
- Valued Contributor
- Posts: 1191
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
- Contact:
Re: interrupt activate and deactivate
Hi MJU,
When the ISR is running other interrupts (and the main program) are blocked until the routine returns. If another interrupt has occurred then it will run after the first ISR returns too (but only one of each type - nested interrupts would eat stack space very quickly and are not supported - there is only a flag (single bit) for each interrupt)
Other problems may occur with a blocking ISR - say you want to time the race / or flash some LEDs etc...
How about starting a timer (or just a count) when the beam is broken. A second count can also be started if the second beam is interrupted (basically time how long the beam is broken for) - the count/timer increments should be in the main loop of your code. (Have two (or more) counters/timers - one for each lane)
If, when the beam is restored, the counter exceeds a certain 'value' then the lane with the highest count has won the race..
If, when the beam is restored, the timer is less than a certain value - it was a false alarm! (or a raindrop?) - so reset the timer. Note that it would be difficult to block all false sensing without some other check (someone puts their hand in the beam?)
Martin
When the ISR is running other interrupts (and the main program) are blocked until the routine returns. If another interrupt has occurred then it will run after the first ISR returns too (but only one of each type - nested interrupts would eat stack space very quickly and are not supported - there is only a flag (single bit) for each interrupt)
Other problems may occur with a blocking ISR - say you want to time the race / or flash some LEDs etc...
How about starting a timer (or just a count) when the beam is broken. A second count can also be started if the second beam is interrupted (basically time how long the beam is broken for) - the count/timer increments should be in the main loop of your code. (Have two (or more) counters/timers - one for each lane)
If, when the beam is restored, the counter exceeds a certain 'value' then the lane with the highest count has won the race..
If, when the beam is restored, the timer is less than a certain value - it was a false alarm! (or a raindrop?) - so reset the timer. Note that it would be difficult to block all false sensing without some other check (someone puts their hand in the beam?)
Martin
-
- Posts: 502
- Joined: Wed Nov 07, 2007 6:51 pm
- Location: Antwerp Belgium
- Has thanked: 121 times
- Been thanked: 108 times
- Contact:
Re: interrupt activate and deactivate
Without trying to steal this topic: thank you for you suggestion.mnf wrote:Hi MJU,
How about starting a timer (or just a count) when the beam is broken. A second count can also be started if the second beam is interrupted (basically time how long the beam is broken for) - the count/timer increments should be in the main loop of your code. (Have two (or more) counters/timers - one for each lane)
If, when the beam is restored, the counter exceeds a certain 'value' then the lane with the highest count has won the race..
If, when the beam is restored, the timer is less than a certain value - it was a false alarm! (or a raindrop?) - so reset the timer. Note that it would be difficult to block all false sensing without some other check (someone puts their hand in the beam?)
Martin
I will build the setup and after that try these suggestions.
-
- Posts: 89
- Joined: Wed Aug 31, 2016 4:48 pm
- Been thanked: 4 times
- Contact:
Re: interrupt activate and deactivate
Hello MJU
with PICs I have solved the problem, because there is an interrupt enable flag which disables the interrupt until the interrupt is activated again.
with PICs I have solved the problem, because there is an interrupt enable flag which disables the interrupt until the interrupt is activated again.