[STM32F1] STM32F103RE 串口2通信异常

[复制链接]
942|7
 楼主| NIKEjian 发表于 2021-12-23 09:32 | 显示全部楼层 |阅读模式
用的是串口实验的例子,main里面就是循环Uart2_SendStr("AT\r\n"),结果就是打印为
sendbuf=
sendbuf=
sendbuf=
sendbuf=
具体看下面:硬件连线上确认没问题,这是属于没有发送出去吗?还请各位帮忙看看,感激不尽!!!!


static void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* 嵌套向量中断控制器组选择 */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

  /* 配置USART为中断源 */
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  /* 抢断优先级*/
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  /* 子优先级 */
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  /* 使能中断 */
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  /* 初始化配置NVIC */
  NVIC_Init(&NVIC_InitStructure);
}


void uart2_init(u32 bound){
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;

        // 打开串口GPIO的时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
        
        // 打开串口外设的时钟
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

        // 将USART Tx的GPIO配置为推挽复用模式
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

  // 将USART Rx的GPIO配置为浮空输入模式
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        // 配置串口的工作参数
        // 配置波特率
        USART_InitStructure.USART_BaudRate = bound;
        // 配置 针数据字长
        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;
        // 完成串口的初始化配置
        USART_Init(USART2, &USART_InitStructure);
        
        // 串口中断优先级配置
        NVIC_Configuration();
        
        // 使能串口接收中断
        USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);        
        
        // 使能串口
        USART_Cmd(USART2, ENABLE);               


}

void UART2_send_byte(uint8_t   data)
{
    USART_SendData(USART2, data);
    while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);

}

void Uart2_SendStr(char *SendBuf)
{
   unsigned int k=0;
  do
  {
      printf("sendbuf=%s\n", *(SendBuf + k));
      UART2_send_byte( *(SendBuf + k) );
      k++;
  } while(*(SendBuf + k)!='\0');

  /* 等待发送完成 */
  while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET)
  {}
}
呐咯密密 发表于 2021-12-23 11:55 | 显示全部楼层
把*(SendBuf + k)改为一个16进制常量试试
 楼主| NIKEjian 发表于 2021-12-23 12:24 | 显示全部楼层
呐咯密密 发表于 2021-12-23 11:55
把*(SendBuf + k)改为一个16进制常量试试

16进制常量也发送不了,前面配置我觉得没有问题啊,就是发不出去
呐咯密密 发表于 2021-12-23 12:48 | 显示全部楼层
NIKEjian 发表于 2021-12-23 12:24
16进制常量也发送不了,前面配置我觉得没有问题啊,就是发不出去

printf是用的哪个USART
 楼主| NIKEjian 发表于 2021-12-23 14:17 | 显示全部楼层
呐咯密密 发表于 2021-12-23 12:48
printf是用的哪个USART

用的是USART1
呐咯密密 发表于 2021-12-23 14:26 | 显示全部楼层

printf("sendbuf=%d\n", 10);
printf("sendbuf=%x\n", 0x55);
这样有打印吗
 楼主| NIKEjian 发表于 2021-12-23 16:07 | 显示全部楼层
呐咯密密 发表于 2021-12-23 14:26
printf("sendbuf=%d\n", 10);
printf("sendbuf=%x\n", 0x55);
这样有打印吗

这样由打印,printf("sendbuf=%s\n", “AT”),也可以有打印
呐咯密密 发表于 2021-12-23 16:44 | 显示全部楼层
NIKEjian 发表于 2021-12-23 16:07
这样由打印,printf("sendbuf=%s\n", “AT”),也可以有打印

那就不是驱动的问题啊,说明你之前写的那个没取到值,还有UART2_send_byte( *(SendBuf + k) );用的是串口2,你是打印不出来的,因为你接到串口助手的是串口1
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

15

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部