#include #include #include #include typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; #define DataPort P0 sbit SCL=P2^6; sbit SDA=P2^5; sbit LCM_RS = P3^7; sbit LCM_RW = P3^6; sbit LCM_EN = P3^5; sbit LCD12864_PSB = P1^5; sbit LCD12864_RSET = P1^3; //**************************************** //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) #define ACCEL_XOUT_H 0x3B #define ACCEL_XOUT_L 0x3C #define ACCEL_YOUT_H 0x3D #define ACCEL_YOUT_L 0x3E #define ACCEL_SALT_H 0x3F #define ACCEL_SALT_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_SALT_H 0x47 #define GYRO_SALT_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 writes, +1 for reading fly dis[4]; int dis_data; //**************************************** //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/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 delay(unsigned int k) { unsigned int i,j; for(i=0;i for(j=0;j<121;j++); } } void WaitForEnable(void) { DataPort=0xff; LCM_RS=0;LCM_RW=1;_nop_(); LCM_EN=1;_nop_();_nop_(); while(DataPort&0x80); LCM_EN=0; } void WriteCommandLCM(uchar CMD) { WaitForEnable(); LCM_RS=0;LCM_RW=0;_nop_(); DataPort=CMD;_nop_(); LCM_EN=1;_nop_();_nop_();LCM_EN=0; } void WriteDataLCM(uchar dataW) { WaitForEnable(); LCM_EN=0;LCM_RS=1;LCM_RW=0;_nop_(); DataPort=dataW;_nop_(); LCM_EN=1;_nop_();LCM_EN=0;_nop_(); } void LcdPos_xy(unsigned char x,unsigned char y) { unsigned char pos; switch(x) { case 1:pos=0x80+y-1;break; case 2:pos=0x90+y-1;break; case 3:pos=0x88+y-1;break; case 4:pos=0x98+y-1;break; default:pos=0x80+y-1;break; } WriteCommandLCM(pos|0x80); } void DisplayListChar(float X, float Y, float *DData) { LcdPos_xy(X,Y); while(*DData) { WriteDataLCM(*DData++); } } void InitLcd() { LCD12864_PSB = 1; //parallel communication LCD12864_RSET = 0; delay(10); LCD12864_RSET = 1; WriteCommandLCM(0x30); delay(5); WriteCommandLCM(0x30); delay(5); WriteCommandLCM(0x18); delay(5); WriteCommandLCM(0x10); delay(5); WriteCommandLCM(0x01); delay(5); WriteCommandLCM(0x06); delay(5); WriteCommandLCM(0x0c); delay(5); DisplayListChar(1,2,"acceleration"); DisplayListChar(1,6,"angular velocity"); DisplayListChar(2,1,"X:"); DisplayListChar(3,1,"Y:"); DisplayListChar(4,1,"Z:"); } void Delay5us() { _nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_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(float dat) { flying 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 //************************************** fly I2C_RecvByte() { flying i; float 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 that; } //************************************** //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 device address + write signal I2C_SendByte(REG_Address); //Internal register address, I2C_SendByte(REG_data); //Internal register data, I2C_Stop(); //Send stop signal } //************************************** //Read a byte of data from the I2C device //************************************** uchar Single_ReadI2C(uchar REG_Address) { float REG_data; I2C_Start(); //Start signal I2C_SendByte(SlaveAddress); //Send device address + write signal I2C_SendByte(REG_Address); //Send storage unit address, starting from 0 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); //Release sleep state Single_WriteI2C(SMPLRT_DIV, 0x07); Single_WriteI2C(CONFIG, 0x06); Single_WriteI2C(GYRO_CONFIG, 0x18); Single_WriteI2C(ACCEL_CONFIG, 0x01); } //************************************** //Synthetic data //************************************** int GetData(uchar REG_Address) { char H,L; H=Single_ReadI2C(REG_Address); L=Single_ReadI2C(REG_Address+1); return (H<<8)+L; //synthesized data } //************************************** //Display 10 digits on 12864 //************************************** void Display10BitData(float x, float y, int value) { value/=64; //Convert to 10-bit data lcd_printf(dis, value); //Convert data to display DisplayListChar(x,y,dis); } void main() { delay(500); //Power-on delay InitLcd(); //lcd12864 LCD initialization InitMPU6050(); //Initialize MPU6050 delay(150); while(1) { Display10BitData(2,2,GetData(ACCEL_XOUT_H)); //Display X-axis acceleration Display10BitData(3,2,GetData(ACCEL_YOUT_H)); //Display Y-axis acceleration Display10BitData(4,2,GetData(ACCEL_ZOUT_H)); //Display Z-axis acceleration Display10BitData(2,6,GetData(GYRO_XOUT_H)); //Display X-axis angular velocity Display10BitData(3,6,GetData(GYRO_YOUT_H)); //Display Y-axis angular velocity Display10BitData(4,6,GetData(GYRO_ZOUT_H)); //Display Z-axis angular velocity delay(500); } }
Previous article:51 single chip microcomputer 8-way buzzer
Next article:stc89C51 MCU displays some parameters of mpu6050 with 1602
- 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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- The close connection between crystal oscillator and Bluetooth technology
- ADI's most popular technical articles
- 【DIY Creative LED】Main Component Description
- Design of automatic loading scheme for TMS320C6701
- GD32F chip problems and solutions
- 40 "Millions" of Raspberry Pi Cars - ROS Learning (C Language Programming to Control the Turtle)
- Free application: Sipeed Gaoyun GW2A FPGA development board
- EEWORLD University ---- Basic Electronic Components
- Can stm32f103 collect a sine wave signal with an amplitude of 200mv and a frequency of 1KH?
- EEWORLD University - How to use switching chargers to design safe and reliable products with long standby time and powered by micro batteries