The circuit diagram shows the connection of RTC with the microcontroller. Port P2 is used as data port for LCD while port P0 of the microcontroller AT89C51 is used as data port for the RTC DS12C887. The pins P1^0 - P1^7 of the microcontroller are configured as reset, rs, rw, e, dig_hr1, dig_min1, start, setmode, am_pm pins respectively.
Clock principally works on two modes. In the 24 hour mode hours are counted from 0 to 23 and in the 12 hour mode hours are counted from 0 to 11. In the 12 hour mode the RTC returns the BCD value 1-12 for AM and 81-92 for PM. The RTC returns the BCD value 1 for 1 AM.
The values corresponding to the output 1 to 12 i.e., 0 to 11 are stored in an array nummin. An array numhr is used to store the values from 81 to 92. When RTC sends the output, microcontroller reads hours and if it matches with any of the numhr variable it displays corresponding value from nummin array else it displays the same value for hours. This trick is used to convert the RTC from 24 hour mode to 12 hour mode.