cancel
Showing results for 
Search instead for 
Did you mean: 

having trouble setting up TIM1 to measure input period and detecting no input

mchahn
Senior

I need to measure the rpm of a tachometer signal from a fan. I want to measure the period. I also need to know when the fan is stopped (i.e. the signal stops changing).

I am trying to use TIM1 for what seems to me to be a simple setup. I want an input signal edge to save the counter in a capture register (CCR1), reset the counter, and generate an interrupt so I can save the measured count. If the counter overflows then I will know the input stopped and I can record the rpm as zero. Does this seem like a good plan?

So I need interrupts on CCR1 capturing and on counter overflow. I am trying this setup. I think the following pseudo-code shows the pertinent registers. (I am programming registers directly. I personally hate any HAL like the peripheral library).

TIM1_SMCR_TS, Trigger Selection, is input source TI1

TIM1_SMCR_SMS, Slave Mode Selection mask, is trigger reset mode

TIM1_IER_TIE, input trigger interrupt, is enabled

TIM1_IER_UIE, overflow interrupt, is enabled

TIM1_CCMR_CC1S, Capture/Compare Selection, is TI1FP1

TIM1_CCER1_CC1E, Capture/Compare Enable, is enabled

TIM1_ARR, auto-reload register, is set to 0xffff

Other things like counter-enable and pre-scaler are set also.

The problem is that I'm getting overflow interrupts and no input trigger interrupts. I'm thinking maybe I'm confusing CCR capturing with the trigger reset mode. The two can be used at the same time, right?

3 REPLIES 3
mchahn
Senior

I found my problem. I didn't realize I had to set an option bit to access TIM1_CH1. So the input was missing. My schematic symbol labels showed TIM1_CH1 the same as any other pin function (easyeda).

When I discovered this I had already changed to using a simple capture on timer 2 for measuring the frequency and a timeout made with timer 4 for the zero frequency case. I am happy with this standard solution although my first one in the post above was pretty darn elegant. It did the frequency and timeout in one timer with simple interrupts.

SBrow.1
Associate II

I'm doing something similar, and seem to be having a similar problem (reading 0 count). Here's a link to my post:

https://community.st.com/s/question/0D53W00000KpFv2SAF/stm8-frequency-counter

Could you elaborate me on your solution? How did you enable access to TIM1_CH1?

mchahn
Senior

My problem was simple. I didn't set the alternate function option. I'm sorry but I don't have time to look at your code.