You are on page 1of 13

Zero Cross Detection

V1.2 - Nov 01, 2006 English Version

19, Innovation First Road Science Park Hsin-Chu Taiwan 300 R.O.C. Tel: 886-3-578-6005 Fax: 886-3-578-4418 E-mail: mcu@sunplus.com

http://www.sunplusmcu.com

http://mcu.sunplus.com

Zero Cross Detection

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLOGY CO. is believed to be accurate and reliable. However, SUNPLUS No TECHNOLOGY CO. makes no warranty for any errors which may appear in this document. Contact SUNPLUS TECHNOLOGY CO. to obtain the latest version of device specifications before placing your order. responsibility is assumed by SUNPLUS TECHNOLOGY CO. for any infringement of patent or other rights of third parties which may result from its use. In addition, SUNPLUS products are not authorized for use as critical components in life support systems or aviation systems, where a malfunction or failure of the product may reasonably be expected to result in significant injury to the user, without the express written approval of Sunplus.

Sunplus Technology Co., Ltd.

PAGE 1

V1.2 - Nov 01 2006

Zero Cross Detection

Revision History
Translated By Li Jing Li Jing Proofreading Translate Zero Cross Detection V1.1, Chinese version Page Number(s)

Revision V1.2 V1.1

Date 2006/11/01 2006/03/07

Remark

Sunplus Technology Co., Ltd.

PAGE 2

V1.2 - Nov 01 2006

Zero Cross Detection

Table of Content
PAGE

1 System Design.................................................................................................................................... 4 1.1 System Design Description ........................................................................................................... 4 1.2 System Block Diagram .................................................................................................................. 4 1.3 MCU Resource .............................................................................................................................. 4 2 Hardware Design ................................................................................................................................ 6 2.1 Function......................................................................................................................................... 6 2.2 Design Principle............................................................................................................................. 6 2.3 Time Error Between Interrupt Generation Point and Actual Zero Point of AC220V ...................... 8 3 Software Description.......................................................................................................................... 9 3.1 Source File .................................................................................................................................... 9 3.2 Subroutines ................................................................................................................................... 9 3.3 Variables ...................................................................................................................................... 10 4 Flowcharts..........................................................................................................................................11 4.1 Main Flowchart .............................................................................................................................11 4.2 Interrupt Subroutine......................................................................................................................11 4.3 Display Subroutine ...................................................................................................................... 12

Sunplus Technology Co., Ltd.

PAGE 3

V1.2 - Nov 01 2006

Zero Cross Detection

1 System Design
1.1 System Design Description
This document is intended to guide you how to detect the zero crossing point for AC220V on SUNPLUS SPMC65x series. Here we use SPMC65P2404A*28P as an example for demonstration purpose.

1.2 System Block Diagram


AC220v

IRQ0

Transform

Commute

SPMC65P2404A *28P

6 LEDs showing

16MHz

Figure 1-1 System Block Diagram

1.3 MCU Resource


MCU Resource RAM Occupation Zero crossing display Main program ROM Initialization Interrupt process Zero crossing display Hardware on zero crossing interrupt Interrupt I/O PORT INT0 external interrupt triggered by rising edge in PB4 PORTA Not used Only PA0~PA5 1 byte 15 bytes 38 bytes 31 bytes 31 bytes Memo

Sunplus Technology Co., Ltd.

PAGE 4

V1.2 - Nov 01 2006

Zero Cross Detection

MCU Resource PORTB PORTC PORTD

Occupation INT0 external interrupt Not used Not used

Memo Only PB4

Sunplus Technology Co., Ltd.

PAGE 5

V1.2 - Nov 01 2006

Zero Cross Detection

2 Hardware Design
2.1 Function
This application is taking SPMC65P2404A*28P as the main controller to detect the zero crossing point of AC220V through the external interrupt on SUNPLUS SPMC65x series, which mainly performs the following three functions. Triac trigger. Through detecting the zero crossing point, the turn-on time of triac can be controlled, thus to control the voltage. Relay protection. When using relay to control AC220V power on/off, if the relay is closed around the peak voltage of AC220V, a larger spark will occur thus to shorten the relay lifetime and generate the electromagnetic interference (EMI); if closed at the zero crossing point, these influences will be decreased. Timing. AC220V has a frequency of 50Hz and a period of 20ms. After performing the full-wave rectification, the interrupt occurs at each zero crossing point at a 10ms interval.

2.2 Design Principle


As shown in Figure 2-1, 220V AC (alternating current) is converted into 9V AC by transformer. After full-wave rectification, AC can be used to detect the zero crossing directly. and the filter capacitor c1 can connect with the other circuit (such as the circuit based on 7805 voltage regulator). In this power supply circuit diagram, D1 is used to isolate the parts of rectification and filtering so as to keep the waveform of ZDS and offer source for zero detection. The sample signal is obtained at the zero point, and then it is connected with two 10K resistors to divide the voltage. After that it connects a triac, which operates on Switching mode. When the base voltage Vbe0.7V, the triac is turned on and the low voltage is output to PB4; otherwise, the triac is turned off and the high voltage is output to PB4. The corresponding relationship between the sample signal at the zero point and the input signal of PB4 is shown in Figure 2-2. Since the rising edge in PB4 will trigger a 10ms external interrupt, when the interrupt occurs, AC220V is just at the zero point. In this application, six LEDs are adopted for display, which are refreshed when the zero crossing interrupt occurs. The LEDs refresh frequencies are shown as follows: LED0 50HZ blink:

