first, use wiringPi mudt download and install it
download wiringPI
git clone https://github.com/BPI-SINOVOIP/BPI-WiringPi.git -b BPI_M2
install wiringPI
cd BPI-WiringPi
chmod +x ./build
sudo ./build
main function: • int wiringPiI2CSetup (int devId) ; •intwiringPiI2CWriteReg8 (int fd, int reg, int data) ; •intwiringPiI2CWriteReg16 (int fd, int reg, int data) ; •int wiringPiI2CReadReg8(int fd, int reg) ; •intwiringPiI2CReadReg16 (int fd, int reg) ;
code:
#include <stdio.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
int MPU6050_ADD= 0x68;
int main(){
int fd;
short int ax,ay,az,tmp,gx,gy,gz;
printf("This is the MPU6050 test!\n");
wiringPiSetup();
pinMode(8,I2C_PIN);
pinMode(9,I2C_PIN);
if((fd=wiringPiI2CSetup(MPU6050_ADD))<0){
printf("can't find i2c!!\n");
return fd;
}
wiringPiI2CWriteReg8(fd,0x6B,00);
while(1){
ax=wiringPiI2CReadReg8(fd,0x3B)<<8|wiringPiI2CReadReg8(fd,0x3C);
ay=wiringPiI2CReadReg8(fd,0x3D)<<8|wiringPiI2CReadReg8(fd,0x3E);
az=wiringPiI2CReadReg8(fd,0x3F)<<8|wiringPiI2CReadReg8(fd,0x40);
gx=wiringPiI2CReadReg8(fd,0x43)<<8|wiringPiI2CReadReg8(fd,0x44);
gy=wiringPiI2CReadReg8(fd,0x45)<<8|wiringPiI2CReadReg8(fd,0x46);
gz=wiringPiI2CReadReg8(fd,0x47)<<8|wiringPiI2CReadReg8(fd,0x48);
printf("ax=%d ay=%d az=%d\n",ax,ay,az);
printf("gx=%d gy=%d gz=%d\n",gx,gy,gz);
delay(100);
}
return 0;
}