Sunday, August 21, 2016

How to Make a Digital Clock With Atmega8 Microcontroller

Time is so precious .... on this occasion I will give a tutorial how to make a digital clock using microcontroller Atmega8. Digital Clock is very simple. Atmega8A only consists of Microcontroller, RTC DS1307, and several supporting components. Digital clock can be set and added Alarm. Digital Clock is suitable mounted on the wall.

How to Make a Digital Clock With Atmega8 Microcontroller
Shematic Digital Clock Atmega8A

of the first plan is to be made with the right size with a field of seven segment as the primary display. the size of seven sections 1 "I think it is very appropriate, not too big and not too small. total size of seven segments are 10x4.5 cm. PCB size will follow the size 10x4.5 cm. very difficult to design a PCB with a size of 10x4.5 cm with a single layer PCB. therefore I had to use a Double Layer PCB.
below are the results of the PCB design

How to Make a Digital Clock With Atmega8 Microcontroller
PCB Digital Clock Atmega8A


How to Make a Digital Clock With Atmega8 Microcontroller

How to Make a Digital Clock With Atmega8 Microcontroller

How to Make a Digital Clock With Atmega8 Microcontroller


the following code digital clock Atmega 8A:

'jam digital
'atmega8a
'by fauzi hp

Config Timer0 = Timer , Prescale = 64

On Ovf0 Tim0_isr
Enable Timer0
Enable Interrupts

'buat PORTC & PORTD sbg output

Config Portc = Input
Config Portb = Output

Portc = &B11

Ddrd = &B10011111
Portd = &B01000000

'pengaturan kaki RTC, menyesuaikan kaki RTC dgn kaki Mikrokontroler

Config Sda = Portc.2
Config Scl = Portc.3

'alamat device RTC, setiap perangkat I2C mempunyai alamat device berbeda-beda

'kebetulan untuk IC Jam RTC alamatnya "&HD0" untuk operasi Write dan &HD1 untuk
'operasi Read
Const Ds1307w = &HD0
Const Ds1307r = &HD1

Config Clock = User


'variabel2 yg diperlukan

Dim Weekday As Byte
Dim Digit As Byte
Dim Satdetik As Byte
Dim Puldetik As Byte
Dim Satmin As Byte
Dim Pulmin As Byte
Dim Satjam As Byte
Dim Puljam As Byte
Dim M0 As Bit
Dim M1 As Bit , M2 As Bit , M3 As Bit , M4 As Bit
Dim Jam As Byte , Jams As Byte
Dim Menit As Byte , Menits As Byte
Dim Ctrbzr As Word
Dim Bzr As Bit
Dim Aljam As Byte , Almen As Byte
Dim Disp As Bit
Dim Satmintmr As Byte , Pulmintmr As Byte
Dim Satjamtmr As Byte , Puljamtmr As Byte
Dim Alrmjam As Byte , Alrmmin As Byte
Dim Aljamadr As Byte , Alminadr As Byte
Dim Kedip As Byte
Dim Kdp As Bit

Dim Satsuhu As Byte , Pulsuhu As Byte , Suhu As Byte

Dim Temp As Single
Dim Vadc As Word
Dim Tamp As Bit
Dim Dlytemp As Word
Dim Jamsuhu As Word
Dim Komsuh As Byte
Dim Satkom As Byte , Pulkom As Byte

Alminadr = 1

Aljamadr = 2

If Pinc.0 = 0 Then

   Time$ = "14:01:59"
   Date$ = "12-10-15"
End If

'aktifkan detik di RTC

I2cstart
I2cwbyte Ds1307w
I2cwbyte 7
I2cwbyte &B00010000
I2cstop

Readeeprom Aljam , Aljamadr

Readeeprom Almen , Alminadr

Do


Loop


End