10ms on, 10ms off, synchronize with the zero point of power supply LED1 25HZ blink:

20ms on, 20ms off, synchronize with the zero point of power supply
Sunplus Technology Co., Ltd. PAGE 6 V1.2 - Nov 01 2006

Zero Cross Detection

LED2

12.5HZ blink:

40ms on, 40ms off, synchronize with the zero point of power supply LED3 6.25HZ blink:

80ms on, 80ms off, synchronize with the zero point of power supply LED4 3.125HZ blink:

180ms on, 180ms off, synchronize with the zero point of power supply LED5 1.5625HZ blink:

320ms on, 320ms off, synchronize with the zero point of power supply
Zero
D1
AC220v--AC9v

AC220v

+5V

10k

10k
Zero

8050
10k
0.1u

PA0
PA1

1k

LED0

1k
1k
1k

LED1
LED2
LED3

PA2

PA3
PA4

1k

LED4

PA5

1k

LED5

Figure 2-1 Zero Crossing Detection Schematic

Sunplus Technology Co., Ltd.

PAGE 7

~
1k

~ +

C1

PB4

V1.2 - Nov 01 2006

Zero Cross Detection

Figure 2-2 Corresponding Relationship Between the Sample Signal at the Zero Point (upper) and the Input Signal in PB4 (lower)

2.3 Time Error Between Interrupt Generation Point and Actual Zero Point of AC220V

Figure 2-3 Actual Interrupt Generation Point

When the voltage decreases to 0.7V, as shown in Figure 2-3, the rising edge occurs on PB4 and the interrupt occurs at the same time. Between the interrupt generation point and the actual zero point of the AC220V, we can see a time error which is related to the output voltage of the transformer and the voltage dividing of the resistance connected to the triac. In this circuit, the ideal calculation for the time error between the interrupt generation point and the actual zero point is: 0.7=4.52 *sin(w * t) where 4.52 is obtained by dividing the peak voltage 92 with two 10K resistors. W=2*3.14* f (f 50Hz) After calculation, t=350us. Measured in practical via oscillograph, the time error between the interrupt generation point and the actual zero point is approximately 500us.
Sunplus Technology Co., Ltd. PAGE 8 V1.2 - Nov 01 2006

Zero Cross Detection

3 Software Description
3.1 Source File
File Name main.asm Zero_Detect.asm SPMC65P2404A.inc Function Main process Zero cross detection process SPMC65X symbol definition Type Source file (Sample) Source file (User) Head file

3.2 Subroutines
V_Reset
Content Input Output Description Caller None None Vector address pointed after MCU power on reset main.asm Memo

V_IRQ
Content Input Output Description Caller None None Zero crossing display which synchronizes with the zero point of power supply. main.asm Memo

F_Main
Content Input Output Description Caller None None Wait for the generation of zero crossing interrupt. Main.asm Memo

Sunplus Technology Co., Ltd.

PAGE 9

V1.2 - Nov 01 2006

Zero Cross Detection

F_Light
Content Input Output Description Caller None None Drive the LED which synchronizes with the zero point of power supply Zero_Detect.asm Memo

F_IO_Init
Content Input Output Description Caller None None Initialize PA5~PA0 as the low level output pins, PB4 as the input pin. Zero_Detect.asm Memo

F_IRQ0_Init
Content Input Output Description Caller None None Configure PB4 as external interrupt which is triggered on the rising edge. Zero_Detect.asm Memo

3.3 Variables
IB_10msCount
Content Description Size Initialization Symbol Content 10ms counter, which increases 1 every 10ms. 1byte Random None $0~$FF Memo

Sunplus Technology Co., Ltd.

PAGE 10

V1.2 - Nov 01 2006

Zero Cross Detection

4 Flowcharts
4.1 Main Flowchart

Start V_Reset

Initialization for interrupt (F_IRQ0_Init) and IO port (F_IO_Init)

Circular wait

Figure 4-1 Main Flowchart

4.2 Interrupt Subroutine


V_IRQ

Zero crossing displayF_Light

Return

Figure 4-2 Main Flowchart

Sunplus Technology Co., Ltd.

PAGE 11

V1.2 - Nov 01 2006

Zero Cross Detection

4.3 Display Subroutine


F_Light

IB_10msCount+ 1

Send bit5~bit0 of IB_10msCount to PA5~PA0 for display

Return

Figure 4-3 Zero Crossing Display

Sunplus Technology Co., Ltd.

PAGE 12

V1.2 - Nov 01 2006

You might also like