Membuat Jam Digital Menggunakan RTC ( Real Time Clock ) dan Mikrokontroller
Pada saat yang berbahagia ini saya akan menjelaskan mengenai bagaimana cara membuat jam digital menggunakan mikrokontroller dan RTC sebagai kendali waktunya, Adanya IC RTC yaitu DS1307 dan battery 3,3 volt membuat mikrokontroller update selalu waktunya biarpun alat dalam keadaan OFF atau mati, jika dihidupkan kembali maka waktu yang tertera akan update atau real time yaitu waktu sekarang biarpun sebelumnya alat dalam posisi mati, itu karena adanya supply dan detak pulsa dari rangkaian RTC. Perlu diingat bahwa pada bascom AVR ada dua cara mengakses RTC atau jam digital, pertama menggunakan library dan kedua tidak menggunakan library, waktu menggunakan library biasanya saat dikonfigrasikan dengan SD card Module atau device yang menggunakan SPI Port, saat tidak menggunakan library yaitu saat tidak menggunakan SPI Port. Untuk lebih jelasnya berikut skema dan programnya.
a. Minimum System + RTC
b. Program Bascom AVR tidak menggunakan Library RTC
$regfile = "m16def.dat"
$crystal = 12000000
$baud = 9600
Config Lcdpin = Pin , Rs = Portc.2 , E = Portc.3 , Db4 = Portc.4
Config Lcdpin = Pin , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7
Config Lcd = 16 * 2
Cls
Cursor Off
Config Sda = Portc.1
Config Scl = Portc.0
Const Ds1307w = &HD0
Const Ds1307r = &HD1
Dim Oke As Integer
Dim Mark As Byte
Dim _sec As Byte
Dim _min As Byte
Dim _hour As Byte
Dim _day As Byte
Dim _month As Byte
Dim _year As Byte
Dim _weekday As Byte
Dim Sa(3) As String * 2
Dim Sb(3) As String * 2
Declare Sub Getdatetime
Declare Sub Settime
Declare Sub Getsec
Declare Sub Disptime
Declare Sub Dispdate
Declare Sub Format_00
Declare Sub Setdate
Declare Sub Subset
Dim I As Byte
Dim Flagset As Bit '0 = Waktu, 1 = Tanggal
Dim Flagreset As Bit
Dim Line1 As String * 1
Dim Weekday As Byte
Dim Jam_puluhan As Integer
Dim Jam_satuan As Integer
Dim Min_puluhan As Integer
Dim Min_satuan As Integer
Dim Sec_puluhan As Integer
Dim Sec_satuan As Integer
Ddrb.0 = 1
Ddrb.1 = 1
Ddrb.2 = 1
Ddrb.3 = 1
'_sec = 00
'_hour = 14
'_min = 59
'Settime
'_day = 28
'_month = 11
'_year = 15
'Setdate
I = 13
Line1 = Chr(13)
Do
Portb.0 = 1
Portb.1 = 0
Portb.2 = 0
Portb.3 = 0
Wait 3
Portb.0 = 0
Portb.1 = 0
Portb.2 = 0
Portb.3 = 0
Wait 1
Getdatetime
Upperline
Disptime
Lowerline
Dispdate
Loop
Sub Getdatetime:
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 0 ' start address in 1307
I2cstart ' Generate start code
I2cwbyte Ds1307r ' send address
I2crbyte _sec , Ack
I2crbyte _min , Ack ' MINUTES
I2crbyte _hour , Ack ' Hours
I2crbyte _weekday , Ack ' Day of Week
I2crbyte _day , Ack ' Day of Month
I2crbyte _month , Ack ' Month of Year
I2crbyte _year , Nack ' Year
I2cstop
_sec = Makedec(_sec) : _min = Makedec(_min) : _hour = Makedec(_hour)
_day = Makedec(_day) : _month = Makedec(_month) : _year = Makedec(_year)
End Sub
Sub Settime:
_sec = Makebcd(_sec) : _min = Makebcd(_min) : _hour = Makebcd(_hour)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 0 ' starting address in 1307
I2cwbyte _sec ' Send Data to SECONDS
I2cwbyte _min ' MINUTES
I2cwbyte _hour ' Hours
I2cstop
End Sub
Sub Setdate
_day = Makebcd(_day) : _month = Makebcd(_month) : _year = Makebcd(_year)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 4 ' starting address in 1307
I2cwbyte _day ' Send Data to SECONDS
I2cwbyte _month ' MINUTES
I2cwbyte _year ' Hours
I2cstop
End Sub
$crystal = 12000000
$baud = 9600
Config Lcdpin = Pin , Rs = Portc.2 , E = Portc.3 , Db4 = Portc.4
Config Lcdpin = Pin , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7
Config Lcd = 16 * 2
Cls
Cursor Off
Config Sda = Portc.1
Config Scl = Portc.0
Const Ds1307w = &HD0
Const Ds1307r = &HD1
Dim Oke As Integer
Dim Mark As Byte
Dim _sec As Byte
Dim _min As Byte
Dim _hour As Byte
Dim _day As Byte
Dim _month As Byte
Dim _year As Byte
Dim _weekday As Byte
Dim Sa(3) As String * 2
Dim Sb(3) As String * 2
Declare Sub Getdatetime
Declare Sub Settime
Declare Sub Getsec
Declare Sub Disptime
Declare Sub Dispdate
Declare Sub Format_00
Declare Sub Setdate
Declare Sub Subset
Dim I As Byte
Dim Flagset As Bit '0 = Waktu, 1 = Tanggal
Dim Flagreset As Bit
Dim Line1 As String * 1
Dim Weekday As Byte
Dim Jam_puluhan As Integer
Dim Jam_satuan As Integer
Dim Min_puluhan As Integer
Dim Min_satuan As Integer
Dim Sec_puluhan As Integer
Dim Sec_satuan As Integer
Ddrb.0 = 1
Ddrb.1 = 1
Ddrb.2 = 1
Ddrb.3 = 1
'_sec = 00
'_hour = 14
'_min = 59
'Settime
'_day = 28
'_month = 11
'_year = 15
'Setdate
I = 13
Line1 = Chr(13)
Do
Portb.0 = 1
Portb.1 = 0
Portb.2 = 0
Portb.3 = 0
Wait 3
Portb.0 = 0
Portb.1 = 0
Portb.2 = 0
Portb.3 = 0
Wait 1
Getdatetime
Upperline
Disptime
Lowerline
Dispdate
Loop
Sub Getdatetime:
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 0 ' start address in 1307
I2cstart ' Generate start code
I2cwbyte Ds1307r ' send address
I2crbyte _sec , Ack
I2crbyte _min , Ack ' MINUTES
I2crbyte _hour , Ack ' Hours
I2crbyte _weekday , Ack ' Day of Week
I2crbyte _day , Ack ' Day of Month
I2crbyte _month , Ack ' Month of Year
I2crbyte _year , Nack ' Year
I2cstop
_sec = Makedec(_sec) : _min = Makedec(_min) : _hour = Makedec(_hour)
_day = Makedec(_day) : _month = Makedec(_month) : _year = Makedec(_year)
End Sub
Sub Settime:
_sec = Makebcd(_sec) : _min = Makebcd(_min) : _hour = Makebcd(_hour)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 0 ' starting address in 1307
I2cwbyte _sec ' Send Data to SECONDS
I2cwbyte _min ' MINUTES
I2cwbyte _hour ' Hours
I2cstop
End Sub
Sub Setdate
_day = Makebcd(_day) : _month = Makebcd(_month) : _year = Makebcd(_year)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 4 ' starting address in 1307
I2cwbyte _day ' Send Data to SECONDS
I2cwbyte _month ' MINUTES
I2cwbyte _year ' Hours
I2cstop
End Sub
Sub Dispdate
Sa(1) = Str(_day)
Sa(2) = Str(_month)
Sa(3) = Str(_year)
Format_00
Lcd "Date: " ; Sb(1) ; "-" ; Sb(2) ; "-" ; Sb(3)
End Sub
Sub Disptime
Sa(1) = Str(_hour)
Sa(2) = Str(_min)
Sa(3) = Str(_sec)
Format_00
Lcd "Time: " ; Sb(1) ; ":" ; Sb(2) ; ":" ; Sb(3)
End Sub
Sub Format_00
For I = 1 To 3 Step 1
Sb(i) = Format(sa(i) , "00")
Next I
End Sub
Sa(1) = Str(_day)
Sa(2) = Str(_month)
Sa(3) = Str(_year)
Format_00
Lcd "Date: " ; Sb(1) ; "-" ; Sb(2) ; "-" ; Sb(3)
End Sub
Sub Disptime
Sa(1) = Str(_hour)
Sa(2) = Str(_min)
Sa(3) = Str(_sec)
Format_00
Lcd "Time: " ; Sb(1) ; ":" ; Sb(2) ; ":" ; Sb(3)
End Sub
Sub Format_00
For I = 1 To 3 Step 1
Sb(i) = Format(sa(i) , "00")
Next I
End Sub
c. Program Bascom AVR menggunakan Library RTC
$regfile = "m16def.dat"
$crystal = 12000000
$baud = 9600
Config Lcdpin = Pin , Rs = Portc.6 , E = Portc.7 , Db4 = Portc.2
Config Lcdpin = Pin , Db5 = Portc.3 , Db6 = Portc.4 , Db7 = Portc.5
Config Lcd = 16 * 2
Cls
Cursor Off
$lib "ds1307clock.lib"
Config Sda = Portc.1
Config Scl = Portc.0
Const Ds1307w = &HD0
Const Ds1307r = &HD1
Config Clock = User
Dim Weekday As Byte
Dim Char As String * 10 , Tanda As String * 1
Dim Jam As String * 10 , Menit As String * 10 , Detik As String * 10
Dim Hari As String * 10 , Bulan As String * 10 , Tahun As String * 10
Dim A As Byte , B As Byte , C As Byte
Dim X As Byte , Y As Byte , Z As Byte
Dim Waktu As String * 10
Dim Tanggal As String * 10
'Date$ = "09-11-15"
'Time$ = "15-13-10"
Ddrd.0 = 1
Print "Tanggal: " ; Date$
Print "Waktu:" ; Time$
Cls
Cursor Off
Locate 1 , 1
Lcd "Tanggal: " ; _day ; "-" ; _month ; "-" ; _year
Locate 2 , 1
Lcd "Waktu : " ; _hour ; ":" ; _min ; ":" ; _sec
Wait 1
Mulai:
Do
Print "Tanggal: " ; Date$
Print "Waktu:" ; Time$
Cls
Cursor Off
Locate 1 , 1
Lcd "Tanggal: " ; _day ; "-" ; _month ; "-" ; _year
Locate 2 , 1
Lcd "Waktu : " ; _hour ; ":" ; _min ; ":" ; _sec
Wait 1
If _hour = 4 Then 'setting waktu alarm
Portd.0 = 1
Else
Portd.0 = 0
End If
Loop
End
'Pengambilan data RTC DS1307
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
_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
$crystal = 12000000
$baud = 9600
Config Lcdpin = Pin , Rs = Portc.6 , E = Portc.7 , Db4 = Portc.2
Config Lcdpin = Pin , Db5 = Portc.3 , Db6 = Portc.4 , Db7 = Portc.5
Config Lcd = 16 * 2
Cls
Cursor Off
$lib "ds1307clock.lib"
Config Sda = Portc.1
Config Scl = Portc.0
Const Ds1307w = &HD0
Const Ds1307r = &HD1
Config Clock = User
Dim Weekday As Byte
Dim Char As String * 10 , Tanda As String * 1
Dim Jam As String * 10 , Menit As String * 10 , Detik As String * 10
Dim Hari As String * 10 , Bulan As String * 10 , Tahun As String * 10
Dim A As Byte , B As Byte , C As Byte
Dim X As Byte , Y As Byte , Z As Byte
Dim Waktu As String * 10
Dim Tanggal As String * 10
'Date$ = "09-11-15"
'Time$ = "15-13-10"
Ddrd.0 = 1
Print "Tanggal: " ; Date$
Print "Waktu:" ; Time$
Cls
Cursor Off
Locate 1 , 1
Lcd "Tanggal: " ; _day ; "-" ; _month ; "-" ; _year
Locate 2 , 1
Lcd "Waktu : " ; _hour ; ":" ; _min ; ":" ; _sec
Wait 1
Mulai:
Do
Print "Tanggal: " ; Date$
Print "Waktu:" ; Time$
Cls
Cursor Off
Locate 1 , 1
Lcd "Tanggal: " ; _day ; "-" ; _month ; "-" ; _year
Locate 2 , 1
Lcd "Waktu : " ; _hour ; ":" ; _min ; ":" ; _sec
Wait 1
If _hour = 4 Then 'setting waktu alarm
Portd.0 = 1
Else
Portd.0 = 0
End If
Loop
End
'Pengambilan data RTC DS1307
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
_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
e. Cara Penggunaan
Pada bagian code berikut pada program pertama
'_sec = 00
'_hour = 14
'_min = 59
'Settime
'_day = 28
'_month = 11
'_year = 15
'Setdate
Pada bagian code berikut pada program kedua
'_hour = 14
'_min = 59
'Settime
'_day = 28
'_month = 11
'_year = 15
'Setdate
harus
di-uncomment atau hilangkan tanda " ' " dahulu untuk memasukkan data
waktu dan tanggal kedalam DS1307, setelah di-upload ke mikrokontroller kemudian
upload kembali dengan menonaktifkan code tersebut agar tidak kembali
reset ke nilai awal.
'Date$ = "09-11-15"
'Time$ = "15-13-10"
'Time$ = "15-13-10"
harus
di-uncomment atau hilangkan tanda " ' " dahulu untuk memasukkan data
waktu dan tanggal kedalam DS1307, setelah di-upload ke mikrokontroller kemudian
upload kembali dengan menonaktifkan code tersebut agar tidak kembali
reset ke nilai awal.
FOTO HASILNYA