How do you correctly setup Timer9 (TIM9)on an STM32F405xx to count pulses on rising edge? Is there a special configuration for the pin to the timer outside of this short example?

Here's what I've done so far and also followed the BRIEF counter example from the reference manual copied here:

For example, to configure the upcounter to count in response to a rising edge on the TI2 input, use the following procedure:

  1. Configure channel 2 to detect rising edges on the TI2 input by writing CC2S = ‘01’ in the TIMx_CCMR1 register.

  2. Configure the input filter duration by writing the IC2F[3:0] bits in the TIMx_CCMR1 register (if no filter is needed, keep IC2F=’0000’).

  3. Select the rising edge polarity by writing CC2P=’0’ and CC2NP=’0’ in the TIMx_CCER register.
  4. Configure the timer in external clock mode 1 by writing SMS=’111’ in the TIMx_SMCR register.
  5. Select TI2 as the trigger input source by writing TS=’110’ in the TIMx_SMCR register.
  6. Enable the counter by writing CEN=’1’ in the TIMx_CR1 register.

I believe I've done the above correctly, but reading the register returns 0 always. Is there any additional setup within a different register to connect the to the Timer? I was reading about alternate functions but could not find anything specific to Timers and Pins.

I am loosely following this as a code reference: https://www.ghielectronics.com/docs/108/register-access

Chip Datasheet: http://www.st.com/web/en/resource/technical/document/datasheet/DM00037051.pdf

Chip Family Reference Manual: http://www.st.com/web/en/resource/technical/document/reference_manual/DM00031020.pdf

  • \$\begingroup\$ Have you enabled all the required clocks? For example, TIM9 is on APB2 so if you haven't enabled that clock the timer won't work. \$\endgroup\$
    – A.Mac
    Mar 15, 2016 at 22:53
  • \$\begingroup\$ @A.Mac Interesting. I found the register: RCC_APB2ENRin the ref manual, but I can't find the address in the datasheet. \$\endgroup\$
    – GisMofx
    Mar 16, 2016 at 0:39
  • \$\begingroup\$ @A.Mac I found RCC in the AHB1 Bus starting at 0x40023800 and the ref manual shows an offset of 0x44 for RCC_APB2ENR. bit 16 is TIM9EN. I set that bit and I'm still getting zero. \$\endgroup\$
    – GisMofx
    Mar 16, 2016 at 0:51
  • \$\begingroup\$ It's not really necessary to directly deal with register addresses. \$\endgroup\$
    – A.Mac
    Mar 16, 2016 at 1:25
  • \$\begingroup\$ Sorry, I'm pretty new to posting on SE, so I posted the comment when I was just trying to add a linebreak. Anyway, as I was saying, instead of dealing directly with the register addresses, you can use the more human understandable #defines like the <code>RCC_APB2ENR</code>. As long as you've included the proper headers the compiler will know what you're talking about. For example, to enable the clock for TIM2 you would use <code> RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; </code> It would also be useful for you to post your code (Preferably after updating from using register addresses.) \$\endgroup\$
    – A.Mac
    Mar 16, 2016 at 1:43

1 Answer 1


First thing I always check is to make sure that all the clocks I need are turned on. You'll need to make sure that the clocks for APB2 (TIM9) and AHB1 (GPIO) are turned on. Additionally, timer functions are alternate functions, so you'll need to set the GPIOx_MODER bits for the appropriate port and pin you're using, as well as the appropriate GPIOx_AFRy bits.

The datasheet and reference manual should have all the info you need, sometimes it just takes some doing to find what you're looking for.

  • \$\begingroup\$ Thanks again! I forgot to ask, what's the correct way to reset the count in TIMx_CNT register back to 0? I tried using RCC_APB2RSTR to resete, but I think that sets it all back to default. \$\endgroup\$
    – GisMofx
    Mar 16, 2016 at 13:13
  • \$\begingroup\$ I think I got it: Setting UG bit to 1 in TIMx_EGR seems to reset the counter and not affect the rest of the setup..so far \$\endgroup\$
    – GisMofx
    Mar 16, 2016 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.