原创 stm32学习——两轮平衡小车之MPU6050模块数据读取和解析(USART的DMA功能应用)

2016-4-9 13:07 557 6 2 分类: MCU/ 嵌入式

由于硬件上采用了mpu6050模块,这模块是自带姿态解算和卡尔曼滤波功能的,所以省了不少事儿。直接通过串口读模块发送的数据,然后按照模块固定的协议把数据解析出来,就能得到三轴角速度,三轴加速度,三轴角度。其实mpu6050内部只集成了陀螺仪测角速度,加速度计测加速度,没有直接测量角度的传感器,但是角度可以通过角速度积分得到,并且通过重力加速度在各轴上的分量进行校正,通过数据融合的算法(比如卡尔曼滤波),就可以比较准确的得到角度啦。当然这个过程都在模块内部完成,我们只是应用的话不需要太关注(以后做四轴的话应该得好好研究一下姿态解算和数据融合方面的东西)。

好了,废话不多说。先说代码思路,然后就上代码。Stm32F405的usart3连接模块,接收数据,通过DMA直接存到一个11字节的数组GYRO_buffer[11]里面,当数组存满了,也就是接受到了完整的一帧数据(一帧数据包括可能是角度数据、加速度数据或者是角速度数据,要根据帧头的第二个字节加以区分,具体的数据帧格式见附件里面的模块资料),然后触发DMA中断,在中断中对数据进行解析,得到最终数据,存储到一个GYRO结构体变量中,这个变量被设置为全局变量,可以在其他的函数中使用该变量的值,做姿态的控制。

代码:

#include "main.h"

 

GYRO gyro = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};

unsigned char GYRO_buffer[11];

 

void usart3_configuration(void)

{

         //一定注意配置顺序,先除能再使能等等

         USART_InitTypeDef usart3;

         GPIO_InitTypeDef  gpio;

         NVIC_InitTypeDef  nvic;

         DMA_InitTypeDef   dma;

 

        

         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);

         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);

         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);

        

         /*配置GPIO*/

         GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_USART3);

         GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_USART3);

         gpio.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

         gpio.GPIO_Mode = GPIO_Mode_AF;

         gpio.GPIO_OType = GPIO_OType_PP;

         gpio.GPIO_Speed = GPIO_Speed_100MHz;

         gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;

         GPIO_Init(GPIOC,&gpio);

        

         /*配置DMA*/        

         DMA_Cmd(DMA1_Stream1,DISABLE);

         while(DMA_GetCmdStatus(DMA1_Stream1) != DISABLE);//等待DMA可配置

         DMA_DeInit(DMA1_Stream1);

         dma.DMA_Channel= DMA_Channel_4;

         dma.DMA_PeripheralBaseAddr = (uint32_t)&(USART3->DR);

         dma.DMA_Memory0BaseAddr = (uint32_t)GYRO_buffer;

         dma.DMA_DIR = DMA_DIR_PeripheralToMemory;

         dma.DMA_BufferSize = 11;

         dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

         dma.DMA_MemoryInc = DMA_MemoryInc_Enable;

         dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;

         dma.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

         dma.DMA_Mode = DMA_Mode_Circular;

         dma.DMA_Priority = DMA_Priority_VeryHigh;

         dma.DMA_FIFOMode = DMA_FIFOMode_Disable;

         dma.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;

         dma.DMA_MemoryBurst = DMA_MemoryBurst_Single;

         dma.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

         DMA_Init(DMA1_Stream1,&dma);

         DMA_Cmd(DMA1_Stream1,ENABLE);

        

         /*配置NVIC*/

         nvic.NVIC_IRQChannel = DMA1_Stream1_IRQn;

         nvic.NVIC_IRQChannelPreemptionPriority = 1;

         nvic.NVIC_IRQChannelSubPriority = 1;

         nvic.NVIC_IRQChannelCmd = ENABLE;

         NVIC_Init(&nvic);

        

         DMA_ITConfig(DMA1_Stream1,DMA_IT_TC,ENABLE);

        

         /*配置usart3*/

         usart3.USART_BaudRate = 115200;

         usart3.USART_WordLength = USART_WordLength_8b;

         usart3.USART_StopBits = USART_StopBits_1;

         usart3.USART_Parity = USART_Parity_No;

         usart3.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;

         usart3.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

         USART_Init(USART3,&usart3);/*usart3初始化*/

         //USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);/*配置usart3中断:读数据寄存器非空则中断*/

         USART_Cmd(USART3,ENABLE);/*使能usart3*/

         USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE);

        

}

 

void DMA1_Stream1_IRQHandler(void)

