Skip to main content
DKunz.1
Associate II
January 7, 2021
Question

Hello, i have the task to make the same LED on the MCU blink at different frequencies when pushing a push button. I know that this is possible with External Interrupts.

  • January 7, 2021
  • 6 replies
  • 1531 views

I can make LEDs blink at different frequencies but only 1 frequency...

So far i managed to get the LED config going with CubeMX.

I want to make this by using External Interrupts.

But how can i make the same LED change frequency by pushing a button on my STM32F407VG?

I managed to make other LEDs light with interrupts, but the same? oh no :(

I would appreciate your help a lot ty:)

This topic has been closed for replies.

6 replies

waclawek.jan
Super User
January 7, 2021

Homework?

> I want to make this by using External Interrupts.

EXTI is not suitable for pushbuttons. They produce many edges at push/release due to mechanical bouncing of contacts.

For homework, what's wrong with

while(1) {
 LedToggle();
 if (ButtonInput() == 0) {
 Delay(LONG);
 } else {
 Delay(SHORT);
 }
}

?

JW

DKunz.1
DKunz.1Author
Associate II
January 7, 2021

Ye homework...

Thanks for the quick answer

Hm i guess that works.

But we are supposed to practice external inputs x(

waclawek.jan
Super User
January 7, 2021

You mean, external interrupts.

Yes, a typical school assignment. It's the wrong task, as I've said above. And, actually, should you genuinely need to handle a noisy signal in EXTI, it's actually quite hard to do correctly.

What you are expected to do is:

  • have a global variable, say Speed
  • I expect you know that each module needs to have clock enabled in RCC, and I expect you've already read RM0090 chapters RCC, GPIO, SYSTICK and EXTI (that's a subchapter of the interrupts chapter)
  • set up given pin as GPIO input in GPIO (maybe with pullup or pulldown, depending on the actual circuit)
  • map given pin to EXTI in SYSCFG
  • enable detection of first edge in EXTI, say falling (if the button shorts to GND and there's a pullup), and enable interrupt on it in the EXTI interrupt mask register
  • enable the EXTI interrupt in NVIC
  • write an EXTI interrupt handler, which clears the pending register, then - based on current Speed value - clears one edge enable and sets the other, and toggles Speed,
  • in main()'s loop, perform the same blink as above, just not based on ButtonInput() but based on Speed

This will work, somehow.

Write the simple version I outlined above, without EXTI, first, and get it running.

Then try to write the interrupt version and debug it until it works somehow. If in trouble, try harder; if in serious trouble, ask.

If you want to go further and understand the problem which is in this approach, draw yourself waveform of a bouncing button http://www.ganssle.com/debouncing.htm then imagine that several of the edges occur in a rapid succession in between consecutive steps taken within the ISR, or in between invocations of the ISR.

JW

DKunz.1
DKunz.1Author
Associate II
January 8, 2021

Really nice explanation!

Thank you so much...

Why cant you be my Prof. at University LOL.

He just says: find out yourself hihihi

what a *******...

Works now thanks...

Piranha
Principal III
January 9, 2021

Good information on basics:

https://www.embeddedrelated.com/showarticle/453.php

Parts 5 and 6 are about interrupts.

DKunz.1
DKunz.1Author
Associate II
January 9, 2021

Oh thanks. This might be helpful in the future ;)