check schematic of ac motor speed controller input on adc pin for zero crossing.... no code because i never worked on 8051. if you code for pic or st microcontroller i can help you. https://obrazki.elektroda.pl/1681773100_1366401596.html
use moc3021 to isolate triac and 8051.. use second normal opto isolator to detect zero voltage condition...
moc is must you can not connect triac directly to micro...
in 8051 you can use ext interrupt from opto iso.. when it comes send a predefined pulse to moc and wait for next interrupt..