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

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3842|回復(fù): 0
收起左側(cè)

基于nrf51822單片機(jī)的工程:MPU6050輸出姿態(tài)數(shù)據(jù)

[復(fù)制鏈接]
ID:326995 發(fā)表于 2018-5-10 17:23 | 顯示全部樓層 |閱讀模式
給大家分享一個(gè)nrf51822+MPU6050輸出姿態(tài)數(shù)據(jù)的單片機(jī)源程序如下:
  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輸出姿態(tài)數(shù)據(jù)實(shí)驗(yàn)
  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. /* 開(kāi)發(fā)板中MPU6050模塊和串口串口占用的nRF51822管腳資源
  30. P0.09:UART_TXD   :串口發(fā)送
  31. P0.11:UART_RXD   :串口接收
  32. P0.08:UART_CTS
  33. P0.10:UART_RTS

  34. 串口需要短接對(duì)應(yīng)的跳線帽

  35. P0.01:IIC時(shí)鐘
  36. P0.04:IIC數(shù)據(jù)
  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. * 描  述 : 串口發(fā)送數(shù)據(jù),數(shù)據(jù)格式為匿名四軸上位機(jī)軟件(V2.6版本)數(shù)據(jù)格式
  75. * 入  參 : fun:功能碼
  76. *        : dat:數(shù)據(jù)緩存區(qū)地址,最多28字節(jié)
  77. *        : len:數(shù)據(jù)長(zhǎng)度,最大28字節(jié)
  78. * 返回值 : 無(wú)
  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字節(jié)數(shù)據(jù)
  85.         send_buf[len+3]=0;        //校驗(yàn)數(shù)置零
  86.         send_buf[0]=0x88;          //幀頭
  87.         send_buf[1]=fun;          //功能碼
  88.         send_buf[2]=len;          //數(shù)據(jù)長(zhǎng)度
  89.         for(i=0;i<len;i++)send_buf[3+i]=dat[i];                              //復(fù)制數(shù)據(jù)
  90.         for(i=0;i<len+3;i++)send_buf[len+3]+=send_buf[i];        //計(jì)算校驗(yàn)和       
  91.         for(i=0;i<len+4;i++)app_uart_put(send_buf[i]);          //串口輸出數(shù)據(jù)
  92. }
  93. /***********************************************************************************
  94. * 描  述 : 發(fā)送加速度傳感器數(shù)據(jù)和陀螺儀數(shù)據(jù)
  95. * 入  參 : aacx,aacy,aacz:x,y,z三個(gè)方向上面的加速度值
  96. *          gyrox,gyroy,gyroz:x,y,z三個(gè)方向上面的陀螺儀值
  97. * 返回值 : 無(wú)
  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姿態(tài)數(shù)據(jù)
  118. * 入  參 : aacx,aacy,aacz:x,y,z三個(gè)方向上面的加速度值
  119. *        : gyrox,gyroy,gyroz:x,y,z三個(gè)方向上面的陀螺儀值
  120. *        : roll:橫滾角.單位0.01度。 -18000 -> 18000 對(duì)應(yīng) -180.00  ->  180.00度
  121. *        : pitch:俯仰角.單位 0.01度。-9000 - 9000 對(duì)應(yīng) -90.00 -> 90.00 度
  122. *        : yaw:航向角.單位為0.1度 0 -> 3600  對(duì)應(yīng) 0 -> 360.0度
  123. * 返回值 : 無(wú)
  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函數(shù)
  151. * 入  參 : 無(wú)
  152. * 返回值 : 無(wú)
  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;                  //加速度傳感器原始數(shù)據(jù)
  160.           short gyrox,gyroy,gyroz;        //陀螺儀原始數(shù)據(jù)
  161.        
  162.           nrf_gpio_cfg_output(LED_1);//配置管腳P0.21為輸出,驅(qū)動(dòng)指示燈D1
  163.     nrf_gpio_pin_set(LED_1);   //設(shè)置指示燈D1初始狀態(tài)為熄滅
  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);            //讀取加速度傳感器數(shù)據(jù)
  193.                               MPU6050_ReadGyro(&gyrox,&gyroy,&gyroz);        //讀取陀螺儀數(shù)據(jù)
  194.                               mpu6050_send_dat(aacx,aacy,aacz,gyrox,gyroy,gyroz);//用自定義幀發(fā)送加速度和陀螺儀原始數(shù)據(jù)
  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*******************************************/
復(fù)制代碼

所有資料51hei提供下載:
實(shí)驗(yàn)1 - MPU6050輸出姿態(tài)數(shù)據(jù).rar (2.59 MB, 下載次數(shù): 56)
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 欧美视频在线播放 | 日本又色又爽又黄的大片 | 亚洲欧美激情精品一区二区 | 欧美日韩亚洲一区 | 成人久久18免费网站图片 | 久久中文字幕一区 | 久久精品国产久精国产 | 国产精品777一区二区 | 精品国产一区二区久久 | 午夜在线观看免费 | 欧美一区二区三区在线播放 | cao在线| 国产一区二区不卡 | 超碰最新在线 | 国产亚洲精品综合一区 | 91玖玖 | 亚洲国产成人在线观看 | 国产精品成人在线 | 欧美中文字幕在线 | 久久久久久久一区 | 久久成人国产精品 | 国产精品亚洲一区二区三区在线 | 中文在线一区 | 午夜视频网| 欧美又大粗又爽又黄大片视频 | 久久久视频在线 | 国产精品国产自产拍高清 | 91精品一区 | 欧美精品一区二区在线观看 | 国产精品伦一区二区三级视频 | 精品国产一区二区久久 | 日本视频中文字幕 | 久久一日本道色综合久久 | 1000部精品久久久久久久久 | www.久久| 欧美成人h版在线观看 | 久久国产精品一区二区 | 欧美精品一区在线发布 | 免费黄色在线观看 | 97av视频 | 日韩第一夜 |