Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara mengakses sensor jarak dikombinasikan dengan motor servo, aplikasi dari alat ini yaitu sebagai palang pintu otomatis rel kereta api, jadi jika ada kereta api lewat, sensor jarak akan mendeteksi adanya benda, kemudian akan mengirimkan sinyal ke palang pintu agar ditutup secara otomatis. sensor jarak yang dipakai yaitu ada dua buah, satu sebagai detektor kepala kereta atau lokomotif, kemudian satunya lagi untuk mendeteksi ekor kereta atau gerbong terakhir kereta. untuk mengetahui lebih lanjut berikut adalah skema dan programnya.
a. Minimum System
b. Micro Servo
c. Sensor SRF04
d. Program Bascom AVR
Atas:
$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
Config Timer0 = Timer , Prescale = 256
Cls
Cursor Off
Config Portd.6 = Output
Config Pind.7 = Input
Declare Sub Cek_jarak
Dim Data_jarak As Word
Dim Data_jarak_olah As Word
Dim Data_jarak_fix As Word
Dim Mark As Integer
Mark = 1
Triger1 Alias Portd.6
Pecho1 Alias Pind.7
'buzzer
Ddrb.1 = 1
'led 1
Ddrb.2 = 1
Ddrb.3 = 1
Ddrb.4 = 1
'led 2
Ddrb.5 = 1
Ddrb.6 = 1
Ddrb.7 = 1
'led 3
Ddrd.0 = 1
Ddrd.1 = 1
Ddrd.2 = 1
'led 4
Ddrd.3 = 1
Ddrc.6 = 1
Ddrc.7 = 1
Config Portd.4 = Output
Config Pind.5 = Input
Declare Sub Cek_jarak1
Declare Sub Serv
Dim Data_jarak1 As Word
Dim Data_jarak_olah1 As Word
Dim Data_jarak_fix1 As Word
Triger2 Alias Portd.4
Pecho2 Alias Pind.5
Enable Interrupts
'kuning
Portb.3 = 0
Portb.6 = 0
Portd.1 = 0
Portc.6 = 0
'merah
Portb.2 = 1
Portb.5 = 0
Portd.0 = 0
Portd.3 = 1
'hijau
Portb.4 = 0
Portb.7 = 1
Portd.2 = 1
Portc.7 = 0
Cls
Cursor Off
Mulai:
Portb.1 = 0
Do
Call Cek_jarak
Data_jarak = Data_jarak
Data_jarak_olah = Data_jarak * 100
Data_jarak_fix = Data_jarak_olah / 256
Call Cek_jarak1
Data_jarak1 = Data_jarak1
Data_jarak_olah1 = Data_jarak1 * 100
Data_jarak_fix1 = Data_jarak_olah1 / 256
Upperline
Lcd Data_jarak_fix1 ; " cm "
Lowerline
Lcd Data_jarak_fix ; " cm "
If Data_jarak_fix1 < 10 And Data_jarak_fix > 10 Then
Portb.1 = 1
'kuning
Portb.3 = 0
Portb.6 = 0
Portd.1 = 0
Portc.6 = 0
'merah
Portb.2 = 0
Portb.5 = 0
Portd.0 = 0
Portd.3 = 0
'kuning
Portb.3 = 1
Portb.6 = 1
Portd.1 = 1
Portc.6 = 1
Wait 1
'kuning
Portb.3 = 0
Portb.6 = 0
Portd.1 = 0
Portc.6 = 0
'merah
Portb.2 = 0
Portb.5 = 1
Portd.0 = 1
Portd.3 = 0
'hijau
Portb.4 = 1
Portb.7 = 0
Portd.2 = 0
Portc.7 = 1
Goto Servtutup
End If
Loop
Sub Cek_jarak:
Reset Triger1
Waitus 10
Set Triger1
Waitus 20
Reset Triger1
Tcnt0 = 0
Bitwait Pecho1 , Set
Start Timer0
Do
If Pecho1 = 0 Then
Data_jarak = Tcnt0
Stop Timer0
Exit Do
End If
If Tifr.0 = 1 Then
Stop Timer0
Tifr.0 = 1
Data_jarak = &HFF
Exit Do
End If
Loop
Stop Timer0
Waitms 15
End Sub
Sub Cek_jarak1:
Reset Triger2
Waitus 10
Set Triger2
Waitus 20
Reset Triger2
Tcnt0 = 0
Bitwait Pecho2 , Set
Start Timer0
Do
If Pecho2 = 0 Then
Data_jarak1 = Tcnt0
Stop Timer0
Exit Do
End If
If Tifr.0 = 1 Then
Stop Timer0
Tifr.0 = 1
Data_jarak1 = &HFF
Exit Do
End If
Loop
Stop Timer0
Waitms 15
End Sub
Servtutup:
Config Servos = 1 , Servo1 = Portb.0 , Reload = 10
Config Portb.0 = Output
Do
If Mark = 1 Then
Servo(1) = 170
Waitms 500
Servo(1) = 160
Waitms 500
Servo(1) = 150
Waitms 500
Servo(1) = 140
Waitms 500
Servo(1) = 130
Waitms 500
Servo(1) = 120
Waitms 500
Servo(1) = 110
Waitms 500
Servo(1) = 100
Waitms 500
Goto Diam
End If
If Mark = 2 Then
Servo(1) = 100
Waitms 500
Servo(1) = 120
Waitms 500
Servo(1) = 130
Waitms 500
Servo(1) = 140
Waitms 500
Servo(1) = 150
Waitms 500
Servo(1) = 160
Waitms 500
Servo(1) = 170
Waitms 500
Goto Atas
End If
Loop
Diam:
Cls
Data_jarak_fix = 0
Data_jarak_fix1 = 0
Do
Call Cek_jarak
Data_jarak = Data_jarak
Data_jarak_olah = Data_jarak * 100
Data_jarak_fix = Data_jarak_olah / 256
Call Cek_jarak1
Data_jarak1 = Data_jarak1
Data_jarak_olah1 = Data_jarak1 * 100
Data_jarak_fix1 = Data_jarak_olah1 / 256
Upperline
Lcd Data_jarak_fix1 ; " cm "
Lowerline
Lcd Data_jarak_fix ; " cm "
If Data_jarak_fix1 > 10 And Data_jarak_fix > 10 Then
Mark = 2
Goto Servtutup
End If
Loop
$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
Config Timer0 = Timer , Prescale = 256
Cls
Cursor Off
Config Portd.6 = Output
Config Pind.7 = Input
Declare Sub Cek_jarak
Dim Data_jarak As Word
Dim Data_jarak_olah As Word
Dim Data_jarak_fix As Word
Dim Mark As Integer
Mark = 1
Triger1 Alias Portd.6
Pecho1 Alias Pind.7
'buzzer
Ddrb.1 = 1
'led 1
Ddrb.2 = 1
Ddrb.3 = 1
Ddrb.4 = 1
'led 2
Ddrb.5 = 1
Ddrb.6 = 1
Ddrb.7 = 1
'led 3
Ddrd.0 = 1
Ddrd.1 = 1
Ddrd.2 = 1
'led 4
Ddrd.3 = 1
Ddrc.6 = 1
Ddrc.7 = 1
Config Portd.4 = Output
Config Pind.5 = Input
Declare Sub Cek_jarak1
Declare Sub Serv
Dim Data_jarak1 As Word
Dim Data_jarak_olah1 As Word
Dim Data_jarak_fix1 As Word
Triger2 Alias Portd.4
Pecho2 Alias Pind.5
Enable Interrupts
'kuning
Portb.3 = 0
Portb.6 = 0
Portd.1 = 0
Portc.6 = 0
'merah
Portb.2 = 1
Portb.5 = 0
Portd.0 = 0
Portd.3 = 1
'hijau
Portb.4 = 0
Portb.7 = 1
Portd.2 = 1
Portc.7 = 0
Cls
Cursor Off
Mulai:
Portb.1 = 0
Do
Call Cek_jarak
Data_jarak = Data_jarak
Data_jarak_olah = Data_jarak * 100
Data_jarak_fix = Data_jarak_olah / 256
Call Cek_jarak1
Data_jarak1 = Data_jarak1
Data_jarak_olah1 = Data_jarak1 * 100
Data_jarak_fix1 = Data_jarak_olah1 / 256
Upperline
Lcd Data_jarak_fix1 ; " cm "
Lowerline
Lcd Data_jarak_fix ; " cm "
If Data_jarak_fix1 < 10 And Data_jarak_fix > 10 Then
Portb.1 = 1
'kuning
Portb.3 = 0
Portb.6 = 0
Portd.1 = 0
Portc.6 = 0
'merah
Portb.2 = 0
Portb.5 = 0
Portd.0 = 0
Portd.3 = 0
'kuning
Portb.3 = 1
Portb.6 = 1
Portd.1 = 1
Portc.6 = 1
Wait 1
'kuning
Portb.3 = 0
Portb.6 = 0
Portd.1 = 0
Portc.6 = 0
'merah
Portb.2 = 0
Portb.5 = 1
Portd.0 = 1
Portd.3 = 0
'hijau
Portb.4 = 1
Portb.7 = 0
Portd.2 = 0
Portc.7 = 1
Goto Servtutup
End If
Loop
Sub Cek_jarak:
Reset Triger1
Waitus 10
Set Triger1
Waitus 20
Reset Triger1
Tcnt0 = 0
Bitwait Pecho1 , Set
Start Timer0
Do
If Pecho1 = 0 Then
Data_jarak = Tcnt0
Stop Timer0
Exit Do
End If
If Tifr.0 = 1 Then
Stop Timer0
Tifr.0 = 1
Data_jarak = &HFF
Exit Do
End If
Loop
Stop Timer0
Waitms 15
End Sub
Sub Cek_jarak1:
Reset Triger2
Waitus 10
Set Triger2
Waitus 20
Reset Triger2
Tcnt0 = 0
Bitwait Pecho2 , Set
Start Timer0
Do
If Pecho2 = 0 Then
Data_jarak1 = Tcnt0
Stop Timer0
Exit Do
End If
If Tifr.0 = 1 Then
Stop Timer0
Tifr.0 = 1
Data_jarak1 = &HFF
Exit Do
End If
Loop
Stop Timer0
Waitms 15
End Sub
Servtutup:
Config Servos = 1 , Servo1 = Portb.0 , Reload = 10
Config Portb.0 = Output
Do
If Mark = 1 Then
Servo(1) = 170
Waitms 500
Servo(1) = 160
Waitms 500
Servo(1) = 150
Waitms 500
Servo(1) = 140
Waitms 500
Servo(1) = 130
Waitms 500
Servo(1) = 120
Waitms 500
Servo(1) = 110
Waitms 500
Servo(1) = 100
Waitms 500
Goto Diam
End If
If Mark = 2 Then
Servo(1) = 100
Waitms 500
Servo(1) = 120
Waitms 500
Servo(1) = 130
Waitms 500
Servo(1) = 140
Waitms 500
Servo(1) = 150
Waitms 500
Servo(1) = 160
Waitms 500
Servo(1) = 170
Waitms 500
Goto Atas
End If
Loop
Diam:
Cls
Data_jarak_fix = 0
Data_jarak_fix1 = 0
Do
Call Cek_jarak
Data_jarak = Data_jarak
Data_jarak_olah = Data_jarak * 100
Data_jarak_fix = Data_jarak_olah / 256
Call Cek_jarak1
Data_jarak1 = Data_jarak1
Data_jarak_olah1 = Data_jarak1 * 100
Data_jarak_fix1 = Data_jarak_olah1 / 256
Upperline
Lcd Data_jarak_fix1 ; " cm "
Lowerline
Lcd Data_jarak_fix ; " cm "
If Data_jarak_fix1 > 10 And Data_jarak_fix > 10 Then
Mark = 2
Goto Servtutup
End If
Loop
e. VIDEO HASILNYA
No comments:
Post a Comment