• • • • •
Interrupt Driven Modular C Source Code Size Efficient Code Accurate and Fast Detection A Minimum of External Components
8-bit RISC Microcontroller Application Note
One of the many issues with developing modern applications is to keep the spikes and EMI at a minimum, especially when switching AC mains in and out. Most of today’snew applications are controlled by one or more microcontrollers and this gives the possibility to prevent this noise in a simple and cost efficient way. Noise produced during switching is dependant on the amplitude of the AC sinus at the actual switching point. To get this noise as low as possible the ideal switching would be when the amplitude is 0 volt. The amplitude is crossing 0 volt at thesinus “zero crossing”. Switching mains in and out at the zero crossing requires a way of detecting when the next crossing will be and launching a switching action at the crossing. This raises the need for a cost efficient way to detect the zero crossing. This application note explains how to do that. Zero cross detection can also be used for other purposes, such as frequency calculation andrelative phase measuring.
Figure 1. Zero Cross Detector Using AVR®
Serial Input Resistor Mains 1M
GND PD2/EXT INT0
1M Mains GND GND
This application note shows the user how to implement a zero cross detector with a minimum of external components. It should be noted that this solution will not give any galvanicisolation for the microcontroller against the AC mains. The zero cross sense resistor can be a way for electronic noise to get into the system. This will not be described in this application note. Please see “AVR040: EMC Design Considerations” for further details about this. The application uses ATmega16, but the code can be recompiled for any AVR device.
To protect the device fromvoltages above VCC and below GND, the AVR has internal clamping diodes on the I/O pins (see Figure 1). The diodes are connected from the pins to VCC and GND and keep all input signals within the AVR’s operating voltage (see Figure 2). Any voltage higher than VCC + 0.5V will be forced down to VCC + 0.5V (0.5V is the voltage drop over the diode) and any voltage below GND - 0.5V will be forced up toGND - 0.5V. By adding a large resistor in series, these diodes can be used to convert a high voltage sinus signal down to a low voltage square wave signal, with amplitude within the AVR’s operating voltage ± 0.5V. The diodes will thus clamp the high voltage signal down to the AVR’s operating voltage. Note that the series resistor and the pin input capacitance form an RC filter that will introduce asmall phase difference between the square wave and the AC mains signal. The phase difference is insignificant in the current example, see “RC Filter and Delay Between VCC/2 and the Actual Zero Cross” on page 7 for more details. As the square wave signal is in phase with the AC mains, using the falling edge will tell very accurately where the zero crossing happens. By using this signal the AVR canbe programmed to be a very accurate zero cross detector with a very small and interruptdriven code. The square wave is the mains signal with its tops cut off and will have the same voltage from VCC - 0.5V to VCC + 0.5V as the mains signal (see Figure 2). When the square
wave triggers the AVR’s falling edge interrupt at around VCC/2, the mains amplitudewill also be at VCC/2 and just before a zero crossing. If this is done on a falling edge the AVR will get an interrupt just before the zero crossing and will have time to start a zero crossing action at the actual crossing point. The interrupt will be triggered at around VCC/2, as this is the middle of the AVR’s logical threshold voltage. The signal is connected to the External Interrupt 0-pin...