用的是STM32F405,使用USART3
问题描述: 连续发送多次数据,只进一次空闲中断,然后DMA不再接收数据,找不到问题在哪?!求教!求教!
程序代码如下:
void USART3_IRQHandler(void) //串口中断服务函数
{
uint32_t temp = 0;
uint16_t i = 0;
unsigned char r = 0;
if((SET==USART_GetFlagStatus(USART3,USART_IT_IDLE)))
{
DMA_Cmd(DMA1_Stream1, DISABLE);
temp = USART3->SR;
temp = USART3->DR; //清USART_IT_IDLE标志
for( r = 0; r < 7; r++)
{
USART3_temp += USART3_R_BUF[r];
}
temp = 128 - DMA_GetCurrDataCounter(DMA1_Stream1);
for( r = 7; r >= 0; r--)
{
USART3_R_BUF[r] = 0;
}
DMA1_Stream1->NDTR = 128; //重新装载,从地址0开始
DMA_Cmd(DMA1_Stream1, ENABLE);
DMA_ClearITPendingBit(DMA1_Stream1, USART_IT_IDLE);
receive_flag = 1; //接收数据标志位置1
}
}
配置为:
void USART3_Configuration(uint32_t bound) // 串口配置函数
{
//一定注意配置顺序,先除能再使能等等
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure; //
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //使能GPIOC时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//使能USART3时钟,挂在APB1上
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); // 开启DMA1时钟
/// //////////////////////////////////////////////////////////////////////////
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_USART3); //GPIOC10复用USART3_TX
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_USART3); //GPIOC11复用USART3_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; //GPIO10,GPIO11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 ?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //上拉
// 串口3对应引脚复用映射
//USART3 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//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(USART3, &USART_InitStructure); //初始化串口3
USART_Cmd(USART3, ENABLE); //使能串口3
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //串口3中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
DMA_Cmd(DMA1_Stream1, DISABLE);
while (DMA_GetCmdStatus(DMA1_Stream1) != DISABLE){} //等待DMA 可配置
DMA_DeInit(DMA1_Stream1);
DMA_InitStructure.DMA_Channel = DMA_Channel_4; //外设的接收通道
DMA_InitStructure.DMA_PeripheralBaseAddr = (int32_t)&USART3->DR; //source buf
DMA_InitStructure.DMA_Memory0BaseAddr = (int32_t)USART3_R_BUF; //target buf
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //外设寄存器 -> 内存缓冲区?
DMA_InitStructure.DMA_BufferSize = 16; //BuffSize;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA_Mode_Circular; //DMA_Mode_Normal; //接收设计为循环收,否则接收一次后,无法正常接收到数据
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //指定FIFO阈值水平
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;// DMA_MemoryBurst_Single; //指定Burst转移配置内存传输,单次传输
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; // 指定的burst转移配置外围转移
DMA_Init(DMA1_Stream1, &DMA_InitStructure);
USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE); // 开启串口DMA接
DMA_Cmd(DMA1_Stream1, ENABLE);
USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
DMA_ClearITPendingBit(DMA1_Stream1, USART_IT_IDLE);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
谢谢!果然这边写错了,但是我改成USART_ClearITPendingBit(USART3, USART_IT_IDLE)之后,她一直都不进中断了,只是在接收寄存器里面不停重载。。。。。
一周热门 更多>