久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3842|回復: 0
收起左側

基于nrf51822單片機的工程:MPU6050輸出姿態數據

[復制鏈接]
ID:326995 發表于 2018-5-10 17:23 | 顯示全部樓層 |閱讀模式
給大家分享一個nrf51822+MPU6050輸出姿態數據的單片機源程序如下:
  1. /****************************************Copyright (c)****************************************************
  2. **                                       
  3. **                                 
  4. **
  5. **--------------File Info---------------------------------------------------------------------------------
  6. ** File name:                             main.c
  7. ** Last modified Date:      
  8. ** Last Version:                  
  9. ** Descriptions:                   使用的SDK版本-SDK_11.0.0
  10. **                               
  11. **--------------------------------------------------------------------------------------------------------
  12. ** Created by:                        FiYu
  13. ** Created date:                2016-7-1
  14. ** Version:                            1.0
  15. ** Descriptions:                MPU6050輸出姿態數據實驗
  16. **--------------------------------------------------------------------------------------------------------*/
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include "app_uart.h"
  21. #include "app_error.h"
  22. #include "nrf_delay.h"
  23. #include "nrf_gpio.h"
  24. #include "boards.h"
  25. #include "mpu6050.h"
  26. #include "twi_master.h"
  27. #include "inv_mpu.h"
  28. #include "inv_mpu_dmp_motion_driver.h"

  29. /* 開發板中MPU6050模塊和串口串口占用的nRF51822管腳資源
  30. P0.09:UART_TXD   :串口發送
  31. P0.11:UART_RXD   :串口接收
  32. P0.08:UART_CTS
  33. P0.10:UART_RTS

  34. 串口需要短接對應的跳線帽

  35. P0.01:IIC時鐘
  36. P0.04:IIC數據
  37. */

  38. #define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
  39. #define UART_RX_BUF_SIZE 1                           /**< UART RX buffer size. */


  40. void uart_error_handle(app_uart_evt_t * p_event)
  41. {
  42.     if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
  43.     {
  44.         APP_ERROR_HANDLER(p_event->data.error_communication);
  45.     }
  46.     else if (p_event->evt_type == APP_UART_FIFO_ERROR)
  47.     {
  48.         APP_ERROR_HANDLER(p_event->data.error_code);
  49.     }
  50. }

  51. //串口初始化。禁止流控,波特率:115200
  52. void uart_init(void)
  53. {
  54.           uint32_t err_code;
  55.     const app_uart_comm_params_t comm_params =
  56.     {
  57.           RX_PIN_NUMBER,
  58.           TX_PIN_NUMBER,
  59.           RTS_PIN_NUMBER,
  60.           CTS_PIN_NUMBER,
  61.           APP_UART_FLOW_CONTROL_DISABLED, //禁止流控
  62.           false,
  63.           UART_BAUDRATE_BAUDRATE_Baud115200//波特率115200
  64.     };

  65.     APP_UART_FIFO_INIT(&comm_params,
  66.                          UART_RX_BUF_SIZE,
  67.                          UART_TX_BUF_SIZE,
  68.                          uart_error_handle,
  69.                          APP_IRQ_PRIORITY_LOW,
  70.                          err_code);

  71.     APP_ERROR_CHECK(err_code);
  72. }
  73. /***********************************************************************************
  74. * 描  述 : 串口發送數據,數據格式為匿名四軸上位機軟件(V2.6版本)數據格式
  75. * 入  參 : fun:功能碼
  76. *        : dat:數據緩存區地址,最多28字節
  77. *        : len:數據長度,最大28字節
  78. * 返回值 : 無
  79. **********************************************************************************/
  80. void Uart_SendDat_ToPC(uint8_t fun,uint8_t *dat,uint8_t len)
  81. {
  82.         uint8_t send_buf[32];
  83.         uint8_t i;
  84.         if(len>28)return;          //最多28字節數據
  85.         send_buf[len+3]=0;        //校驗數置零
  86.         send_buf[0]=0x88;          //幀頭
  87.         send_buf[1]=fun;          //功能碼
  88.         send_buf[2]=len;          //數據長度
  89.         for(i=0;i<len;i++)send_buf[3+i]=dat[i];                              //復制數據
  90.         for(i=0;i<len+3;i++)send_buf[len+3]+=send_buf[i];        //計算校驗和       
  91.         for(i=0;i<len+4;i++)app_uart_put(send_buf[i]);          //串口輸出數據
  92. }
  93. /***********************************************************************************
  94. * 描  述 : 發送加速度傳感器數據和陀螺儀數據
  95. * 入  參 : aacx,aacy,aacz:x,y,z三個方向上面的加速度值
  96. *          gyrox,gyroy,gyroz:x,y,z三個方向上面的陀螺儀值
  97. * 返回值 : 無
  98. **********************************************************************************/
  99. void mpu6050_send_dat(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz)
  100. {
  101.         uint8_t tx_buf[12];
  102.         tx_buf[0]=(aacx>>8)&0xFF;
  103.         tx_buf[1]=aacx&0xFF;
  104.         tx_buf[2]=(aacy>>8)&0xFF;
  105.         tx_buf[3]=aacy&0xFF;
  106.         tx_buf[4]=(aacz>>8)&0xFF;
  107.         tx_buf[5]=aacz&0xFF;
  108.         tx_buf[6]=(gyrox>>8)&0xFF;
  109.         tx_buf[7]=gyrox&0xFF;
  110.         tx_buf[8]=(gyroy>>8)&0xFF;
  111.         tx_buf[9]=gyroy&0xFF;
  112.         tx_buf[10]=(gyroz>>8)&0xFF;
  113.         tx_buf[11]=gyroz&0xFF;
  114.         Uart_SendDat_ToPC(0xA1,tx_buf,12);//自定義幀,0XA1
  115. }       

  116. /***********************************************************************************
  117. * 描  述 : 串口上傳MPU6050姿態數據
  118. * 入  參 : aacx,aacy,aacz:x,y,z三個方向上面的加速度值
  119. *        : gyrox,gyroy,gyroz:x,y,z三個方向上面的陀螺儀值
  120. *        : roll:橫滾角.單位0.01度。 -18000 -> 18000 對應 -180.00  ->  180.00度
  121. *        : pitch:俯仰角.單位 0.01度。-9000 - 9000 對應 -90.00 -> 90.00 度
  122. *        : yaw:航向角.單位為0.1度 0 -> 3600  對應 0 -> 360.0度
  123. * 返回值 : 無
  124. **********************************************************************************/
  125. void Uart_ReportIMU(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,short roll,short pitch,short yaw)
  126. {
  127.         uint8_t i,tx_buf[28];

  128.         for(i=0;i<28;i++)tx_buf[i]=0;//清0
  129.         tx_buf[0]=(aacx>>8)&0xFF;
  130.         tx_buf[1]=aacx&0xFF;
  131.         tx_buf[2]=(aacy>>8)&0xFF;
  132.         tx_buf[3]=aacy&0xFF;
  133.         tx_buf[4]=(aacz>>8)&0xFF;
  134.         tx_buf[5]=aacz&0xFF;
  135.         tx_buf[6]=(gyrox>>8)&0xFF;
  136.         tx_buf[7]=gyrox&0xFF;
  137.         tx_buf[8]=(gyroy>>8)&0xFF;
  138.         tx_buf[9]=gyroy&0xFF;
  139.         tx_buf[10]=(gyroz>>8)&0xFF;
  140.         tx_buf[11]=gyroz&0xFF;       
  141.         tx_buf[18]=(roll>>8)&0xFF;
  142.         tx_buf[19]=roll&0xFF;
  143.         tx_buf[20]=(pitch>>8)&0xFF;
  144.         tx_buf[21]=pitch&0xFF;
  145.         tx_buf[22]=(yaw>>8)&0xFF;
  146.         tx_buf[23]=yaw&0xFF;
  147.         Uart_SendDat_ToPC(0xAF,tx_buf,28);//匿名四軸飛控顯示幀,0xAF
  148. }
  149. /**********************************************************************************************
  150. * 描  述 : main函數
  151. * 入  參 : 無
  152. * 返回值 : 無
  153. ***********************************************************************************************/
  154. int main(void)
  155. {
  156.     int16_t AccValue[3],GyroValue[3];
  157.           uint8_t id;
  158.           float pitch,roll,yaw;                 //歐拉角
  159.           short aacx,aacy,aacz;                  //加速度傳感器原始數據
  160.           short gyrox,gyroy,gyroz;        //陀螺儀原始數據
  161.        
  162.           nrf_gpio_cfg_output(LED_1);//配置管腳P0.21為輸出,驅動指示燈D1
  163.     nrf_gpio_pin_set(LED_1);   //設置指示燈D1初始狀態為熄滅
  164.        
  165.     uart_init();  //配置串口,禁止流控,波特率:115200       
  166.                
  167.                 twi_master_init();
  168.        
  169.           if(mpu6050_init(0x68) == false)
  170.                 {
  171.                     while (true)
  172.                           {
  173.                                     printf("mpu6050 init fail\r\n");
  174.                                           nrf_delay_ms(100);
  175.                                 }       
  176.                 }
  177.                 else
  178.                 {nrf_delay_ms(1000);printf("mpu6050 init ok\r\n");}
  179.                
  180.     mpu6050_register_read(0x75U, &id, 1);
  181.                 printf("mpu6050 id is %d \r\n",id);
  182.                
  183.                 while(mpu_dmp_init())
  184.                 {
  185.                     nrf_delay_ms(1000);
  186.                           printf("mpu6050 init Error\r\n");
  187.                 }

  188.     while (true)
  189.     {
  190.                           if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
  191.                     {
  192.                               MPU6050_ReadAcc(&aacx,&aacy,&aacz);            //讀取加速度傳感器數據
  193.                               MPU6050_ReadGyro(&gyrox,&gyroy,&gyroz);        //讀取陀螺儀數據
  194.                               mpu6050_send_dat(aacx,aacy,aacz,gyrox,gyroy,gyroz);//用自定義幀發送加速度和陀螺儀原始數據
  195.                               Uart_ReportIMU(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)(pitch*100),(int)(yaw*10));
  196.             nrf_delay_ms(50);
  197.                     }
  198.     }
  199. }
  200. /********************************************END FILE*******************************************/
