Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Code for Gyro reading

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-8051 code here
View previous topic :: View next topic  
Author Message
mastermindjp

Bascom Member



Joined: 18 Dec 2012
Posts: 51
Location: INDIA

india.gif
PostPosted: Wed Jul 15, 2015 9:40 am    Post subject: Code for Gyro reading Reply with quote

Hello all,
Here is a working code for reading Gyroscope, based on MPU6050, using AT89S52


$crystal = 11059200
$large
Config Scl = P2.2
Config Sda = P2.0
Dim I2ctemp As Integer
Dim Ba As Word
Config Lcdpin = Pin , Db4 = P1.4 , Db5 = P1.5 , Db6 = P1.6 , Db7 = P1.7 , E = P1.3 , Rs = P1.2
Const Mpu6050_wr = &HD0
Const Mpu6050_rd = &HD1
Dim T As Single , X As Word , S As Word , Count As Word , Clock As Word , T1 As Single
Main:
X = 0
S = 0
Clock = 0
T = 0
Waitms 200
Call Write_i2c
If I2ctemp < 100 Then X = X + 1
If I2ctemp > 110 And I2ctemp < 245 Then S = S + 1
Waitms 10
Clock = X + S
T = 500 / Clock
Cls
Lcd "SPEED = " ; T
Lowerline
Lcd "VALUE = " ; I2ctemp
Waitms 50
If T >= 100 Then P0.0 = 0
If T <= 99 Then P0.0 = 1
Waitms 200

Goto Main


Sub Write_i2c
I2cstart
I2cwbyte Mpu6050_wr
I2cwbyte &H6B
I2cwbyte 09
I2cstart
I2cwbyte Mpu6050_wr
I2cwbyte &H1B
I2cwbyte 3
I2cstart
I2cwbyte Mpu6050_wr
I2cwbyte &H19
I2cwbyte 0
I2cstart
I2cwbyte Mpu6050_wr
I2cwbyte &H47
I2cstop
Call Read_i2c
Waitms 10
End Sub


Sub Read_i2c
I2cstart
I2cwbyte Mpu6050_rd
I2crbyte I2ctemp , Nack
I2cstop
Waitms 10
Cls
Lowerline
Lcd "VALUE= " ; I2ctemp
End Sub


also, if code can be improved then kindly let me know...

thanks

regards
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4705
Location: Holland

blank.gif
PostPosted: Wed Jul 15, 2015 11:55 am    Post subject: Reply with quote

thank you for sharing. i would write goto main as a do..loop.

Code:

$crystal = 11059200
$large
Config Scl = P2.2
Config Sda = P2.0
Dim I2ctemp As Integer
Dim Ba As Word
Config Lcdpin = Pin , Db4 = P1.4 , Db5 = P1.5 , Db6 = P1.6 , Db7 = P1.7 , E = P1.3 , Rs = P1.2
Const Mpu6050_wr = &HD0
Const Mpu6050_rd = &HD1
Dim T As Single , X As Word , S As Word , Count As Word , Clock As Word , T1 As Single

Do
   X = 0
   S = 0
   Clock = 0
   T = 0
   Waitms 200
   Call Write_i2c
   If I2ctemp < 100 Then X = X + 1
   If I2ctemp > 110 And I2ctemp < 245 Then S = S + 1
   Waitms 10
   Clock = X + S
   T = 500 / Clock
   Cls
   Lcd "SPEED = " ; T
   Lowerline
   Lcd "VALUE = " ; I2ctemp
   Waitms 50
   If T >= 100 Then P0.0 = 0
   If T <= 99 Then P0.0 = 1
   Waitms 200
Loop

Sub Write_i2c
   I2cstart
   I2cwbyte Mpu6050_wr
   I2cwbyte &H6B
   I2cwbyte 09
   I2cstart
   I2cwbyte Mpu6050_wr
   I2cwbyte &H1B
   I2cwbyte 3
   I2cstart
   I2cwbyte Mpu6050_wr
   I2cwbyte &H19
   I2cwbyte 0
   I2cstart
   I2cwbyte Mpu6050_wr
   I2cwbyte &H47
   I2cstop
   Call Read_i2c
   Waitms 10
End Sub

Sub Read_i2c
   I2cstart
   I2cwbyte Mpu6050_rd
   I2crbyte I2ctemp , Nack
   I2cstop
   Waitms 10
   Cls
   Lowerline
   Lcd "VALUE= " ; I2ctemp
End Sub
 

