//51 MCU reads the serial port MPU6050 module example program
//testing platform:
//51 MCU development board YL-39, chip STC89C52
//Notice:
// The 1.51 microcontroller has only one download serial port, and it needs to be connected to MPU6050 at the same time. Therefore, you need to unplug the TX line of MPU6050 when downloading, and then plug it in after the program is successfully downloaded.
//Wiring method:
// 51 single chip microcomputer JY901 module
// +5V ---- VCC
//TX (not connected) RX
// RX ---- TX
// GND ---- GND
///////////////////////////////////////////////////////
/*Preprocessing commands*/
#include #include #include "JY901.h" void delay_ms(unsigned short i) { unsigned short k; while(i--) for (k=0;k<100;k++); } void main(void) { unsigned char i=0; TMOD=0x20; //Use the timer to set the serial port baud rate to 9600 TH1=0xfd; TL1=0xfd; TR1=1; TI=1; REN=1; //Serial port initialization SM0=0; SM1=1; EA=1; // Enable general interrupt ES=1; printf("STC89S52 Read JY901 module demorn"); printf("-------------BY:JYZK-------------rn"); printf("---http://RobotControl.taobao.com---rn"); while(1) { delay_ms(10); printf("Time:20%d-%d-%d %d:%d:%.3frn",(short)stcTime.ucYear,(short)stcTime.ucMonth, (short)stcTime.ucDay, (short)stcTime.ucHour, (short)stcTime.ucMinute, (float)stcTime.ucSecond+(float)stcTime.usMiliSecond/1000); printf("Acc:%.3f%.3f%.3frn",(float)stcAcc.a[0]/32768*16,(float)stcAcc.a[1]/32768*16,(float)stcAcc.a[2]/32768*16); printf("Gyro:%.3f%.3f%.3frn",(float)stcGyro.w[0]/32768*2000,(float)stcGyro.w[1]/32768*2000,(float)stcGyro.w[2]/32768*2000); printf("Angle:%.3f%.3f%.3frn",(float)stcAngle.Angle[0]/32768*180,(float)stcAngle.Angle[1]/32768*180,(float)stcAngle.Angle[2]/32768*180); printf("Mag:%d %d %drn",stcMag.h[0],stcMag.h[1],stcMag.h[2]); printf("Pressure:%lx Height%.2frn",stcPress.lPressure,(float)stcPress.lAltitude/100); printf("DStatus:%d %d %d %drn",stcDStatus.sDStatus[0],stcDStatus.sDStatus[1],stcDStatus.sDStatus[2],stcDStatus.sDStatus[3]); printf("Longitude:%ldDeg%.5fm Lattitude:%ldDeg%.5fmrn",stcLonLat.lLon/10000000,(double)(stcLonLat.lLon % 10000000)/1e5,stcLonLat.lLat/10000000,(double)(stcLonLat.lLat % 10000000)/1e5); printf("GPSHeight:%.1fm GPSYaw:%.1fDeg GPSV:%.3fkm/hrnrn",(float)stcGPSV.sGPSHeight/10,(float)stcGPSV.sGPSYaw/10,(float)stcGPSV.lGPSVelocity/1000); } } void ser() interrupt 4 { if (RI) { RI=0; CopeSerialData(SBUF); } } Copy code #include #include "JY901.h" struct STime stcTime={0}; struct SAcc stcAcc={0}; struct SGyro stcGyro={0}; struct SAngle stcAngle={0}; struct SMag stcMag={0}; struct SDStatus stcDStatus={0}; struct SPress stcPress={0}; struct SLonLat stcLonLat={0}; struct SGPSV stcGPSV={0}; void CharToLong(char Dest[],char Source[]) { *Dest = Source[3]; *(Dest+1) = Source[2]; *(Dest+2) = Source[1]; *(Dest+3) = Source[0]; } void CopeSerialData(unsigned char ucData) { static unsigned char ucRxBuffer[12]; static unsigned char ucRxCnt = 0; ucRxBuffer[ucRxCnt++]=ucData; if (ucRxBuffer[0]!=0x55) //The data header is incorrect, so restart the search for the 0x55 data header { ucRxCnt=0; return; } if (ucRxCnt<11) {return;}//If the data is less than 11, return else { switch(ucRxBuffer[1]) { case 0x50: stcTime.ucYear = ucRxBuffer[2]; stcTime.ucMonth = ucRxBuffer[3]; stcTime.ucDay = ucRxBuffer[4]; stcTime.ucHour = ucRxBuffer[5]; stcTime.ucMinute = ucRxBuffer[6]; stcTime.ucSecond = ucRxBuffer[7]; stcTime.usMiliSecond=((unsigned short)ucRxBuffer[9]<<8)|ucRxBuffer[8]; break; case 0x51: stcAcc.a[0] = ((unsigned short)ucRxBuffer[3]<<8)|ucRxBuffer[2]; stcAcc.a[1] = ((unsigned short)ucRxBuffer[5]<<8)|ucRxBuffer[4]; stcAcc.a[2] = ((unsigned short)ucRxBuffer[7]<<8)|ucRxBuffer[6]; break; case 0x52: stcGyro.w[0] = ((unsigned short)ucRxBuffer[3]<<8)|ucRxBuffer[2]; stcGyro.w[1] = ((unsigned short)ucRxBuffer[5]<<8)|ucRxBuffer[4]; stcGyro.w[2] = ((unsigned short)ucRxBuffer[7]<<8)|ucRxBuffer[6]; break; case 0x53: stcAngle.Angle[0] = ((unsigned short)ucRxBuffer[3]<<8)|ucRxBuffer[2]; stcAngle.Angle[1] = ((unsigned short)ucRxBuffer[5]<<8)|ucRxBuffer[4]; stcAngle.Angle[2] = ((unsigned short)ucRxBuffer[7]<<8)|ucRxBuffer[6]; stcAngle.T = ((unsigned short)ucRxBuffer[9]<<8)|ucRxBuffer[8]; break; case 0x54: stcMag.h[0] = ((unsigned short)ucRxBuffer[3]<<8)|ucRxBuffer[2]; stcMag.h[1] = ((unsigned short)ucRxBuffer[5]<<8)|ucRxBuffer[4]; stcMag.h[2] = ((unsigned short)ucRxBuffer[7]<<8)|ucRxBuffer[6]; stcAngle.T = ((unsigned short)ucRxBuffer[9]<<8)|ucRxBuffer[8]; break; case 0x55: stcDStatus.sDStatus[0] = ((unsigned short)ucRxBuffer[3]<<8)|ucRxBuffer[2]; stcDStatus.sDStatus[1] = ((unsigned short)ucRxBuffer[5]<<8)|ucRxBuffer[4]; stcDStatus.sDStatus[2] = ((unsigned short)ucRxBuffer[7]<<8)|ucRxBuffer[6]; stcDStatus.sDStatus[3] = ((unsigned short)ucRxBuffer[9]<<8)|ucRxBuffer[8]; break; case 0x56: ucRxBuffer[2] = 0x12;ucRxBuffer[3] = 0x34;ucRxBuffer[4] = 0x56;ucRxBuffer[5] = 0x78; CharToLong((char*)&stcPress.lPressure,(char*)&ucRxBuffer[2]); CharToLong((char*)&stcPress.lAltitude,(char*)&ucRxBuffer[6]); break; case 0x57: CharToLong((char*)&stcLonLat.lLon,(char*)&ucRxBuffer[2]); CharToLong((char*)&stcLonLat.lLat,(char*)&ucRxBuffer[6]); break; case 0x58: stcGPSV.sGPSHeight = ((unsigned short)ucRxBuffer[3]<<8)|ucRxBuffer[2]; stcGPSV.sGPSYaw = ((unsigned short)ucRxBuffer[5]<<8)|ucRxBuffer[4]; CharToLong((char*)&stcGPSV.lGPSVelocity,(char*)&ucRxBuffer[6]); break; } ucRxCnt=0; } }
Previous article:MCU MQ-2 smoke detection + ADC0809 AD conversion + lcd1602 display program
Next article:Design of DS18B20 temperature control system based on 51 single chip microcomputer
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Discussion on the Reliability of Electromechanical Products
- About MSP430 interrupt mechanism
- Advantages of LXI-Based Instrumentation for Physical Measurements
- How to set Keil compilation file not to compile a certain group file
- The MSP430 I2C bus problem was solved today
- EEWORLD University ---- Black Gold ZYNQ FPGA Video Tutorial
- Take part in the quiz with prizes and win the industry’s red book “Low-Level Manual”!
- 【NUCLEO-WL55JC2 Review】+ Learn about WL55JC2 and development board hardware
- Are there any good EDA design tool manufacturers in China?
- TI JTAG error message troubleshooting