convert frequency from pulsin
Archiver
Posts: 46,084
I like using PULSIN to measure frequency because it's much faster
than using COUNT. However, how do you use Stamp math to get
3 digit frequency precision? I'm using a BS2P. I need to measure 20.0
Hz to 700 Hz. Frequency is 50.00% duty cycle.
Thanx,
-Barry
than using COUNT. However, how do you use Stamp math to get
3 digit frequency precision? I'm using a BS2P. I need to measure 20.0
Hz to 700 Hz. Frequency is 50.00% duty cycle.
Thanx,
-Barry
Comments
high-going period of 25 milliseconds. Divide this by 0.75 microseconds
(the units in BS2p PULSIN) and you get 33,333. If you do the same thing
with 700 Hz you get 952. I plugged those values into a spreadsheet
using 200 and 7000 (frequency in 0.1 Hz) as the X inputs and got a slope
of -0.21 and an intercept of 7200.
So, the math becomes: freq (in tenths) = period * -0.21 + 7200.
In PBASIC, you can't use negative values in multiplication so we
rearrange as: freq = 7200 - (period * 0.21). And here's the PBASIC
code:
Calc_Freq:
freq = 7200 - (period ** 13765)
RETURN
-- Jon Williams
-- Applications Engineer, Parallax
-- Dallas Office
Original Message
From: prius_lover2002 [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=nBtCkZZGl1TT-h2y-KHuG5uCp4JY1eZ1MSulQd8MKo1GMCi-8LAULnDu2LDWhtkfsyM5h08kjxdGG-ji8S6AoJA]prius_lover2002@y...[/url
Sent: Saturday, January 17, 2004 2:29 PM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] convert frequency from pulsin
I like using PULSIN to measure frequency because it's much faster than
using COUNT. However, how do you use Stamp math to get
3 digit frequency precision? I'm using a BS2P. I need to measure 20.0 Hz
to 700 Hz. Frequency is 50.00% duty cycle. Thanx, -Barry
That works if it's a straight line. But it's a 1/X curve. How do I code 1/X for
that frequency range?
Thanx,
-Barry
--- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...> wrote:
> You have to start with the basics ... a 20 Hz square wave will have a
> high-going period of 25 milliseconds. Divide this by 0.75 microseconds
> (the units in BS2p PULSIN) and you get 33,333. If you do the same thing
> with 700 Hz you get 952. I plugged those values into a spreadsheet
> using 200 and 7000 (frequency in 0.1 Hz) as the X inputs and got a slope
> of -0.21 and an intercept of 7200.
>
> So, the math becomes: freq (in tenths) = period * -0.21 + 7200.
>
> In PBASIC, you can't use negative values in multiplication so we
> rearrange as: freq = 7200 - (period * 0.21). And here's the PBASIC
> code:
>
> Calc_Freq:
> freq = 7200 - (period ** 13765)
> RETURN
>
> -- Jon Williams
> -- Applications Engineer, Parallax
> -- Dallas Office
>
>
>
Original Message
> From: prius_lover2002 [noparse][[/noparse]mailto[noparse]:p[/noparse]rius_lover2002@y...]
> Sent: Saturday, January 17, 2004 2:29 PM
> To: basicstamps@yahoogroups.com
> Subject: [noparse][[/noparse]basicstamps] convert frequency from pulsin
>
>
> I like using PULSIN to measure frequency because it's much faster than
> using COUNT. However, how do you use Stamp math to get
> 3 digit frequency precision? I'm using a BS2P. I need to measure 20.0 Hz
> to 700 Hz. Frequency is 50.00% duty cycle. Thanx, -Barry
An answer is not coming to me at the moment, so I'll try again later.
-- Jon Williams
-- Parallax
Original Message
From: prius_lover2002 [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=fKUAByPBGPvA6xEnAzMU_Du1jAoM8bBKiXKPPw-b48ye6U726wXUhne_M6Fl5H2KWgNlEOebIcQMvpVEehvcJd-jPQ]prius_lover2002@y...[/url
Sent: Saturday, January 17, 2004 4:06 PM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] Re: convert frequency from pulsin
Jon,
That works if it's a straight line. But it's a 1/X curve. How do I code
1/X for that frequency range? Thanx, -Barry
--- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...>
wrote:
> You have to start with the basics ... a 20 Hz square wave will have a
> high-going period of 25 milliseconds. Divide this by 0.75
> microseconds (the units in BS2p PULSIN) and you get 33,333. If you do
> the same thing with 700 Hz you get 952. I plugged those values into a
> spreadsheet using 200 and 7000 (frequency in 0.1 Hz) as the X inputs
> and got a slope of -0.21 and an intercept of 7200.
>
> So, the math becomes: freq (in tenths) = period * -0.21 + 7200.
>
> In PBASIC, you can't use negative values in multiplication so we
> rearrange as: freq = 7200 - (period * 0.21). And here's the PBASIC
> code:
>
> Calc_Freq:
> freq = 7200 - (period ** 13765)
> RETURN
>
> -- Jon Williams
> -- Applications Engineer, Parallax
> -- Dallas Office
>
>
>
Original Message
> From: prius_lover2002 [noparse][[/noparse]mailto[noparse]:p[/noparse]rius_lover2002@y...]
> Sent: Saturday, January 17, 2004 2:29 PM
> To: basicstamps@yahoogroups.com
> Subject: [noparse][[/noparse]basicstamps] convert frequency from pulsin
>
>
> I like using PULSIN to measure frequency because it's much faster than
> using COUNT. However, how do you use Stamp math to get 3 digit
> frequency precision? I'm using a BS2P. I need to measure 20.0 Hz to
> 700 Hz. Frequency is 50.00% duty cycle. Thanx, -Barry
period VAR Word
freq VAR Word
Main:
period = 33333
period = period ** 493 ' x 0.0075 (for BS2p)
freq = 50000 / period
DEBUG DEC (freq / 10), ".", DEC1 freq
END
-- Jon Williams
-- Applications Engineer, Parallax
-- Dallas Office
>
Original Message
> From: prius_lover2002 [noparse][[/noparse]mailto[noparse]:p[/noparse]rius_lover2002@y...]
> Sent: Saturday, January 17, 2004 2:29 PM
> To: basicstamps@yahoogroups.com
> Subject: [noparse][[/noparse]basicstamps] convert frequency from pulsin
>
>
> I like using PULSIN to measure frequency because it's much faster than
> using COUNT. However, how do you use Stamp math to get 3 digit
> frequency precision? I'm using a BS2P. I need to measure 20.0 Hz to
> 700 Hz. Frequency is 50.00% duty cycle. Thanx, -Barry
> using COUNT. However, how do you use Stamp math to get 3 digit
> frequency precision? I'm using a BS2P. I need to measure 20.0 Hz to
> 700 Hz. Frequency is 50.00% duty cycle. Thanx, -Barry
Hi Barry,
I think the following will work for 21.0 hz to 700.0 hz:
result VAR word ' from PULSIN
frequency VAR word ' 21.0 to 700.0 hz
N VAR word ' for calculation
F VAR word ' ditto fractional part
J VAR nib ' loop index
PULSIN 0,0,result ' result is 952 to 31746
N=13333 ' 10000/0.75 microseconds
frequency=N/result ' integer part, 0 to 14
' now calculate fractional part...
'
binary division loop
FOR J=15 TO 0 ' 16 bits
N=N//result<<1 ' remainder*2
F.bit0(J)=N/result ' next bit
NEXT
frequency = ((frequency * 1000) + (1000 ** F)) / 2
' from 210 to 7000
DEBUG DEC frequency/10,".",DEC1 frequency,CR
With further thought it could go all the way down to 20 hz. The
limitation is that the denominator in the binary division loop has to
be less than 32768, and the PULSIN value on the BS2p for 1/2 cycle at
20hz would be 33333. The maximum period PULSIN measurement on the
BS2p is 49.125 milliseconds. The logic of this math is explained in
excruciating detail at this URL:
<http://www.emesystems.com/BS2math2.htm>
One more note. If the signal does not have exactly 50% duty cycle,
or if the rise and fall times are significantly different, measure
both the 010 and the 101 halves and average them.
-- Tracy