Quantcast
Channel: MCS Electronics Forum
Viewing all articles
Browse latest Browse all 20867

BASCOM-AVR : measurement step in the ADXL345 : NEWTOPIC

$
0
0
I have a problem in measuring steps and distance at ADXL345 sensor, I do not have a reference for the measurement. if you can write a program for me? please i need help for my final project. This code determines the output value of x, y, z of the ADXL345 thanks :D [code:1:3c38389ef3]'=====[ Compiler Directives ]=================================================== $crystal = 12000000 $regfile = "m32def.dat" $baud = 9600 $hwstack = 64 $swstack = 64 $framesize = 64 '------------------------------------------------------------------------------- Config Lcdpin = Pin , Rs = Portc.5 , E = Portc.4 , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.7 , Db7 = Portc.6 Config Lcd = 16 * 2 Cursor Off , Noblink 'Const Adxl_r = &H3B 'Const Adxl_w = &H3A 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 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 '=====[ Global Vars & Constants ]=============================================== Dim Tick As Dword ' used for 100Hz tick count Dim Tickn As Dword ' used for next timer tick event Dim N1 As Integer , N2 As Integer , N3 As Integer ' temp var for visual positioning Dim Xg As Integer Dim Xgl As Byte At Xg + 0 Overlay Dim Xgh As Byte At Xg + 1 Overlay Dim Yg As Integer Dim Ygl As Byte At Yg + 0 Overlay Dim Ygh As Byte At Yg + 1 Overlay Dim Zg As Integer Dim Zgl As Byte At Zg + 0 Overlay Dim Zgh As Byte At Zg + 1 Overlay Dim Temp As Byte Dim Status As Byte Dim Test_gyro As Byte '' list of L3G4200D register positions 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 '------------------------------------------------------------------------------- '====[ ALAMAT HMC5883L ]======================================================== Const Hmc_w = &H3C Const Hmc_r = &H3D Const Mode_register_compass = &H02 Const Continous_register_mode = &H00 '------------------------------------------------------------------------------- '====[ 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 Dim Arah As String * 10 '------------------------------------------------------------------------------- '=====[ Setup Timer0 for Tick isr ]============================================= Config Timer2 = Timer , Prescale = 1024 ', Compare A= Disconnect, Clear Timer= 1 ' crystal / Prescale / CountWanted = reload value ' 18432000 / 1024 / 100Hz = 180 (trigger every 100th sec (10ms)) Ocr2 = 180 - 1 On Oc2 Tickcounter_isr Enable Oc2 '------------------------------------------------------------------------------- '=====[ I2C BUS INIT ]========================================================== 'configure the scl and sda pins $lib "I2C_TWI.LBX" ' uncomment for hardware I2C 'Config Scl = Portc.0 ' uncomment for software I2C 'Config Sda = Portc.1 ' uncomment for software I2C Config Twi = 400000 I2cinit '------------------------------------------------------------------------------- '=====[ Misc Initilization ]==================================================== Config Serialout = Buffered , Size = 254 'print "{027}[2J{027}[1;1H"; "Starting..." Enable Interrupts Cls Locate 1 , 1 Lcd "InitGyro: "; Waitms 500 Gosub Initgyro Gosub Initaccel Gosub Hmc_initialize '------------------------------------------------------------------------------- '=====[ Start of main loop ]==================================================== Cls Do If Tick >= Tickn Then Gosub Read_gyro Gosub Read_accel Gosub Hmc_readdata Gosub Hmc_readstatus ' konversi integer ke float X_comp = Hmc_x Y_comp = Hmc_y Z_comp = Hmc_z ' Hitung Sudut: ' angle = atan2((double)y,(double)x) * (180 / 3.14159265) + 180 Angle = Atn2(y_comp , X_comp) Angle = Angle * 57.29577951 ' (180 / 3.14159265) Angle = Angle + 180 Select Case Angle Case 0 To 45 : Arah = "UTARA" Case Is >= 315 : Arah = "UTARA" Case 45 To 135 : Arah = "BARAT" Case 135 To 225 : Arah = "SELATAN" Case 225 To 315 : Arah = "TIMUR" End Select N1 = Xg / 256 : N1 = N1 + 40 ' : Print "{027}[10;" ; N1 ; "H|"; N2 = Yg / 256 : N2 = N2 + 40 ': Print "{027}[11;" ; N2 ; "H|"; N3 = Zg / 256 : N3 = N3 + 40 ': Print "{027}[12;" ; N3 ; "H|"; Print "*" ; X ; "," ; Y ; "," ; Z ; "," ; N1 ; "," ; N2 ; "," ; N3 ; "," ; X_comp ; "," ; Y_comp ; "," ; Z_comp ; "," ; "BELUM" ; "," ; Arah ; "," ; Fusing(angle , "#.##") ; "^" '( Print "{027}[5;1H" ; "Count : " ; Tick ; "{027}[K" Print "{027}[K" Print "Status: " ; Bin(status) ; "{027}[K" Print "Temp : " ; Temp ; "{027}[K" Print "{027}[K" Print "{027}[10;1H"; Print "Gyro-X: " ; X ; " " '; "{027}[K" Print "Gyro-Y: " ; Y ; " " '; "{027}[K" Print "Gyro-Z: " ; Z ; " " '; "{027}[K" '' print visual position of values Print "{027}[10;20H[" ; : Print "{027}[10;60H]"; Print "{027}[11;20H[" ; : Print "{027}[11;60H]"; Print "{027}[12;20H[" ; : Print "{027}[12;60H]"; Locate 1 , 1 Lcd Xg ; " " ; Yg ; " " ; Zg ; " " Locate 2 , 1 Lcd Status ; " " ; Test_gyro ; " " N1 = Xg / 256 : N1 = N1 + 40 ' : Print "{027}[10;" ; N1 ; "H|"; N2 = Yg / 256 : N2 = N2 + 40 ': Print "{027}[11;" ; N2 ; "H|"; N3 = Zg / 256 : N3 = N3 + 40 ': Print "{027}[12;" ; N3 ; "H|"; Print "*" ; N1 ; "," ; N2 ; "," ; N3 ; "^" ') ' print "{027}[13;1H"; "{027}[J"; Tickn = Tickn + 20 'Waitms 50 End If Config Powermode = Idle Loop End '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Initaccel: I2cstart I2cwbyte Adxl_w I2cwbyte Data_format I2cwbyte &H03 'Enable x, y, z and turn off power down I2cstop Print "TEST" I2cstart I2cwbyte Adxl_w I2cwbyte Power_ctl I2cwbyte &H08 'Enable x, y, z and turn off power down I2cstop Return '------------------------------------------------------------------------------- '=====[ Read Accel Data ]======================================================== Read_accel: I2cstart I2cwbyte Adxl_w I2cwbyte Data_xl ' (OUT_TEMP AND &B10000000) I2cstart I2cwbyte Adxl_r ' I2crbyte Temp , Ack ' I2crbyte Status , Ack I2crbyte Xl , Ack I2crbyte Xh , Ack I2crbyte Yl , Ack I2crbyte Yh , Ack I2crbyte Zl , Ack I2crbyte Zh , Nack 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 ' Print "&B"; bin(Test_gyro); " [&h"; Hex(Test_Gyro); "]"; ' if test_gyro = 11010011 (D3) then connection is ok I2cstart I2cwbyte L3g4200d_w I2cwbyte Ctrl_reg1 I2cwbyte &B00000_1111 'Enable x, y, z and turn off power down 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 ' (OUT_TEMP AND &B10000000) I2cstart I2cwbyte L3g4200d_r I2crbyte Temp , Ack I2crbyte Status , Ack I2crbyte Xgl , Ack I2crbyte Xgh , Ack I2crbyte Ygl , Ack I2crbyte Ygh , Ack I2crbyte Zgl , Ack I2crbyte Zgh , 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 '-------------------------------------------------------------------------------[/code:1:3c38389ef3][/list][/quote] [b:3c38389ef3][color=red:3c38389ef3](BASCOM-AVR version : 2.0.7.6 )[/b:3c38389ef3][/color:3c38389ef3]

Viewing all articles
Browse latest Browse all 20867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>