A frequently asked question. Here are 3 common ways: If the PIC is not isolated from the power line, just connect the AC line through a large resistor to one of the port pins. Generally 2 5 or 10 M resistors in series are used. Using 2 resistors in series helps keep high-voltage transisents from breaking down the resistor and damaging the PIC. Also, do not use the RA4 pin since it doesn't have a protection diode to Vdd. If that's the only pin left, add an external diode. If the PIC is powered via a small line-frequency transformer, use a resistor to the transformer secondary instead. The phase shift through the transformer is minimal. Finally, an optoisolator with the input driven by half or full-wave rectified AC line can be used. This is the most exepensive way since it needs an optocoupler, some diodes, and also a 2W resistor to limit the current through the LED. The resistor to port pin methods will make the pin read high when the AC line is more positive than about 2.5V, and low when it is negative. This isn't too noticeable if the input is 120 or 240V but could be a major error working from a transformer secondary. If you need an exact reading, likely a small capacitor could be added to delay the 1-0 transition to exactly the zero crossing. Synthesize the 0-1 crossing by delaying a nominal 1/2 cycle. Now that some information about the line phase is input to the PIC, it's still necessary to use it to trigger the triac at consistent phase times, while at the same time receiving IR messages which aren't syncronized to anything. There are a lot of ways to do this. One of the simpler ones that uses the '84s resources would be to use interrupts for the AC-related stuff. The power line 1-0 transistion would cause a INT interrupt. This interrupt writes a starting value (related to the desired brightness) to TMR0. The TMR0 interrupt triggers the triac and sets TMR0 to expire again 1/2 cycle later (since the sync is only on half of the zero crossings). These ISRs could be fast and infrequent enough that they don't too badly affect software timing done to decode the IR. Once the first INT interrupt has occurred, it may be a good idea to disable further INT interrupts until after the triac has triggered for the second time. This would prevent noise near the zero-crossing from causing repeated interrupts.