This article assumes that the user is aware of real time clock and the basic interfacing of DS12C887 with 8051 microcontroller (AT89C51) including the pin description, memory and registers of RTC DS 12C887.
RTC 12C887 has three interrupts, namely, Alarm interrupt, Periodic interrupt & Update ended interrupt. For detailed information, check RTC interrupts. All the three interrupts works independent of each other.
Whenever an interrupt of DS12C887 occurs the following events take place:
1. The bit 7 and the corresponding interrupt flag of the Register C is set to 1.
2. The IRQ pin goes low only if the enable bit of the corresponding interrupt is set in Register B.
Steps to handle interrupt:
1. Connect the IRQ pin of the RTC to external hardware interrupt input pin of the microcontroller.
2. Initialize the external hardware interrupts in the microcontroller to receive interrupt from the RTC.
3. Initialize the interrupt(s) of the RTC to be used by configuring the corresponding bits of the Register B.
4. Whenever an interrupt occurs read the value of Register C to check which interrupt has occurred.
Register B and C are used to handle interrupts. Register B is used to enable the interrupts. By setting the bits 4-6 in this register the corresponding interrupt is enabled.
Register C is a read only register which tells as to which interrupt has occurred by setting the corresponding flag to high whenever an interrupt has occurred. Check DS12C887 for more details on Register C. Whenever an interrupt occurs, the bit 7 of this register gets set along with the corresponding flag bit of the interrupt which has occurred. The values of flag bits get cleared either by giving a low to the RESET pin or simply reading the value of Register C.
The Register C is updated every time an interrupt occurs irrespective of whether we are using the interrupt or not. In case this Register C is not cleared after the first interrupt and the second interrupt has occurred than in that case the value of the flag corresponding to the first interrupt is retained in the Register C.
The pin 19 (interrupt request output, IRQ pin) of the RTC is used to indicate the occurrence of an interrupt. This is an output pin of the RTC and it goes low only when the following conditions are matched simultaneously:
1. Any of the three interrupt occurs.
2. The enable bit of the corresponding interrupt is set in Register B.
This pin can be used to provide external hardware interrupt to the microcontroller. This pin is multiplexed to all the three interrupts i.e., it goes low whenever any of the interrupt occurs and hence indicates that an interrupt has occurred. However to identify which interrupt has occurred it is required to read the value of the Register C. The IRQ pin will remain low, until the flag bit of the interrupt remains high in the Register C and the corresponding enable pin in Register B is set. Once an interrupt has occurred it is required to clear the IRQ pin so that the RTC is ready to serve the next interrupt. The IRQ pin can be cleared by either of the two ways:
1. A low signal on reset pin will clear the IRQ pin.
2. By reading the Register C, the IRQ pin will be cleared by the processor.
In this code we have used the second method to clear the IRQ pin. Reading the register C will also tell as to which interrupt has occurred.
The connection of the RTC DS12C887 with the microcontroller is shown in the circuit diagram. This article uses update interrupt to keep the track of the time. Every time the update interrupt comes, the clock is incremented by one second. The output is displayed on the LCD. The clock presented in this article also has the provision of setting the time. The clock uses external interrupt 2 of the microcontroller AT89C51 for setting the time. A user can set time by pressing the switch connected to pin 13 of the microcontroller, which is interrupt 2. The hour and minutes can be set using pin 5 and pin 6 of the controller AT89C51 respectively. Once the time is set, the user needs to press the start pin (pin 8 of controller) to start the clock. The code for interfacing RTC DS12C887 is written in C.