First, connect the microcontroller board MPU6050 circuit diagram, see: MPU6050 development - first knowledge Look at the four lines from the circuit diagram: power, GND, SCL, SDA. Connect to the microcontroller board. Part of the circuit diagram of the microcontroller: Thinking: Which I/O port should SCL and SDA be connected to? The STC89C52 does not have an integrated I2C controller and can only implement I2C functions through software simulation methods. Therefore, ordinary I/O ports can be implemented without fixing. You can use the two pins of the STC89C52 microcontroller as data and clock, and then write the module I2C code yourself. Then we define 51 MCU ports: //**************************************** Second, the test procedure //**************************************** // Update to MPU6050 by shinetop // MCU: STC89C52 // 2012.3.1 // Function: Display 10-bit raw data for accelerometers and gyroscopes //**************************************** / / Using the microcontroller STC89C52 // Crystal: 11.0592M // Display: serial port // Compile environment Keil uVision2 //**************************************** #include #include #include #include Typedef unsigned char uchar; Typedef unsigned short ushort; Typedef unsigned int uint; //**************************************** / / Define 51 microcontroller port //**************************************** Sbit SCL=P1^5; //IIC clock pin definition Sbit SDA=P1^4; //IIC data pin definition //**************************************** / / Define the MPU6050 internal address //**************************************** #define SMPLRT_DIV 0x19 //Gyro sampling rate, typical value: 0x07 (125Hz) #define CONFIG 0x1A //Low pass filter frequency, typical value: 0x06 (5Hz) #define GYRO_CONFIG 0x1B //Gyro self-test and measurement range, typical value: 0x18 (not self-test, 2000deg/s) #define ACCEL_CONFIG 0x1C // Accelerometer self-test, measurement range and high-pass filter frequency, typical value: 0x01 (not self-test, 2G, 5Hz) #define ACCEL_XOUT_H 0x3B #define ACCEL_XOUT_L 0x3C #define ACCEL_YOUT_H 0x3D #define ACCEL_YOUT_L 0x3E #define ACCEL_ZOUT_H 0x3F #define ACCEL_ZOUT_L 0x40 #define TEMP_OUT_H 0x41 #define TEMP_OUT_L 0x42 #define GYRO_XOUT_H 0x43 #define GYRO_XOUT_L 0x44 #define GYRO_YOUT_H 0x45 #define GYRO_YOUT_L 0x46 #define GYRO_ZOUT_H 0x47 #define GYRO_ZOUT_L 0x48 #define PWR_MGMT_1 0x6B //Power management, typical value: 0x00 (normally enabled) #define WHO_AM_I 0x75 //IIC address register (default value 0x68, read only) #define SlaveAddress 0xD0 //Address byte data when IIC is written, +1 is read //************************************************ ************************************************** / / Define the type and variable //************************************************ ************************************************** Uchar dis[6]; //display a character array of numbers (-511 to 512) Int dis_data; //variable //************************************************ ************************************************** / / Function statement //************************************************ ************************************************** Void Delay5us(); Void delay(unsigned int k); //delay Void lcd_printf(uchar *s,int temp_data); //********************************MPU6050 operation function ************* ************************************** Void InitMPU6050(); //Initialize MPU6050 Void I2C_Start(); Void I2C_Stop(); Void I2C_SendACK(bit ack); Bit I2C_RecvACK(); Void I2C_SendByte(uchar dat); Uchar I2C_RecvByte(); Void I2C_ReadPage(); Void I2C_WritePage(); Void display_ACCEL_x(); Void display_ACCEL_y(); Void display_ACCEL_z(); Uchar Single_ReadI2C (uchar REG_Address); / / read I2C data Void Single_WriteI2C(uchar REG_Address, uchar REG_data); //Write data to I2C //************************************************ ******************************** //Integer to string //************************************************ ******************************** Void lcd_printf(uchar *s,int temp_data) { If(temp_data<0) { Temp_data=-temp_data; *s='-'; } Else *s=' '; *++s =temp_data/10000+0x30; Temp_data=temp_data%10000; //Remaining operation *++s =temp_data/1000+0x30; Temp_data=temp_data%1000; //Remaining operation *++s =temp_data/100+0x30; Temp_data=temp_data%100; //Remaining operation *++s =temp_data/10+0x30; Temp_data=temp_data%10; //Remaining operation *++s =temp_data+0x30; } Newmax Electronics Co.,Limited , https://www.advvape.com
//****************************************
Sbit SCL=P1^5;//IIC clock pin definition
Sbit SDA=P1^4;//IIC data pin definition