找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 18923|回复: 10
收起左侧

GY-25倾斜度角度模块 串口直接输出角度数据 MPU-6050模块 传感器

  [复制链接]
ID:124148 发表于 2016-5-31 01:25 | 显示全部楼层 |阅读模式
给大家点好东西  ,这个资料挺不错的,GY-25倾斜度角度模块 串口直接输出角度数据 MPU-6050模块 传感器.rar 就和大家分享下

0.png

51单片机程序:
  1. //  GY-25
  2. //  使用单片机:STC89C52
  3. //  晶振:  11.0592 M
  4. //  液晶屏:LCD1602
  5. //  波特率:9600
  6. #include<REG52.H>       
  7. #include<math.h>       //Keil library  
  8. #include<stdio.h>      //Keil library       
  9. #include<INTRINS.H>
  10. #define uchar unsigned char
  11. #define uint  unsigned int       
  12.        
  13. //********定义变量*****************************
  14. uchar   BUF[10];          //数据缓存区
  15. uchar   cnt=0;           //接收数据的累计值
  16. uchar   sign=0;
  17. int YPR[3];
  18. //********定义函数*****************************
  19. void    delay(unsigned int k);
  20. void    SeriPushSend(uchar send_data);
  21. void           display(void);
  22. //*********************************************
  23. //串口中断
  24. void serial_serve(void) interrupt 4       
  25. {

  26.   if(RI==1){         //中断标志
  27.      RI=0;
  28.     BUF[cnt]=SBUF;       
  29.           cnt++;        //接收计数
  30.     if(cnt==8)                //
  31.     {   
  32.        cnt=0;                 //
  33.        sign=1;
  34.     }
  35.   }
  36.        
  37. }
  38. //*********************************************
  39. //串口初始化
  40. //9600 bps @ 11.059 MHz
  41. void init_uart()
  42. {
  43.         TMOD=0x21;                               
  44.         TH1=0xfd;                               
  45.         TL1=0xfd;                               
  46.         SCON=0x50;
  47.         PS=1;      //串口中断设为高优先级别
  48.         TR0=1;           //启动定时器                       
  49.         TR1=1;
  50.         ET0=1;     //打开定时器0中断                       
  51.         ES=1;       
  52. }

  53. //**********延时函数***************
  54. void delay(unsigned int k)       
  55. {                                               
  56.         unsigned int i,j;                               
  57.         for(i=0;i<k;i++)
  58.         {                       
  59.         for(j=0;j<121;j++)                       
  60.         {;}
  61.         }                                               
  62. }                                                           
  63. //*********串口数据发送******************
  64. void  SeriPushSend(uchar send_data)
  65. {
  66.     SBUF=send_data;  
  67.         while(!TI);TI=0;          
  68. }
  69. //*************************************
  70. void dis_play(int num)       
  71. {
  72.         if(num<0)
  73.         {
  74.                 num=-num;
  75.                 SeriPushSend('-');
  76.         }
  77.         else
  78.                 SeriPushSend('+');

  79.         SeriPushSend(0x30|(num/10000));       
  80.         SeriPushSend(0x30|(num%10000/1000));
  81.         SeriPushSend(0x30|(num%1000/100));
  82.         SeriPushSend(0x2e);
  83.         SeriPushSend(0x30|(num%100/10));
  84.         SeriPushSend(0x30|(num%10));
  85.         SeriPushSend(',');
  86. }




  87. //*******************数据接收并显示**********************
  88. void display(void)
  89. {
  90.                
  91.   if(sign){
  92.          sign=0;       

  93.              if(BUF[0]==0XAA&&BUF[7]==0X55){                         //帧头判断   
  94.                                  
  95.           YPR[0]=(BUF[1]<<8|BUF[2]);  
  96.           YPR[1]=(BUF[3]<<8|BUF[4]);
  97.           YPR[2]=(BUF[5]<<8|BUF[6]);
  98.                                        
  99.                  SeriPushSend('#');
  100.              SeriPushSend('Y');
  101.              SeriPushSend('P');
  102.              SeriPushSend('R');
  103.              SeriPushSend('=');
  104.                          
  105.              dis_play(YPR[0]);
  106.                  dis_play(YPR[1]);
  107.              dis_play(YPR[2]);  
  108.                          
  109.                  SeriPushSend(0X0d);
  110.              SeriPushSend(0X0a);                                 
  111.         }
  112.    }  
  113. }
  114. //***************************************
  115. //主程序
  116. void main()
  117. {
  118.    delay(2000);                                                      //延时               
  119.    init_uart();                                               //初始化串口
  120.    EA=1;                                                      //开总中断
  121.    cnt=0;
  122.    SeriPushSend(0XA5);
  123.    SeriPushSend(0X52);                                        //发送1帧读取命令,连续读取
  124.    while(1){

  125.    display();                                             

  126.    }                                                                  //end while
  127. }

复制代码


