Timer counter on STM32F407

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
pp5mgt
Posts: 2
Joined: Fri Dec 18, 2020 8:07 pm

Timer counter on STM32F407

Post by pp5mgt » Fri Dec 18, 2020 8:15 pm

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?

User avatar
dhylands
Posts: 3505
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Timer counter on STM32F407

Post by dhylands » 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.

pp5mgt
Posts: 2
Joined: Fri Dec 18, 2020 8:07 pm

Re: Timer counter on STM32F407

Post by pp5mgt » Mon Jan 11, 2021 11:47 am

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

Post Reply