Saelee
Joined: 16 Oct 2016 Posts: 4
|
how to receive value from 2 mpu6050 ? |
Posted: Sat Oct 22, 2016 11:39 pm |
|
|
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: 9249 Location: Greensville,Ontario
|
|
Posted: Sun Oct 23, 2016 5:26 am |
|
|
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 |
|