i'm using msp430f2013 micro controller in my project.. in that i need to calculate the incoming train of pulse signal frequency.... i don't know how to do it.... can an开发者_运维技巧yone help me in this.. example code is more usefull to me.... advance thanks for
You need to read the manual for the micro-controller, then work out how to set up a timer which can measure the interval between two pulse edges (e.g. from one leading edge to the next). The frequency, f, will be the reciprocal of this time interval, t, i.e.
f = 1 / t
There are various ways to do this, perhaps the simplest to understand is to setup a timer as a simple counter. Poll the input pin, when it changes state save the count on the timer, when it changes state again save the count on the timer, subtract one time from the other and that is how many clock ticks of some frequency X ticks per second. your difference is y ticks per input pulse. y / x the ticks cancel out and you get seconds per pulse. If you are measuring a full period rising edge to rising edge or falling edge to falling edge then it is the same solution the difference is which timer samples to subtract (last rising edge and current rising edge for example).
Some microcontrollers have the ability to interrupt when there is a state change on the input pin (or at least the same edge, rising or falling), and you may prefer to use that method to sample the timer, subtract and get ticks per period, etc to get cycles per second (frequency).
Using a timer can be tricky, I always start by using the timer to blink an led, first once per second to get in the ball park, then once every 5 or 10 or 30 seconds, and compare that to a second hand on a watch or some other reference to verify that you are accurate and not a dozen percent off this way or that. That establishes understanding of the timer and its divisor, from there you can start to work on using it to measure the input. to make sure I have the gpio programmed right (the led exercise covers some of that already) I sample the input pin and change the led state with the input pin state and can often then look at the led to see blinks or a dull glow to see that I am able to sample the gpio pin. then put it all together and sample the timer when the input changes state, first polling then if need be interrupts or other.
精彩评论