Pagi hari yang cerah ditemani oleh sejuknya angin dingin, seakan membuat diri ini terbang ke nirwana, kali ini saya akan menjelaskan mengenai bagaimana cara mengakses sensor magnetometer atau compass dengan menggunakan bahasa Bascom AVR, sensor ini adalah sensor yang sangat sering sekali dipakai untuk keperluan membaca rotasi atau arah hadap dari sensor, range output dari sensor ini adalah 0 derajat sampai 360 derajat maka dari itu jika menginginkan pebacaan rotasi penuh sangat dianjurkan untuk menggunakan sensor ini, misal untuk menentukan arah utara, selatan, timur dan barat, atau bisa juga untuk petunjuk arah mata angin dsb. Untuk lebih jelasnya berikut penjelasan lengkapnya.
a. Minimum System
b. Sensor GY-273 / HMC5883L
c. Program Bascom AVR
$regfile = "m16def.dat"
$crystal = 11059200$baud = 9600
$hwstack = 64
$swstack = 64
$framesize = 64
'-------------------------------------------------------------------------------
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
Main:
'=====[ 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 X1 As Single
Dim Y1 As Single
Dim Z1 As Single
Dim Angle1 As Single
Dim X2 As Single
Dim Y2 As Single
Dim Z2 As Single
Dim Angle2 As Single
Dim Jumlah As Integer
Dim W As Integer
W = 0
Jumlah = 0
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
'-------------------------------------------------------------------------------
'*****[ Start of main loop ]*******
Do
Gosub Hmc_readdata
Gosub Hmc_readstatus
' convert integer to float
X1 = Hmc_x
Y1 = Hmc_y
Z1 = Hmc_z
Angle1 = Atn2(y1 , X1)
Angle1 = Angle1 * 57.29577951
Angle1 = Angle1 + 180
'jika output sensor salah maka lakukan kalibrasi dengan regresi linier
'catat output sudut sensor dan sesuaikan dengan busur drajat
'catat di ms.Exel lalu tampilkan grafik
'pilih menu linier dan akan terlihat rumusnya
'maka akan tampil seperti rumus (misal) y = 0.971x - 20.62
'lalu masukkan kedalam program
'Angle1 = Angle1 + 20.62
'Angle1 = Angle1 / 0.971
If Angle1 > 30 And W = 0 Then
Incr Jumlah
W = 1
Waitms 200
End If
If Angle1 < 30 And W = 1 Then
W = 0
End If
Upperline
Lcd "sudut 1 = " ; Fusing(angle1 , "#.#")
Lowerline
Lcd "count = " ; Jumlah
Waitms 200
Cls
Loop
End
'-------------------------------------------------------------------------------
'=====[ 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
'-------------------------------------------------------------------------------
d. DEMO VIDEO
No comments:
Post a Comment