Code:
'Microcontroller Einstellungen Definieren.
$lib "lcd_i2c.lib"
$regfile = "m32def.dat"
$framesize = 60 '32
$swstack = 60 ' 32
$hwstack = 60 '32
$crystal = 16000000
$baud = 9600
'Funktionen für Sensor:
Declare Sub Lsm303_init()
Declare Sub Acc_writereg(byval Breg As Byte , Byval Bval As Byte)
Declare Sub Acc_readsensordata() '-> iAx, iAy, iAz
Declare Sub Acc_calcgvalues() '-> sGx, sGy, sGz
Declare Sub Magnet_writerega(byval Dreg As Byte , Byval Dval As Byte)
Declare Sub Magnet_writeregb(byval Ereg As Byte , Byval Eval As Byte)
Declare Sub Magnet_writeregm(byval Mreg As Byte , Byval Mval As Byte)
Declare Sub Magnet_readsensordata()
Declare Sub Magnet_calcmvalues()
'Variablen für Beschleunigungs Sensor:
Const Acc_w_addr = &H30
Const Acc_r_addr = &H31
Const Ctrl_reg1_a = &H20
Const Ctrl_reg4_a = &H23
Const Out_x_l_a = &H28
Const Out_x_h_a = &H29
Const Out_y_l_a = &H2A
Const Out_y_h_a = &H2B
Const Out_z_l_a = &H2C
Const Out_z_h_a = &H2D
'Variablen für Magnetfeld Sensor :
Const Magnet_w_addr = &H3C
Const Magnet_r_addr = &H3D
Const Cra_reg_m = &H00
Const Crb_reg_m = &H01
Const Mr_reg_m = &H02
Const Out_x_h_m = &H03 ' Magnetfeld
Const Out_x_l_m = &H04
Const Out_y_h_m = &H05
Const Out_y_l_m = &H06
Const Out_z_h_m = &H07
Const Out_z_l_m = &H08
Const Res_2g = 0.0009765625 'Auflösung bei +- 2g
Dim Abaccdata(6) As Byte
Dim Abmagdata(6) As Byte
Dim Iax As Integer At Abaccdata(1) Overlay
Dim Iay As Integer At Abaccdata(3) Overlay
Dim Iaz As Integer At Abaccdata(5) Overlay
Dim Imx As Integer At Abmagdata(1) Overlay
Dim Imy As Integer At Abmagdata(3) Overlay
Dim Imz As Integer At Abmagdata(5) Overlay
Dim Sgx As Single 'Beschleunigungsvariablen
Dim Sgy As Single
Dim Sgz As Single
Dim Magnetx As Single 'Magnetfeldvariablen
Dim Magnety As Single
Dim Magnetz As Single
'****************************I2C##******************
Config Scl = Portc.0 'I2C SCL Pin
Config Sda = Portc.1 'I2C SDA Pin
Config I2cdelay = 20 '200 'I2C Bus Geschwindigkeit
I2cinit
Print "LSM303DLH Beschleunigungssensor und Magnometer"
Lsm303_init
Wait 2
Acc_readsensordata
Acc_calcgvalues
Magnet_readsensordata
Magnet_calcmvalues
Print "Beschleunigung: X: " ; Fusing(sgx , "#.##") ; " Y: " ; Fusing(sgy , "#.##") ; " Z: " ; Fusing(sgz , "#.##") 'Aus anderem Threat übernommen
Print "Magnetfeld: X: " ; Fusing(magnetx , "#.##") ; " Y: " ; Fusing(magnety , "#.##") ; " Z: " ; Fusing(magnetz , "#.##") 'Aus anderem Threat übernommen
Waitms 500
Loop
End
'LSM303DLH Sensor auslesen (Funktionen)
Sub Lsm303_init() 'Einstellungen definieren
Acc_writereg Ctrl_reg1_a , &H27 'normal power mode, 50 Hz data rate, all axes enabled
Magnet_writerega Cra_reg_m , &H14 'Output Rate 30Hz , Normal measurement configuration
Magnet_writeregb Crb_reg_m , &H20 'Gain setting 1,3 Gauss
Magnet_writeregm Mr_reg_m , &H00 ' magnetic sensor into continuous mode
End Sub
Sub Acc_writereg(byval Breg As Byte , Byval Bval As Byte) 'Einstellungen für Beschleunigung
I2cstart
I2cwbyte Acc_w_addr
I2cwbyte Breg
I2cwbyte Bval
I2cstop
End Sub
Sub Magnet_writerega(byval Dreg As Byte , Byval Dval As Byte) ' 1. Einstellungen für Magnet
I2cstart
I2cwbyte Magnet_w_addr
I2cwbyte Dreg
I2cwbyte Dval
End Sub
Sub Magnet_writeregb(byval Ereg As Byte , Byval Eval As Byte) ' 2. Einstellungen für Magnet
I2cstart
I2cwbyte Magnet_w_addr
I2cwbyte Ereg
I2cwbyte Eval
End Sub
Sub Magnet_writeregm(byval Mreg As Byte , Byval Mval As Byte) '3. Einstellung für Magnet
I2cstart
I2cwbyte Magnet_w_addr
I2cwbyte Mreg
I2cwbyte Mval
End Sub
Sub Acc_readsensordata()
I2cstart
I2cwbyte Acc_w_addr
I2cwbyte &HA8
I2cstart
I2cwbyte Acc_r_addr
I2crbyte Abaccdata(1) , Ack
I2crbyte Abaccdata(2) , Ack
I2crbyte Abaccdata(3) , Ack
I2crbyte Abaccdata(4) , Ack
I2crbyte Abaccdata(5) , Ack
I2crbyte Abaccdata(6) , Nack
I2cstop
Print "xxxx " ; Bin(abaccdata(1)) ; " xx " ; Bin(abaccdata(2))
Shift Iax , Right , 4 , Signed 'Aus anderem Threat übernommen
Shift Iay , Right , 4 , Signed 'Aus anderem Threat übernommen
Shift Iaz , Right , 4 , Signed 'Aus anderem Threat übernommen
End Sub
Sub Acc_calcgvalues()
Sgx = Iax * Res_2g 'Aus anderem Threat übernommen
Sgy = Iay * Res_2g 'Aus anderem Threat übernommen
Sgz = Iaz * Res_2g 'Aus anderem Threat übernommen
End Sub
Sub Magnet_readsensordata()
Dim Temp As Byte
I2cstart
I2cwbyte Magnet_w_addr
I2cwbyte Out_x_h_m
I2cstart
I2cwbyte Magnet_r_addr
I2crbyte Abmagdata(1) , Ack
I2crbyte Abmagdata(2) , Ack
I2crbyte Abmagdata(3) , Ack
I2crbyte Abmagdata(4) , Ack
I2crbyte Abmagdata(5) , Ack
I2crbyte Abmagdata(6) , Nack
I2cstop
End Sub
Sub Magnet_calcmvalues()
Magnetx = Imx
Magnety = Imy
Magnetz = Imz
End Sub
Lesezeichen