Page 1 of 1

Timer counter on STM32F407

Posted: Fri Dec 18, 2020 8:15 pm
by pp5mgt
I'm trying to count events from a GPIO on STM32F407 from Discovery Board using Timer 2.

Code: Select all

import time
import machine
from pyb import LED, Timer, UART, ADC, Pin

pin_a = pyb.Pin("A1", pyb.Pin.AF_PP, pull=pyb.Pin.PULL_NONE, af=pyb.Pin.AF1_TIM2)
pin_b = pyb.Pin("A15", pyb.Pin.AF_PP, pull=pyb.Pin.PULL_NONE, af=pyb.Pin.AF1_TIM2)

enc_timer = pyb.Timer(2, prescaler=1, period=100000)

enc_channel = enc_timer.channel(2, pyb.Timer.ENC_B)

enc_timer.counter(0)

while True:
	time.sleep_ms(100)
	print(enc_timer.counter())
The code works fine, except for the counter value that sometimes increases and in some resets the value decreases when the GPIO A1 change the input value.

Are there some additional configs for the timer settings to increase the counts on GPIO change?

Re: Timer counter on STM32F407

Posted: Fri Dec 18, 2020 9:51 pm
by dhylands
You've setup a quadrature decoder, which has 2 inputs.

So whether changes in A1 will increase or decrease the count depends on the value on the other pin (A15 in your example).

If you have nothing hooked up to A15 then it will be floating and will capacitively couple to nearby signals on the circuit board.

You can either add an external resistor, or use the internal one.

If you were just intending to use a single pin, I think that you can use a timer pin that has the ETR designation on it (ETR = external trigger) and then configure the timer to use the ETR as a clock source. Then the timer will increment each time the ETR toggles. I suspect that it will increment on either the rising/falling edge and it may be configurable - you'd need to check with the reference manual.

Re: Timer counter on STM32F407

Posted: Mon Jan 11, 2021 11:47 am
by pp5mgt
dhylands wrote:
Fri Dec 18, 2020 9:51 pm
You've setup a quadrature decoder, which has 2 inputs.

So whether changes in A1 will increase or decrease the count depends on the value on the other pin (A15 in your example).

If you have nothing hooked up to A15 then it will be floating and will capacitively couple to nearby signals on the circuit board.

You can either add an external resistor, or use the internal one.

If you were just intending to use a single pin, I think that you can use a timer pin that has the ETR designation on it (ETR = external trigger) and then configure the timer to use the ETR as a clock source. Then the timer will increment each time the ETR toggles. I suspect that it will increment on either the rising/falling edge and it may be configurable - you'd need to check with the reference manual.
Thanks for the answer, I did forget the pull down from A15