Translate

Membuat lampu Traffic Light dengan AVR dan Bascom

Membuat lampu Traffic Light dengan AVR dan Bascom


                Siang yang cerah ini saya akan menjelaskan bagaimana cara membuat traffic light sendiri atau lebih tepatnya kontroller modul menggunakan ATMega16 dan bahasa bascom AVR. Traffic light yang akan dibuat ini dilengkapi dengan input jam dan juga input delay, jadi user bisa memasukkan data jam dan data untuk lama delay merah / hijau, selain itu traffic light ini jika sudah pukul 23.00 lampu ini akan flashing dengan sendirinya hingga pukul 05.00, selebihnya lampu akan berjalan normal kembali. berikut adalah skematik dan programnya.


a. Skematik Minimum System 



b. Skematik RTC dan Kendali Lampu 220 V



c Skematik Sumber Tegangan untuk MOC3021


*Note = emitter 2n3055 untuk dihubungkan ke Vcc IC MOC3021



d, Program Bascom AVR

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

Config Lcdpin = Pin , Rs = Portc.0 , E = Portc.1 , Db4 = Portc.2
Config Lcdpin = Pin , Db5 = Portc.3 , Db6 = Portc.4 , Db7 = Portc.5
Config Lcd = 16 * 2

Cls
Cursor Off

Config Sda = Portc.7
Config Scl = Portc.6

Const Ds1307w = &HD0
Const Ds1307r = &HD1

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

'=========================================RTC

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
Dim Ax As Integer

Dim Detmerah1 As Eram Integer
Dim Detkuning1 As Eram Integer
Dim Dethijau1 As Eram Integer

Dim Merah1 As Integer
Dim Kuning1 As Integer
Dim Hijau1 As Integer

Dim Xjam As Byte
Dim Xmenit As Byte
Dim Ok_jam As Eram Byte
Dim Ok_menit As Eram Byte


Ax = 1
I = 13
Line1 = Chr(13)

'_sec = 00
'_hour = 18
'_min = 00
'Settime

'_day = 14
'_month = 3
'_year = 15
'Setdate

Ddra = &B11111111

'merah1
Ddrb.0 = 1
'kuning1
Ddrb.1 = 1
'hijau
Ddrb.2 = 1

'merah2
Ddrb.3 = 1
'kuning2
Ddrb.4 = 1
'hijau
Ddrd.7 = 1

'indikator
Ddrd.6 = 1

'tombol1
Ddrd.0 = 0
'tombol2
Ddrd.1 = 0
'tombol3
Ddrd.2 = 0
'tombol4
Ddrd.3 = 0

Tombolsetting Alias Pind.0
Tombolup Alias Pind.1
Tomboldown Alias Pind.2
Tombolselect Alias Pind.3

Set Portd.0
Set Portd.1
Set Portd.2
Set Portd.3

Xmerah1 Alias Portb.0
Xkuning1 Alias Portb.1
Xhijau1 Alias Portb.2
Xmerah2 Alias Portb.3
Xkuning2 Alias Portb.4
Xhijau2 Alias Portd.7
Ind Alias Portd.6

Lampmer1 Alias Porta.0
Lampkun1 Alias Porta.1
Lamphij1 Alias Porta.2
Lampmer2 Alias Porta.3
Lampkun2 Alias Porta.4
Lamphij2 Alias Porta.5


Main:
Ind = 1
Ax = 1

Cls

Merah1 = Detmerah1
Kuning1 = Detkuning1
Hijau1 = Dethijau1


Do

Upperline
Lcd "LOADING"
Lowerline
Lcd "Flashing = " ; Ax

Xmerah1 = 1
Lampmer1 = 1
Xkuning1 = 0
Lampkun1 = 0
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 1
Lampmer2 = 1
Xkuning2 = 0
Lampkun2 = 0
Xhijau2 = 1
Lamphij2 = 1
Waitms 500


Xmerah1 = 1
Lampmer1 = 1
Xkuning1 = 1
Lampkun1 = 1
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 1
Lampmer2 = 1
Xkuning2 = 1
Lampkun2 = 1
Xhijau2 = 1
Lamphij2 = 1
Waitms 500

Incr Ax

Loop Until Ax = 11

Cls
Upperline
Lcd "ALL RED"

Xmerah1 = 0
Lampmer1 = 0
Xkuning1 = 1
Lampkun1 = 1
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 0
Lampmer2 = 0
Xkuning2 = 1
Lampkun2 = 1
Xhijau2 = 1
Lamphij2 = 1
Wait 2

Cls
Cursor Off

Getdatetime
Upperline
Dispdate
Lowerline
Disptime

Oke:
Ind = 1

Do

Getdatetime
Upperline
Dispdate
Lowerline
Disptime

If Tombolsetting = 0 Then
Goto Setting
End If
If Tombolselect = 0 Then
Goto Flashman
End If

If _hour > 22 Then
Goto Flashing
End If



'm-m
Xmerah1 = 0
Lampmer1 = 0
Xkuning1 = 1
Lampkun1 = 1
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 0
Lampmer2 = 0
Xkuning2 = 1
Lampkun2 = 1
Xhijau2 = 1
Lamphij2 = 1
Wait 2

'm-h
Xmerah1 = 0
Lampmer1 = 0
Xkuning1 = 1
Lampkun1 = 1
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 1
Lampmer2 = 1
Xkuning2 = 1
Lampkun2 = 1
Xhijau2 = 0
Lamphij2 = 0

If Tombolsetting = 0 Then
Goto Setting
End If
If Tombolselect = 0 Then
Goto Flashman
End If

Wait Hijau1

