Digital Clock Using Timer with 8051/89c51
Here I am writing program of Digital Clock using timer for 8051/89c51 microcontroller. To understand complete code of Digital Clock using timer you must have to look at the connection diagram first which is shown in image below :-
In this connection diagram i have only connect the 16x2 LCD with 8051/89c51 microcontroller to display time. Here you can see that LCD is connected in 4-bit mode.
Now I’ll explain you the digital clock program. The main file of Digital Clock program is shown in image which is given below:-
As you can see in the main program firstly I have initialize the Timer 1 through which I have generated 50 msec interrupt. So for more explanation about Timer 1 interrupt generation you can see my earlier post “Timer 1interrupt in 8051”.Then after I have initialized the LCD and send the command 0x80 to display text from 1st position of first line on LCD. After that I have write the text “Format : HH:MM:SS” on LCD .For more explanation to display text on LCD you can see my earlier post “LCD interfacing with 8051 using 4 datalines”.
Now after entering in while loop I have display time in Hour, Minute and Second format. This Time’s Hours, minutes and seconds is managed in Timer 1 interrupt. Which you can see in interrupt function which is shown in image below:-
Here in this interrupt function firstly I have increment msec counter. when this msec counter gets equal to 20 then I have increment sec counter because it is a 50 msec interrupt and 50x20 =1000 msec (1 sec) also I have make msec counter 0. So when sec counter gets equal to 60 then I have increment min counter and when min counter gets equal to 60 than I have increment hour Counter. In this manner we can manage seconds, minutes and hours. This Time will start from 0 when Microcontroller gets start.
Here in this program one more thing which you have to see that I have used a function name “variable_lcd_diaplay” through which I have display variable on LCD is shown in image below:-
In this function firstly I have break a variable and the 1s digit and 10s digit is taken in different –different variable which are a and b. Then after both variable is converted into ASCII by adding 48. After that these ASCII values has been displayed on LCD by using “LCD_write_data” function.
After completion of program the LCD display will looks like as per the image given below:-
Download Link
To download complete code of Digital Clock Using Timer you can click here. If you are unable to download the complete code than you can leave your email id in comment section and i’ll send you the complete code.