Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

MPU-6050 Not sending data

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
snow

Bascom Member



Joined: 28 Jun 2005
Posts: 200
Location: Ashburton / Mid Canterbury / New Zealand

newzealand.gif
PostPosted: Thu Dec 17, 2020 5:59 am    Post subject: MPU-6050 Not sending data Reply with quote

Hi
I've got 25 boards back from the assembly crowd. They have has a MPU-6050 Gyro/Accel on the board.
A fairly high amount of the boards, (11/25), The MPU-6050 chip is not sending the correct data back to the micro for processing. The AccelZ MEMS data and the Temperature data is stuck on 0.
I can communicate to the MPU-6050 correctly as I can request the ID address (&H75) and it replies with the correct data. I can also read the PWR_MGMT_1 register and it replies with the correct data.
I'm resetting the device as part of the INIT startup. I'm missing something obviously simple, as I can get other boards running correctly with the same code. Is there a factory setting that I'm missing that need to be added..



Code:


$regfile = "m328pbdef.dat"                                     'Field=i Board
$crystal = 16000000                                            'used crystal 16MHZ frequency
$baud = 9600                                                   'use baud rate
$hwstack = 64
$swstack = 128
$framesize = 256
$version 1 , 0 , 182

'Config Serialin = Buffered , Size = 10

'SETUP CONSTANTS
'***************
Const Compiler = "Bascom 2.0.8.2"                            'With Local CONSTANS
Const _valcheck = 1                                          'Set ERR=1 whenever VAL() function encounters an illegal numeric string


'SYSTEM MATHS CONSTANTS (Don't modify)
'***************
Const On = 1
Const Off = 0
Const True = 1
Const False = 0
Const Pi = 3.1415926
Const Clockwise=0
Const Anticlockwise=1

'PROJECT CONSTANTS
'*****************
'----------list of MPY-6065 register positions----------
Const WHO_AM_I_MPU6050 = &H75                                 'Should reply &b110100 for the correct chip on the bus  GY-521 Module
Const MPU6050_R = &HD1                                        '1101000 Chip address if ADO pin is grounded + 1 for read bit  = 11010001 (hex D1)
Const MPU6050_W = &HD0                                        '1101000 Chip address if ADO pin is grounded + 0 for write bit = 11010000 (hex D0)
'-------------------------------------------------------
'*****************

'SETUP SUBROUTINES
'*****************
Declare Sub ReadAccel()
'*****************

'PIN MAPPING
'***********
Buzzer Alias PortE.0
'***********

 'CONFIGURE I²C bus
'*******************
Config Scl = Portc.5                                             'I²C clock pin
Config Sda = Portc.4                                             'I²C data bus pin
Config Twi = 400000                                              'I²C bus clock speed   400khz
I2cinit


'CONFIGURE PORTS
'*******************
 Config Buzzer = Output                 'Board buzzer
'*******************


'CONFIGURE ADC'S
'***************
'Config Adc = Single , Prescaler = Auto , Reference = Avcc
'Start Adc
'***************

'DIMENSION UP VARIABLES
'**********************

Dim AccelTemp as Integer
Dim L_AccelTemp as byte at AccelTemp + 0 overlay
Dim H_AccelTemp as byte at AccelTemp + 1 overlay

Dim Z_Accel as integer
Dim Zl_Accel as byte at Z_Accel + 0 overlay
Dim Zh_Accel as byte at Z_Accel + 1 overlay
Dim MPU6050_WhoAmIAddress as Byte


Dim Tick as byte
Dim RegisterData as byte

'SETUP REQUIRED TIMERS
'*********************
'*********************

'SET PORT STATE'S
'****************
'****************

'LOAD VARIABLES
'**************
Buzzer = off
'***************

'STARTUP
'*******
Enable Interrupts
Gosub InitMPU6050



'***************************************************************************************
'***************************  M A I N  *************************************************
'***************************************************************************************
Do

   ReadAccel

   If Z_Accel<1000 then    'If blelow threshold turn buzzer on
     Buzzer = on
     waitms 100
     buzzer = off
     waitms 100
   End If

   Print "Accel:";Z_Accel
   Print "Pcb Temp:" ; AccelTemp
   Print "Register &H6B:" ; RegisterData            ' Check to see MPU-6050 not stuck in reset
   Print ""
   Wait  1

 Loop

