2013-10-16 02:17 AM
Hello folks,
i have to try write a C-Code for counting an external frequency in a Range of 250ms. I tried to use the encoder mode, but i think it is impossible only with one signal. In my project i have only one frequency signal in a range of 0Hz to round about 400Hz. Could you help me or show examples how to count external frequency by using timer? Thanks a lot!! #stm32 #stm32 #timer #exti #interrupt2013-10-16 05:18 AM
Well there are several methods, you can just do a raw count over some integration period, or you could use input capture or PWM input, these would permit period/duty type measurements.
[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F4%20Timer%20External%20Pulse%20counting&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=134]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FSTM32F4%20Timer%20External%20Pulse%20counting&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=1342013-10-16 06:13 AM
Hi Clive,
thanks for your replay, i include this part of code:void TIM3_Config(void)
{ GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;/* GPIOC clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);/* GPIOA Configuration: TIM3 CH1 (PC6) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; // Input GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
TIM_TimeBaseStructure.TIM_Period = 21000;
TIM_TimeBaseStructure.TIM_Prescaler = 1000; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);TIM_TIxExternalClockConfig(TIM3, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);
} maybe you can have a look??? Thanks a lot!!!2013-10-16 06:29 AM
TIM_TimeBaseStructure.TIM_Period = 21000;
TIM_TimeBaseStructure.TIM_Prescaler = 1000; Why? Also these take N-1 values, but 400 Hz would easily fit in 16-bit2013-10-16 10:58 PM
Oh okay, i thought i set the time to 250ms.
Can you explain me how i can set the time??2013-10-17 04:27 AM
For that you'd need a second timer, or SysTick, to mark off the 250 ms sample period, and then do a delta measurement of clock ticks on the TIMx->CNT of your external source over the period.
ie freq = (b - a) * 42013-10-17 06:16 AM
Okay, but with your example on
[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F4%20Timer%20External%20Pulse%20counting&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=134]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F4%20Timer%20External%20Pulse%20counting&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=134 i'm able to count the signal? Thats right? Now i need a second Timer, which set the time (250ms) of the first timer/counter?2013-10-17 06:35 AM
Why don't you try it? Then you'd be able to observe first hand what it does.
TIM3->CNT will represent your external count, it will keep counting up, and wrap at the 16-bit boundary. If it reads 123, and then 223 some 250 ms later it has counted 100 ticks, over a second this would represent 400, and thus 400 Hz There are other ways to gate the timer, or measure the individual period/duty, you might want to review the manual to understand the options.