struct DMA_UART_RX DMAUart2Rx;
void Uart2_GPIO_Configrution()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //1ü½Å2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOA, &GPIO_InitStructure); //TX3õê¼»ˉ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //1ü½Å3
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿Õêäèë
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Uart2Configrution()
{
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitStructure.USART_BaudRate = 115200 ; //2¨ìØÂê9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //×Ö3¤8λ
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1λí£Ö1×Ö½ú
USART_InitStructure.USART_Parity = USART_Parity_No; //ÎTÆæżD£Ñé
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;//ÎTá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx |
USART_Mode_Tx; //′ò¿aRx½óêÕoíTx·¢Ëí1|Äü
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); //′®¿ú¿ÕÏDÖD¶Ï
USART_Cmd(USART2, ENABLE);
}
DMA_InitTypeDef DMA_InitStructure;
/********************dmaurat2½óêü**********************/
void Uart2Rx_Dma1Channle6_Configrution()
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel6);
DMA_Cmd(DMA1_Channel6, DISABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART2->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&DMAUart2Rx.buf; //′óadc¶áè¡
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //íaéèμ½Äú′æ
DMA_InitStructure.DMA_BufferSize = 10;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //Enable
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //32bits
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel6,DMA_IT_TC,ENABLE);
USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
DMA_Cmd(DMA1_Channel6, ENABLE);
}
u8 GetUart2DMAIsOk(u8 timeout)
{
int DmaBufRetain;
DmaBufRetain = DMA_GetCurrDataCounter(DMA1_Channel6); //1ms2éÑˉò»′Î μ±Ç°dmaê£óà¶àéùûóDíê3é
DMAUart2Rx.timercnt++;
if(DMAUart2Rx.retainlen < DmaBufRetain) //ÕyÔú½óêü
{
DMAUart2Rx.retainlen = DmaBufRetain;
DMAUart2Rx.endflag = 0;
DMAUart2Rx.timercnt = 0;
return 0;
}
else
{
if(DMAUart2Rx.timercnt>timeout) //timeout=1 Ïàμ±óú2msμÄÅD¶¨
{
DMAUart2Rx.timercnt = 0;
DMAUart2Rx.retainlen = 0;
DMAUart2Rx.len = 10 - DMAUart2Rx.retainlen;
Uart2Rx_Dma1Channle6_Configrution();
DMAUart2Rx.endflag = 1;
return 1;
}
else
{
return 0;
}
}
}
void uart2int()
{
Uart2_GPIO_Configrution();
Uart2Configrution();
Uart2Rx_Dma1Channle6_Configrution();
}
使用的时候将if( 1 == GetUart2DMAIsOk(1) ) 判断放入1ms的tick中,定时器事实查询方法,问什么一直发送数据接受不到啊?谁能解答下???
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
{
temp_val = USART2->SR;
temp_val = USART2->DR;
DMA_Cmd(DMA1_Channel6,DISABLE);
DMAUart2Rx.len = 10 - DMA_GetCurrDataCounter(DMA1_Channel6);
Uart2Rx_Dma1Channle6_Configrution();
DMAUart2Rx.endflag = 1;
}
这样判断, DMAUart2Rx.endflag = 1 是结束标志,这样可以判断在主程序中判断接受完成,为什么接受数据的个数是正确 的,但是接受的数据不对,我想肯定是串口2的dma配置有错误,但是还是找不出来,哪个大神得空帮忙看下,指导下???
一周热门 更多>