Sub ReadAccel()

   I2cstart
   I2cwbyte MPU6050_W
   I2cwbyte &H3F

   I2cstart
   I2cwbyte MPU6050_R
   I2crbyte  Zh_Accel , ack
   I2crbyte  Zl_Accel , ack

   I2crbyte  H_AccelTemp ,ack
   I2crbyte  L_AccelTemp , Nack
   I2cstop

   I2cstart
   I2cwbyte MPU6050_W
   I2cwbyte &H6B
   I2cstart
   I2cwbyte MPU6050_R
   I2crbyte RegisterData  , Nack
   I2cstop



End Sub


'=====[ Initilize MPU-6050 ]=========================================================
InitMPU6050:


   I2cstart                                  'Reset device
   I2cwbyte Mpu6050_w
   I2cwbyte &H6B
   I2cwbyte &b10000000
   I2cstop

   wait 1

   I2cstart
   I2cwbyte Mpu6050_w
   I2cwbyte &H6B
   I2cwbyte &b0000010                       'Turn on internal temperature reading and set Clock source
   I2cstop

   wait 1

   I2cstart                                  'Set the slowest sampling rate
   I2cwbyte Mpu6050_w
   I2cwbyte &H19
   I2cwbyte &B11111111


   I2cstart                   'Set full scale reading
   I2cwbyte Mpu6050_w
   I2cwbyte &H1C
   'I2cwbyte  &B000_00_000  '2g's
   'I2cwbyte  &B000_01_000  '4g's
   'I2cwbyte  &B000_10_000  '8g's
   I2cwbyte &B000_11_000  '16g's

    ' Read the WHO_AM_I register
    I2cstart
    I2cwbyte MPU6050_W
    I2cwbyte WHO_AM_I_MPU6050
    I2cstart
    I2cwbyte MPU6050_R
    I2crbyte MPU6050_WhoAmIAddress , Nack
    I2cstop

    Print "Accel Chip &H" ; Hex(MPU6050_WhoAmIAddress)        'If MPU6050_WhoAmIAddress = &H68 chip found

return

 


Regards Snow

(BASCOM-AVR version : 2.0.8.2 , Latest : 2.0.8.3 )
Back to top
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Thu Dec 17, 2020 1:57 pm    Post subject: Reply with quote

Hi Snow,

My first reply as electronics engineer is what about voltage levels.
The MPU6050 runs at 3.3V and the M328PB needs at 16Mhz >4.5V.
Do you have a good level convertor for the i2c bus?

_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
snow

Bascom Member



Joined: 28 Jun 2005
Posts: 200
Location: Ashburton / Mid Canterbury / New Zealand

newzealand.gif
PostPosted: Thu Dec 17, 2020 8:18 pm    Post subject: Reply with quote

Hi
I'm dropping to 3.3 from 5v with a XC6206P332MR 3.3 volt reg. I've probed it and the voltage seem pretty good 3.305v . On the I^C bus ,I'm using 4.7k pulled up's to 5v.
Just looking on the schematics of a GY521 module that I have been using for the past 2 years on this project. There pull ups are tied to the 3.3 volt line. Maybe that's where my mistake it.??? I'm running a DAC on the same bus that works fine with COMM's etc. , but they are rated to 5v-3.3v by the look of the data sheet.
I can communicate with the MPU-6050 fine... get the Who am I ID , set and re-read the user power registers to make sure they have changed etc..

Maybe it is my pullups?? Didn't really give it much thought as I wouldn't think it wouldn't be an issue (Famous last words)

Cheers Snow
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Fri Dec 18, 2020 9:16 pm    Post subject: Reply with quote

i kind of miss this :
$lib "I2C_TWI-MULTI.lib" 'important for using 2 TWI interfaces

or the normal i2c_twi lib reference.

check out the samples\chips\m328PB.bas sample

when having i2c probs : check the ERR variable after the i2c statement. that will give an indication.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR 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