arduino程序:
  1. //GY-25  ARDUINO  测试代码 IICLCD2004显示角度
  2. //注意1:下载程序时候,请先断开GY25的连线!否则将导致下载不成功
  3. //注意2:GY25模块使用时,上电自校正,建议不要用手拿着模块,
  4. //       保持3秒以上静止状态,才能发送命令开始工作
  5. //   GY25                  arduino pro mini
  6. //   VCC----------------------VCC
  7. //   RX-----------------------TX
  8. //   TX-----------------------RX
  9. //   GND----------------------GND
  10. //-----------------------------------------------
  11. //   IICLCD2004            arduino pro mini
  12. //   VCC----------------------VCC
  13. //   SCL----------------------A5
  14. //   SDA----------------------A4
  15. //   GND----------------------GND
  16. #include <Wire.h>
  17. #include <LiquidCrystal_I2C.h>
  18. int YPR[3];
  19. unsigned char Re_buf[8],counter=0;
  20. unsigned char sign=0;
  21. int led = 13;
  22. LiquidCrystal_I2C lcd(0x20,20,4);  // set the LCD address to 0x20 for a 20 chars and 4 line display

  23. //-----------------------------------------------------------
  24. void setup()
  25. {
  26.   lcd.init();                      // initialize the lcd
  27.   // Print a message to the LCD.   
  28.   Serial.begin(115200);  
  29.   delay(2000);   
  30.   Serial.write(0XA5);
  31.   Serial.write(0X52);    //初始化GY25,连续输出模式
  32.   lcd.backlight();
  33.   lcd.setCursor(0,0);     //I2C接口LCD2004显示初始值
  34.   lcd.print("Yaw:");
  35.   lcd.setCursor(0,1);
  36.   lcd.print("Pitch:");
  37.   lcd.setCursor(0,2);
  38.   lcd.print("Roll:");
  39. }
  40. //-------------------------------------------------------------
  41. void loop() {
  42.   if(sign)
  43.   {  
  44.      sign=0;
  45.      if(Re_buf[0]==0xAA && Re_buf[7]==0x55)        //检查帧头,帧尾
  46.      {                 
  47.             YPR[0]=(Re_buf[1]<<8|Re_buf[2])/100;   //合成数据,去掉小数点后2位
  48.             YPR[1]=(Re_buf[3]<<8|Re_buf[4])/100;
  49.             YPR[2]=(Re_buf[5]<<8|Re_buf[6])/100;
  50.             
  51.             lcd.setCursor(4,0);     
  52.             lcd.print("    ");
  53.             lcd.setCursor(4,0);
  54.             lcd.print(YPR[0]);      //显示航向

  55.             lcd.setCursor(6,1);   
  56.             lcd.print("    ");                    
  57.             lcd.setCursor(6,1);    //显示俯仰角
  58.             lcd.print(YPR[1]);     

  59.             lcd.setCursor(5,2);   
  60.             lcd.print("    ");  
  61.             lcd.setCursor(5,2);     //显示横滚角
  62.             lcd.print(YPR[2]);      
  63.             delay(100);           
  64.    }
  65.   }
  66. }
  67. //---------------------------------------------------------------
  68. void serialEvent() {
  69.   while (Serial.available()) {   
  70.     Re_buf[counter]=(unsigned char)Serial.read();
  71.     if(counter==0&&Re_buf[0]!=0xAA) return;      // 检查帧头         
  72.     counter++;      
  73.     if(counter==8)                //接收到数据
  74.     {   
  75.        counter=0;                 //重新赋值,准备下一帧数据的接收
  76.        sign=1;
  77.     }      
  78.   }
  79. }

复制代码


GY-25倾斜度角度模块 串口直接输出角度数据 MPU-6050模块 传感器.rar

5.76 MB, 下载次数: 135, 下载积分: 黑币 -5

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:192644 发表于 2017-4-23 19:25 | 显示全部楼层
如果没有led,我用电脑串口数据应该怎么办呀
这个程序哪里读的数据呀,不是只有传输吗?
新手,大神帮忙
回复

使用道具 举报

ID:225586 发表于 2017-8-24 13:32 | 显示全部楼层
厉害aa
回复

使用道具 举报

ID:246080 发表于 2017-11-4 20:24 | 显示全部楼层
正好我需要这个
回复

使用道具 举报

ID:289615 发表于 2018-3-9 12:32 | 显示全部楼层
好厉害的样子啊
回复

使用道具 举报

ID:521996 发表于 2019-5-26 16:54 | 显示全部楼层
正好需要,下载来看看!
回复

使用道具 举报

ID:421308 发表于 2019-7-17 15:10 | 显示全部楼层
正好需要,进来看看
回复

使用道具 举报

ID:586056 发表于 2019-7-17 23:32 | 显示全部楼层
谢谢了,
回复

使用道具 举报

ID:594119 发表于 2019-8-5 19:55 | 显示全部楼层
挺不错的资料
回复

使用道具 举报

ID:518004 发表于 2019-10-21 20:16 | 显示全部楼层
xiaozhong1314 发表于 2019-7-17 15:10
正好需要,进来看看

您好,请问你们利用这个程序成功实现了么?我是新手,想请教请教一下 在实验中I2C数据和时钟口应该怎么接呢?
回复

使用道具 举报

ID:628217 发表于 2019-10-23 09:26 | 显示全部楼层
先保存下来,以后能用得上
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表