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
|