Need help with counter

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

I am having trouble understanding how to use the counter component to measure frequency. I did google and find the example for frequency counter and thought I could use software to enable the timer instead of the PWM. But the document does not give any example code for reading the counter - so I assume you simply Counter_ReadCounter().

Attached is the code snippet I am trying and the settings I used for the counter.

The input signal is 125.05 kHz and the sample window is 10ms

The input signal is correct (I am viewing on a scope and have frequency counter attached).

The count should be 1250 but I am getting wild values from 1100 to 2000

From what I read, the counter is enabled and reset when the component is Started, it then starts counting up and when stopped, the count register holds the number of transitions over the period.

Thanks in advance

0 Likes
1 Solution

There are many ways to measure frequency. It is a balance between accuracy and dedicated system resources. I recommend to start with simple working example above and then modify it to your needs. Best counting accuracy which can be achieved this way is ~(1/2) x 125kHz x 10 ms = 625, e.g. 9-bit. Using software delay can only worsen it. Solution depends on what accuracy needed.

If result is needed only once on startup and h/w resources are limited, it all can be done in software by counting pulses directly using interrupt and utilizing SysTick timer to count delay window - no hardware needed. The 125kHz is low enough to use pin interrupt. Of course, the system will hang for some 10ms, but there is no penalty for that on startup.

/odissey1

View solution in original post

0 Likes
5 Replies
lock attach
Attachments are accessible only for community members.
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

This thread has several examples for high-resolution frequency meters

HELP, with example of frequency measurement Error

I believe that you were looking for basic counter over fixed time window (which works well at high frequencies). Please see such example below.

/odissey1

Frequency meter_01b.png

Frequency meter_01a.png

0 Likes

I'm confused by the information provided in your link. Where does it explain how to start and stop the time in software and read the number of pulses received at the input please?

0 Likes

See updated post above for basic example

/odissey1

0 Likes

So is using the PWM more accurate than a delay?

I assume I set up same components and interrupt and implement code below at power up (I only need the measurement done once and then input is used for other function).

PWM_Windows_Start()

PWM_windowPeriod = 0.01;

while (!flag_DtrReady)

{

  uint32 counter_countVal = Counter_ReadCapture(); // Read the Counter capture register

  uint32 input_freq = (uint32) ((double) counter_countVal * PWM_freq);

}

PWM_Windows_Stop(();

0 Likes

There are many ways to measure frequency. It is a balance between accuracy and dedicated system resources. I recommend to start with simple working example above and then modify it to your needs. Best counting accuracy which can be achieved this way is ~(1/2) x 125kHz x 10 ms = 625, e.g. 9-bit. Using software delay can only worsen it. Solution depends on what accuracy needed.

If result is needed only once on startup and h/w resources are limited, it all can be done in software by counting pulses directly using interrupt and utilizing SysTick timer to count delay window - no hardware needed. The 125kHz is low enough to use pin interrupt. Of course, the system will hang for some 10ms, but there is no penalty for that on startup.

/odissey1

0 Likes