復制代碼

所有資料51hei提供下載:
實驗1 - MPU6050輸出姿態數據.rar (2.59 MB, 下載次數: 56)
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 91最新入口 | 成人久久久久 | 在线中文一区 | 综合久久综合久久 | 亚洲成人在线视频播放 | 一区二区视频在线观看 | 一区二区av在线 | 男女羞羞视频在线观看 | 精品福利在线视频 | 免费在线一区二区三区 | 色播av | 成人在线视频免费观看 | 国产一区二区三区 | 中文字幕视频一区二区 | 精品成人在线 | 亚洲综合热 | 亚洲欧美日韩精品久久亚洲区 | 欧美精品在线播放 | 欧美日韩一区在线观看 | 99热这里有精品 | 国产亚洲一区二区三区 | 欧美视频三区 | 久久国产精品久久久久久 | 日韩国产精品一区二区三区 | 中文字幕不卡 | 视频一区中文字幕 | 在线观看成年人视频 | 久久视频精品 | 国产精品视频yy9299一区 | 午夜伦4480yy私人影院 | 91精品欧美久久久久久久 | 午夜影院在线观看免费 | 国产第一页在线播放 | 一级黄色淫片 | 国产一区二区日韩 | 国产日韩精品视频 | 国产精品18久久久久久久 | 女同久久 | 最新国产精品 | 色资源在线观看 | 成人精品在线 |