2014-07-02 04:39 AM
Hello everybody,
How can i config my timer ( i work on stm32f100 ) to count external pulses this is my code but it doesn't work:void
ConfigTimer17(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* TIM17 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE); /* GPIOB Configuration: TIM17 CH1 (PB9) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Connect TIM17 pins to AF */ GPIO_PinRemapConfig(GPIO_Remap_TIM17,DISABLE); //<--> PB9 TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM17, &TIM_TimeBaseStructure); TIM_TIxExternalClockConfig(TIM17, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0); TIM_Cmd(TIM17, ENABLE); /////////////////////////////////////////// } when i read capture1 ( TIM_GetCapture1(TIM17)) i get random numbers ( also negative ). thanks in advance2014-07-02 05:02 AM
Do you have the GPIOB and AFIO clocks enabled?
If you get ''negative'' numbers you should perhaps be using unsigned variables, and routines to print them.2014-07-02 06:12 AM
2014-07-02 07:23 AM
In the External Clock context you should use TIM_GetCounter() to get the TIMx->CNT value that's ticked by your external input. If you sample this periodically (1Hz if the frequency is low, and 16-bit counters don't wrap) you can get the frequency out.
With the Input Capture context, you have the counter free run at the TIMCLK frequency, and the value of the Counter is latched into the Capture Register, effectively time stamping it against the time base. Getting subsequent values out allows you to measure the period, and consequently the frequency.2014-07-02 07:56 AM
So the TIM17->CNT is number that i need to calc ( multiple with counter clk , for example 0.041uSec ) in order to get the external pulse counting.
I thought that the increment of each bit in the TIM17->CNT is eqaul to each rising edge input. let me know if i finnaly understand this input counter.2014-07-02 01:22 PM
..... I'm looking for rising edge count not timer count.
my app is dc motor feedback ,and when i read the TIM_CNT while running the motor very slowely ... ~1 cycle per second it looks like timer count because i get big number instead small one.... only few cycles.
2014-07-02 11:47 PM
I just notice something
In the STM library ( attached below ) its written that i cant'nt config Timer 17 to count external clock only as trigger to timer : ( i've marked in function comment ) /** * @brief Selects the Input Trigger source * @param TIMx: where x can be1, 2, 3, 4, 5, 8, 9, 12 or 15
to select the TIM peripheral. * @param TIM_InputTriggerSource: The Input Trigger source. * This parameter can be one of the following values: * @arg TIM_TS_ITR0: Internal Trigger 0 * @arg TIM_TS_ITR1: Internal Trigger 1 * @arg TIM_TS_ITR2: Internal Trigger 2 * @arg TIM_TS_ITR3: Internal Trigger 3 * @arg TIM_TS_TI1F_ED: TI1 Edge Detector * @arg TIM_TS_TI1FP1: Filtered Timer Input 1 * @arg TIM_TS_TI2FP2: Filtered Timer Input 2 * @arg TIM_TS_ETRF: External Trigger input * @retval None */ void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource) Unforthenetly i must to work with timer 17 beacause my external input hard wire ( on PCB ) to PB9.