Zero-crossing and phase detecting?

Diecimila (ATmega168); IDE 1.6.1

I want to do a project wherein I mark the zero-crossing of 2 sine wave inputs (on 2 analog pins). When each cross zero an interrupt is triggered.

The first signal's interrupt starts a timer. The second signal's interrupt stops the timer and reads the value. From the timer value I can calculate phase between the 2 signals. I can then generate a pwm output relative to the phase.

The challenge will be with timing. The interrupts will be happening every 40 microseconds. What is the Arduino's (the ATmega168's?) specification regarding how quickly it can process interrupts? What should I look for in the Atmel datasheet to know this?

This article got me thinking about this (it's a PDF):

Zero-Cross Detector

I'd like any thoughts you have about pitfalls, ideas, pitfalls (c; ...

Never dealt with Arduino's timers or interrupts.

Thanks.

PS, I note that some posts' subjects have "Solved" or such added (likely in other forums, like the Programming Questions forum). Does the OP (me) do this when I'm successful or is this done by the moderator?

I vaguely remember an overhead of about 50µs before an interrupt handler code is entered. A similar delay for the return from interrupt. The entry code saves the return address and some registers, the exit code will restore them. I doubt that this can be accomplished much faster in assembly language :-(

I'd suggest to add an external counter, started by the first and stopped by the second crossing. Depending on your needs you may have to add a higher frequency clock source, instead of the 16MHz provided by the Arduino board,

Sparkyy: This article got me thinking about this (it's a PDF): Zero-Cross Detector I'd like any thoughts you have about pitfalls, ideas, pitfalls (c; ...

Death. Fire. Trip in an ambulance.

On the plus side, you will be getting a new computer.

PS, I note that some posts' subjects have "Solved" or such added (likely in other forums, like the Programming Questions forum). Does the OP (me) do this when I'm successful or is this done by the moderator?

You. Modify the first post. Click [u]More/u then [u]Modify[/u].

Hi,

PS, I note that some posts' subjects have "Solved" or such added (likely in other forums, like the Programming Questions forum). Does the OP (me) do this when I'm successful or is this done by the moderator?

Unfortunately its not done enough, but its up to the op to do it as it depends if he/she is happy with results.

Tom...... :)

Hi, try here for safer zero crossing detection.

http://playground.arduino.cc/main/ACPhaseControl://

Tom..... :)

Why are you using the Arduino to do this? It sounds like you need a phase-frequency detector (do a google search for the details). A PFD will put out two PWM signals (one for positive phase difference, the other for negative phase difference). PFDs are used in phase-locked loops to detect phase difference.

[quote author=Coding Badly date=1428217009 link=msg=2172803] Death. Fire. Trip in an ambulance.[/quote] No worries. It's what I do for a living. I eat 3-phase 220v for breakfast. :D

TomGeorge: ACPhaseControl

That uses mains frequency--50/60 cycle (16 milliseconds period). I am measuring 30,000 cycle frequency (40 microseconds period). It looks like Arduino (or any microcontroller) can't do it this fast. I'll have to use a hardware solution.

TomGeorge: Everything runs on smoke, let the smoke out, it stops running.

Smoke fears me. :D

No worries. It's what I do for a living. I eat 3-phase 220v for breakfast. :D

Smoke fears me. :D

Very scary

charliesixpack:
Why are you using the Arduino to do this?

Ignorance. :wink:

It sounds like you need a phase-frequency detector (do a google search for the details). A PFD will put out two PWM signals (one for positive phase difference, the other for negative phase difference). PFDs are used in phase-locked loops to detect phase difference.

I studied PLLs and saw that PFD (didn’t know its name) might work but also saw that PLL locks a generated frequency (VCO) to one input frequency. Not what I need. If only a PLL could take 2 input signals and output an error signal…

I’ll look closer to see if stand-alone PFD is available or as part of a PLL can access inputs and outputs on PLL’s pins.

Quick 'net read shows that PFD may have promise. Thanks.

A rising edge from Signal A could trigger a flip-flop, or a one-shot, and a rising edge from Signal B could clear it - that would give you your error signal. Now you need something to accurately read that time.

Sparkyy: I am measuring 30,000 cycle frequency (40 microseconds period). It looks like Arduino (or any microcontroller) can't do it this fast.

Or can... https://www.pjrc.com/teensy/td_libs_FreqMeasure.html#compare

In the best case, with a 16 MHz board, you will get...

40/(1000000/16000000) = 640 steps ...a granularity of about 0.16%.

You need a phase-frequency detector. It does exactly what you need. It takes two inputs and compares the rising edges (or falling edges depending on the flip flops used). I am repeating myself but the output is a PWM signal proportional to the phase difference between the compared edges.

Hi, What is the application that you need to detect zero crossing at 30kHz?

Tom..... :)

Sounds to me like you can use this part to do what you need. You can do a google search for the data sheet. Since you don't have square waves for inputs you could use comparators to detect your zero crossings before feeding your signals into the PFD.

Be aware that your periodic input signals have many zero crossings. No matter what method you use to detect the phase difference, the crossings that are measured may not be the crossings you intended. This may or may not be a problem depending on your application. When a PFD is used in a PLL application it is not a problem since whatever edges are first detected (depending on the state of the PFD flip flops) those edges are aligned by the PLL feedback loop.