ZCD with PIC 18LF45K40
I want to implement phase angle control for a single phase ac fan. To achieve that we need a ZCD circuit interfaced with a microcontroller to get a pulse at zero crossing. As our circuit has some space constraints, we have implemented the same using PIC 18LF45k40 which has built-in ZCD circuitry which generates pulses at Zero Crossing. Below is the circuit for implementing zcd- PIC ZCD implementation circuit
The above circuit is referenced from Technical Brief document - Link here
As you can see, the circuit has a common ground with the AC Neutral which makes the circuit bit questionable as the whole circuit has to be treated as live and raises safety concerns. (Please correct me if I'm wrong)
Putting the above concern apart, the problem I'm facing is the pulses generated from microcontroller is not stable. It swings in the x-axis(time) in the oscilloscope. The code for generating pulse is as follows -
void __interrupt() ZCD(void)
if(ZCDIE && ZCDIF)
LATA1 = 1;
ZCDIF = 0;
TMR0IF = 0;
LATA1 = 0;
LATA1 = 1;
The ZCD Input when the ground is not connected with neutral is a plain DC line. When the ground is connected with Neutral, sine wave superimposes on the DC line as shown below-
The ZCD in pin gets the following waveform when neutral and dc ground are common -
Following are my questions -
1 - Is the ZCD waveform expected this way? There is a very limited resource available on this issue.
2 - The below is the circuit which I've implemented for Zero Crossing. Is this circuit safe and practical?
Triac circuit -
3 - Please go through the code and let me know the issue.
4 - Please suggest any circuit or app note which implements the zcd.