Problem with input at STM32 - Page 1
Forum section: electronics / general

author: Anastasia
author_rank: Newbie
timestamp: October 12, 2025, 06:04:14 pm
content: My task is to measure the time interval of a signal. I am having trouble getting my STM32F103 to detect the output of an LM339 comparator using TIM2 Input Capture on PA1.I have configured PA1 as a digital input for TIM2_CH2 and set the capture polarity to falling edge. The timer runs (UIF flag sets), but CC2IF never sets and the capture callback is never called.I need help identifying why the STM32 cannot capture the edge from the comparator’s output.

author: ME
author_rank: Frequent Contributor
timestamp: October 13, 2025, 12:49:58 am
content: Maybe ask in the microcontroller section rather than beginners?.

author: pcprogrammer
author_rank: Super Contributor
timestamp: October 13, 2025, 06:47:29 am
content: You can move the topic with the button at the bottom of the page. It is only there if you started the topic. See the attached pictures for more information. Select the microcontrollers section in the drop down list and click on move topic.Best to also include the code you are using to try capturing the signal.

author: Anastasia
author_rank: Newbie
timestamp: October 13, 2025, 08:52:47 am
content: Sorry for the misunderstanding, but for some reason I decided this question wasn't "worthy" of being in the microcontrollers thread.The question contains a code.c file that contains the signal capture settings.

author: radiolistener
author_rank: Super Contributor
timestamp: October 13, 2025, 10:32:17 am
content: First check if the MCU can see the signal when PA1 is configured as a simple GPIO input.If it can, the problem is in your timer setup.If not, verify that the comparator output actually reaches valid logic levels (close enough to 0 V and 3.3 V) and that the signal edges are sharp — slow or noisy transitions can prevent proper capture.This way you can isolate whether the issue is electrical or in your code.

author: pcprogrammer
author_rank: Super Contributor
timestamp: October 13, 2025, 10:57:14 am
content: Quote from: Anastasia on October 13, 2025, 08:52:47 amOops my bad, missed that one.Unfortunately you are using HAL and I hate it due to it being not transparent. Hard to say what it does under the hood.In the GPIO init, it does seem to be setting the PA1 pin as input for the timer, but it also sets pin PA5 to output and enables the clock for port D for some reason.I see the USART1 being initialized, but the TX pin on PA9 is not set for it. All in all a bit of a mess, and hard to grasp what it should be doing.My advise is to learn bear metal programming and use the registers directly.

author: woofy
author_rank: Frequent Contributor
timestamp: October 13, 2025, 11:53:35 am
content: Are you are running this on the Proteus simulation.If so, I would suggest real hardware instead and use the ST debugging tools.

author: 8goran8
author_rank: Contributor
timestamp: October 13, 2025, 08:30:05 pm
content: There is an similar HAL example in the STM32F1Cube Package for the STM3210E_EVAL board.You can compare it with your project.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 15, 2025, 08:03:24 pm
content: Quote from: pcprogrammer on October 13, 2025, 10:57:14 amShould be fine.HAL_UART_InitcallsHAL_UART_Msp_Initwhich initializes stuff like that.Quote from: pcprogrammer on October 13, 2025, 10:57:14 amSeeing that is pretty easy, HAL is barely convolutedIs PA5 actually toggling? Make sure SYSTEM CORE > SYS > TimeBase Source = Systick, otherwise timings or delays won't work.Edit: I made my own quick version, Input CCP worked in real HW, didn't in Proteus.

author: pcprogrammer
author_rank: Super Contributor
timestamp: October 16, 2025, 06:35:20 am
content: Quote from: DavidAlfa on October 15, 2025, 08:03:24 pmMaybe once you know it, but so is using the bare metal registers.

author: Anastasia
author_rank: Newbie
timestamp: October 16, 2025, 01:02:27 pm
content: Thanks a lot for this advice, now I'm connecting a +3 source to the PA1 pin.3V and in debugging I see that the STM32 reads this signal as "low", it switches to "high" only at 5V. I don't know what it's related to.However, if I set the value to 5V in the Pulse Generator, the STM32 still recognizes it as "0". If you change the generator type from "Pulse" to "Clock", then recognition is working properly.This method does not suit me, according to the assignment, the voltage should vary from 0V to -4V, I have to convert it and analyze the time interval.I'm going to continue experimenting, maybe something will work out.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 18, 2025 04:44:45 pm
content: As I said, the problem lies in Proteus, CCP doesn't work, use real HW.Converting -4V signal to +3.3V is very simple... Just take in mind the polarity gets inverted.Your original circuit is wrong all the way, you can't feed negative voltage to an op-amp powered with only +VDD and GND (-VDD), it also need negative in VDD- voltage to be able to work with it.Or, you can use some resistors to shift the voltage: