March 08, 2012

Digital Clock with Alarm Function

This design is to answer the questions from visitors of this blog about Digital Clock with Alarm Function.


$regfile = "m8535.dat"
$crystal = 12000000

'------------------------  lcd -------------------------------------------------
Config Lcd = 16 * 2
Config Lcdpin = Pin , Rs = Pina.0 , E = Pina.1 , Db4 = Pina.4 , Db5 = Pina.5 , Db6 = Pina.6 , Db7 = Pina.7

'---------------------------  ds 1307 -----------------------------------------
$lib "ds1307clock.lib"
'configure the scl and sda pins
Config Sda = Portc.7
Config Scl = Portc.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 Portd.2 = Output

$eeprom

'-------------------------------------------------------------------------------
Dim Seco As Integer , Mine As Integer , Hour As Integer , , Al1 As Integer , Al2 As Integer

'-------------------------------------------------------------------------------
Cursor Off
 '------------------------------------------------------------------------------
Cls
Locate 1 , 1
Lcd "  Digital Clock  "
Waitms 100
Cls

'-------------------------------------------------------------------------------
Main:
Do
          Gosub Ds1307
          Gosub 24_12
          Gosub Chekkey
          Gosub Alarm
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)

 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 "Clock = " ; Hour ; ":" ; Mine ; ":" ; Seco ; "  "
         Locate 2 , 6
         Lcd "(24 Hour)"
         Wait 1

 Return

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


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

         Locate 1 , 1
         Lcd "Clock = " ; Hour ; ":" ; Mine ; ":" ; Seco ; "  "
         Locate 2 , 6
         Lcd "(12 Hour)"
 Return

'-------------------------------------------------------------------------------
'                           Select the settings
'-------------------------------------------------------------------------------
Chekkey:

   If Pinc.1 = 0 Then
      Cls
      Bitwait Pinc.1 , Set
      Locate 1 , 1
      Lcd "****Setting****"
      Locate 2 , 1
      Lcd "Time"
      Locate 2 , 12
      Lcd "alarm"
      Do
      Loop Until Pinc.2 = 0 Or Pinc.3 = 0

'-------------------------------------------------------------------------------
'                                          Set clock
'-------------------------------------------------------------------------------
      If Pinc.2 = 0 Then
         Cls
         Bitwait Pinc.2 , Set
         Locate 1 , 1
         Lcd "**Setting Time**"
         Locate 2 , 1
         Lcd "Time = " ; Hour ; ":" ; Mine ; "          "

         Do
         If Pinc.2 = 0 Then
            Incr Hour
            Waitms 10
               If Hour > 24 And Pinc.4 = 1 Then Hour = 0
               If Hour > 12 And Pinc.4 = 0 Then Hour = 0
         End If
         If Pinc.3 = 0 Then
            Decr Hour
            Waitms 10
            If Hour < 0 And Pinc.4 = 1 Then Hour = 24
            If Hour < 0 And Pinc.4 = 0 Then Hour = 12
         End If

         Locate 2 , 1
         Lcd "      " ; Hour ; ":" ; Mine ; "          "
         Loop Until Pinc.1 = 0

         Bitwait Pinc.1 , Set
         Gosub Hour
         Waitms 10

         Do
         If Pinc.2 = 0 Then
            Incr Mine
            Waitms 10
               If Mine > 60 Then Mine = 0
         End If
         If Pinc.3 = 0 Then
            Decr Mine
            Waitms 10
            If Mine < 0 Then Mine = 60
         End If

         Locate 2 , 1
         Lcd "      " ; Hour ; ":" ; Mine ; "               "
         Loop Until Pinc.1 = 0

         Bitwait Pinc.1 , Set
         Gosub Mine
         Waitms 10
         Cls
      End If

