Strictly necessary cookies are on by default and cannot be turned off. Functional, Performance and Tracking/targeting/sharing cookies can be turned on below based on your preferences (this banner will remain available for you to accept cookies). You may change your cookie settings by deleting cookies from your browser. Then this banner will appear again. You can learn more details about cookies HERE.
Strictly necessary (always on)
Functional, Performance and Tracking/targeting/sharing (default off)
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.
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.