_________________
Mark
Back to top
View user's profile Visit poster's website
mastermindjp

Bascom Member



Joined: 18 Dec 2012
Posts: 51
Location: INDIA

india.gif
PostPosted: Wed Jul 15, 2015 1:44 pm    Post subject: Reply with quote

Hello Mark,
Thank you.
Will keep that in mind for future projects...

Thanks Very Happy

Regards
Aniruddha
TechiMechi
Back to top
View user's profile Visit poster's website
Debu

Bascom Member



Joined: 06 Jan 2008
Posts: 21
Location: Dhaka

bangladesh.gif
PostPosted: Tue Sep 05, 2017 6:19 pm    Post subject: Reply with quote

Hallo all
I am trying this code on Xmega32A4u but I can't.
I use GY-88 module consist MPU6050 HMC5883L BMP085.
The HMC5883L and BMP085 work fine but the MPU6050 don't.
I was try separate code for HMC5883L and BMP085 they are working.
But this code doesn't work.
It is hang on <Print #1 , "Write_i2c :">

Can anybody help me ???? Please

(Sorry for my bad English)
Code:

'----------------------------------------------------------------

$regfile = "Xm32A4def.dat"
$crystal = 32000000
$hwstack = 84
$swstack = 80
$framesize = 80

$lib "xmega.lib"
$lib"i2c_twi.lbx"
$external _xmegafix_clear
$external _xmegafix_rol_r1014

Config Osc = Enabled , 32mhzosc = Enabled
Config Sysclock = 32mhz
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1  'Internal 32MHz, no prescaler
Config Submode = New
Config Priority = Static , Vector = Application , Lo = Enabled , Med = Enabled       ' the RTC uses LO priority interrupts

Config Lcdpin = Pin , Db4 = Porta.4 , Db5 = Porta.5 , Db6 = Porta.6 , Db7 = Porta.7 , E = Porta.2 , Rs = Porta.0
Config Lcd = 20x4 , Chipset = Dogm162v3

Cursor Off, NoBlink
Cls



Config Com1 = 19200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Config Input1 = Cr , Echo = Crlf                           ' CR is used for input, we echo back CR and LF

Open "COM1:" For Binary As #1


Cls
Dim Twi_start As Byte 'This Variable is used by the I2C functions
Open "twic" For Binary As #2
Config Twic = 100000 ' set the I2C bus to 'fast' speed
I2cinit #2


Declare Sub Write_i2c
Declare Sub Read_i2c
Dim I2ctemp As Integer
Dim Ba As Word
Const Mpu6050_wr = &HD0
Const Mpu6050_rd = &HD1
Dim T As Single , X1 As Word , S As Word , Count As Word , Clock As Word , T1 As Single
'-------------------------------------------------------------------------------
'*****[ Start of main loop ]****************************************************

Do
  Print #1 , "Main loop "
   X1 = 0
   S = 0
   Clock = 0
   T = 0
   Waitms 200
   Call Write_i2c
   If I2ctemp < 100 Then X1 = X1 + 1
   If I2ctemp > 110 And I2ctemp < 245 Then S = S + 1
   Waitms 10
   Clock = X1 + S
   T = 500 / Clock
   Cls
   Lcd "SPEED = " ; T
   Lowerline
   Lcd "VALUE = " ; I2ctemp
   Waitms 50
   'If T >= 100 Then P0.0 = 0
   'If T <= 99 Then P0.0 = 1
   Waitms 200
Loop

Sub Write_i2c
  Print #1 , "Write_i2c :"
   I2cstart
   I2cwbyte Mpu6050_wr
   I2cwbyte &H6B
   I2cwbyte 09
   I2cstart
   I2cwbyte Mpu6050_wr
   I2cwbyte &H1B
   I2cwbyte 3
   I2cstart
   I2cwbyte Mpu6050_wr
   I2cwbyte &H19
   I2cwbyte 0
   I2cstart
   I2cwbyte Mpu6050_wr
   I2cwbyte &H47
   I2cstop
   Print #1 , "Call Read_i2c :"
   Call Read_i2c
   Waitms 10
End Sub

Sub Read_i2c
Print #1 , "Read_i2c:"
   I2cstart
   I2cwbyte Mpu6050_rd
   I2crbyte I2ctemp , Nack
   I2cstop
   Waitms 10
   Cls
   Lowerline
   Lcd "VALUE= " ; I2ctemp
End Sub
Shocked Sad
Back to top
View user's profile AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-8051 code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum