Using Zero Cross Detect (ZCD) Peripheral for Triac Firing
Background
Triacs are commonly used to chop Alternate Current (AC) waveforms to control the amount of average power to the intended load. The firing angle delay is the amount of time between the zero-cross of the AC signal and the time that the triac is turned on (T1 and T2 in the figure below). To decrease the amount of power that is transferred to the load, the firing angle delay is increased. The microcontroller will turn on the triac and does not need to turn the triac off. The triac will turn itself off when the conduction current falls below the holding current level of the triac.
Problem
When switching AC voltages, there could be an excess of 240V RMS that needs to be switched. The microcontroller is a much lower voltage device and needs to be isolated from this high voltage. Many engineers use external interface circuits to provide this isolation, such as opto-isolators, transistor circuits, etc. These external circuits will also introduce delays in zero-cross detection.
Is there a way to use the Zero Cross Detector (ZCD) peripheral for ZCD and to control the delay?
Solution
The ZCD can quickly detect the zero cross point of the AC signal and immediately notify the firmware that the zero cross occurred. Interrupts can be set up to detect the rising and falling zero cross edges that allow detection of the positive AC half-cycle, negative half-cycle, or both. The ZCD works by providing a current to maintain a constant voltage on the pin. Therefore, it both sources and sinks a very small amount of current during the AC cycle. At any time during code execution, the firmware can detect if the ZCD is sinking or sourcing current and can determine if the AC is in the positive or negative half cycle.
Plus, with the ZCD, only a resistor is needed to provide this isolation. This reduces the number of external components required and also lowers the cost of the circuit and simplifies the design.
When the zero cross occurs, an interrupt will occur if the ZCD interrupt is enabled. The zero cross interrupt flag needs to be cleared. A timer could then be used to create a delay. When this timer expires, fire the triac.
Sample Code
The ZCD setup is simple. Just set the positive or negative (or both) edge detect and enable the peripheral. If the firmware needs to know the exact time of the zero cross, enable the ZCD interrupt. An interrupt will occur at the zero cross point when the signal is rising or falling (or both). If desired, the interrupt flag could be polled without actually enabling the hard interrupt.
ZCD1EN = 1; //Turn on the ZCD peripheral
ZCD1INTN = 1; //Turn on the negative edge polarity detect
ZCD1INTP = 1; //Turn on the positive edge polarity detect
ZCDIF = 0; //Clear the interrupt flag
ZCDIE = 1; //Turn on the ZCD interrupt
PEIE = 1; //Enable peripheral interrupt
GIE = 1; //Enable global interrupt
void interrupt interrupt_handler(void)
{
if(ZCDIE && ZCDIF)
{
ZCDIF = 0;
//Timer delay routine is added here to create the phase angle firing delay.
//When this timer expires, fire the triac.
}