LAMPIRAN
LAMPIRAN
Program yang digunakan pada “RTC (Real Time Clock) DS1307 Sebagai Pengatur Waktu Pada Sistem Traffic Light Adaptif”
$regfile = "m8def.dat" $crystal = 16000000 $lib "ds1307clock.lib"
Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.1 , Db7 = Portc.0 , Rs = Portc.5 , E = Portc.4
Config Lcd = 16 * 2 Cursor Off
Cls
Config Sda = Portd.7 Config Scl = Portd.6 Const Ds1307w = &HD0 Const Ds1307r = &HD1 Sqw Alias Pind.5
Tbl_menu Alias Pind.2 Tbl_up Alias Pind.3 Tbl_down Alias Pind.4 Pin_data Alias Portb.0
St_cp Alias Portb.1 'Enable
Sh_cp Alias Portb.2 'clock
Awal:
Enable Int0
Enable Interrupts On Int0 Set_waktu Dim Jam As Byte Dim Menit As Byte Dim Detik As Byte Dim Tunda As Byte Dim Temp As Byte
Dim Tunda_set As Byte Dim Slot As Byte
Dim Data1 As Byte Dim Data2 As Byte Locate 1 , 1
Locate 1 , 8
Gosub Ambil_waktu Gosub Cek_slot Gosub Tampil If Slot = 1 Then
Data1 = &H42 Data2 = &H42 Gosub Kirim_data Waitms 250
Data1 = 0 Data2 = 0
Gosub Kirim_data Waitms 250
Else
Gosub Looping_lampu End If
Loop
Kirim_data:
Shiftout Pin_data , Sh_cp , Data1 , 1 Shiftout Pin_data , Sh_cp , Data2 , 1 Set St_cp
Waitus 200 Reset St_cp Return
Ambil_waktu: I2cstart
I2cwbyte Ds1307w I2cwbyte 0
I2cstart
I2cwbyte Ds1307r I2crbyte Detik , Ack I2crbyte Menit , Ack I2crbyte Jam , Nack I2cstop
Incr Tunda End If
Temp = Detik Return
Tampil:
Locate 1 , 8 If Jam < 10 Then
Locate 1 , 8 Lcd "0"
End If Lcd Jam
Locate 1 , 11
If Menit < 10 Then Locate 1 , 11 Lcd "0"
End If Lcd Menit Locate 1 , 14
If Detik < 10 Then Locate 1 , 14 Lcd "0"
End If Lcd Detik Locate 2 , 14
If Tunda < 10 Then Locate 2 , 14 Lcd "0"
End If Lcd Tunda Locate 2 , 7 Lcd Slot Return
Set_waktu: Disable Int0 Waitms 250 Do
If Tbl_up = 0 Then Incr Menit
If Menit > 59 Then Menit = 0
End If
If Tbl_down = 0 Then Decr Menit
If Menit > 254 Then Menit = 59
End If Waitms 80 End If
Gosub Tampil Waitms 80
Loop Until Tbl_menu = 0 Waitms 150
Do
If Tbl_up = 0 Then Incr Jam
If Jam > 23 Then Jam = 0
End If Waitms 80 End If
If Tbl_down = 0 Then Decr Jam
If Jam > 254 Then Jam = 23
End If Waitms 80 End If
Gosub Tampil Waitms 80
Loop Until Tbl_menu = 0 Waitms 250
Detik = 0
Detik = Makebcd(detik) Menit = Makebcd(menit) Jam = Makebcd(jam) I2cstart
I2cwbyte Ds1307w I2cwbyte 0
I2cwbyte Detik I2cwbyte Menit I2cwbyte Jam I2cstop
Lcd "Waktu: " Locate 1 , 8 Lcd " : : " Locate 2 , 1
Lcd "Slot: Det: " Goto Awal
Return
Tunda_set = 5 End If
End If
If Jam >= 6 And Menit >= 0 Then If Jam <= 8 And Menit <= 59 Then
Slot = 3
Tunda_set = 15 End If
End If
If Jam >= 9 And Menit >= 0 Then
If Jam <= 11 And Menit <= 59 Then Slot = 4
Tunda_set = 10 End If
End If
If Jam >= 12 And Menit >= 0 Then If Jam <= 13 And Menit <= 59 Then
Slot = 5
Tunda_set = 15 End If
End If
If Jam >= 14 And Menit >= 0 Then If Jam <= 16 And Menit <= 59 Then
Slot = 6
End If
If Jam >= 17 And Menit >= 0 Then If Jam <= 20 And Menit <= 59 Then
Slot = 7
Tunda_set = 15 End If
End If
If Jam >= 21 And Menit >= 0 Then If Jam <= 23 And Menit <= 59 Then
Slot = 8
Tunda_set = 5 End If
End If
If Jam = 0 And Menit <= 59 Then Slot = 8
Tunda_set = 10 End If
Return
Looping_lampu: Data1 = &H24 Data2 = &H84 Gosub Kirim_data Do
Gosub Ambil_waktu Gosub Tampil
Loop Until Tunda = Tunda_set Tunda = 0
Data1 = &H44 Data2 = &H84 Gosub Kirim_data Do
Gosub Ambil_waktu Gosub Tampil
Loop Until Tunda = 2 Tunda = 0
Data1 = &H84 Data2 = &H24 Gosub Kirim_data Do
Gosub Tampil
Loop Until Tunda = Tunda_set Tunda = 0
Data1 = &H84 Data2 = &H44 Gosub Kirim_data Do
Gosub Ambil_waktu Gosub Tampil
Loop Until Tunda = 2 Tunda = 0
Data1 = &H81 Data2 = &H84 Gosub Kirim_data Do
Gosub Ambil_waktu Gosub Tampil
Loop Until Tunda = Tunda_set Tunda = 0
Data1 = &H82 Data2 = &H84 Gosub Kirim_data Do
Gosub Ambil_waktu Gosub Tampil
Loop Until Tunda = 2 Tunda = 0
Data1 = &H84 Data2 = &H81 Gosub Kirim_data Do
Gosub Ambil_waktu Gosub Tampil
Loop Until Tunda = Tunda_set Tunda = 0
Gosub Ambil_waktu Gosub Tampil
Loop Until Tunda = 2 Tunda = 0