First, connect the microcontroller board to the MPU6050 module. Refer to the circuit diagram of the MPU6050 for details. You can find it in the section titled "MPU6050 Development - First Knowledge".
Look at the four lines shown in the circuit diagram: power, GND, SCL, and SDA. These are the essential connections you need to make between the MPU6050 and the microcontroller board.
Here is a part of the microcontroller's circuit diagram:
Now, think about this: which I/O ports should the SCL and SDA lines be connected to?
The STC89C52 microcontroller does not have an integrated I2C controller, so you will need to implement I2C communication through software simulation. This means that any two general-purpose I/O pins can be used for SCL and SDA. You just need to define them in your code and write the necessary I2C functions yourself.
We can define the I/O ports on the 51 MCU as follows:
//****************************************
sbit SCL = P1^5; //IIC clock pin definition
sbit SDA = P1^4; //IIC data pin definition
//****************************************
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 internal addresses of the MPU6050
//****************************************
#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 types and variables
//************************************************ **************************************************
uchar dis[6]; //Display a character array for numbers (-511 to 512)
int dis_data; //Variable
//************************************************ **************************************************
// Function declarations
//************************************************ **************************************************
void Delay5us();
void delay(unsigned int k); //Delay function
void lcd_printf(uchar *s, int temp_data);
//********************************MPU6050 operation functions **************************************
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
//************************************************ ********************************
// Convert 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;
}
24V Dc Adapter,Adaptor Ac Dc 24V,24V Ac Dc Power Adapter,Adaptor Output 24V
ShenZhen Yinghuiyuan Electronics Co.,Ltd , https://www.yhypoweradapter.com