'-------------------------------------------------------------------------------
'                                              set alarm
'-------------------------------------------------------------------------------
      If Pinc.3 = 0 Then
           Cls
           Bitwait Pinc.3 , Set
           Waitms 10
           Readeeprom Al1 , 2
           Waitms 10
           Readeeprom Al2 , 4
           Waitms 10
         Locate 1 , 1
         Lcd "*Set alarm*"

            Do
               If Pinc.2 = 0 Then
                  Incr Al1
                  Waitms 10
                  If Al1 > 24 And Pinc.4 = 1 Then Al1 = 0
                  If Al1 > 12 And Pinc.4 = 0 Then Al1 = 0
               End If
               If Pinc.3 = 0 Then
                  Decr Al1
                   Waitms 10
                   If Al1 < 0 Then Al1 = 24
               End If

               Locate 2 , 1
               Lcd "      " ; Al1 ; ":" ; Al2 ; "               "
            Loop Until Pinc.1 = 0

            Writeeeprom Al1 , 2
            Waitms 10
            Bitwait Pinc.1 , Set

            Do
               If Pinc.2 = 0 Then
                  Incr Al2
                  Waitms 10
                  If Al2 > 60 Then Al2 = 0
               End If
               If Pinc.3 = 0 Then
                  Decr Al2
                  Waitms 10
                  If Al2 < 0 Then Al2 = 60
               End If

               Locate 2 , 1
               Lcd "      " ; Al1 ; ":" ; Al2 ; "               "
            Loop Until Pinc.1 = 0

            Writeeeprom Al2 , 4
            Waitms 10
            Bitwait Pinc.1 , Set
            Cls
      End If
   End If

'-------------------------------------------------------------------------------
 Mine:
          Mine = Makebcd(mine)
          I2cstart                                          ' Generate start code
          I2cwbyte Ds1307w                                  ' send address
          I2cwbyte 1                                        ' starting address in 1307
          I2cwbyte Mine
          I2cstop
 Return
'-------------------------------------------------------------------------------
 Hour:
          Hour = Makebcd(hour)
          I2cstart                                          ' Generate start code
          I2cwbyte Ds1307w                                  ' send address
          I2cwbyte 1                                        ' starting address in 1307
          I2cwbyte Hour
          I2cstop
 Return

'-------------------------------------------------------------------------------
 Alarm:
           Readeeprom Al1 , 2
           Waitms 10
           Readeeprom Al2 , 4
           Waitms 10
           If Al1 = Hour And Al2 = Mine Then
               Portd.2 = 1
               Do
               Loop Until Pinc.1 = 0
           End If
           Reset Portd.2
 Return

11 comments:

  1. wah bagus mas, itu pake atmega8 ya mas? pas banget ini, lagi butuh trims ya mas :D

    ReplyDelete
    Replies
    1. semoga bermanfaat mas. di sini saya menggunkan atmega 8535. dengan sedikit modifikasi bisa juga diterapkan dengan tipe mikrokontroler jenis lain.

      Delete
  2. emang bisa ya 8535 LCD dikasih di port A??kalo disimulasi sih bisa,kalo di hardware gag bisa

    ReplyDelete
  3. mas kalu pake segment bagai mana programnya mas?
    Mohon bantuanya mas?

    ReplyDelete
  4. wah, makasih mas postingnya, saya sedang membutuhkan sekali...

    ReplyDelete
  5. mas saya memakai attiny26,setelah di compile ternyata "overwrite bootloader" apakah memorinya tidak cukup?mohon penjelasannya..

    ReplyDelete
  6. mas, kalau pakai seven segment gimana programnya mas??

    ReplyDelete
  7. juga bagaimana penerapannya di rangkaian??? harus disambungkan kemana??? mohon bantuan pencerahannya mas,,,,

    ReplyDelete
  8. izin copy ya mas....

    ReplyDelete
  9. mas, da di coba tapi kok, buzzernya gak bersuara ya mas, saya coba di ptoteus

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete

Let's us sharing

Recent Comment

Grab This Widget