Pada pembahasan kali ini akan dijelaskan
mengenai bagaimana cara membuat alat yang dapat mengukur kemiringan benda, arah
hadap benda, percepatan gerak benda, tekanan dan temperatur, memang banyak sekali
data yang bisa diperoleh melalui sensor MPU GY-80 ini, karena sensor ini
terdiri dari 4 buah sensor, yaitu sensor accelerometer ADXL345, sensor
gyroscope L3G4200D, sensor tekanan dan temperatur BMP085 dan juga sensor kompas
/ magnetometer HMC5883L. Alat ini menggunakan ATMega32 sebagai pusat pengolah
data dari sensor sehingga data yang dikirim oleh sensor bisa diterjemahkan
dengan baik dan dapat dipahami oleh orang / pengguna.
Seperti pada Gambar 6.49a terdapat sebuah LCD
display 16x2 yang digunakan untuk menampilkan data yang didapat oleh sensor
yang sebelumnya telah diolah oleh mikrokontroller. Jika menginginkan tampilan
yang lebih baik bisa menggunakan ukuran LCD display yang lebih besar sehingga
bisa menampilkan banyak data. Alat ini juga dlengkapi sebuah tombol yang
berfungsi untuk mengganti tampilan yang ditampilkan pada layar LCD 16x2, karena
tempatnya terbatas dan data yang harus ditampilkan banyak.
Bentuk Sensor dan Keterangannya
Sensor
MPU GY-80 adaah sensor yang terdiri dari 4 sensor yang fungsinya berbeda-beda,
sensor tersebut yaitu sensor L3G4200D sebagai sensor gyroscope, sensor ADXL345
sebagai sensor accelerometer, sensor HMC5883L sebagai sensor kompas atau
magnetometer dan yang terakhir sensor BMP085 sebagai sensor barometer dan
temperatur, diambil dari datasheet bahwa sensor penyusun tersebut memiliki
karakteristik yang berbeda, berikut keterangannya.
1. Sensor
L3G4200D adalah sensor Gyroscope 3 axis 16 bit, memiliki 3 viriasi skala yang bisa
dipilih yaitu 250 , 500 , 2000 derajat per detik, sensor ini bekerja
optimal pada suhu -40oC sampai 85oC.
2. Sensor
ADXL345 adalah sensor accelerometer 3 axis 10-13 bit, dengan variasi range
pengukuran yaitu 2g, 4g, 8g dan 16g, sensor bekerja optimal pada suhu -40oC
sampai 85oC.
3. Sensor
HMC5883L adalah sensor kompas atau magnetometer 3 axis 12 bit, dengan range
yang direkomendasikan yaitu 0.88, 1.3, 1.9, 2.5, 4.0 , 4.7, 5.6, 8.1 gauss, sensor bekerja optimal pada suhu -30oC
sampai 85oC.
4. Sensor
BMP085 adalah sensor tekanan (16-19 bit) dan temperatur (16 bit) atau biasa
disebut barometer, yang memiliki tekanan range 300 sampai 1100hPa.
Cara menghubungkan sensor dengan mikrokontroller
yaitu seperti Gambar 6.49c, port yang digunakan hanya 5 yaitu vcc 5 volt, vcc
3,3 volt, gnd, SDA dan SCL
Program Bascom AVR
'menggunakan Bascom AVR versi 2.0.7.5$regfile = "m16def.dat"
$crystal = 12000000
$baud = 9600
$hwstack = 32
$swstack = 10
$framesize = 40
Config Lcdpin = Pin , Rs = Portc.2 , E = Portc.3 , Db4 = Portc.4
Config Lcdpin = Pin , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7
Config Lcd = 16 * 2
Ddrd.7 = 0
Set Portd.7
Dim Lop As Integer
Lop = 1
Cls
Cursor Off , Noblink
'=====[ I2C BUS INIT ]==========================================================
'configure the scl and sda pins
$lib "I2C_TWI.LBX"
Config Scl = Portc.0
Config Sda = Portc.1
Config Twi = 400000
I2cinit
'=====[ Setup Timer0 for Tick isr ]=============================================
Config Timer2 = Timer , Prescale = 1024
' crystal / Prescale / CountWanted = reload value
' 18432000 / 1024 / 100Hz = 180 (trigger every 100th sec (10ms))
Ocr2 = 180 - 1
On Oc2 Tickcounter_isr
Enable Oc2
'-------------------------------------------------------------------------------
'====[ ALAMAT ADXL345 ]=========================================================
Const Adxl_r = &HA7
Const Adxl_w = &HA6
Const Power_ctl = &H2D
Const Data_format = &H31
Const Data_xl = &H32
Const Data_xh = &H33
Const Data_yl = &H34
Const Data_yh = &H35
Const Data_zl = &H36
Const Data_zh = &H37
'-------------------------------------------------------------------------------
'====[ ALAMAT BMP085 ]==========================================================
Const Addr_schreiben = &B11101110
Const Addr_lesen = &B11101111
'-------------------------------------------------------------------------------
'====[ ALAMAT HMC5883L ]========================================================
Const Hmc_w = &H3C
Const Hmc_r = &H3D
Const Mode_register_compass = &H02
Const Continous_register_mode = &H00
'-------------------------------------------------------------------------------
'====[ ALAMAT L3G4200D ]========================================================
Const Who_am_i = &H0F
Const L3g4200d_r = &HD3
Const L3g4200d_w = &HD2
Const Ctrl_reg1 = &H20
Const Ctrl_reg2 = &H21
Const Ctrl_reg3 = &H22
Const Ctrl_reg4 = &H23
Const Ctrl_reg5 = &H24
'-------------------------------------------------------------------------------
'====[ Variabel ADXL345 ]=======================================================
Dim X As Integer
Dim Xl As Byte At X + 0 Overlay
Dim Xh As Byte At X + 1 Overlay
Dim Y As Integer
Dim Yl As Byte At Y + 0 Overlay
Dim Yh As Byte At Y + 1 Overlay
Dim Z As Integer
Dim Zl As Byte At Z + 0 Overlay
Dim Zh As Byte At Z + 1 Overlay
'-------------------------------------------------------------------------------
'====[ Variabel BMP085 ]========================================================
Dim Ac1 As Integer
Dim Ac1a As Long
Dim Ac2 As Integer
Dim Ac3 As Integer
Dim Ac4 As Word
Dim Ac5 As Word
Dim Ac5a As Single
Dim Ac6 As Word
Dim B1 As Integer
Dim B2 As Integer
Dim B3 As Long
Dim B3a As Long
Dim B4 As Long
Dim B5 As Long
Dim B6 As Integer
Dim B6a As Long
Dim B7 As Long
Dim Mb As Integer
Dim Mc As Integer
Dim Mc2 As Long
Dim Md As Integer
Dim Adres As Byte
Dim Wert As Byte
Dim Ut As Long
Dim X1 As Long
Dim X1t As Single
Dim X2 As Long
Dim X3 As Long
Dim T As Single
Dim T2 As Long
Dim Temperatur As String * 6
Dim Luftdruck As String * 8
Dim Druck As Single
Dim P As Long
Dim P2 As Long
Dim 2potenz15 As Long
Dim 2potenz11 As Integer
Dim 2potenz13 As Integer
Dim 2potenz8 As Integer
Dim 2potenz4 As Integer
Dim 2potenz2 As Integer
Dim 2potenz16 As Long
Dim 2potenz12 As Integer
Dim Potenz5255 As Single
Dim Altitude As Single
Dim Altitudes As String * 8
Dim Msb As Long
Dim Lsb As Long
Dim Up As Long
Dim Druckvar As Byte
Declare Sub Berechne_temp
Declare Sub Berechne_druck
Declare Sub Schreibe_bmp085(byval Adres As Byte , Byval Wert As Byte)
Declare Sub Lese_bmp085(byval Adres As Byte , Wert As Byte)
Declare Sub Lese_roh_daten
Declare Sub Messen
Declare Sub Lese_kalib_data
'====[ Variabel HMC5883L ]======================================================
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_comp As Single
Dim Y_comp As Single
Dim Z_comp As Single
Dim Angle As Single
'-------------------------------------------------------------------------------
'=====[ Variabel L3G4200D ]=====================================================
Dim Tick As Word ' used for 100Hz tick count
Dim Tickn As Word ' used for next timer tick event
Dim N As Integer ' temp var for visual positioning
Dim Xg As Integer
Dim Xlg As Byte At Xg + 0 Overlay
Dim Xhg As Byte At Xg + 1 Overlay
Dim Yg As Integer
Dim Ylg As Byte At Yg + 0 Overlay
Dim Yhg As Byte At Yg + 1 Overlay
Dim Zg As Integer
Dim Zlg As Byte At Zg + 0 Overlay
Dim Zhg As Byte At Zg + 1 Overlay
Dim Temp As Byte
Dim Status As Byte
Dim Test_gyro As Byte
Enable Interrupts
'-------------------------------------------------------------------------------
Cls
Locate 1 , 1
Lcd "Inisialisasi"
Locate 2 , 1
Lcd "ADXL345"
Wait 1
Gosub Initaccel
Cls
Locate 1 , 1
Lcd "Inisialisasi"
Locate 2 , 1
Lcd "BMP085"
Wait 1
Call Lese_kalib_data
Cls
Locate 1 , 1
Lcd "Inisialisasi"
Locate 2 , 1
Lcd "HMC5883L"
Wait 1
Gosub Hmc_initialize
Cls
Locate 1 , 1
Lcd "Inisialisasi"
Locate 2 , 1
Lcd "L3G4200D"
Wait 1
Gosub Initgyro
Cls
Do
If Pind.7 = 0 Then
Incr Lop
Cls
End If
If Lop = 7 Then
Lop = 1
Waitms 200
End If
Gosub Read_accel
Gosub Hmc_readdata
Gosub Hmc_readstatus
If Lop = 1 Then
Locate 1 , 1
Lcd "Accelerometer"
Locate 2 , 1
Lcd X ; " " ; Y ; " " ; Z ; " "
End If
Call Messen
T = T / 10
Temperatur = Fusing(t , "###.##")
Potenz5255 = 1 / 5.255
Altitude = P / 101325
Altitude = Altitude ^ Potenz5255
Altitude = 1 - Altitude
Altitude = Altitude * 44330
Druck = P
Druck = Druck / 100
Luftdruck = Fusing(druck , "####.##")
Altitudes = Fusing(altitude , "#####.##")
If Lop = 2 Then
Locate 1 , 1
Lcd "Temp= " ; Temperatur
Locate 2 , 1
Lcd "Tinggi= " ; Altitudes
End If
If Lop = 3 Then
Locate 1 , 1
Lcd "Pressure "
Locate 2 , 1
Lcd "press= " ; Luftdruck
End If
'Convert integer to float
X_comp = Hmc_x
Y_comp = Hmc_y
Z_comp = Hmc_z
'Calculate angle:
Angle = Atn2(y_comp , X_comp)
Angle = Angle * 57.29577951
Angle = Angle + 180
If Tick >= Tickn Then
Gosub Read_gyro
Xg = Xg / 256
Yg = Yg / 256
Zg = Zg / 256
If Lop = 4 Then
Locate 1 , 1
Lcd "Gyroscope"
Locate 2 , 1
Lcd "X=" ; Xg ; " " ; "Y=" ; Yg ; " " ; "Z=" ; Zg
End If
Tickn = Tickn + 20
End If
'Config Powermode = Idle
If Lop = 5 Then
Locate 1 , 1
Lcd "Magnet "
Locate 1 , 8
Lcd "x=" ; X_comp
Locate 2 , 1
Lcd "y=" ; Y_comp ; " " ; "z=" ; Z_comp
End If
If Lop = 6 Then
Locate 1 , 1
Lcd "magnetometer"
Locate 2 , 1
Lcd "Angle : " ; Fusing(angle , "#.#")
End If
Loop
'=====[ Inisialisasi BMP085]=========================================================
'=====[ Read the raw temp from the BMP085 ]=====================================
Sub Messen
'Temparatur messen
Call Schreibe_bmp085(&Hf4 , &H2E)
Adres = &HF6
Call Lese_roh_daten
'Print "msb " ; Msb
'Print "lsb " ; Lsb
Shift Msb , Left , 8
'Print "msb shift " ; Msb
Ut = Msb + Lsb
Call Berechne_temp
'Druck messen
Call Schreibe_bmp085(&Hf4 , &H74 )
Adres = &HF6
Call Lese_roh_daten
Shift Msb , Left , 16
Shift Lsb , Left , 8
Up = Msb + Lsb
Shift Up , Right , 6
'Print "msb " ; Msb ; "lsb " ; Lsb ; "up " ; Up
Call Berechne_druck
End Sub
Sub Berechne_temp
'X1
Ut = Ut - Ac6
2potenz15 = 2 ^ 15
Ac5a = Ac5 / 2potenz15
X1t = Ut * Ac5a
'Print "X1 " ; X1t
'X2
2potenz11 = 2 ^ 11
Mc2 = Mc * 2potenz11
X3 = X1t + Md
X2 = Mc2 / X3
'Print "X2 " ; X2
'B5
B5 = X1t + X2
'Print "B5 " ; B5
'T
2potenz4 = 2 ^ 4
T2 = B5 + 8
T = T2 / 2potenz4
End Sub
Sub Berechne_druck
'B6
B6 = B5 - 4000
'X1
2potenz12 = 2 ^ 12
B6a = B6 * B6
B6a = B6a / 2potenz12
X1 = B2 * B6a
X1 = X1 / 2potenz11
'X2
X2 = Ac2 * B6
X2 = X2 / 2potenz11
'X3
X3 = X1 + X2
'B3
Ac1a = Ac1 * 4
Ac1a = Ac1a + X3
Shift Ac1a , Left , 2
Ac1a = Ac1a + 2
B3 = Ac1a / 4
'X1 die zweite
2potenz13 = 2 ^ 13
B6a = B6 / 2potenz13
X1 = Ac3 * B6a
'X2 die zweite
B6a = B6 * B6
B6a = B6a / 2potenz12
X2 = B1 * B6a
2potenz16 = 2 ^ 16
X2 = X2 / 2potenz16
'X3 die zweite
X3 = X1 + X2
X3 = X3 + 2
2potenz2 = 2 ^ 2
X3 = X3 / 2potenz2
'B4
B4 = X3 + 32768
B4 = B4 * Ac4
B4 = B4 / 2potenz15
If B4 <= 0 Then B4 = B4 * -1
'Print B4
'B7
B7 = Up - B3
B3a = 50000
Shift B3a , Right , 2
B7 = B7 * B3a
If B7 <= 0 Then B7 = B7 * -1
'Print B7
If B7 < &H80000000 Then
P = B7 * 2
'Print "p1 " ; P
P = P / B4
'Print "p1 " ; P
Else
P = B7 / B4
'Print "p2 " ; P
P = P * 2
'Print "p2 " ; P
End If
'Print P
'nochmal X1
2potenz8 = 2 ^ 8
P2 = P / 2potenz8
X1 = P2 * P2
'Print X1
' schon wieder x1 O.ó
X1 = X1 * 3038
X1 = X1 / 2potenz16
'Print X1
' nochmal X2
X2 = -7357 * P
X2 = X2 / 2potenz16
' endlich das finale!!! Der Druck
'Print X2
P2 = P
P = X2 + 3791
P = P + X1
P = P / 2potenz4
P = P + P2
End Sub
Sub Lese_kalib_data
Adres = &HAA
'Ac1 lesen
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
Shift Msb , Left , 8
Ac1 = Msb + Lsb
Adres = Adres + 1
'Ac2 lesen
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
Shift Msb , Left , 8
Ac2 = Msb + Lsb
Adres = Adres + 1
'Ac3 lesen
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
Shift Msb , Left , 8
Ac3 = Msb + Lsb
Adres = Adres + 1
'Ac4 lesen
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
Shift Msb , Left , 8
Ac4 = Msb + Lsb
Adres = Adres + 1
'Ac5 lesen
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
Shift Msb , Left , 8
Ac5 = Msb + Lsb
Adres = Adres + 1
'Ac6 lesen
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
Shift Msb , Left , 8
Ac6 = Msb + Lsb
Adres = Adres + 1
'B1 lesen
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
Shift Msb , Left , 8
B1 = Msb + Lsb
Adres = Adres + 1
'B2 lesen
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
Shift Msb , Left , 8
B2 = Msb + Lsb
Adres = Adres + 1
'MB lesen
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
Shift Msb , Left , 8
Mb = Msb + Lsb
Adres = Adres + 1
'MB lesen
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
Shift Msb , Left , 8
Mc = Msb + Lsb
Adres = Adres + 1
'Md lesen
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
Shift Msb , Left , 8
Md = Msb + Lsb
Adres = Adres + 1
End Sub
Sub Lese_roh_daten
Call Lese_bmp085(adres , Wert)
Msb = Wert
Adres = Adres + 1
Call Lese_bmp085(adres , Wert)
Lsb = Wert
End Sub
Sub Schreibe_bmp085(byval Adres As Byte , Byval Wert As Byte)
I2cstart
I2cwbyte Addr_schreiben
I2cwbyte Adres
I2cwbyte Wert
I2cstop
Waitms 10
End Sub
Sub Lese_bmp085(byval Adres As Byte , Wert As Byte)
I2cstart
I2cwbyte Addr_schreiben
I2cwbyte Adres
I2cstart
I2cwbyte Addr_lesen
I2crbyte Wert , Nack
I2cstop
Waitms 10
End Sub
Return
'--------------------------------------------------------------------------------
'=====[ Inisisalisasi Accelero ]================================================
Initaccel:
I2cstart
I2cwbyte Adxl_w
I2cwbyte Data_format
I2cwbyte &H03
I2cstop
I2cstart
I2cwbyte Adxl_w
I2cwbyte Power_ctl
I2cwbyte &H08
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Read Accel Data ]========================================================
Read_accel:
I2cstart
I2cwbyte Adxl_w
I2cwbyte Data_xl
I2cstart
I2cwbyte Adxl_r
I2crbyte Xl , Ack
I2crbyte Xh , Ack
I2crbyte Yl , Ack
I2crbyte Yh , Ack
I2crbyte Zl , Ack
I2crbyte Zh , Nack
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Initialize HMC Compass ]================================================
Hmc_initialize:
' Initialize free running mode for compass
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H02
I2cwbyte &H00 ' set register possition to 0
I2cwbyte &B0101_1000 ' set ConfRegA [4samples, 75Hz, normal]
I2cwbyte &H0010_0000 ' set ConfRegC [gain to default (1090)]
I2cwbyte &H0000_0000 ' set free running mode
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Read HMC Compass ]======================================================
Hmc_readstatus:
' Read HMC Compass status
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H09 ' set read position
I2cstart
I2cwbyte Hmc_r
I2crbyte Hmc_status , Nack ' read in status
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Read HMC Compass ]======================================================
Hmc_readdata:
' Read HMC Compass X, Y, Z integer values
I2cstart
I2cwbyte Hmc_w
I2cwbyte &H03 ' set read position
I2cstart
I2cwbyte Hmc_r
I2crbyte Hmc_xh , Ack ' read in high order x
I2crbyte Hmc_xl , Ack ' read in low order x
I2crbyte Hmc_zh , Ack ' read in high order z
I2crbyte Hmc_zl , Ack ' read in low order z
I2crbyte Hmc_yh , Ack ' read in high order y
I2crbyte Hmc_yl , Nack ' read in low order y
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Tick timer ISR ]========================================================
Tickcounter_isr:
Incr Tick
Return
'-------------------------------------------------------------------------------
'=====[ Initilize Gyro]=========================================================
Initgyro:
I2cstart
I2cwbyte L3g4200d_w
I2cwbyte Who_am_i
I2cstart
I2cwbyte L3g4200d_r
I2crbyte Test_gyro , Nack
I2cstop
I2cstart
I2cwbyte L3g4200d_w
I2cwbyte Ctrl_reg1
I2cwbyte &B00000_1111
I2cstop
I2cstart
I2cwbyte L3g4200d_w
I2cwbyte Ctrl_reg2
I2cwbyte &B0010_1001 'adjust/use the HPF
I2cstop
I2cstart
I2cwbyte L3g4200d_w
I2cwbyte Ctrl_reg3
I2cwbyte &B0000_1000
I2cstop
I2cstart
I2cwbyte L3g4200d_w
I2cwbyte Ctrl_reg4
' I2cwbyte &B0000_0000 'scale 250
' I2cwbyte &B0001_0000 'scale 500
I2cwbyte &B1011_0000 'scale 2000
I2cstop
I2cstart
I2cwbyte L3g4200d_w
I2cwbyte Ctrl_reg5
I2cwbyte &B0000_0000 'high-pass filtering of outputs
I2cstop
Return
'-------------------------------------------------------------------------------
'=====[ Read Gyro Data ]========================================================
Read_gyro:
I2cstart
I2cwbyte L3g4200d_w
I2cwbyte &B1010_0110
I2cstart
I2cwbyte L3g4200d_r
I2crbyte Temp , Ack
I2crbyte Status , Ack
I2crbyte Xlg , Ack
I2crbyte Xhg , Ack
I2crbyte Ylg , Ack
I2crbyte Yhg , Ack
I2crbyte Zlg , Ack
I2crbyte Zhg , Nack
I2cstop
Return
'-------------------------------------------------------------------------------
Cara Penggunaan Alat
Cara penggunaan alat ini yaitu menghidupkan minimum
sistem dan sensor dengan memberikan tegangan 9v atau 12v dc ke minimum sistem,
kemudian vcc 5 volt dan 3,3v ke sensor MPU GY-80 setelah itu LCD display akan
menyala dan akan menampilkan data yang didapat sensor, tekan tombol push button
untuk mengganti tampilan datanya.
VIDEO HASILNYA
VIDEO HASILNYA
master..klu pake 335
ReplyDelete