{

         if(DMA_GetITStatus(DMA1_Stream1, DMA_IT_TCIF1))

         {

                   DMA_ClearFlag(DMA1_Stream1, DMA_FLAG_TCIF1);

                   DMA_ClearITPendingBit(DMA1_Stream1, DMA_IT_TCIF1);

                   if(GYRO_buffer[0] == 0x55)

                   {

                            switch(GYRO_buffer[1])

                            {

                                     case 0x51:

                                               gyro.AX = ((short)(GYRO_buffer[3]<<8 | GYRO_buffer[2]))/32768.0*16;//g

                                               gyro.AY = ((short)(GYRO_buffer[5]<<8 | GYRO_buffer[4]))/32768.0*16;

                                               gyro.AZ = ((short)(GYRO_buffer[7]<<8 | GYRO_buffer[6]))/32768.0*16;

                                               gyro.Temperature = ((short)(GYRO_buffer[9]<<8 | GYRO_buffer[8])) /340.0f+36.53f;

                                               led_red_on();

                                               led_green_off();

                                               break;

                                     case 0x52:

                                               gyro.GX = ((short)(GYRO_buffer[3]<<8 | GYRO_buffer[2]))/32768.0*2000;//°/s

                                               gyro.GY = ((short)(GYRO_buffer[5]<<8 | GYRO_buffer[4]))/32768.0*2000;

                                              gyro.GZ = ((short)(GYRO_buffer[7]<<8 | GYRO_buffer[6]))/32768.0*2000;

                                               gyro.Temperature = ((short)(GYRO_buffer[9]<<8 | GYRO_buffer[8])) /340.0f+36.53f;

                                               led_red_off();

                                               led_green_on();

                                               break;

                                     case 0x53:

                                               gyro.PITCH = ((short)(GYRO_buffer[3]<<8 | GYRO_buffer[2]))/32768.0*180;//度

                                               gyro.ROLL = ((short)(GYRO_buffer[5]<<8 | GYRO_buffer[4]))/32768.0*180;

                                               gyro.YAW = ((short)(GYRO_buffer[7]<<8 | GYRO_buffer[6]))/32768.0*180;

                                               gyro.Temperature = ((short)(GYRO_buffer[9]<<8 | GYRO_buffer[8])) /340.0f+36.53f;

                                               led_red_on();

                                               led_green_on();

                                               break;

                            }

                   }

         }

}

其中GYRO结构体的结构是这样的:

typedef struct

{

         float AX;

         float AY;

         float AZ;

         float GX;

         float GY;

         float GZ;

         float PITCH;

         float ROLL;

         float YAW;

         float Temperature;

}GYRO;

 

我开始在调试的过程中一直进不了DMA中断,后来发现是DMA配置的问题。下面把DMA配置部分的代码单独调出来说一下

         /*配置DMA*/        

         DMA_Cmd(DMA1_Stream1,DISABLE);

         while(DMA_GetCmdStatus(DMA1_Stream1) != DISABLE);//等待DMA可配置

         DMA_DeInit(DMA1_Stream1);

         dma.DMA_Channel= DMA_Channel_4;

         dma.DMA_PeripheralBaseAddr = (uint32_t)&(USART3->DR);

         dma.DMA_Memory0BaseAddr = (uint32_t)GYRO_buffer;

         dma.DMA_DIR = DMA_DIR_PeripheralToMemory;

         dma.DMA_BufferSize = 11;

         dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

         dma.DMA_MemoryInc = DMA_MemoryInc_Enable;

         dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;

         dma.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

         dma.DMA_Mode = DMA_Mode_Circular;

         dma.DMA_Priority = DMA_Priority_VeryHigh;

         dma.DMA_FIFOMode = DMA_FIFOMode_Disable;

         dma.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;

         dma.DMA_MemoryBurst = DMA_MemoryBurst_Single;

         dma.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

         DMA_Init(DMA1_Stream1,&dma);

         DMA_Cmd(DMA1_Stream1,ENABLE);

配置的时候首先用DMA_Cmd(DMA1_Stream1,DISABLE);除能DMA,因为DAM在运行过程中是不能配置的。然后用while(DMA_GetCmdStatus(DMA1_Stream1) != DISABLE);语句等待DMA被除能(可能是因为除能需要一定的时间吧,现在也不是很清楚为啥),确定DMA除能后,开始配置DMA,配置完成后,用DMA_Cmd(DMA1_Stream1,ENABLE);重新使能DMA。然后在使能usart3之后,还要加上USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE);语句,使能usart3的DAM功能。

         关于stm32的DMA功能具体的配置,网上有很多资料,就不详细说啦。这里只把我看了网上的资料后仍然没有解决,最终基本靠自己摸索解决的问题写出来,供大家参考。

文章评论0条评论)

登录后参与讨论
相关推荐阅读
Andrew Young 2016-04-09 16:17
利用Snubber电路消除开关电源和ClassD功放电路中的振铃
概述 开关电源和ClassD功放,因为电路工作在开关状态,大大降低了电路的功率损耗,在当今的电子产品中得到了广泛的应用。由于寄生电感和寄生电容的存在,电路的PWM开关波形在跳变时,常常伴随着振铃现象。...
Andrew Young 2016-04-09 12:38
stm32学习——两轮平衡小车之定时器正交编码器功能应用
废话不说,先上代码 #include "main.h" /* PA7——TIM3_CH2 PA6——TIM3_CH1 PB7——TIM4_CH2 PB6——TIM4_CH1...
Andrew Young 2016-03-18 19:29
stm32学习——两轮平衡小车之硬件设计
学习一种单片机最快的方式莫过于用它实际制作一个作品了,前些天看到有人在玩平衡小车,感觉非常有趣,于是就决定自己动手制作一个基于stm32的两轮平衡小车。从电路板设计,到程序编写,一步一步的,希望自己在...
Andrew Young 2015-10-11 23:41
stm32学习——JLINK与JTAG的区别
调试ARM,要遵循ARM的调试接口协议,JTAG就是其中的一种。当仿真时,IAR、KEIL、ADS等都有一个公共的调试接口,RDI就是其中的一种,那么我们如何完成RDI-->ARM调试协议(...
Andrew Young 2015-10-11 12:10
stm32学习——MDK软件的安装教程
1.安装文件和注册机下载链接http://pan.baidu.com/s/1hqjOCio(本来想传附件,太大传不了) 2.双击打开MDK514.exe文件,开始安装。安装过程与普通软件无异,...
广告
我要评论
0
6
1
2
3
4
5
6
7
8
9
0
广告
关闭 热点推荐上一条 /6 下一条