Thank you David.
How accurate does your phase measurement need to be? The power line is pretty noisy making an exact zero crossing measurement difficult but does it need to be better than a simple optocoupler can deliver?
You are right about the noise. Particularly since I am introducing more of it by switching up to 10A mid cycle with MOSFETs. -- see project link at top.
I don't need extreme accuracy. Lets, for the sake of the thread, estimate the required accuracy (I already did this).
A 50Hz 230VAC RMS line has an equation of V(t) = Asin(wt) , where
A=sqrt(2) * 230 = 325V and
w (angular frequency omega) = 2 * pi * frequency = 314.159 rad/s
Its slope equation (ie first derivative) will be V'(t) = Awcos(wt)
At very small angles, wt ~ 0 => cos(wt) ~ 1. So the slope is just V'(0) = Aw ~ 100kV/s (confirmed on the scope for those who prefer that). Note that 110V 60Hz is obviously slower => easier (20% higher w but only half the A).
If, during trailing edge dimming, for example, (mode2 see original project link at top) I want to turn on the MOSFET pair at the crossover, then I would like to do it, when the line voltage is less than, say, Vswitch, because this will help reduce EMI, inrush currents for capacitive loads etc.
So my timing for firing the MOSFET, needs to have the following accuracy (I am not going to worry about small, known, constant offsets here, like time for the MOSFET to fire or time for the MCU to process the interrupt):
Lets shoot for Vswitch =< 10V as a first goal, ie fire MOSFET when mains voltage is less than 10V, How close to the true zero cross do we need to be in time?
dt = 10 / 100000 = 0.1ms or 100us.
My current method with the AC optocoupler gets me a pulse width of ~ 800us (using Schmitt trigger inputs on the pic), but the key is that this is very stable. ie it is almost symmetrical and of nearly constant width and of very consistent shape (ie any slight asymmetry is nearly constant). The standard deviation of the width at 50% of peak is < 10us, as measured on scope over half an hour.
The first estimate of the true zero cross is at 50% of the width after the leading edge. This gets me to within ~100us of the zero cross. It turns out, I am firing consistently late. However, I have already met my goal of 10V Vswitch - again confirmed on scope. It's not right on zero due to the slight asymmetry in the pulse and the Schmitt trigger levels. If I use the knowledge I have of the asymmetry, and pick a time ~40% through the (pic detected!) pulse, I can get within ~ 10us (the random error in my signal).
There are any number of circuits / devices which could give a narrower pulse. The issue is that a narrower pulse, by definition, is detecting a smaller mains voltage, and that smaller mains voltage will be more subject to noise (see below). So actually using a device that triggers repeatably and symmetrically at a higher voltage, and then halving its pulse width, is not a bad approach.
For my application, there doesn't seem to be much point aiming for more accuracy than I have (unless I get it "for free", see below). If I can get within 5V of zero cross (ie about 50us) that would be more than enough, because any EMI will be completely swamped by what happens when I turn the MOSFET off again when the mains are at 325V and 10A is flowing! Plus for "mode3" (see original project link at top), which is switching the sine wave at 10kHz+, it almost doesn't matter at all.
For a really accurate zero crossing measurement, the noise needs to be removed however sharp filters will create phase errors themselves.
Spot on, yes, filtering is a double edged sword. Similar to step-down transformers. They introduce new delays / phase shifts.
High performance designs phase lock a clean oscillator to the power line allowing the loop filter to remove the noise without creating phase error but I think that is way overkill for your application.
Hadn't seen that, so thank you, but yes, agree that sounds like overkill.
So why this thread? This thread was not about designing a hyper accurate zero cross detector. This post was to clarify whether I can simplify my design, and get more accuracy, lower power consumption and software convenience for free, by using already existing, specialised circuitry in the form of the pic18 built in ZCD peripheral.
We established the answer in the first reply: No, not safely. Which is what I thought. I just wasn't clear why Microchip bothered at all, and the answer is: If you can let your MCU float then it's OK. I can't, so that's not an option for me.
Case closed?