2021-10-09

1 篇文章 0 订阅
void USART3_Init(u32 baud)
{
  GPIO_InitTypeDef GPIO_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	USART_InitTypeDef USART_InitStruct;
  //GPIO端口设置
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);	//使能USART3,GPIOB时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
	
	//USART3_TX   GPIOA.9
  //串口使用的GPIO口配置  
  // Configure USART3 Rx (PB.11) as input floating    
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;  
  GPIO_Init(GPIOB, &GPIO_InitStruct);
  // Configure USART3 Tx (PB.10) as alternate function push-pull  
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;  
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;  
  GPIO_Init(GPIOB, &GPIO_InitStruct); 
	
#ifdef USART3_RS485
	//USART3_RS485
  GPIO_InitStruct.GPIO_Pin = USART3_RS485_TX_Pin;
  GPIO_InitStruct.GPIO_Mode = USART3_RS485_TX_Mode;
  GPIO_Init(USART3_RS485_TX_Port, &GPIO_InitStruct);
	USART3_RS485_TX_Disable;
#endif
	
  //Usart3 NVIC 配置
  NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;//抢占优先级3
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;		//子优先级3
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStruct);	//根据指定的参数初始化VIC寄存器
	
   //USART 初始化设置
	USART_InitStruct.USART_BaudRate = baud;//串口波特率
	USART_InitStruct.USART_WordLength = USART_WordLength_9b;//字长为8位数据格式
	USART_InitStruct.USART_StopBits = USART_StopBits_1;//一个停止位
	USART_InitStruct.USART_Parity = USART_Parity_Even;//无奇偶校验位
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
	USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//收发模式

  USART_Init(USART3, &USART_InitStruct); //初始化串口3
	
  USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启串口接受中断
	
  USART_Cmd(USART3, ENABLE);                    //使能串口3
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值