1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

how to determine the difference between 60 / 50 hz in C

Discussion in 'Embedded' started by Las, Dec 21, 2003.

  1. Las

    Las Guest

    Can anyone explain how this is done? I have the ability to detect zero
    crossing of the sine wave. Thanks...
    Las, Dec 21, 2003
    1. Advertisements

  2. Las

    Jim Stewart Guest

    Is this a troll? You will have twice as many
    zero crossings as the frequency. If you have
    a timer tick or some sort of clock, just count
    the zero crossings over some time constant.
    Jim Stewart, Dec 21, 2003
    1. Advertisements

  3. Las

    Casey Guest

    Las said...
    Uh... count the zero crossings in a second and divide by two.
    Should get around 50 or 60, right?

    Casey, Dec 21, 2003
  4. Las

    mackys Guest

    Kinda need to know what platform you're working on, or more
    usefully, what kind of function calls for timing things are in
    the C libraries you're working with.

    If you have POSIX around, just do "alarm(1)" and then go into
    a while loop that counts zero crossings. The trigger to get
    out of the while loop is a module variable that is set/cleared/however
    you want to do it by the function that catches SIG_ALRM - which is
    what will get called when the one second alarm() expires.

    Then count the zero crossings. If it's closer to 120, you
    have 60Hz. If it's closer to 100, you have 50Hz.

    If you don't have alarm(), you can do a similiar thing just
    by doing a while() loop with an internal call to a gettime()
    or similiar function. Basically, you make note of the current time
    just before entering the loop, then keep looking at the time
    and counting zero crossings until it's a second later - however
    "second" is defined for you particular version of gettime().

    Vague answer, I know, but you asked a vague question, so...

    mackys, Dec 21, 2003
  5. Las

    Las Guest

    Kinda need to know what platform you're working on, or more
    Sorry....I'm kinda new at this....The C library doesn't have a function for
    timing that I can tell...What I do know is that the chip is a Z8...that's
    right, not the newest version of zilog....I have a 12 MHz crystal; a
    prescaler from 1 - 64 and a timer that can be set from 1 to 256 that calls
    an interrupt when the value in the interrupt is reached. I have an interrupt
    fired at both zero crossings.
    Las, Dec 23, 2003
  6. The difference is 10 Hz.
    Michael R. Kesti, Dec 23, 2003
  7. Las

    Paul Burke Guest

    Well I'm making a few assumptions about the way the timer works here-
    never used the Z8. But if you set the timer to alternate values of 187
    and 188 (less a bit for the btime it takes to get into the interrupt?)
    you will get (on average) a 1ms interrupt rate. Zero a counter on the
    first input edge, increment it in the 1ms interrupt, and latch the value
    on the next input edge. You should get a value of 25 (or so) which can
    be distinguished from a value of 30 (or so). The difference will be
    bigger if you only use one direction of crossing, so a bit more reliable.

    Or forget the pfaff of setting alternate values, choose one of them and
    have a slightly off-millisecond interrupt rate, you will still get
    recognisably different values but not so obviously related to 50/60Hz.

    Paul Burke
    Paul Burke, Dec 23, 2003
  8. Las

    Bradley Guest

    Michael wrote
    Thats a funny one!
    Bradley, Dec 23, 2003
  9. Las

    Bradley Guest

    Hi, can't help you in C but on the Z8 one way I would do it would be
    have the timers set up to run a milisecond interrupt. Put the AC
    input signal into a port 3 pin that can trigger an interrupt. Count
    the milliseconds between interrupts. 50hz= 1/50= 20mS, 20/2= 10mS
    between zero crossings. 60hz= 1/60= 16.6mS, 16.6/2= 8.3mS between
    zero crossings.

    Another way might be to set up your timers to run a milisecond
    interrupt then scale that to 1 second resolution in software. Set the
    ac signal to come into a port 3 interrupt and count the number of zero
    crossings in 1 second. This is similar to what Ben
    () said.

    Hope this helps. If you want more information you might try
    alt.microcontrollers.8bit or http://groups.yahoo.com/group/z8encore
    which is a Z8 Encore group.

    Bradley, Dec 23, 2003
  10. Las

    Las Guest

    My problem was more elementary than I had first figured. The system crystal
    runs at 12MHz divided down to 1.5MHz. From there the prescaler which is set
    to 63 (a down counter) gives me a clock value of 42us. With this and a timer
    set to 255, the timer will be deincremented each 42us to give me a value of
    198 for 60Hz and 238 for 50Hz. The area I was having difficulty
    understanding was the value of the clock. 12MHz = .0000000833333333 per
    cycle; and this being divided down to the counter value of 42us. After the
    light bulb went on, everything else was easier.....thanks everyone for your
    Las, Dec 29, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.