我的程序的思路大致是:USART3接收到的MPU6050模块的数据通过DMA存到一个11bit的数组GYRO_buff[11]中,存满11个数据之后产生DMA传输完成中断,在中断中的处理数据。
可是现在有几个问题:1.根本进不了DMA中断 2.将GYRO_buff[11]中的数据打印出来发现也不是想要的。。求问大神,可能是什么原因呀?下面是我的程序。
USART3和DMA配置函数:
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);
/*配置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*/
/*配置NVIC*/
nvic.NVIC_IRQChannel = DMA1_Stream1_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 0;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
/*配置DMA*/
DMA_DeInit(DMA1_Stream1);
while(DMA_GetCmdStatus(DMA1_Stream1) != DISABLE);//等待DMA可配置
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_ITConfig(DMA1_Stream1,DMA_IT_TC,ENABLE);
DMA_Cmd(DMA1_Stream1,ENABLE);
USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE);
}
DMA中断处理函数:
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;
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;
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;
break;
}
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
配置函数:
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);
}
一周热门 更多>