Atmel describes how to implement zero cross detection on an AVR. Zero cross detection is used to find the exact moment when a signal crosses from positive into negative, or vice-versa. This is useful in periodic AC signals as it gives you a reference point of when a new half period has started.
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
galvanic isolation for the microcontroller against the AC mains. The zero cross sense resistor can be a way for electronic noise to get into the system.