Thursday 22 October 2015

8051 based digital clock using LCD display


8051 based digital clock using LCD display 

  

This post provides the implementation of a digital clock display ( using c language ) for 8051 micro-controller ( e-g for AT89C51 or AT89C52 etc ). The LCD display is continuously updated after every second with the new value of current time in HH:MM:SS format.

The code and Proteus simulation is given in the 'Downloads' section at the bottom of this page.

The following figure shows the circuit of digital clock using 8051 in Proteus.

Figure 1.   Digital clock display using 8051 micro-controller
The above figure was taken at time, 1 minute and 14 seconds after the start of simulation in Proteus. The LCD update rate is 1 Hz i-e LCD is updated with new value of current time after every second.

The code for the main function is shown below.

Figure 2.   Main code for digital clock using 8051
Timer0 is used in this code to generate the base for the clock.

Timer0   increments the ms Counter  variable, after exactly 1ms. And when  ms Counter reaches 1000, then its value is reset to 0 again. At this instant, 1 second time is elapsed and Display Time To LCD () function is called, which displays the new updated time on the LCD.

There are some global variables which store current time. These variables are used  through  out the code. For example, ms Counter stores the current milli-second count. And sec Counter stores the current value of seconds, min Counter stores the current value of minutes, hr Counter stores the current value of hours etc.

The code for timer0 interrupt service routine function and also for the Update Time Counters() function is displayed below.

Figure 3.   Timer0 ISR and UpdateTimeCounter functions
In the timer0 ISR, usec Counter variable (which stores the current value of micro-seconds) is incremented after every 250usec (As timer0 is configured to give an interrupt after every 250usec). When usec Counter reaches 1000 value (i-e 1000usec = 1msec), then ms Counter variable is incremented.

In the Update Time Counters() function, whenever ms Counter reaches 1000 (i-e 1000msec = 1sec), then it increments the sec Counter. Similarly, when  sec Counter reaches a value of 60, then min Counter is incremented and so on. This "update time counter" function ensures each variable has correct value to display time.

The actual function which displays time on the LCD is shown below.

Figure 4.   DisplayTimeToLCD function code
DisplayTimeToLCD() function displays the current time value on the LCD in HH:MM:SS format. Firstly, LCD screen is cleared, then Hours value is displayed on the screen. Then ':' symbol is displayed on the LCD. After which Minutes are displayed, then ':', and in the end seconds are displayed. All of this happens so fast, that we instantly see new updated value on the LCD.

You can leave your comments in the comment section below. 

Downloads


The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation 
click here.

No comments:

Post a Comment