Home arrow Support arrow Forums

Luminary Micro Forums

<< Start < Prev 1 2 Next > End >>

tim17

Fresh Boarder

2008/09/25 08:14

How to measure frequencies with the timer module?

Hello,

does anyone have an example/idea how to measure the frequency of a simple signal (continuously via Interrupts)?
I have no idea how to do this with the LMI timer module.

login or register to reply

LMI Dave

Platinum Boarder

2008/09/25 09:18

Re:How to measure frequencies with the timer module?

Tim,

I can't offer a concrete solution for you without more details of your situation but here are a few suggestions that may give you ideas about where to start. The basic questions are whether you are measuring an analog or digital signal, what frequency range you want to measure and how accurate you need the measurement to be.

If your signal is digital, you have a couple of options.

Digital Option A - attach the signal to the relevant CCP pin of your device and set up the associated timer in 16 bit, edge count mode. Set up an additional timer (or the other 16-bit half of this timer) to free run.

In edge count mode, the timer will count down from the load value you specify and generate an interrupt when it reaches your match value (in other words, after it detects your chosen number of edges on the input signal). You can decide whether to count rising, falling or both edges. When the interrupt fires, you know the number of edges that have been counted and, reading your other timer, you know the elapsed time. From these values, you can determine the frequency of your signal.

Digital Option B - attach the signal to a GPIO input and set the GPIO to interrupt on one edge. Start a timer such that it counts upwards freely and wraps automatically. Each time the interrupt fires, read the timer and reset it to 0. The timer value you read represents 1 period of your signal and you can derive the frequency easily from this.

This scheme is easy to implement and should work well for low frequency (KHz range?) signals. The measurement will contain jitter dependent upon the interrupt configuration and relative priority of the GPIO interrupt and any other interrupts you are running.

Digital Option C - if the Stellaris part you are using contains a QEI peripheral, you can use this is clock/direction mode to count input pulses over a given time period and interrupt at a regular interval. When you are interrupted, you can read the pulse count and, knowing this and the interrupt frequency, calculate the frequency of the signal. This has low CPU overhead and should (mostly) eliminate the jitter you would experience if using option A. This method doesn't use a general purpose timer, though, so may not meet your requirements :-)

If you are measuring an analog signal rather than a digital one, you could try the following:

Analog Option A - Attach your signal to one of the Analog Comparators on your Stellaris part and set the reference voltage appropriately so that the comparator generates a reasonably square output (i.e. set the reference to the center point in the signal or thereabouts). Configure the comparator to send its output to a pin. This output then becomes the digital signal you can use with any of the suggestions above.

Analog Option B - If you don't want to use an additional pin on the device, and your signal is suitably slow, another option may be to use the Analog Comparator to trigger the ADC. Have the ADC interrupt on the first sample captured (it doesn't matter what you sample - you will ignore the ADC value read) and, using an independent, free-running timer, determine the period by looking at the time between ADC interrupts.

I hope one of these suggestions gives you something to start playing with. Post some more details and we'll try to offer more detailed suggestions.

login or register to reply

LMI Dave

Platinum Boarder

2008/09/25 09:18

Re:How to measure frequencies with the timer module?

<erased duplicate post>

Post edited by: LMI Dave, at: 2008/09/25 09:18

login or register to reply

tim17

Fresh Boarder

2008/09/25 10:49

Re:How to measure frequencies with the timer module?

Thank you for your answer!

It's a digital signal, 50kHz to 200kHz.
Minimum accuracy is +/-0.3kHz at 50kHz, +/-1kHz at 200kHz.
Core clock is 50MHz, all QEI ports are already in use.
I'm going to try Option A.

login or register to reply

tim17

Fresh Boarder

2008/09/25 10:49

Re:How to measure frequencies with the timer module?

Thank you for your answer!

It's a digital signal, 50kHz to 200kHz.
Minimum accuracy is +/-0.3kHz at 50kHz, +/-1kHz at 200kHz.
Core clock is 50MHz, all QEI ports are already in use.
I'm going to try Option A.

login or register to reply

cb1

Platinum Boarder

2008/09/25 16:28

Re:How to measure frequencies with the timer module?

I too like LMI-Dave's Digital "A"

Have these suggestions/slight modifications which may simplify your code:

a) driving a CCP pin your maximum input frequency can extend to Sys_Clk/4=12.5MHz. So you have an excellent safety factor.

b) I would prefer for you "not" to have this 16 bit accumulating counter generate an interrupt. Instead - I'd like you to simply enable/disable this counter via the 2nd timer. (in this manner you are filling the counter/timer - like a bucket)

c) Since your max frequency is 200KHz - and the counter has a capacity of 16 bits (65K) - you must limit your enable (or gate) time to 65/200 second. (this to prevent the accumulating counter from over-flowing)

d) We can simplify the math by choosing an enable/gate time of 1/4 second. (1/3 second "just misses" yielding 66,666 counts) Using 1/4 second you will accumulate 50K counts - which is 77% of the capacity of the counter. All things being equal - the more you "fill" the counter the higher your accuracy.

The beauty of a 1/4 second gate time is that you have an easy "shift left twice" to produce your frequency result. Fast and "no" dreaded floating point.

e) I'm display-centric - 1/4 second shows up nicely on "fast" Lcd fluid (or on a TFT). If you note last digit "bobble" you may want to average 4 computations and present that to the display.

Hope that you found this of some value...

login or register to reply
<< Start < Prev 1 2 Next > End >>