移植官方例程F4到F1,基于stm32f103ze+mpu9250通過官方DMP庫串口輸出角度,感謝眾多開源大佬
單片機源程序如下:
- #include "stm32f10x.h"
- #include "stdio.h"
- #include "UART1.h"
- #include "systick.h"
- // mpu9250 include files
- #include "sys.h"
- #include "mpu9250.h"
- #include "mpuiic.h"
- #include "inv_mpu.h"
- #include "inv_mpu_dmp_motion_driver.h"
- float Q0,Q1,Q2,Q3; // 歐拉角
- u32 status2=0;
- void printf_init() //printf初始化
- {
- GPIO_InitTypeDef GPIO_InitStructure; //聲明一個結構體變量,用來初始化GPIO
- NVIC_InitTypeDef NVIC_InitStrue;//定義中斷相關結構體
- USART_InitTypeDef USART_InitStructure; //串口結構體定義
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2時鐘
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//TX
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//RX
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate=115200; //波特率設置為9600
- USART_InitStructure.USART_WordLength=USART_WordLength_8b;
- USART_InitStructure.USART_StopBits=USART_StopBits_1;
- USART_InitStructure.USART_Parity=USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
- USART_Init(USART2,&USART_InitStructure);
- USART_Cmd(USART2, ENABLE);
-
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//使能或者失能指定的USART中斷 接收中斷
- USART_ClearFlag(USART2,USART_FLAG_TC);//清除USARTx的待處理標志位
-
- NVIC_InitStrue.NVIC_IRQChannel=USART2_IRQn;//定義中斷通道
- NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;//開啟中斷通道
- NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;//設定搶占優先級為1
- NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;//設定子優先級為1
- NVIC_Init(&NVIC_InitStrue);//中斷初始化
- }
- void USART2_IRQHandler(void)//編寫中斷處理函數
- {
- u8 res;//無符號字符res
- if(USART_GetITStatus(USART2,USART_IT_RXNE))//接收數據進入中斷,判斷串口1接收緩存器非空使能為1與否
- {
- res= USART_ReceiveData(USART2); //為1,則將串口1的數據給變量
- if(res==0x10)//等待
- {
- USART_SendData(USART2,'1');
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
- USART_SendData(USART2,'0');
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
- EXTI->IMR |= EXTI_Line0;//使能外部中斷4
- EXTI->IMR |= EXTI_Line3;//使能外部中斷4
- EXTI->IMR |= EXTI_Line4;//使能外部中斷4
- EXTI->IMR |= EXTI_Line5;//使能外部中斷4
- status2=0;
- }
- else if(res==0x11)//小孩
- {
- EXTI->IMR &= ~(EXTI_Line0);// 屏蔽外部中斷0
- EXTI->IMR &= ~(EXTI_Line3);// 屏蔽外部中斷3
- EXTI->IMR &= ~(EXTI_Line4);// 屏蔽外部中斷4
- EXTI->IMR &= ~(EXTI_Line5);// 屏蔽外部中斷5
- status2=1;
- }
- else if(res==0x13)//校準
- {
- mpu_dmp_init();
- if(mpu_dmp_init()==0)
- printf("13");
- while(mpu_dmp_init())
- {
- mpu_dmp_init();
- if(mpu_dmp_init()==0)
- printf("13");
- }
- }
- }
- }
- void Led_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PE5接
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //設為推挽輸出模式
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOE, &GPIO_InitStructure); //初始化外設GPIO
- GPIO_SetBits(GPIOE,GPIO_Pin_5);
- }
- int main(void)
- {
- u8 status;
- float pitch_dmp,roll_dmp,yaw_dmp; // 歐拉角
- delay_ms(1000);
- delay_ms(1000);
- Initial_UART1(115200);
- Led_Configuration();
- printf_init();
- delay_ms(1000);
- // MPU9250姿態傳感器初始化
- do{
- status = mpu_dmp_init();
- if(status)
- {
- printf("Initialization--MPU9250 Error!!!\r\n");
- }
- }while(status);
- printf("Initialization--MPU9250 OK!!!\r\n");
- while(1)
- { status= mpu_mpl_get_data(&pitch_dmp,&roll_dmp,&yaw_dmp);
- //status=mpu_dmp_get_data(&pitch_dmp,&roll_dmp,&yaw_dmp);
- if(!status)
- {
- if(status2==1)
- { //printf("pitch:\t\t%8.2f\r\n roll:\t\t%8.2f\r\n yaw:\t\t%8.2f\r\n",pitch_dmp,roll_dmp,yaw_dmp);
- printf("%.3f|%.3f|%.3f|%.3f|%.1d\n",Q0,Q1,Q2,Q3,GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2));//四元數
- //GPIO_WriteBit(GPIOE, GPIO_Pin_5, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOE, GPIO_Pin_5))); //引腳取反
- //delay_ms(10);
- }
- else
- {
- // printf("### Get Eulerian angle failed! ###\r\n");
- delay_ms(10);
- }
- }
- }
- }
- int fputc(int ch,FILE *p)
- {
- USART_SendData(USART2,(u8)ch);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
- return ch;
- }
復制代碼
所有資料51hei提供下載:
MPU9250.7z
(441.99 KB, 下載次數: 274)
2019-7-14 21:59 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|