国民技术-FT232串口通信-USART2初始化配置

1、USART2初始化

 

USART_InitType USART_InitStructure;
	
    USART_InitStructure.BaudRate = 115200;    // 波特率
    USART_InitStructure.WordLength          = USART_WL_8B;    // 8位
	USART_InitStructure.StopBits            = USART_STPB_1;
	USART_InitStructure.Parity              = USART_PE_NO;
	USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
	USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;


  /* USART2 configuration */
  USART_Init(USART2, USART_InitStruct);

  /* Enable the USART2 Receive interrupt */
  USART_ConfigInt(USART2, USART_INT_RXDNE, ENABLE);
	
  /* Enable USART2 */
  USART_Enable(USART2, ENABLE);

2、GPIO和NVIC初始化

  GPIO_InitType GPIO_InitStructure;
	NVIC_InitType NVIC_InitStructure;

	/* Enable GPIO clock */
	RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_AFIO, ENABLE);
	/* Enable USART2 Clock */
	RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USART2, ENABLE);
	/* Remap GPIO*/
	GPIO_ConfigPinRemap(GPIO_RMP2_USART2,ENABLE);
	// AFIO复用重映射配置寄存器
	/*USART2 的重映像
		这些位可由软件置’1’或置’0’, 与 USART2_RMP_1 配合使用,组成USART2_RMP[1:0],控制 USART2 的 CTS、RTS、CK、TX 和 RX 复用功能在 GPIO 端口的映像。
		00: 没有重映像(CTS/PA0、RTS/PA1、TX/PA2,RX/PA3、CK/PA4);
		01: 重映像(CTS/PD3、RTS/PD4、TX/PD5,RX/PD6、CK/PD7);
		10: 重映像(CTS/PC6、RTS/PC7、TX/PC8,RX/PC9、CK/-);
		11: 重映像(CTS/PA15、RTS/PB3、TX/PB4,RX/PB5、CK/PA4)。
		注: 10 时不支持同步模式*/
	
  
  /* Configure USART2 Tx as alternate function push-pull */
  GPIO_InitStructure.Pin = GPIO_PIN_8;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);


  /* Configure USART2 Rx as input floating */
	GPIO_InitStructure.Pin = GPIO_PIN_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);

	/* Enable the USARTy Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel            = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd         = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

3、USART2_IRQn中断处理

void USART2_IRQHandler(void)
{
  if (USART_GetFlagStatus(USART2, USART_FLAG_RXDNE) != RESET)
  {
    /* Send the received data*/
    USART2_Send_Data();
  }

  /* If overrun condition occurs, clear the ORE flag and recover communication */
  if (USART_GetFlagStatus(USART2, USART_FLAG_OREF) != RESET)
  {
    (void)USART_ReceiveData(USART2);
  }
}

4、USART2接收和发送

/**
 * @brief  Returns the most recent received data by the USARTx peripheral.
 * @param USARTx Select the USART or the UART peripheral.
 *   This parameter can be one of the following values:
 *   USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
 * @return The received data.
 */
uint16_t USART_ReceiveData(USART_Module* USARTx)
{
    /* Check the parameters */
    assert_param(IS_USART_ALL_PERIPH(USARTx));

    /* Receive Data */
    return (uint16_t)(USARTx->DAT & (uint16_t)0x01FF);
}
void USART2_Send_Data(void)
{

	
	if (linecoding.datatype == 7)
	{
		USART2_Rx_Buffer[USART2_Rx_ptr_in++] = USART_ReceiveData(USART2) & 0x7F;
	}
	else if (linecoding.datatype == 8)
	{
		USART2_Rx_Buffer[USART2_Rx_ptr_in++] = USART_ReceiveData(USART2);
	}
	
	
	
  
  /* To avoid buffer overflow */
  if(USART2_Rx_ptr_in == USART_RX_DATA_SIZE)
  {
    USART2_Rx_ptr_in = 0;
  }
	

}

/**
 * @brief  Transmits single data through the USARTx peripheral.
 * @param USARTx Select the USART or the UART peripheral.
 *   This parameter can be one of the following values:
 *   USART1, USART2, USART3, UART4, UART5, UART6 or UART7.
 * @param Data the data to transmit.
 */
void USART_SendData(USART_Module* USARTx, uint16_t Data)
{
    /* Check the parameters */
    assert_param(IS_USART_ALL_PERIPH(USARTx));
    assert_param(IS_USART_DATA(Data));

    /* Transmit Data */
    USARTx->DAT = (Data & (uint16_t)0x01FF);
}

5、往USART2发送数据

		for (i = 0; i < 15; i++)
		{
			USART_SendData(USART2, *(data_buffer + i));
			while(USART_GetFlagStatus(USART2, USART_FLAG_TXDE) == RESET); 
		}  

6、接收的数据放在USART2_Rx_Buffer 数组

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值