Mô phỏng mạch trên Proteus

RB0: Chân nhận tín hiệu ngắt ngoài từ mạch phát hiện điểm “không”.
RD0: Chân thực thi công việc khi xảy ra ngắt từ chân RB0.
Tín hiệu điện áp sau khi qua diode cầu BR1:

Điện áp xoay chiều đầu vào và tín hiệu RB0 khi có điểm không:

Tín hiệu tại chân RD0 khi được thực thi, mỗi khi phát hiện điểm không thì tạo một xung ở mức cao 1 ms, chân RB0 được cài đặt ngắt theo sườn xuống:

Code cho PIC16F877A
--------------------------------------------------------------------------------------------------------- Here is the code for PIC16F877A: (You can download the source file from https://rapidshare.com/files/604474700/ZeroCrossing.c ) --------------------------------------------------------------------------------------------------------- //Programmer: Syed Tahmid Mahbub //Compiler: mikroC PRO for PIC v4.60 //Target PIC: PIC16F877A //Program for zero-cross detection //--------------------------------------------------------------------------------------------------------- unsigned char FlagReg; sbit ZC at FlagReg.B0; void interrupt(){ if (INTCON.INTF){ //INTF flag raised, so external interrupt occured ZC = 1; INTCON.INTF = 0; } } void main() { PORTB = 0; TRISB = 0x01; //RB0 input for interrupt PORTD = 0; TRISD = 0; //PORTD all output OPTION_REG.INTEDG = 0; //interrupt on falling edge INTCON.INTF = 0; //clear interrupt flag INTCON.INTE = 1; //enable external interrupt INTCON.GIE = 1; //enable global interrupt while (1){ if (ZC){ //zero crossing occurred PORTD.B0 = 1; //Send a 1ms pulse delay_ms(1); PORTD.B0 = 0; ZC = 0; } } } ---------------------------------------------------------------------------------------------------------
Chi tiết tham khảo tại: http://tahmidmc.blogspot.com/2012/10/zero-crossing-detection-with-pic16f877a.html