CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

how to receive value from 2 mpu6050 ?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Saelee



Joined: 16 Oct 2016
Posts: 4

View user's profile Send private message Send e-mail AIM Address

how to receive value from 2 mpu6050 ?
PostPosted: Sat Oct 22, 2016 11:39 pm     Reply with quote

Hello everybody

Now I'm able to read acceleration from 1 mpu6050.

But I want to read acceleration from 2 mpu6050 simultaneously (or nearly).

I find about that in this forum and tried to modify my code.

Please suggest me.
Code:

#include <16f887.h>
#FUSES NOWDT                   
#FUSES NOBROWNOUT               
#FUSES NOLVP                   
#FUSES HS                       
#use    delay(clock=20MHz) 
#use I2C(stream=number1,master, sda=PIN_d0, scl=PIN_d1, fast=400000)
#use I2C(stream=number2,master, sda=PIN_d2, scl=PIN_d3, fast=400000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,errors)   

#include "MPU6050.C"
#include <math.h>

signed int8 A_data[6];
signed int16 Xa=0,Ya=0,Za=0;
signed int16 Xa2=0,Ya2=0,Za2=0; 
int8 count=0;

void main()
{
delay_ms(2);
mpu6050_init();   
printf("\f");
printf(" MPU6050 Gyro  \n ");
printf(" Accelerometer \n ");
delay_ms(3000);
printf("\f");

   while(TRUE)
   {
   A_data[0]=mpu6050_read(0x3B); //Read X axis(LSB)
   A_data[1]=mpu6050_read(0x3C); //Read X axis(MSB)
   A_data[2]=mpu6050_read(0x3D); //Read Y axis(LSB)
   A_data[3]=mpu6050_read(0x3E); //Read Y axis(MSB)
   A_data[4]=mpu6050_read(0x3F); //Read Z axis(LSB)
   A_data[5]=mpu6050_read(0x40); //Read Z axis(MSB)

   
   Xa=make16(A_data[0],A_data[1]);
   Ya=make16(A_data[2],A_data[3]);
   Za=make16(A_data[4],A_data[5]);

   
   Xa2=make16(A_data[0],A_data[1]);
   Ya2=make16(A_data[2],A_data[3]);
   Za2=make16(A_data[4],A_data[5]);
 
   count=count+1;
   if(count%2==0){
   i2c_write(number1);
   printf("********************************sensor1");
   printf("\n X:%ld \n ",Xa);
   printf("Y:%ld \n ",Ya);
   printf("Z:%ld \n ",Za);
   printf("********************************");
   }
   
   else{
   i2c_write(number2);
   printf("********************************sensor2");
   printf("\n X2:%ld \n ",Xa2);
   printf("Y2:%ld \n ",Ya2);
   printf("Z2:%ld \n ",Za2);
   printf("********************************");
   }
   delay_ms(100);
   }
}
temtronic



Joined: 01 Jul 2010
Posts: 9247
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Oct 23, 2016 5:26 am     Reply with quote

The 'easy' way is to create a function (first MPU6050 that accesses the 1st sensor. Once this works (you say your code does...) then simply COPY the function and give it a name like 'secondMPU6050()'. Remember, you'll need to change the address and setup parameters!
You may also need 2 'MPU_init()' functions as well.

Now main() will consist of

do ...
firstMPU6050();
secondMPU6050();
....
...
.while true()


Since each MPU6050 function is selfcontained it will 'cost' a little bit in memory space BUT most PICs have LOTS of space these days.
This method works fine for any number of sensors though after 6 -8 it becomes 'better' to rewrite the driver to accept a 'device,action' method.

Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
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


Powered by phpBB © 2001, 2005 phpBB Group