Looking for some simple interrupt examples

Post here first, or if you can't find a relevant section!
Post Reply
bobemoe
Posts: 10
Joined: Sun May 30, 2021 11:43 am

Looking for some simple interrupt examples

Post by bobemoe »

I'm trying to attach an interrupt on pin PB0 of my STM32F4 (BlackPill WeAct v3) so I am looking for a simple example to start with but am struggling to find one.

This is almost exactly what I am trying to do and would be the perfect starting place:
https://github.com/stm32duino/STM32Exam ... rement.ino but I can not get this to work. It compiles and runs but outputs "Frequency: 0" constantly.

The example uses pin D2 which is not present on my board. So I have updated that to pin PB0 which is the only change I have made to the example.

The example also states: "Note: Please verify that for your board,'pin' used for PWM has HardwareTimer capability" Does it really mean PWM in that comment? It feels like this is left over from the other example in that folder that uses PWM.

I saw some other thread where the user was also getting "Frequency: 0" and they changed to another pin and it started working.

How can I find what pins support interrupts? or has "HardwareTimer" capability? Reading this https://stm32f4-discovery.net/2014/08/s ... -tutorial/ it looks like PB0 should be an acceptable pin, on line0. Is this accurate resource? Its not really for stm32duino.

What am I missing? Is there something else I need to change in that example to use my desired pin?

Are there any other examples of using simple interrupts like the above example but for the F4 series?

What I am trying to achieve is to measure pulses per minute. Blocking solutions will not work as I have other real time code that needs to sun smooth.

Many thanks y'all :)
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: Looking for some simple interrupt examples

Post by dannyf »

that's using a timer for interrupt -> a little bit weird.

maybe you can use external interrupts or pin change interrupts instead? the more conventional way.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Looking for some simple interrupt examples

Post by ag123 »

For input signals, it may take a comparator (e.g. lm393, lm393?, before the pin to ensure that your voltages are switching in the correct ranges.

If your signals are after all *analog* signals, they may not reach the trigger levels and/or cause false triggering

Other things are to check if after all the pins are correctly configured for AFIO etc.
To make a pin available for AFIO, it may require to set it as e.g. input (pinMode(pin, INPUT) + configure it for AFIO + map that AFIO pin to your timer input, and maybe I'm not sure if there are other configs at the Timer side to see that signal. e.g. are the timers clocked, did you enable the timer? etc
missing any of these steps and it could seem like the Timer is 'not working'.

generally, the hw timer library should work (quite well)
https://github.com/stm32duino/Arduino_C ... er-library

timer codes normally looked like

Code: Select all


int pin = LED_BUILTIN;

void callback() {
	  digitalWrite(pin, !digitalRead(pin));
}

void setup() {
  HardwareTimer *MyTim = new HardwareTimer(TIM1);

  // configure pin in output mode
  pinMode(pin, OUTPUT);

  MyTim->pause();
  MyTim->setOverflow(2, HERTZ_FORMAT); // 2 Hz
  MyTim->attachInterrupt(callback);
  MyTim->refresh();
  MyTim->resume();
}
other examples
https://github.com/stm32duino/STM32Exam ... llback.ino

The above example is for an 'internal' timer interrupt just to call back the interrupt hook at regular intervals.
For inputs to timers, there are more things to look into.
sarausa
Posts: 1
Joined: Tue Oct 24, 2023 4:24 am

Re: Looking for some simple interrupt examples

Post by sarausa »

I have learned many new and interesting things from reading your post and hope you will post more interesting information in the near future.
Post Reply

Return to “General discussion”