Pada kesempatan yang berbahagia ini saya akan menjelaskan mengenai bagaimana cara membuat alat untuk mengetahui arah angin atau arah mata angin, sebenarnya prinsip kerjanya sangatlah sederhana yaitu petunjuk arah mata angin dibagi kedalam 4 buah warna, tiap warna mewakili satu sudut arah mata angin,misal warna hitam menunjukkan arah sudut 220 derajat, begitu pula dengan warna lainnya. sensor yang digunakan adalah sensor warna TCS230 dan sensor HMC5883L untuk magnetometernya. berikut penjelasan lebih lanjutnya.
a. Minimum System
b. Sensor TCS230
c. Sensor HMC5883L
d. Motor Stepper 5V + Driver ULN2003
e. Program Bascom AVR
$regfile = "m32def.dat"
$crystal = 12000000
$baud = 9600
$hwstack = 40
$swstack = 16
$framesize = 32
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
' **** inisialisasi penggunaan pin pada sensor warna
Config Portc = Output
Config Portd.6 = Output
Config Portd.2 = Output
Config Portd.3 = Output
Config Portd.4 = Output
Config Portd.5 = Output
Config Portd.7 = Input
Portd.7 = 1
Outhz Alias Pind.7
S2 Alias Portd.6
S3 Alias Portd.2
S0 Alias Portd.3
S1 Alias Portd.4
Oe Alias Portd.5
Ddrb.0 = 1
Ddrb.1 = 1
Ddrb.2 = 1
Ddrb.3 = 1
Ddra.0 = 0
Ddra.1 = 0
Ddrd.2 = 0
Porta.0 = 1
Porta.1 = 1
Portd.2 = 1
Tmbdown Alias Pina.1
Tmbok Alias Pind.2
Tmbup Alias Pina.0
'*** inisialisasi frequensi
Dim Tanda1detik As Bit
Dim Frequensi As Word
Dim Fmerah As Word
Dim Fhijau As Word
Dim Fbiru As Word
Dim Fputih As Word
Dim Merah As Integer , Hitam As Integer , Hijau As Integer , Orange As Integer
Dim Merahx As Eram Integer , Hitamx As Eram Integer , Hijaux As Eram Integer , Orangex As Eram Integer
Dim Merahs As Integer , Hitams As Integer , Hijaus As Integer , Oranges As Integer
Dim Merahsx As Eram Integer , Hitamsx As Eram Integer , Hijausx As Eram Integer , Orangesx As Eram Integer
Dim K As Integer
K = 5
Main:
Portc = 255
' inisialisasi penggunaan timer 1/4 detik untuk penghitung frekuensi
Config Timer1 = Timer , Prescale = 256
Const Inisial = &HAE80 '5740
On Timer1 Timer1_isr ' timer0 overflow interrupt
Tcnt1l = Low(inisial)
Tcnt1h = High(inisial)
Disable Timer1 ' aktifkan timer enable Ovf0
Enable Interrupts ' aktifkan semua interup
S0 = 0
S1 = 1
Oe = 0
'=====[ Global Vars & Constants ]========
Dim Hmc_x As Integer
Dim Hmc_xl As Byte At Hmc_x + 0 Overlay
Dim Hmc_xh As Byte At Hmc_x + 1 Overlay
Dim Hmc_y As Integer
Dim Hmc_yl As Byte At Hmc_y + 0 Overlay
Dim Hmc_yh As Byte At Hmc_y + 1 Overlay
Dim Hmc_z As Integer
Dim Hmc_zl As Byte At Hmc_z + 0 Overlay
Dim Hmc_zh As Byte At Hmc_z + 1 Overlay
Dim Hmc_status As Byte
Dim X As Single
Dim Y As Single
Dim Z As Single
Dim Angle As Single
Const Hmc_w = &H3C
Const Hmc_r = &H3D
'-------------------------------------------------------------------------------
'=====[ Misc. config ]==============
Config Scl = Portc.0
Config Sda = Portc.1
Config Twi = 400000
I2cinit
Gosub Hmc_initialize
'-------------------------------------------------------------------------------
Merah = Merahx
Merahs = Merahsx
Hitam = Hitamx
Hitams = Hitamsx
Hijau = Hijaux
Hijaus = Hijausx
Orange = Orangex
Oranges = Orangesx
'Merah = 220
'Merahs = 300
'Hitam = 100
'Hitams = 150
'Hijau = 150
'Hijaus = 220
'Orange = 300
'Oranges = 400
Do
'*** deteksi putih
S2 = 1
S3 = 0
Frequensi = 0
Tanda1detik = 0
Tcnt1l = Low(inisial)
Tcnt1h = High(inisial)
Enable Timer1
Do
Bitwait Outhz , Set
Bitwait Outhz , Reset
Loop Until Tanda1detik = 1
Disable Timer1 ' aktifkan timer enable Ovf0
'*** deteksi merah
S2 = 0
S3 = 0
Frequensi = 0
Tanda1detik = 0
Tcnt1l = Low(inisial)
Tcnt1h = High(inisial)
Enable Timer1
Do
Bitwait Outhz , Set
Bitwait Outhz , Reset
Frequensi = Frequensi + 1
Loop Until Tanda1detik = 1
Disable Timer1 ' aktifkan timer enable Ovf0
Fmerah = Frequensi
'*** deteksi biru
S2 = 0
S3 = 1
Frequensi = 0
Tanda1detik = 0
Tcnt1l = Low(inisial)
Tcnt1h = High(inisial)
Enable Timer1
Do
Bitwait Outhz , Set
Bitwait Outhz , Reset
Frequensi = Frequensi + 1
Loop Until Tanda1detik = 1
Disable Timer1 ' aktifkan timer enable Ovf0
Fbiru = Frequensi
'*** deteksi hijau
S2 = 1
S3 = 1
Frequensi = 0
Tanda1detik = 0
Tcnt1l = Low(inisial)
Tcnt1h = High(inisial)
Enable Timer1
Do
Bitwait Outhz , Set
Bitwait Outhz , Reset
Frequensi = Frequensi + 1
Loop Until Tanda1detik = 1
Disable Timer1 ' aktifkan timer enable Ovf0
Fhijau = Frequensi
'Lcd " R B G W "
Locate 2 , 1
Lcd Fmerah ; " "
Locate 2 , 5
Lcd Fbiru ; " "
Locate 2 , 9
Lcd Fhijau ; " "
Locate 2 , 13
Lcd Fputih ; " "
If Fmerah > Hitam And Fmerah < Hitams Then
Print "220"
Upperline
Lcd "warna hitam "
If K <> 0 Then
Cls
Goto Hitamzone
End If
Elseif Fmerah > Hijau And Fmerah < Hijaus Then
Print "120"
Upperline
Lcd "warna hijau "
If K <> 1 Then
Cls
Goto Hijauzone
End If
Elseif Fmerah > Orange And Fmerah < Oranges Then
Print "40"
Upperline
Lcd "warna orange "
If K <> 2 Then
Cls
Goto Orangezone
End If
Elseif Fmerah > Merah And Fmerah < Merahs Then
Print "300"
Upperline
Lcd "warna merah "
If K <> 3 Then
Cls
Goto Merahzone
End If
End If
Wait 1
If Tmbup = 0 Then
Goto Setting
End If
Cls
Loop
Timer1_isr: ' untuk 1 kali interupsi/detik
Tcnt1l = Low(inisial) ' inisialisasi spt ini
Tcnt1h = High(inisial)
Tanda1detik = 1
Toggle Portc.0
Return
End
Setting:
Cls
'setting warna hitam
Do
Locate 1 , 1
Lcd "SET HITAM BWH"
Locate 2 , 1
Lcd "hitam = " ; Hitam
If Tmbup = 0 Then
Incr Hitam
Waitms 100
Cls
End If
If Tmbdown = 0 Then
Decr Hitam
If Hitam < 1 Then
Hitam = 0
End If
Waitms 100
Cls
End If
If Tmbok = 0 Then
Waitms 100
Bitwait Tmbok , Set
Hitamx = Hitam
Hitam = Hitamx
Exit Do
End If
Loop
Cls
Do
Locate 1 , 1
Lcd "SET HITAM ATS"
Locate 2 , 1
Lcd "hitam = " ; Hitams
If Tmbup = 0 Then
Incr Hitams
Waitms 100
Cls
End If
If Tmbdown = 0 Then
Decr Hitams
If Hitams < 1 Then
Hitams = 0
End If
Waitms 100
Cls
End If
If Tmbok = 0 Then
Waitms 100
Bitwait Tmbok , Set
Hitamsx = Hitams
Hitams = Hitamsx
Exit Do
End If
Loop
Cls
'setting warna hijau
Do
Locate 1 , 1
Lcd "SET HIJAU BWH"
Locate 2 , 1
Lcd "hijau = " ; Hijau
If Tmbup = 0 Then
Incr Hijau
Waitms 100
Cls
End If
If Tmbdown = 0 Then
Decr Hijau
If Hijau < 1 Then
Hijau = 0
End If
Waitms 100
Cls
End If
If Tmbok = 0 Then
Waitms 100
Bitwait Tmbok , Set
Hijaux = Hijau
Hijau = Hijaux
Exit Do
End If
Loop
Cls
Do
Locate 1 , 1
Lcd "SET HIJAU ATS"
Locate 2 , 1
Lcd "hijau = " ; Hijaus
If Tmbup = 0 Then
Incr Hijaus
Waitms 100
Cls
End If
If Tmbdown = 0 Then
Decr Hijaus
If Hijaus < 1 Then
Hijaus = 0
End If
Waitms 100
Cls
End If
If Tmbok = 0 Then
Waitms 100
Bitwait Tmbok , Set
Hijausx = Hijaus
Hijaus = Hijausx
Exit Do
End If
Loop
Cls
'setting warna orange
Do
Locate 1 , 1
Lcd "SET ORANGE BWH"
Locate 2 , 1
Lcd "orange = " ; Orange
If Tmbup = 0 Then
Incr Orange
Waitms 100
Cls
End If
If Tmbdown = 0 Then
Decr Orange
If Orange < 1 Then
Orange = 0
End If
Waitms 100
Cls
End If
If Tmbok = 0 Then
Waitms 100
Bitwait Tmbok , Set
Orangex = Orange
Orange = Orangex
Exit Do
End If
Loop
Cls
Do
Locate 1 , 1
Lcd "SET ORANGE ATS"
Locate 2 , 1
Lcd "orange = " ; Oranges
If Tmbup = 0 Then
Incr Oranges
Waitms 100
Cls
End If
If Tmbdown = 0 Then
Decr Oranges
If Oranges < 1 Then
Oranges = 0
End If
Waitms 100
Cls
End If
If Tmbok = 0 Then
Waitms 100
Bitwait Tmbok , Set
Orangesx = Oranges
Oranges = Orangesx
Exit Do
End If
Loop
'setting warna merah
Do
Locate 1 , 1
Lcd "SET MERAH BWH"
Locate 2 , 1
Lcd "merah = " ; Merah
If Tmbup = 0 Then
Incr Merah
Waitms 100
Cls
End If
If Tmbdown = 0 Then
Decr Merah
If Merah < 1 Then
Merah = 0
End If
Waitms 100
Cls
End If
If Tmbok = 0 Then
Waitms 100
Bitwait Tmbok , Set
Merahx = Merah
Merah = Merahx
Exit Do
End If
Loop
Cls
Do
Locate 1 , 1
Lcd "SET MERAH ATS"
Locate 2 , 1
Lcd "merah = " ; Merahs
If Tmbup = 0 Then
Incr Merahs
Waitms 100
Cls
End If
If Tmbdown = 0 Then
Decr Merahs
If Merahs < 1 Then
Merahs = 0
End If
Waitms 100
Cls
End If
If Tmbok = 0 Then
Waitms 100
Bitwait Tmbok , Set
Merahsx = Merahs
Merahs = Merahsx
Exit Do
End If
Loop
Return
'hitam adc0 < 1000
'merah adc1 > 500
'orange adc2 < 520
'hijau adc3 > 511
Hitamzone:
Do
Portb = &B00000001
Waitms 3
Portb = &B00000010
Waitms 3
Portb = &B00000100
Waitms 3
Portb = &B00001000
Waitms 3
Gosub Hmc_readdata
Gosub Hmc_readstatus
' convert integer to float
X = Hmc_x
Y = Hmc_y
Z = Hmc_z
Angle = Atn2(y , X)
Angle = Angle * 57.29577951
Angle = Angle + 180
Loop Until Angle > 100 And Angle < 120
K = 0
Goto Main
Return
Hijauzone:
Do
Portb = &B00001000
Waitms 3
Portb = &B00000100
Waitms 3
Portb = &B00000010
Waitms 3
Portb = &B00000001
Waitms 3
Gosub Hmc_readdata
Gosub Hmc_readstatus
' convert integer to float
X = Hmc_x
Y = Hmc_y
Z = Hmc_z
Angle = Atn2(y , X)
Angle = Angle * 57.29577951
Angle = Angle + 180
Loop Until Angle > 220 And Angle < 240
K = 1
Goto Main
Return
Orangezone:
Do
Portb = &B00000001
Waitms 3
Portb = &B00000010
Waitms 3
Portb = &B00000100
Waitms 3
Portb = &B00001000
Waitms 3
Gosub Hmc_readdata
Gosub Hmc_readstatus
' convert integer to float
X = Hmc_x
Y = Hmc_y
Z = Hmc_z
Angle = Atn2(y , X)
Angle = Angle * 57.29577951
Angle = Angle + 180
Loop Until Angle > 310 And Angle < 330
K = 2
Goto Main
Return
Merahzone:
Do
Portb = &B00001000
Waitms 3
Portb = &B00000100
Waitms 3
Portb = &B00000010
Waitms 3
Portb = &B00000001
Waitms 3
Gosub Hmc_readdata
Gosub Hmc_readstatus
' convert integer to float
X = Hmc_x
Y = Hmc_y
Z = Hmc_z
Angle = Atn2(y , X)
Angle = Angle * 57.29577951
Angle = Angle + 180
Loop Until Angle > 60 And Angle < 70
K = 3
Goto Main
Return
'=====[ Initialize HMC Compass ]========
Hmc_initialize:
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H00
I2cwbyte &B0101_1000
I2cwbyte &H0010_0000
I2cwbyte &H0000_0000
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Read HMC Compass ]===========
Hmc_readstatus:
' Read HMC Compass status
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H09
I2cstart
I2cwbyte Hmc_r
I2crbyte Hmc_status , Nack
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Read HMC Compass ]=========
Hmc_readdata:
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H03
I2cstart
I2cwbyte Hmc_r
I2crbyte Hmc_xh , Ack
I2crbyte Hmc_xl , Ack
I2crbyte Hmc_zh , Ack
I2crbyte Hmc_zl , Ack
I2crbyte Hmc_yh , Ack
I2crbyte Hmc_yl , Nack
I2cstop
Return
'-------------------------------------------------------------------------------
f. VIDEO HASILNYA