Shop OBEX P1 Docs P2 Docs Learn Events
Can I measure frequency and duty cycle on one pin ? — Parallax Forums

Can I measure frequency and duty cycle on one pin ?

IbsenIbsen Posts: 68
edited 2005-08-15 19:41 in General Discussion
Can I measure frequency and duty cycle on one pin at the same time ???

If so how would I do that ???

We are talking about low frequency here. In the 100's Hz range.



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


*.*

Ibsen

" It's nice to be important, but
·· more important to be nice... "

Comments

  • BeanBean Posts: 8,129
    edited 2005-08-15 18:19
    Sure...
    Wait for input to go low.
    Wait for input to go high.
    Keep track of time until input goes low again. (this is the "on" time)
    Keep track of time until input goes high again. (this is the "off" time)

    Duty Cycle = "On" time / ("On" time + "Off" time)
    Freq = 1 / ("On" time + "Off" time)

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    Product web site: www.sxvm.com

    "One experiment is worth a thousand theories"
    ·
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2005-08-15 19:41
    Correct,

    Freq = 1 / ("On" time + "Off" time)

    Assuming that this shall be done with an SX, and w/o knowing the required precision, and the approximation time, let me add some remarks:

    BUT (there is always a but) - how can you easily calculate the reciprocal (1/x) of some value on the SX ?

    An alternative to calculating the reciprocal, you could use a timer ISR to count the number of - say - low to high transistions on the _same_ input pin that is used to measure the duty cycle within a certain period of time. Assuming that this period would be one second, the counter result would directly be the frequency in Hertz. If one second approximation time is too long, you might use 0.5 seconds, and shift left the result by one bit (i.e. multiply it by 2), loosing precision though. Actually, this is how frequency counters normally work - they open a gate for a certain period of time, counting the number of positive or negative transitions passing the gate while it is open.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    G
Sign In or Register to comment.