View previous topic :: View next topic |
Author |
Message |
freddie
Joined: 26 Sep 2010 Posts: 68
|
Posted: Sat Sep 24, 2011 9:43 pm Post subject: L3G4200D 3-axis gyroscope |
|
|
Hello! Have a problem with L3G4200D. Sensor connected with ATmega128A by I2C bus.
I read Who_am_i register correctly (get back 11010011)
But when i read axis i get "514" for all axis every time.
Please help.
"C" code is here: http://bildr.org/2011/06/l3g4200d-arduino/
Datasheet is here http://www.sparkfun.com/products/10612
Code: |
$regfile = "m128def.dat"
$crystal = 18432000
$hwstack = 100
$swstack = 100
$framesize = 100
$baud = 115200
Wait 5
'*****************************I2C BUS INIT****************************************
'configure the scl and sda pins
Config Sda = Porta.2
Config Scl = Porta.1
Config Twi = 400000
I2cinit
'*******************************L3G4200D (I2C)***********************************
Dim X As Integer , Y As Integer , Z As Integer
Dim Xh As Byte , Xl As Byte , Yh As Byte , Yl As Byte , Zh As Byte , Zl As Byte
Dim Test_gyro As Byte , Test_gyro2 As String * 8
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
Const Reference = &H25
Const Out_temp = &H26
Const Status_reg = &H27
Const Out_x_l = &H28
Const Out_x_h = &H29
Const Out_y_l = &H2A
Const Out_y_h = &H2B
Const Out_z_l = &H2C
Const Out_z_h = &H2D
Const Fifo_ctrl_reg = &H2E
Const Fifo_src_reg = &H2F
Const Int1_cfg = &H30
Const Int1_src = &H31
Const Int1_tsh_xh = &H32
Const Int1_tsh_xl = &H33
Const Int1_tsh_yh = &H34
Const Int1_tsh_yl = &H35
Const Int1_tsh_zh = &H36
Const Int1_tsh_zl = &H37
Const Int1_duration = &H38
I2cstart
I2cwbyte L3g4200d_w
I2cwbyte Who_am_i
Waitms 10
I2cstart
I2cwbyte L3g4200d_r
I2crbyte Test_gyro , Nack
I2cstop
Test_gyro2 = Bin(test_gyro)
Print Test_gyro2 ' if test_gyro = 11010011 (D3) then connection ok
I2cstart
I2cwbyte L3g4200d_w
I2cwbyte Ctrl_reg1
I2cwbyte &B10001111 'Enable x, y, z and turn off power down
I2cstop
Waitms 10
'I2cstart
'I2cwbyte L3g4200d_w
'I2cwbyte Ctrl_reg2
'I2cwbyte &B00000000 'adjust/use the HPF
'I2cstop
'Waitms 10
'I2cstart
'I2cwbyte L3g4200d_w
'I2cwbyte Ctrl_reg4
'I2cwbyte &B00000000 'scale 250
'I2cwbyte &B00010000 'scale 500
'I2cwbyte &B00110000 'scale 2000
'I2cstop
'Waitms 10
'I2cstart
'I2cwbyte L3g4200d_w
'I2cwbyte Ctrl_reg5
'I2cwbyte &B00000000 'high-pass filtering of outputs
'I2cstop
'Waitms 10
'********************************************************************************
Do
If Inkey() = 13 Then
Print "ready for upload, wait 5 sec"
Wait 5
Goto &HFC00
Elseif Inkey() = 49 Then
Print "start"
Gosub Work
End If
Waitms 100
Loop
End
Work:
Do
Gosub Read_l3g4200d
Print X ; " " ; Y ; " " ; Z
If Inkey() = 48 Then
Print "stop"
jmp 0
End If
Waitms 50
Loop
Return
Read_l3g4200d:
I2cstart
I2cwbyte L3g4200d_w
I2cwbyte &H28
'I2cstop
Waitms 10
I2cstart
I2cwbyte L3g4200d_r
I2crbyte Xl , Ack
I2crbyte Xh , Ack
I2crbyte Yl , Ack
I2crbyte Yh , Ack
I2crbyte Zl , Ack
I2crbyte Zh , Nack
'I2cstop
Waitms 10
X = Makeint(xl , Xh)
Y = Makeint(yl , Yh)
Z = Makeint(zl , Zh)
Return
|
|
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
Posted: Sun Sep 25, 2011 11:27 am Post subject: |
|
|
Hi,
From the datasheet;
Quote: |
In order to read multiple bytes, it is necessary to assert the most significant bit of the subaddress
field. In other words, SUB(7) must be equal to 1, while SUB(6-0) represents the
address of the first register to be read.
|
That will make something like this;
Code: |
Read_l3g4200d:
I2cstart
I2cwbyte L3g4200d_w
I2cwbyte &H28 + &B10000000 <-------
'I2cstop
Waitms 10
I2cstart
I2cwbyte L3g4200d_r
I2crbyte Xl , Ack
I2crbyte Xh , Ack
I2crbyte Yl , Ack
I2crbyte Yh , Ack
I2crbyte Zl , Ack
I2crbyte Zh , Nack
'I2cstop
Waitms 10
X = Makeint(xl , Xh)
Y = Makeint(yl , Yh)
Z = Makeint(zl , Zh)
Return
|
Have fun.
Ps I noticed that the arduino guy has the same problem, if it's working maybe you can notify him to. _________________ www.evertdekker.com Bascom code vault |
|
Back to top |
|
|
freddie
Joined: 26 Sep 2010 Posts: 68
|
Posted: Mon Sep 26, 2011 4:07 pm Post subject: |
|
|
Thanks for help Evert! I read first from &HA8 like you wrote and i think it`s right, but data still frozen. |
|
Back to top |
|
|
glena
Joined: 25 Jul 2007 Posts: 284 Location: PA near Philly
|
|
Back to top |
|
|
Tiny
Joined: 10 Nov 2010 Posts: 101 Location: The Netherlands
|
Posted: Sat Dec 31, 2011 12:33 pm Post subject: |
|
|
Hello,
I want to use an 3 axis gyro sensor, communication with the sensor all ready works
i can use 2 axis calculation with the command "atn2"
But does some one have an example for code to calculation the angle with tilt compensation.
regards Tiny |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Sun Jan 01, 2012 11:28 pm Post subject: |
|
|
Gyros measure the rate of change of rotation around different axes. From these, you can integrate to get angles of rotation. From these, you can tilt compensate something else, like eg a magnetic compass. If this is what you want, study carefully the code in AN 171 and the discussion in the AR7212 forum.
In fact, its much easier to tilt compensate using accelerometers rather than gyros, since than you dont have to worry about the gyro integration drift. You need gyros only in a dynamic platform like an aircraft. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
Adamescu23
Joined: 06 Jul 2014 Posts: 1
|
Posted: Sun Jul 06, 2014 8:58 am Post subject: ITG-3200 ATmega328p connected by I2C |
|
|
Hello! I have a problem with ITG-3200. I don`t know how to connect ATmega328p by I2C. |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Sun Jul 06, 2014 5:25 pm Post subject: |
|
|
Dear Adamescu23,
10 seconds with google I find the datasheet of your 3axis reader
Don't forget the R10K pull up to 3.3volt max for SDA et SCL
Bon courage
JP |
|
Back to top |
|
|
yeti
Joined: 06 Jul 2014 Posts: 1
|
Posted: Sun Jul 06, 2014 8:13 pm Post subject: Re: ITG-3200 ATmega328p connected by I2C |
|
|
Adamescu23 wrote: | Hello! I have a problem with ITG-3200. I don`t know how to connect ATmega328p by I2C. |
Hard or soft? |
|
Back to top |
|
|
|