給大家分享一個nrf51822+MPU6050輸出姿態數據的單片機源程序如下:
- /****************************************Copyright (c)****************************************************
- **
- **
- **
- **--------------File Info---------------------------------------------------------------------------------
- ** File name: main.c
- ** Last modified Date:
- ** Last Version:
- ** Descriptions: 使用的SDK版本-SDK_11.0.0
- **
- **--------------------------------------------------------------------------------------------------------
- ** Created by: FiYu
- ** Created date: 2016-7-1
- ** Version: 1.0
- ** Descriptions: MPU6050輸出姿態數據實驗
- **--------------------------------------------------------------------------------------------------------*/
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include "app_uart.h"
- #include "app_error.h"
- #include "nrf_delay.h"
- #include "nrf_gpio.h"
- #include "boards.h"
- #include "mpu6050.h"
- #include "twi_master.h"
- #include "inv_mpu.h"
- #include "inv_mpu_dmp_motion_driver.h"
- /* 開發板中MPU6050模塊和串口串口占用的nRF51822管腳資源
- P0.09:UART_TXD :串口發送
- P0.11:UART_RXD :串口接收
- P0.08:UART_CTS
- P0.10:UART_RTS
- 串口需要短接對應的跳線帽
- P0.01:IIC時鐘
- P0.04:IIC數據
- */
- #define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
- #define UART_RX_BUF_SIZE 1 /**< UART RX buffer size. */
- void uart_error_handle(app_uart_evt_t * p_event)
- {
- if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
- {
- APP_ERROR_HANDLER(p_event->data.error_communication);
- }
- else if (p_event->evt_type == APP_UART_FIFO_ERROR)
- {
- APP_ERROR_HANDLER(p_event->data.error_code);
- }
- }
- //串口初始化。禁止流控,波特率:115200
- void uart_init(void)
- {
- uint32_t err_code;
- const app_uart_comm_params_t comm_params =
- {
- RX_PIN_NUMBER,
- TX_PIN_NUMBER,
- RTS_PIN_NUMBER,
- CTS_PIN_NUMBER,
- APP_UART_FLOW_CONTROL_DISABLED, //禁止流控
- false,
- UART_BAUDRATE_BAUDRATE_Baud115200//波特率115200
- };
- APP_UART_FIFO_INIT(&comm_params,
- UART_RX_BUF_SIZE,
- UART_TX_BUF_SIZE,
- uart_error_handle,
- APP_IRQ_PRIORITY_LOW,
- err_code);
- APP_ERROR_CHECK(err_code);
- }
- /***********************************************************************************
- * 描 述 : 串口發送數據,數據格式為匿名四軸上位機軟件(V2.6版本)數據格式
- * 入 參 : fun:功能碼
- * : dat:數據緩存區地址,最多28字節
- * : len:數據長度,最大28字節
- * 返回值 : 無
- **********************************************************************************/
- void Uart_SendDat_ToPC(uint8_t fun,uint8_t *dat,uint8_t len)
- {
- uint8_t send_buf[32];
- uint8_t i;
- if(len>28)return; //最多28字節數據
- send_buf[len+3]=0; //校驗數置零
- send_buf[0]=0x88; //幀頭
- send_buf[1]=fun; //功能碼
- send_buf[2]=len; //數據長度
- for(i=0;i<len;i++)send_buf[3+i]=dat[i]; //復制數據
- for(i=0;i<len+3;i++)send_buf[len+3]+=send_buf[i]; //計算校驗和
- for(i=0;i<len+4;i++)app_uart_put(send_buf[i]); //串口輸出數據
- }
- /***********************************************************************************
- * 描 述 : 發送加速度傳感器數據和陀螺儀數據
- * 入 參 : aacx,aacy,aacz:x,y,z三個方向上面的加速度值
- * gyrox,gyroy,gyroz:x,y,z三個方向上面的陀螺儀值
- * 返回值 : 無
- **********************************************************************************/
- void mpu6050_send_dat(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz)
- {
- uint8_t tx_buf[12];
- tx_buf[0]=(aacx>>8)&0xFF;
- tx_buf[1]=aacx&0xFF;
- tx_buf[2]=(aacy>>8)&0xFF;
- tx_buf[3]=aacy&0xFF;
- tx_buf[4]=(aacz>>8)&0xFF;
- tx_buf[5]=aacz&0xFF;
- tx_buf[6]=(gyrox>>8)&0xFF;
- tx_buf[7]=gyrox&0xFF;
- tx_buf[8]=(gyroy>>8)&0xFF;
- tx_buf[9]=gyroy&0xFF;
- tx_buf[10]=(gyroz>>8)&0xFF;
- tx_buf[11]=gyroz&0xFF;
- Uart_SendDat_ToPC(0xA1,tx_buf,12);//自定義幀,0XA1
- }
- /***********************************************************************************
- * 描 述 : 串口上傳MPU6050姿態數據
- * 入 參 : aacx,aacy,aacz:x,y,z三個方向上面的加速度值
- * : gyrox,gyroy,gyroz:x,y,z三個方向上面的陀螺儀值
- * : roll:橫滾角.單位0.01度。 -18000 -> 18000 對應 -180.00 -> 180.00度
- * : pitch:俯仰角.單位 0.01度。-9000 - 9000 對應 -90.00 -> 90.00 度
- * : yaw:航向角.單位為0.1度 0 -> 3600 對應 0 -> 360.0度
- * 返回值 : 無
- **********************************************************************************/
- void Uart_ReportIMU(short aacx,short aacy,short aacz,short gyrox,short gyroy,short gyroz,short roll,short pitch,short yaw)
- {
- uint8_t i,tx_buf[28];
- for(i=0;i<28;i++)tx_buf[i]=0;//清0
- tx_buf[0]=(aacx>>8)&0xFF;
- tx_buf[1]=aacx&0xFF;
- tx_buf[2]=(aacy>>8)&0xFF;
- tx_buf[3]=aacy&0xFF;
- tx_buf[4]=(aacz>>8)&0xFF;
- tx_buf[5]=aacz&0xFF;
- tx_buf[6]=(gyrox>>8)&0xFF;
- tx_buf[7]=gyrox&0xFF;
- tx_buf[8]=(gyroy>>8)&0xFF;
- tx_buf[9]=gyroy&0xFF;
- tx_buf[10]=(gyroz>>8)&0xFF;
- tx_buf[11]=gyroz&0xFF;
- tx_buf[18]=(roll>>8)&0xFF;
- tx_buf[19]=roll&0xFF;
- tx_buf[20]=(pitch>>8)&0xFF;
- tx_buf[21]=pitch&0xFF;
- tx_buf[22]=(yaw>>8)&0xFF;
- tx_buf[23]=yaw&0xFF;
- Uart_SendDat_ToPC(0xAF,tx_buf,28);//匿名四軸飛控顯示幀,0xAF
- }
- /**********************************************************************************************
- * 描 述 : main函數
- * 入 參 : 無
- * 返回值 : 無
- ***********************************************************************************************/
- int main(void)
- {
- int16_t AccValue[3],GyroValue[3];
- uint8_t id;
- float pitch,roll,yaw; //歐拉角
- short aacx,aacy,aacz; //加速度傳感器原始數據
- short gyrox,gyroy,gyroz; //陀螺儀原始數據
-
- nrf_gpio_cfg_output(LED_1);//配置管腳P0.21為輸出,驅動指示燈D1
- nrf_gpio_pin_set(LED_1); //設置指示燈D1初始狀態為熄滅
-
- uart_init(); //配置串口,禁止流控,波特率:115200
-
- twi_master_init();
-
- if(mpu6050_init(0x68) == false)
- {
- while (true)
- {
- printf("mpu6050 init fail\r\n");
- nrf_delay_ms(100);
- }
- }
- else
- {nrf_delay_ms(1000);printf("mpu6050 init ok\r\n");}
-
- mpu6050_register_read(0x75U, &id, 1);
- printf("mpu6050 id is %d \r\n",id);
-
- while(mpu_dmp_init())
- {
- nrf_delay_ms(1000);
- printf("mpu6050 init Error\r\n");
- }
- while (true)
- {
- if(mpu_dmp_get_data(&pitch,&roll,&yaw)==0)
- {
- MPU6050_ReadAcc(&aacx,&aacy,&aacz); //讀取加速度傳感器數據
- MPU6050_ReadGyro(&gyrox,&gyroy,&gyroz); //讀取陀螺儀數據
- mpu6050_send_dat(aacx,aacy,aacz,gyrox,gyroy,gyroz);//用自定義幀發送加速度和陀螺儀原始數據
- Uart_ReportIMU(aacx,aacy,aacz,gyrox,gyroy,gyroz,(int)(roll*100),(int)(pitch*100),(int)(yaw*10));
- nrf_delay_ms(50);
- }
- }
- }
- /********************************************END FILE*******************************************/
復制代碼
所有資料51hei提供下載:
實驗1 - MPU6050輸出姿態數據.rar
(2.59 MB, 下載次數: 56)
2018-5-11 05:34 上傳
點擊文件名下載附件
|