This project gives you a real time clock with the RTC chip DS1307. This RTC chip has inbuilt oscillator for clock and it has its own registers for full calender. In this project we dont take care about the days and we just use the time functions.

There are 3 push buttons on the circuit to change the time and the time is displayed over the LCD display. You have to provide battery backup for the RTC chip, so that the time runs inside the chip during power failure.

The code is written in Bascom basic, you can also download the proteus simulation file.

Circuit Diagram

Bascom code

$regfile = "m8def.dat"
$crystal = 1000000
'------------------------  lcd -----------------------------------------------
Config Lcd = 16 * 2
Config Lcdpin = Pin , Rs = Pind.0 , E = Pind.1 , Db4 = Pind.2 , Db5 = Pind.3 , Db6 = Pind.4 , Db7 = Pind.5
Cls

'---------------------------  ds 1307 -----------------------------------------
$lib "ds1307clock.lib"
'configure the scl and sda pins
Config Sda = Portd.7
Config Scl = Portd.6
'address of ds1307
Const Ds1307w = &HD0                                        ' Addresses of Ds1307 clock
Const Ds1307r = &HD1
'------------------------------ key --------------------------------------------
Config Pinc.1 = Input
Config Pinc.2 = Input
Config Pinc.3 = Input
Config Pinc.4 = Input
'------------------------------------------------------------------------------
Config Debounce = 30

Dim A As Byte , B As Byte , Data1 As Byte , C As Byte

Dim Seco As Byte , Mine As Byte , Hour As Byte

'----------------------------------------------------------------------------
Cursor Off
'-----------------------------
Cls
Locate 1 , 6
Lcd "Hello"
Locate 2 , 1
Lcd "avrprojects.info"
Wait 2

For A = 1 To 15
 
Shiftlcd Left
Waitms 300
Next

Cls
'-------------------------------------------------------------------------------
Main
:
Do
        
Gosub Ds1307
        
Gosub 24_12
        
Gosub Chekkey

Loop
'-------------------------------------------------------------------------------
Ds1307
:
       
I2cstart                                            ' Generate start code
       
I2cwbyte Ds1307w                                    ' send address
       
I2cwbyte 0                                          ' start address in 1307
       
I2cstart                                            ' Generate start code
       
I2cwbyte Ds1307r                                    ' send address
       
I2crbyte Seco , Ack                                 'sec
       
I2crbyte Mine , Ack                                 ' MINUTES
       
I2crbyte Hour , Nack                                ' Hours
       
I2cstop

        Seco
= Makedec(seco) : Mine = Makedec(mine) : Hour = Makedec(hour)

       
If Seco > 59 Then Seco = 0
       
If Mine > 59 Then Mine = 0
       
If Hour > 23 Then
        Hour
= 0
       
Gosub Seco
       
End If

Return
'-------------------------------------------------------------------------------
 24_12
:
 
If Pinc.4 = 1 Then Gosub Disply_24
 
If Pinc.4 = 0 Then Gosub Disply_12
 
Return
'-------------------------------------------------------------------------------
Disply_24
:

        
Locate 1 , 1
        
Lcd "Time = " ; Hour ; ":" ;
        
If Mine < 10 Then
           
Lcd "0" ; Mine ; ":" ;
        
Else
           
Lcd Mine ; ":" ;
        
End If

        
If Seco < 10 Then
           
Lcd "0" ; Seco
        
Else
           
Lcd Seco
        
End If

        
Locate 2 , 6
        
Lcd "(24)"
Return

'-------------------------------------------------------------------------------
Disply_12
:

        
If Hour = 0 Then Hour = 12
        
If Hour > 12 Then Hour = Hour - 12

        
Locate 1 , 1
        
Lcd "Time = " ; Hour ; ":" ;
        
If Mine < 10 Then
           
Lcd "0" ; Mine ; ":" ;
        
Else
           
Lcd Mine ; ":" ;
        
End If

        
If Seco < 10 Then
           
Lcd "0" ; Seco
        
Else
           
Lcd Seco
        
End If
        
Locate 2 , 6
        
Lcd "(12)"
Return
'-------------------------------------------------------------------------------
Chekkey
:

        
Debounce Pinc.1 , 0 , Seco , Sub
        
Debounce Pinc.2 , 0 , Mine , Sub
        
Debounce Pinc.3 , 0 , Hour , Sub


Return
'---------------------------------------------
Seco
:
        
Incr Seco
        
If Seco > 59 Then Seco = 0
         Seco
= Makebcd(seco)
        
I2cstart                                           ' Generate start code
        
I2cwbyte Ds1307w                                   ' send address
        
I2cwbyte 0                                         ' starting address in 1307
        
I2cwbyte Seco
        
I2cstop
Return

'-------------------------------------
Mine
:
        
Incr Mine
        
If Mine > 59 Then Mine = 0
         Mine
= Makebcd(mine)
        
I2cstart                                           ' Generate start code
        
I2cwbyte Ds1307w                                   ' send address
        
I2cwbyte 1                                         ' starting address in 1307
        
I2cwbyte Mine