How to make a digital watch with 16X2 LCD using ATmega 8

post footer border
In this project, we learn how to make a digital watch using AVR microcontroller ATmega 8. Here, for time display we are using a 16*2 LCD. 16X2 LCD are widely used for basis text display in microcontroller project. These LCDs have an inbuilt HD44780U ( or some variant), which makes them very easy to control.




In this project, LCD is connected to the PORT C of ATmega 8 .

Pin No
 Function
 Name
1
Ground (0V)
Ground
2
Supply voltage; 5V (4.7V – 5.3V)
 Vcc
3
Contrast adjustment; through a variable resistor
 VEE
4
Selects command register when low; and data register when high
Register Select
5
Low to write to the register; High to read from the register
Read/write
6
Sends data to data pins when a high to low pulse is given
Enable
7
8-bit data pins
DB0
8
DB1
9
DB2
10
DB3
11
DB4
12
DB5
13
DB6
14
DB7
15
Backlight VCC (5V)
Led+
16
Backlight Ground (0V)
Led-



Circuit diagram of the connection of ATmega 8 to LCD


R in the above figure is a varying resistance ( potentiometer ).
 To set the brightness of the LCD we need to set the value of R.


CODE:

here we are using AVR Studio 4.
Our first step is to define all preprocessor directives.

here #include is the preprocessor directive. It's a program that processes our source program before it is passed to the compiler. 
In the above, #include statement is written in two ways. the meaning of each of the statement is 
#include   This command would look for the avr/io.h in the specified list of directories only. 
#include"lcd.c"  This command would look for the lcd.c in the current directory as well as the specified list of directories as mentioned in the include search path that might have been set up.
#define F_CPU1000000UL  This command is used to define the clock speed of the microcontroller.



you have to paste lcd.c and lcd.h files in the current directory ( In the folder were you saved the program file ). To download the lcd.c and lcd.h files , click here




declare the type of the variable



 

TCCR0=0B00000011; is to set the prescalar to 64. TIMSK=0B00000001; is for enabling the overflow interrupt , TCNT0 is for initialization of Timer Count. 
sei( ); is to enable the global interrupt



ISR (TIMER0_OVF_vect ) is the interrupt service routine.
gotoxy(0,0); is to set the initial position of the string on LCD.
lcd_puts( " ");  is to print the string on LCD. Here string " hr:min:sec " will print on the 1st row of the LCD from left to right.

  

When count reaches 1 than screen will display 00:00:1 and so on for sec++. 
Here sprintf ( ); is used to print the values of hr,min,sec on the screen at the second row.












The above statements show that when seconds get

60 than minute becomes +1 and sec becomes 0.





for each min==60 , hr becomes +1 and than min becomes 0.




If hour get equal to 24 (i.e hr==24 ) than screen gets clear and the loop will start from the beginning or else loop will continue

You can download the full Project file ( including c and .hex file ) from below


Link 1

Link 2

Recent Posts

AD Setting