关于stm32f4系列开发板例程 串口与网口的冲突问题

关于stm32f4系列开发板例程 串口与网口的冲突问题

项目场景:

项目场景:在将正点原子的例程:网络通信实验 与 485串口实验进行合并

问题描述:

在进行调试时发现,单独使用网口或者串口,都能正常传输数据

原因分析:

PA2脚同时连接了LAN8720的MDIO脚和USART2的Tx脚,单独使用网口或者串口,都能正常传输数据,但是同时使用就出问题了 ![引脚复用](https://img-blog.csdnimg.cn/20210310103209763.png#pic_center)

解决方案:

换一个空闲的串口即可
void RS485_Init(u32 bound)
{  	 
	
 	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //ê1?üGPIOBê±?ó
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//ê1?üUSART3ê±?ó
	
  //′??ú2òy???′ó?ó3é?
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3); //GPIOB2?′ó??aUSART3
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3); //GPIOB3?′ó??aUSART3
	
	//USART3    
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; //GPIOB2ó?GPIOB3
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//?′ó?1|?ü
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	//?ù?è100MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //í?íì?′ó?ê?3?
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //é?à-
	GPIO_Init(GPIOB,&GPIO_InitStructure); //3?ê??ˉPA2£?PA3
	
	//PG8í?íìê?3?£?485?£ê?????  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //GPIOG8
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ê?3?
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	//?ù?è100MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //í?íìê?3?
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //é?à-
	GPIO_Init(GPIOG,&GPIO_InitStructure); //3?ê??ˉPG8
	

   //USART3 3?ê??ˉéè??
	USART_InitStructure.USART_BaudRate = bound;//2¨ì??êéè??
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×?3¤?a8??êy?Y??ê?
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò???í£?1??
	USART_InitStructure.USART_Parity = USART_Parity_No;//?T????D£?é??
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//?Tó2?têy?Yá÷????
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//ê?·¢?£ê?
  USART_Init(USART3, &USART_InitStructure); //3?ê??ˉ′??ú2
	
  USART_Cmd(USART3, ENABLE);  //ê1?ü′??ú 2
	
	USART3->CR1|=1<<2;  		//′??ú?óê?ê1?ü

	USART_ClearFlag(USART3, USART_FLAG_TC);
	USART_ITConfig(USART3,USART_IT_TC,ENABLE);   //?a??·¢?í?D??
	
#if EN_USART3_RX	
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//?a???óêü?D??
	
	//USART3 NVIC ????
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//?à??ó??è??3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;		//×óó??è??3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQí¨μàê1?ü
	NVIC_Init(&NVIC_InitStructure);	//?ù?Y???¨μ?2?êy3?ê??ˉVIC??′??÷?¢

#endif	
	
	RS485_TX_EN=0;				//??è??a?óê??£ê?	
}

你可能感兴趣的:(串口和网口冲突,stm32)