程序如下:void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=921600;
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(USART1,&USART_InitStructure);
//USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&USART1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr=(u32)receive_data;
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize=UART_RX_LEN ;
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_Normal;
DMA_InitStructure.DMA_Priority=DMA_Priority_High;
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
DMA_Init(DMA1_Channel5,&DMA_InitStructure);
DMA_ITConfig (DMA1_Channel5,DMA_IT_TC,ENABLE);
DMA_Cmd(DMA1_Channel5, ENABLE);
}
void Usart1_Send(u8 *buf,u8 len)
{
u8 t=0;
for(t=0;t<len;t++)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1,buf[t]);
}
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
void USART1_IRQHandler(void)
{
u16 temp=0;
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
USART_ClearITPendingBit( USART1, USART_IT_IDLE );
temp= USART1->SR;
temp = USART1->DR;
DMA_Cmd(DMA1_Channel5, DISABLE );
temp = UART_RX_LEN - DMA_GetCurrDataCounter(DMA1_Channel5);
printf ("The lenght:%d
",temp);
printf ("The data:
");
Usart1_Send(receive_data,temp);
printf ("
Over!
");
//USART_ClearITPendingBit(USART1, USART_IT_IDLE);
DMA_Cmd(DMA1_Channel5, DISABLE ); DMA_SetCurrDataCounter(DMA1_Channel5,UART_RX_LEN);
DMA_Cmd(DMA1_Channel5, ENABLE);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
也不是外部硬件的问题,我之前用中断的方式没问题啊
接收不完整啊
加了一个延时函数就OK了
一周热门 更多>