The microcontroller source program is as follows:
#include #include #include #include #include "lcd1602.h" typedef unsigned short ushort; typedef unsigned int uint; //**************************************** #define DataPort P0 //LCD1602 data port sbit SCL=P2^1; //IIC clock pin definition sbit SDA=P2^0; //IIC data pin definition sbit LCM_RS=P3^5; //LCD1602 command port sbit LCM_EN=P3^4; //LCD1602 command port //**************************************** //Define the internal address of MPU6050 //**************************************** #define SMPLRT_DIV 0x19 //Gyroscope sampling rate, typical value: 0x07 (125Hz) #define CONFIG 0x1A //Low-pass filter frequency, typical value: 0x06 (5Hz) #define GYRO_CONFIG 0x1B //Gyroscope self-test and measurement range, typical value: 0x18 (no self-test, 2000deg/s) #define ACCEL_CONFIG 0x1C //Accelerometer self-test, measurement range and high-pass filter frequency, typical value: 0x01 (no self-test, 2G, 5Hz) 0x09 0x11 0x19 4G 8G 16G #define ACCEL_XOUT_H 0x3B #define ACCEL_XOUT_L 0x3C // Acceleration X-axis output #define ACCEL_YOUT_H 0x3D #define ACCEL_YOUT_L 0x3E //Acceleration Y axis output #define ACCEL_ZOUT_H 0x3F #define ACCEL_ZOUT_L 0x40 // Acceleration Z axis output #define TEMP_OUT_H 0x41 #define TEMP_OUT_L 0x42 #define GYRO_XOUT_H 0x43 #define GYRO_XOUT_L 0x44 // Angular velocity X-axis output #define GYRO_YOUT_H 0x45 #define GYRO_YOUT_L 0x46 // Angular velocity Y axis output #define GYRO_ZOUT_H 0x47 #define GYRO_ZOUT_L 0x48 // Angular velocity Z axis output #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 writes, +1 is for reading MPU I2C device address, slave address //**************************************** //Define types and variables //**************************************** uchar dis[6]; //Character array showing numbers (-511 to 512) int dis_data; //variable //int Temperature,Temp_h,Temp_l; //Temperature and high and low bit data //**************************************** // Function declaration //**************************************** void delay(unsigned int k); //delay //void lcd_printf(uchar *s,int temp_data); //MPU6050 operation function void InitMPU6050(); //Initialize MPU6050 void Delay5us(); 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 integer split and display on LCD screen //**************************************** 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; //Remainder operation *++s =temp_data/1000+0x30; temp_data=temp_data%1000; //Remainder operation *++s =temp_data/100+0x30; temp_data=temp_data%100; //Remainder operation *++s =temp_data/10+0x30; temp_data=temp_data%10; //Remainder operation *++s =temp_data+0x30; } //**************************************** void SeriPushSend(uchar send_data) { SBUF=send_data; // Send data while(!TI);TI=0; //Wait for data to be sent } //**************************************** //Delay //**************************************** void delay(unsigned int k) { unsigned int i,j; for(i=0;i for(j=0;j<121;j++); } } //************************************** //Delay 5 microseconds (STC90C52RC@12M) //TX-1C Tcy=1us //Different working environments require adjustment of this function //When using 1T MCU, please adjust this delay function //************************************** void Delay5us() { _nop_();_nop_();_nop_();_nop_(); _nop_(); } //************************************** //I2C start signal //************************************** void I2C_Start() { SDA = 1; //Pull up the data line SCL = 1; //Pull the clock line high Delay5us(); //Delay SDA = 0; //Generate falling edge Delay5us(); //Delay SCL = 0; //Pull the clock line low } //************************************** //I2C stop signal //************************************** void I2C_Stop() { SDA = 0; //Pull the data line low SCL = 1; //Pull the clock line high Delay5us(); //Delay SDA = 1; //Generate rising edge Delay5us(); //Delay } //************************************** //I2C sends response signal //Input parameter: ack (0:ACK 1:NAK) //************************************** void I2C_SendACK(bit ack) { SDA = ack; //Write response signal SCL = 1; //Pull the clock line high Delay5us(); //Delay SCL = 0; //Pull the clock line low Delay5us(); //Delay } //************************************** //I2C receives the response signal //************************************** bit I2C_RecvACK() { SCL = 1; //Pull the clock line high Delay5us(); //Delay CY = SDA; //Read response signal SCL = 0; //Pull the clock line low Delay5us(); //Delay return CY; } //************************************** //Send a byte of data to the I2C bus //************************************** void I2C_SendByte(uchar dat) { uchar i; for (i=0; i<8; i++) //8-bit counter { dat <<= 1; //Move out the highest bit of the data SDA = CY; //Send data port SCL = 1; //Pull the clock line high Delay5us(); //Delay SCL = 0; //Pull the clock line low Delay5us(); //Delay } I2C_RecvACK(); } //************************************** //Receive a byte of data from the I2C bus //************************************** uchar I2C_RecvByte() { uchar i; uchar dat = 0; SDA = 1; // Enable internal pull-up and prepare to read data. for (i=0; i<8; i++) //8-bit counter { dat <<= 1; SCL = 1; //Pull the clock line high Delay5us(); //Delay dat |= SDA; //read data SCL = 0; //Pull the clock line low Delay5us(); //Delay } return dat; } //************************************** //Write a byte of data to the I2C device //************************************** void Single_WriteI2C(uchar REG_Address,uchar REG_data) { I2C_Start(); //Start signal I2C_SendByte(SlaveAddress); //Send a specific I2C device address + write signal (not plus 1) I2C_SendByte(REG_Address); //Internal register address, I2C_SendByte(REG_data); //Internal register data, I2C_Stop(); //Send stop signal } //************************************** //Read a byte of data (8 bits) from the I2C device //************************************** uchar Single_ReadI2C(uchar REG_Address) { uchar REG_data; I2C_Start(); //Start signal I2C_SendByte(SlaveAddress); //Send device address + write signal I2C_SendByte(REG_Address); //Send the storage unit address, starting from 0 to find the specific address in the slave (MPU6050 specific internal storage addressing) I2C_Start(); //Start signal I2C_SendByte(SlaveAddress+1); //Send device address + read signal REG_data=I2C_RecvByte(); //Read register data I2C_SendACK(1); //Receive the response signal I2C_Stop(); //Stop signal return REG_data; } //************************************** //Initialize MPU6050 //************************************** void InitMPU6050() { Single_WriteI2C(PWR_MGMT_1, 0x00); // Power management, typical value: 0x00 (normally enabled) Release sleep state Single_WriteI2C(SMPLRT_DIV, 0x07); //Gyroscope sampling rate, typical value: 0x07 (125Hz) Single_WriteI2C(CONFIG, 0x06); //Low-pass filter frequency, typical value: 0x06 (5Hz) Single_WriteI2C(GYRO_CONFIG, 0x18); //Gyroscope self-test and measurement range, typical value: 0x18 (no self-test, 2000deg/s) Single_WriteI2C(ACCEL_CONFIG, 0x01); //Accelerometer self-test, measurement range and high-pass filter frequency, typical value: 0x01 (no self-test, 2G, 5Hz) 0x09 0x11 0x19 4G 8G 16G ……………………
Previous article:51 single chip microcomputer MPU6050 digital gyroscope and LCD12864 display
Next article:Speed measurement based on 51 single chip PWM speed regulation digital tube display
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sandia Labs develops battery failure early warning technology to detect battery failures faster
- Analysis of common defects, hazards and causes of circuit board welding
- Network port with a speed of 5G or above
- The origin of the BUCK circuit, with three evolutionary circuits
- DSP Learning (1) Introduction to Digital Signal Processors (DSPs)
- 【Portable Environmental Status Detector】RTC Electronic Clock
- Espressif's ESP32-C3 is an extremely low-power SoC equipped with a RISC-V 32-bit single-core processor
- Please teach me a resistance measurement circuit
- Why does the STACK MODE of almost all pads in ALTIUM DESIGNER change to FULL STACK?
- [Gizwits Gokit3 Review] + AirLink configuration (Arduino)
- Node voltage calculation