0
\$\begingroup\$

I was building a simple blink application on an STM8S. The LED on-off interval was controlled by the delay function using the TIM4 interrupt. A button event monitor is using another external interrupt to prevent interference from the timer interrupt.

I found that the button event is not interrupted by the delay function, but the function associated with it still gets interrupted by the delay function. It will only execute after delay() returns no matter how early I press the button.

Is there a way to bypass this? Should I enable another timer (like TIM2) to independently handle the button event? I need some insights.

The button is controlled by the code here:

INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ //if the Button is pressed if(mode == 2) { mode = 0; } else { mode ++; } } 

main.c

void main(void) { CLK_Config(); GPIO_Config(); TIM4_Config(); /* Infinite loop */ while (1) { Switch_Blink_Pattern(mode); } } 

The delay function is from STM's demo, using TIM4:

 INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ TimingDelay_Decrement(); /* Cleat Interrupt Pending bit */ TIM4_ClearITPendingBit(TIM4_IT_UPDATE); } void Delay(__IO uint32_t nTime) { TimingDelay = nTime; while (TimingDelay != 0); } void TimingDelay_Decrement(void) { if (TimingDelay != 0x00) { TimingDelay--; } } 
\$\endgroup\$
13
  • 1
    \$\begingroup\$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. \$\endgroup\$ Commented Oct 28, 2021 at 8:55
  • 1
    \$\begingroup\$ Please post the code to see what your code does. \$\endgroup\$ Commented Oct 28, 2021 at 8:58
  • \$\begingroup\$ I updated the codes. \$\endgroup\$ Commented Oct 28, 2021 at 9:12
  • \$\begingroup\$ the delay function doesn't interrupt another interrupt. even the timer4 ISR which is used as a delay counter, has a very low priority. it's priority could be pushed further down by enabling software priorities. \$\endgroup\$ Commented Oct 28, 2021 at 9:25
  • \$\begingroup\$ Well your code does not show everything. If your blinking code does a full sequence before even bothering to read that mode has changed the that's what it does. \$\endgroup\$ Commented Oct 28, 2021 at 9:26

1 Answer 1

2
\$\begingroup\$

You handle the button press in interrupt, but actually change pattern in main cycle. It is natural that Switch_Blink_Pattern() get interrupted by timer - this is how interrupts are designed to work. To get immediate reaction you need to call Switch_Blink_Pattern() from interrupt handler, like this:

INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ //if the Button is pressed if(mode == 2) { mode = 0; } else { mode ++; } Switch_Blink_Pattern(mode); } 

Also, you need to check that EXTI interrupt have higher priority than TIM interrupt. You already have that (priority is second parameter of handler, lower number means higher priority).

\$\endgroup\$
1
  • 1
    \$\begingroup\$ meanwhile I was able to sort of achieved it using the idea from Arduino's "blink without delay" with the millis() function (I customized one), your answer let me better understand how interrupts actually work, thank you \$\endgroup\$ Commented Oct 30, 2021 at 6:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.