本帖最后由 shipeng1989 于 2020-7-24 13:55 编辑
最近有项目需要用到STM32F103RCT6的USART1,UART4,UART5。其中USART1和UART4采用DMA方式收发且都能正常工作,唯独这个UART5死活就是能收不能发。而更诡异的是总共焊了5块样板,相同的烧录文件其中有2块UART5可以正常收发另外3块UART5能收不能发。进一步查找原因发现将2块正常的样板和3块不正常的样板交换单片机IC后,发现问题出在单片机IC上,2台正常的样板现在不正常了,而换了IC的2块原来异常的板也正常了。
以下是我的初始化源代码:
void UART45_Init(void)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4|RCC_APB1Periph_UART5, ENABLE);
USART_DeInit(UART4);
//UART45_TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//UART4_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//UART5_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
//USART 初始化设置
USART_InitStructure.USART_BaudRate = 9600;
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(UART4, &USART_InitStructure);
USART_Init(UART5, &USART_InitStructure);
USART_ITConfig(UART4, USART_IT_IDLE, ENABLE);
USART_ITConfig(UART5, USART_IT_IDLE, ENABLE);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
USART_Cmd(UART4, ENABLE);
USART_Cmd(UART5, ENABLE);
DMA_DeInit(DMA2_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART4->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(u4rxd_array);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = sizeof(u4rxd_array);
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_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel3, &DMA_InitStructure);
DMA_ITConfig(DMA2_Channel3, DMA_IT_TC, ENABLE);
USART_DMACmd(UART4, USART_DMAReq_Rx, ENABLE);
DMA_Cmd (DMA2_Channel3,ENABLE);
DMA_DeInit(DMA2_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART4->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(u4txd_array);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 0;
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_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel5, &DMA_InitStructure);
DMA_ITConfig(DMA2_Channel5, DMA_IT_TC, DISABLE);
USART_DMACmd(UART4, USART_DMAReq_Tx, ENABLE);
DMA_Cmd (DMA2_Channel5,DISABLE);
}
debug发现不正常的样板向UART5->DR写数据其值不会发生变化,请高人指点一下@香水城 |