cancel
Showing results for 
Search instead for 
Did you mean: 

Frequency Measuring

nechi
Associate III
Posted on August 20, 2015 at 15:26

Hi ,

Please i have to measure frequency from 1Hz to 3Khz with timer 1 (16bits)

My SysTick clk = 72Mhz, so i set the prescalor to (1000-1) .So i have a Counter Clock=72K

and than i can capture frequency starting from 1Hz. but the problem that i capture as a minimum a value=12362 which gives a frequency= 72k/12362=5.8hz # 1hz ! i can't understand why it can't capture less than 5.8hz
8 REPLIES 8
raptorhal2
Lead
Posted on August 20, 2015 at 15:46

Have you allowed for the fact that the counter is going to overflow once or twice during one second ?

Show the code that processes the capture register values.

Cheers, Hal

Posted on August 20, 2015 at 20:21

72000 > 65536

A 72KHz clock will advance 72000 ticks in one second. A 16-bit counter only has 16-bits, and will wrap.

You'll need to use a bigger prescaler, or auto-range so you can get better resolution/precision.

Consider using a part with a 32-bit timer.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
megahercas6
Senior
Posted on August 21, 2015 at 14:49

I don't know will it be possible for you to setup timer interrupt in this mode, but i know that you can generate overflow interrupt, so when you get first edge, you set overflow variable as zero, and increment each time you get overflow.

After that, simple math will allow you to get +17b timer. freq~1/(overflow*65536+timer_value)*88ns

void InitializeTimer()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
NVIC_InitTypeDef nvicStructure;
TIM_TimeBaseInitTypeDef timerInitStructure;
timerInitStructure.TIM_Prescaler = 40000;
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = 500;
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
timerInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &timerInitStructure);
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
nvicStructure.NVIC_IRQChannelSubPriority = 1;
nvicStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicStructure);
}
void TIM2_IRQHandler()
{
if (TIM_GetITStatus(TIM2,TIM_IT_CC2) != RESET)
{
overflow++;
TIM_ClearITPendingBit(TIM2,TIM_IT_CC2);
}
}

nechi
Associate III
Posted on August 22, 2015 at 14:08

Thank you ,

but the timer will overflow will be after at the  ticks number 65535 and it's able to reach 72000 ticks per second 

nechi
Associate III
Posted on August 22, 2015 at 14:14

I really appreciate ! thanks every body for replying 

in my case i have only 16 bit timer and i can't synchronize both timers to get 32 bit timer!

the hardware that i'm working on restricts me! 

raptorhal2
Lead
Posted on August 22, 2015 at 16:29

Let's see if a picture really is worth a 1000 words. See attached.

As Clive1 suggested, change the prescalar.

Cheers, Hal

________________

Attachments :

TIMEFREQSimple.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I1Ek&d=%2Fa%2F0X0000000bk3%2FakzwqQth2rBEoCGu0OyDyjzZSWDr_5aRLjoceT7WGsc&asPdf=false
raptorhal2
Lead
Posted on August 23, 2015 at 01:48

It would help if my math was correct. See attached corrected figure.

Cheers Hal

________________

Attachments :

TIMEFREQSimple.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I1Dx&d=%2Fa%2F0X0000000bk1%2FeYEWnLRphok0B0AThVeHfvLYTqGm5PVf1MFC0pMAU_w&asPdf=false
nechi
Associate III
Posted on August 24, 2015 at 10:29

Thank you Baird and clive,

it's clear i'll try this.