Tim0_isr:

   'pemanggilan subrutin baca RTC
   Gosub Getdatetime
   'ambil nilai satuan & puluhan detik
   Satdetik = _sec And &B00001111
   Rotate _sec , Right , 4
   Puldetik = _sec And &B00001111

   'ambil nilai satuan & puluhan menit

   Satmin = _min And &B00001111
   Rotate _min , Right , 4
   Pulmin = _min And &B00001111

   'ambil nilai satuan & puluhan jam

   Satjam = _hour And &B00001111
   Rotate _hour , Right , 4
   Puljam = _hour And &B00001111

   'konversi data alarm jam ke bcd untuk ditampilkan di 7S

   Alrmjam = Makebcd(aljam)
   Satjamtmr = Alrmjam And &B00001111
   Rotate Alrmjam , Right , 4
   Puljamtmr = Alrmjam And &B00001111
   'konversi data alarm menit ke bcd untuk ditampilkan di 7S
   Alrmmin = Makebcd(almen)
   Satmintmr = Alrmmin And &B00001111
   Rotate Alrmmin , Right , 4
   Pulmintmr = Alrmmin And &B00001111

   'alarm 1

   If Jam = Aljam Then
   If Menit = Almen Then
      Incr Ctrbzr
      If Ctrbzr = 128 Then
         Ctrbzr = 0
         Toggle Bzr
      End If
      'bagian yg menyalakan buzer
      If Bzr = 1 Then
      Set Portd.7
         Else
            Reset Portd.7
      End If
   End If
   End If

   'mencocokan jam, menggunakan tombol yg terpasang di PORTB.0

   If Disp = 0 Then
   If Pinc.0 = 0 Then
      If M0 = 0 Then
         M0 = 1
         Tamp = 0
         Incr Jam
         If Jam = 24 Then
            Jam = 0
         End If

         'karena format data di RTC adalah BCD, maka ubah dulu ke BCD

         Jams = Makebcd(jam)

         'kemudian kirim data tersebut menggunakan protokol I2C (SCL & SDA)

         I2cstart
         I2cwbyte Ds1307w
         I2cwbyte 2                                         'alamat jam di RTC
         I2cwbyte Jams
         I2cstop
      End If
         Else
            M0 = 0
   End If
   End If

   'mencocokan menit, menggunakan tombol yg terpasang di PORTB.1

   If Disp = 0 Then
   If Pinc.1 = 0 Then
      If M1 = 0 Then
         Tamp = 0
         M1 = 1
         Incr Menit
         If Menit = 60 Then
            Menit = 0
         End If

         'karena format data di RTC adalah BCD, maka ubah dulu ke BCD

         Menits = Makebcd(menit)

         'kemudian kirim data tersebut menggunakan protokol I2C (SCL & SDA)

         I2cstart
         I2cwbyte Ds1307w
         I2cwbyte 1                                         'alamat menit di RTC
         I2cwbyte Menits
         I2cstop
      End If
          Else
            M1 = 0
   End If
   End If

   'menaikan timer

   If Disp = 1 Then
   If Pinc.0 = 0 Then
      If M3 = 0 Then
         M3 = 1
         Incr Aljam
         If Aljam = 24 Then
            Aljam = 0
         End If
         Writeeeprom Aljam , Aljamadr
      End If
         Else
            M3 = 0
   End If
   End If

   'menurunkan timer

   If Disp = 1 Then
   If Pinc.1 = 0 Then
      If M4 = 0 Then
         M4 = 1
         Incr Almen
         If Almen = 60 Then
            Almen = 0
         End If
         Writeeeprom Almen , Alminadr
      End If
          Else
            M4 = 0
   End If
   End If

   'tobol memilih jam/set timer

   If Pind.6 = 0 Then
      If M2 = 0 Then
         M2 = 1
         Toggle Disp
      End If
         Else
            M2 = 0
   End If

   Incr Jamsuhu

   If Jamsuhu = 2500 Then
      'Toggle Tamp
      If Tamp = 1 Then
         Jamsuhu = 1800
            Else
               Jamsuhu = 0
      End If
   End If

   'reset port scaning 7 segmen, biar tampilan halus/benar

   Reset Portd.0
   Reset Portd.1
   Reset Portd.2
   Reset Portd.3

   'bagian yang menampilkan jam/menit/detik & suhu

   If Disp = 0 Then
   If Tamp = 0 Then
         Select Case Digit
            Case 0:
               If Puljam = 0 Then
                  Portb = &B11111111
                     Else
                        Portb = Lookup(puljam , 7seg)
               End If

               Set Portd.0

            Case 1:
               Portb = Lookup(satjam , 7seg)
               If Pinc.4 = 1 Then
                  Reset Portb.7
                     Else
                        Set Portb.7
               End If
               Set Portd.1
            Case 2:
               Portb = Lookup(pulmin , 7seg)
               If Pinc.4 = 1 Then
                  Reset Portb.7
                     Else
                        Set Portb.7
               End If
               Set Portd.2
            Case 3:
               Portb = Lookup(satmin , 7seg)
               Set Portd.3
         End Select
   End If

   If Tamp = 1 Then

         Select Case Digit
            Case 0:
               Portb = Lookup(pulsuhu , 7seg)
               Set Portd.0
            Case 1:
               Portb = Lookup(satsuhu , 7seg)
               Reset Portb.7
               Set Portd.1
            Case 2:
               Portb = Lookup(satkom , 7seg)
               Set Portd.2
            Case 3:
               Portb = &B00100111
               Set Portd.3
         End Select
   End If
   End If

   'bagian yg menampilkan data alarm ketika menseting alarm

   If Disp = 1 Then
   Select Case Digit
      Case 0:
         Portb = Lookup(pulmintmr , 7seg)
         Set Portd.0
      Case 1:
         Portb = Lookup(satmintmr , 7seg)
         Set Portd.1
      Case 2:
         Portb = Lookup(puljamtmr , 7seg)
         Set Portd.2
      Case 3:
         Portb = Lookup(satjamtmr , 7seg)
         Set Portd.3
   End Select
   End If

   'naikan digit,akan tetapi dibatasi sampai 4 Saja(sesuai Jumlah 7 Seg)

   Incr Digit
   If Digit = 4 Then
      Digit = 0
   End If
