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

Can anyone explain how this is done? I have the ability to detect zero crossing of the sine wave. Thanks...

Reply to
Las
Loading thread data ...

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.

Reply to
Jim Stewart

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

Casey

Reply to
Casey

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...

-Ben

--
I am Ben Cantrick. I still think the AnimEigo BGC dubs suck.
To foil spammers, no e-mail adress will be given here. (Spammers also suck.)
If you want to e-mail me, remove any African animals in my e-mail address.
Advanced Supersonic Titanium Nazi Hell Creatures from beneath the hollow earth!
Reply to
mackys

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.

Reply to
Las

The difference is 10 Hz.

-- ======================================================================== Michael Kesti | "And like, one and one don't make | two, one and one make one." snipped-for-privacy@gv.net | - The Who, Bargain

Reply to
Michael R. Kesti

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

Reply to
Paul Burke

Michael wrote

Thats a funny one!

Reply to
Bradley

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 ( snipped-for-privacy@rhinokaosol.net) said.

Hope this helps. If you want more information you might try alt.microcontrollers.8bit or

formatting link
which is a Z8 Encore group.

Bradley

Reply to
Bradley

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 help......
Reply to
Las

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.