你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

请教大家关于STM32L151C8T6串口3只能收不能发问题.

[复制链接]
回家库 提问时间:2017-7-21 11:41 /
在使用STM32L151C8T6的时候遇到的问题,串口3只能收不能发,已经做过串口配置和中断设置,同样的设置,串口1收发正常,求助一下,这个是什么原因?配置代码如下,请大家帮忙解答一下。
void USART3_Config()
{
   //GPIO端口设置
    GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  


  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //使能USART3
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);   //使能GPIOB时钟

//connect PB.10 to usart3's tx
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
  //connect PB.11 to usart3's rx
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);

  
  //USART3_TX   PB.10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //GPIO Output Mode
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
   
    //USART3_RX   PB.11
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//浮空输入
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
   GPIO_Init(GPIOB, &GPIO_InitStructure);  


   //USART3 初始化设置
USART_InitStructure.USART_BaudRate = 115200;//一般设置为115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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; //收发模式
    USART_Init(USART3, &USART_InitStructure); //初始化串口
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启中断
    USART_Cmd(USART3, ENABLE);                    //使能串口
  USART_ClearITPendingBit(USART3, USART_IT_TC);//清除串口TC位
}


    //Usart3 NVIC 配置

NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  //子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器


收藏 评论4 发布时间:2017-7-21 11:41

举报

4个回答
xmshao 回答时间:2017-7-21 17:19:50
UART1 与 UART2是不同的总线上;

注意有无跟其它管脚冲突的问题;

你的配置感觉没啥问题,外部线路也仔细检查下,焊接和连接方面;
MrJiu 回答时间:2017-7-22 09:39:11
还有,请使用Cube工具去配置,HAL库才是官方支持的!!!
废鱼 回答时间:2017-7-22 10:35:00
RX引脚没有配置
小景在线 回答时间:2017-8-6 01:04:39
不知道楼之调试成功没有,下面是我自己的代码,调试是成功的,仅供参考!
void MYUSART3_init(void)
{  
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable GPIO clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);   //PC10-TX   PC11-RX

  /* Enable UART clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

  /* Connect PXx to USARTx_Tx */
  GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_USART3);

  /* Connect PXx to USARTx_Rx */
  GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_USART3);
  
  /* Configure USART Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
   
  /* Configure USART Rx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  
  /* Enable the USARTx Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  

  USART_InitStructure.USART_BaudRate = 57600;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_2;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  /* USART configuration */
  USART_Init(USART3,&USART_InitStructure);
  
  /* Enable USART */
  USART_Cmd(USART3, ENABLE);
   
  USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2

查看全部评分

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版