Return


'data konversi BCD to 7 segmen harus diletakan di ROM/FLASH Memory, untuk itu data

'ini ditulis diluar code utama
7seg:
Data &B11000000                                             'angka 0
Data &B11111001                                             'angka 1
Data &B10100100                                             'angka 2
Data &B10110000                                             'angka 3
Data &B10011001                                             'angka 4
Data &B10010010                                             'angka 5
Data &B10000010                                             'angka 6
Data &B11111000                                             'angka 7
Data &B10000000                                             'angka 8
Data &B10010000                                             'angka 9

'subrutin dibawah ini untuk pembacaan RTC, di tempatkan disini untuk kemudahan

'dan pemahaman program
Getdatetime:
  I2cstart
  I2cwbyte Ds1307w
  I2cwbyte 0

  I2cstart

  I2cwbyte Ds1307r
  I2crbyte _sec , Ack
  I2crbyte _min , Ack
  I2crbyte _hour , Ack
  I2crbyte Weekday , Ack
  I2crbyte _day , Ack
  I2crbyte _month , Ack
  I2crbyte _year , Nack
  I2cstop
  Jam = Makedec(_hour)
  Menit = Makedec(_min)
  '_sec = Makedec(_sec) : _min = Makedec(_min) : _hour = Makedec(_hour)
  '_day = Makedec(_day) : _month = Makedec(_month) : _year = Makedec(_year)
Return

Setdate:

  _day = Makebcd(_day) : _month = Makebcd(_month) : _year = Makebcd(_year)
  I2cstart
  I2cwbyte Ds1307w
  I2cwbyte 4
  I2cwbyte _day
  I2cwbyte _month
  I2cwbyte _year
  I2cstop
Return

Settime:

  _sec = Makebcd(_sec) : _min = Makebcd(_min) : _hour = Makebcd(_hour)
  I2cstart
  I2cwbyte Ds1307w
  I2cwbyte 0
  I2cwbyte _sec
  I2cwbyte _min
  I2cwbyte _hour
  I2cstop
Return

'-------------------------------------------------------------------------------

Artikel Terkait

How to Make a Digital Clock With Atmega8 Microcontroller
4/ 5
Oleh

Berlangganan

Suka dengan artikel di atas? Silakan berlangganan gratis via email

2 komentar

February 1, 2022 at 10:56 PM delete

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene. Arduino library for Proteus

Reply
avatar