STM32F103C8T6三串口配置(亲测有效)

时钟配置
这个时钟配置很关键,很多人都是以为这一步没弄好而串口失效

void RCC_Configuration(void)
{
	SystemInit(); 

	/*APB1 36MHz ´*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 |RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4
						|RCC_APB1Periph_USART2|RCC_APB1Periph_USART3, ENABLE);
	
	/*APB2 72MHz ´*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB
  						| RCC_APB2Periph_GPIOC| RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE
						| RCC_APB2Periph_USART1, ENABLE);
}

注意看三个串口分别对应的时钟

串口配置

void USART1_Configuration(void)
{
	USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	/* Configure USART1 Rx (PA.10) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;		  //¸	   
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/* Configure USART1 Tx (PA.09) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;			  //¸´
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	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;					//
	/* Configure USART1 */
	USART_Init(USART1, &USART_InitStructure);							//
	/* Enable USART1 Receive and Transmit interrupts */
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);                    //
	USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//
	/* Enable the USART1 */
	USART_Cmd(USART1, ENABLE);	                  //
}


void USART2_Configuration(void)
{
	USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	/* Configure USART2 Rx (PA.3) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;		  //¸	   
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/* Configure USART2 Tx (PA.2) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;			  //¸
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	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;					//
	/* Configure USART2 */
	USART_Init(USART2, &USART_InitStructure);							//
	/* Enable USART2 Receive and Transmit interrupts */
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);                    //ʹÄܽÓÊÕÖжÏ
	//USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);//
	/* Enable the USART2 */
	USART_Cmd(USART2, ENABLE);	                  //
}

void USART3_Configuration(void)
{
	USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	/* Configure USART3 Rx (PB.11) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;		  //¸¡¿ÕÊäÈëģʽ	   
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	/* Configure USART3 Tx (PB.10) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;			  //¸´
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	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;					//
	/* Configure USART3 */
	USART_Init(USART3, &USART_InitStructure);							//
	/* Enable USART3 Receive and Transmit interrupts */
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);                    //
	//USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);//
	/* Enable the USART3 */
	USART_Cmd(USART3, ENABLE);	                  //
}

串口中断优先级配置

void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	            //Ö»ÐèÐÞ¸ÄÒ»´Î
	
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;	   	//
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;		   //ÇÀÕ¼
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;			   //×ÓÓÅÏȼ¶Îª0
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;		//
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;	   	//
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;		   //
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;			   //
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;		//
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;	   	// 
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;		   //
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;			   //
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;		//
	NVIC_Init(&NVIC_InitStructure);
	

}

串口中断函数

void USART1_IRQHandler (void)
{
	char res1;
	
	
	if (USART_GetITStatus(USART1, USART_IT_RXNE) !=RESET)										
	{
	    res2=USART_ReceiveData(USART1);  
		  USART_SendData(USART1,res1); 
		  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) ==RESET);              //
		  USART_ClearFlag(USART1, USART_FLAG_RXNE);                              //

	}
	
}
void USART2_IRQHandler (void)
{
	char res2;
	
	
	if (USART_GetITStatus(USART2, USART_IT_RXNE) !=RESET)										
	{
	    res2=USART_ReceiveData(USART2);  
		  USART_SendData(USART2,res2); 
		  while(USART_GetFlagStatus(USART2, USART_FLAG_TC) ==RESET);              //
		  USART_ClearFlag(USART2, USART_FLAG_RXNE);                              //

	}
	
}
void USART3_IRQHandler (void)
{
	char res3;
	
	
	if (USART_GetITStatus(USART3, USART_IT_RXNE) !=RESET)										
	{
	    res3=USART_ReceiveData(USART3);  
		  USART_SendData(USART3,res3); 
		  while(USART_GetFlagStatus(USART3, USART_FLAG_TC) ==RESET);              //
		  USART_ClearFlag(USART3, USART_FLAG_RXNE);                              //

	}
	
}
  • 14
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值