PDA

View Full Version : frequency calculation

trazym
04-03-2009, 06:40 PM
Hi, i've bought a propeller education kit a week ago and i want to extract the period T of a discret signal.

_____---_____---_____
-------->
T?

Fred

MagIO2
04-03-2009, 07:04 PM
Well, that's a lot of information .... missing ;o)

There are lot's of ways to do that. SPIN? PASM? Using WAITPEQ, WAITPNE or using counters ...
How accurate does the result need to be? The most accurate would be to use the counters as hardware itself counts the pulse/pause time. Then you only need the code to read the values, calculate the overall T and reset the counters.
What frequency do you expect?

trazym
04-03-2009, 07:54 PM
"The most accurate would be to use the counters as hardware itself counts the pulse/pause time. Then you only need the code to read the values, calculate the overall T and reset the counters." -> That's exactly what i want to do but i need to transalte the idea into the program .SPIN !!

"What frequency do you expect?" ->about 200Hz

Thank you

TreeLab
04-03-2009, 09:21 PM
In my experience you can have a spin program monitoring a counter at up to 5 kHz without loosing an edge. If you just need to know how many pulses come by in a second, then a counter can be set up from SPIN, then have the cog wait for exactly one second, then wake up to read the counter. You need to decide what precision you need, which will set the time that you need to wait before reading the counter.

The B counter is set up to read edges on Pin using

dira[ Pin ]~
ctrb := %01010<<26+ Pin ' Counter B is looking at the Schmitt-conditioned opto signal (inverted)
frqb := 1
phsb := 0

Cheers!
Paul Rowntree

MagIO2
04-04-2009, 12:37 AM
So, one of the simple ways for one measurement is:

pinMask|=Pin ' create the pin bitmask by giving the pin number (0-31)
waitpne( pinMask, pinMask, 0) ' we are in the middle of somwhere, so we have to wait until we found the start of a clock-cycle
waitpeq( pinMask, pinMask, 0) ' here we wait for the rising edge
startPulse:=cnt ' store the counter value
waitpne( pinMask, pinMask, 0) ' here we wait for the falling edge
startPause:=cnt ' if you want the pulse-pause ratio, you can store the counter here as well
waitpeq( pinMask, pinMask, 0) ' here we wait for the next rising edge
t:=cnt-startPulse ' now we can calculate t, pulse-pause ratio, frequency ...
freq:=clkfreq/t

Please note: this is really a simple implementation. There are a lot of ways to improve it and adapt it to your needs.
E.G.:
- This one blocks if no clock signal is attached to the pin. To avoid that, you have to implement your own waitpne/waitpeq.
- It's only measuring one cycle. If you need continuous measurement then you have to put the stuff in a loop
- For roundabout 200Hz this should be adequate enough, if you need it more adequate, you have to use counters.
......