First, connect the microcontroller board. Refer to the MPU6050 circuit diagram in the "MPU6050 Development - First Knowledge" section. Look at the four lines from the circuit diagram: power, GND, SCL, and SDA. Connect these to the corresponding pins on the microcontroller board.
Here is a part of the microcontroller's circuit diagram:
Now, think about this: which I/O port should SCL and SDA be connected to?
The STC89C52 microcontroller does not have a built-in I2C controller, so it can only implement I2C communication through software simulation. Therefore, you can use any two general-purpose I/O pins for SCL and SDA. For example, we can define P1^5 as SCL and P1^4 as SDA.
Let’s define the ports on the 51 MCU:
//****************************************
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
// Date: 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 address of 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 the type and variable
//************************************************ **************************************************
uchar dis[6]; //Display a character array of numbers (-511 to 512)
int dis_data; //variable
//************************************************ **************************************************
// Function declaration
//************************************************ **************************************************
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;
}
19V Dc Adapter,19Volt Ac Dc Adapter,Interchangeable Plug 19V 3.42A Adapter,19V 3.42A Adapter
ShenZhen Yinghuiyuan Electronics Co.,Ltd , https://www.yhypoweradapter.com