cancel
Showing results for 
Search instead for 
Did you mean: 

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.

DKunz.1
Associate II

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:)

6 REPLIES 6

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
Associate II

Ye homework...

Thanks for the quick answer

Hm i guess that works.

But we are supposed to practice external inputs x(

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
Associate II

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
Chief II

Good information on basics:

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

Parts 5 and 6 are about interrupts.

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