'm-k
Xmerah1 = 0
Lampmer1 = 0
Xkuning1 = 1
Lampkun1 = 1
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 1
Lampmer2 = 1
Xkuning2 = 0
Lampkun2 = 0
Xhijau2 = 1
Lamphij2 = 1

If Tombolsetting = 0 Then
Goto Setting
End If
If Tombolselect = 0 Then
Goto Flashman
End If

Wait 3


'm-m
Xmerah1 = 0
Lampmer1 = 0
Xkuning1 = 1
Lampkun1 = 1
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 0
Lampmer2 = 0
Xkuning2 = 1
Lampkun2 = 1
Xhijau2 = 1
Lamphij2 = 1

Wait 2


'h-m
Xmerah1 = 1
Lampmer1 = 1
Xkuning1 = 1
Lampkun1 = 1
Xhijau1 = 0
Lamphij1 = 0
Xmerah2 = 0
Lampmer2 = 0
Xkuning2 = 1
Lampkun2 = 1
Xhijau2 = 1
Lamphij2 = 1


If Tombolsetting = 0 Then
Goto Setting
End If
If Tombolselect = 0 Then
Goto Flashman
End If

Wait Hijau1


'k-m
Xmerah1 = 1
Lampmer1 = 1
Xkuning1 = 0
Lampkun1 = 0
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 0
Lampmer2 = 0
Xkuning2 = 1
Lampkun2 = 1
Xhijau2 = 1
Lamphij2 = 1

If Tombolsetting = 0 Then
Goto Setting
End If
If Tombolselect = 0 Then
Goto Flashman
End If

Wait 3


Getdatetime
Upperline
Dispdate
Lowerline
Disptime

If Tombolsetting = 0 Then
Goto Setting
End If
If Tombolselect = 0 Then
Goto Flashman
End If

If _hour > 22 Then
Goto Flashing
Else
Goto Oke
End If


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


Setting:
:
Detmerah1 = 0
Detkuning1 = 0
Dethijau1 = 0

Xmerah1 = 1
Xkuning1 = 0
Xhijau1 = 1
Xmerah2 = 1
Xkuning2 = 0
Xhijau2 = 1
Ind = 0

Cls

Merah1 = 0
Kuning1 = 0
Hijau1 = 0


Do
Locate 1 , 1
Lcd "waktu hijau "
Locate 2 , 1
Lcd "detik = " ; Hijau1

If Tombolup = 0 Then
Incr Hijau1
Waitms 100
End If

If Tomboldown = 0 Then
Decr Hijau1
If Hijau1 > 222 Then
Hijau1 = 0
End If
Waitms 100
End If

If Tombolselect = 0 Then
Waitms 100
Bitwait Tombolselect , Set
Dethijau1 = Hijau1

Exit Do
End If
Loop


Cls
Do
Locate 1 , 1
Lcd "set jam "
Locate 2 , 1
Lcd "jam = " ; Xjam

If Tombolup = 0 Then
Incr Xjam
If Xjam > 23 Then
Xjam = 0
End If
Waitms 100
End If

If Tomboldown = 0 Then
Decr Xjam
If Xjam > 200 Or Xjam < 0 Then
Xjam = 0
End If
Waitms 100
End If

If Tombolselect = 0 Then
Waitms 100
Bitwait Tombolselect , Set
Ok_jam = Xjam
_hour = Ok_jam
Exit Do
End If
Loop

Do
Locate 1 , 1
Lcd "set menit "
Locate 2 , 1
Lcd "menit = " ; Xmenit

If Tombolup = 0 Then
Incr Xmenit
If Xmenit > 59 Then
Xmenit = 0
End If
Waitms 100
End If

If Tomboldown = 0 Then
Decr Xmenit
If Xmenit > 200 Or Xmenit < 0 Then
Xmenit = 0
End If
Waitms 100
End If

If Tombolselect = 0 Then
Waitms 100
Bitwait Tombolselect , Set
Ok_menit = Xmenit
_min = Ok_menit
_sec = 0
Settime
Goto Main
Exit Do
End If
Loop
Return






Flashing:

Cls

Do

Getdatetime
Upperline
Dispdate
Lowerline
Disptime

Xmerah1 = 1
Lampmer1 = 1
Xkuning1 = 0
Lampkun1 = 0
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 1
Lampmer2 = 1
Xkuning2 = 0
Lampkun2 = 0
Xhijau2 = 1
Lamphij2 = 1

Waitms 500

Xmerah1 = 1
Lampmer1 = 1
Xkuning1 = 1
Lampkun1 = 1
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 1
Lampmer2 = 1
Xkuning2 = 1
Lampkun2 = 1
Xhijau2 = 1
Lamphij2 = 1
Waitms 500

Loop Until _hour = 5
Goto Main
Return


Flashman:
Cls
Do
Upperline
Lcd "SISTEM OFF"

Xmerah1 = 1
Lampmer1 = 1
Xkuning1 = 0
Lampkun1 = 0
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 1
Lampmer2 = 1
Xkuning2 = 0
Lampkun2 = 0
Xhijau2 = 1
Lamphij2 = 1

Waitms 500

Xmerah1 = 1
Lampmer1 = 1
Xkuning1 = 1
Lampkun1 = 1
Xhijau1 = 1
Lamphij1 = 1
Xmerah2 = 1
Lampmer2 = 1
Xkuning2 = 1
Lampkun2 = 1
Xhijau2 = 1
Lamphij2 = 1

Waitms 500
Loop



*Note :
1. Delay hijau seperti apa yang di masukkan
2. Delay Kuning adalah 3 detik
3. Delay merah adalah delay hijau + 7 detik

*RULE :
1. Merah-merah
2. Merah-hijau
3. Merah Kuning
4. Merah-merah
5. Hijau-Merah
6. Kuning-Merah




No comments:

Post a Comment