打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
STM32F0 TX,RX反了,STM32 TX/RX SWAP功能又救了我一次
 本帖最后由 damiaa 于 2016-9-21 14:35 编辑

今天又犯了一次低级错误,STM32F0 TX,RX反了,STM32 TXRXSWAP功能又救了我一次

今天板子回来,串口没反应,知道坏了,估计是TX,RX反了。
但还是记得以前把TX,RX用软件调换过。今天在QQ和人聊,然后也看了库。
stm32f0xx_usart.c中找到下面函数:
/**
  * @brief  Enables or disables the swap Tx/Rx pins.
  * @param  USARTx: where x can be 1 or 2 to select the USART peripheral.
  * @param  NewState: new state of the USARTx TX/RX pins pinout.
  *          This parameter can be:
  *            @arg ENABLE: The TX and RX pins functions are swapped.
  *            @arg DISABLE: TX/RX pins are used as defined in standard pinout
  * @note   This function has to be called before calling USART_Cmd() function.  
  * @retval None
  */
void USART_SWAPPinCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_USART_ALL_PERIPH(USARTx));
  assert_param(IS_FUNCTIONAL_STATE(NewState));


  if (NewState != DISABLE)
  {
    /* Enable the SWAP feature by setting the SWAP bit in the CR2 register */
    USARTx->CR2 |= USART_CR2_SWAP;
  }
  else
  {
    /* Disable the SWAP feature by clearing the SWAP bit in the CR2 register */
    USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_SWAP);
  }
}


于是在串口初始化后调用 USART_SWAPPinCmd(COM_USART[COM1], ENABLE);
  NVIC_InitTypeDef NVIC_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  USART_InitStructure.USART_BaudRate = 115200;
  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;
  STM_COMInit(COM1, &USART_InitStructure);
        
  USART_SWAPPinCmd(COM_USART[COM1], ENABLE);



串口正常了。
喜欢犯错的朋友们不用犯愁了。哈哈。

附:F1好像没这个功能哦。 看控制寄存器 2(USART_CR2)就知道了。



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
STM32F207串口
实现蓝牙HC
【新提醒】STM32 USART 串口 DMA 接收和发送的源码详解!
串口通讯的原理、代码实现及注释
单片机外围模块漫谈之五,USB开发,这个错误你犯了吗?
stm32串口程序(全)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服