No, you don't want PWM, you want phase control .
I think you're confusing the two.
PWM with mains is normally done at a lower frequency than the mains frequency and is suited to heaters but not lamps which will flash because the frequency is so low.
The algorithm for phase control goes like this:
Wait until the mains voltage is near zero (typically below 15V or so), start a timer.
When the timer finishes, fire the TRIAC.
Go back to the start.
The duration of the timer controls the brightness. The maximum duration is half the period of the mains frequency, in other words, t = 1/(2f). The longer the duration of the delay,, the later the firing, the dimmer the bulb will be, the shorter the brighter the bulb will be, if there is no delay and the TRAIC is fired immediately, you get full power.
A comparator is normally used for zero crossing, the AC voltage from a transformer (which is also normally used to power the rest of the circuit) is compared to the 0V rail, when the comparator is high or low (depending on which way round the inputs are connected) AC signal is 0V.
You need a small mains transformer so you can monitor the mains voltage. An opto-coupler also might work, connect the LED to the mains, with a reverse voltage protection diode in parallel, via a suitable series resistor and connect the photo transistor to an MCU input using a suitable pull-down/up resistor. The LED will only be activated when the AC voltage is above about +1.5V, this will turn the photo transistor on, letting the MCU know that the AC voltage has just passed through 0V, when the voltage drops below about 1.5V, the LED and photo transistor will turn off, letting the MCU know again that the AC voltage is near 0V. You can use either the on, off signals, or both, to start your timing delay, you only need to use one because you know that the mains will be near 0V again after 1/(2f).
There's lots of more information on Google.