搜索
bottom↓
回复: 0

大家好, 请教下 STM32F03vct6 串口配置不成功

[复制链接]

出0入0汤圆

发表于 2021-6-22 13:30:24 | 显示全部楼层 |阅读模式
情况是这样,想用STM32F03vct6 串口1 和串口2。 串口1 接引脚PA9 和PA10 ,能够和电脑通信。 串口2 重映射到PD5 和 PD6引脚,不能够和电脑通信。
代码如下,请指教 ,谢谢啊 

void usart1_init(u32 bound){
  
       GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
         
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);        //ʹÄÜUSART1£¬GPIOAʱÖÓ
  
        //USART1_TX   GPIOA.9
              GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
              GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;       
          GPIO_Init(GPIOA, &GPIO_InitStructure);
   
  //USART1_RX          GPIOA.10
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
          GPIO_Init(GPIOA, &GPIO_InitStructure);

  //Usart1 NVIC
         NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;               
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;               
        NVIC_Init(&NVIC_InitStructure);       
  
   //USART ³õʼ»¯ÉèÖÃ

        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(USART1, &USART_InitStructure);
          USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
          USART_Cmd(USART1, ENABLE);               
       
}


void usart2_init(u32 bound){

       GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
         
       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);       
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD,ENABLE);
          GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);       
       
                //USART2_TX   GPIOD.5
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;       
         GPIO_Init(GPIOD, &GPIO_InitStructure);
   
          //USART2_RX          GPIOD.6
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ;
          GPIO_Init(GPIOD, &GPIO_InitStructure);

          NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
         
           //USART
        USART_InitStructure.USART_BaudRate = 9600;
        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);
        USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
         USART_Cmd(USART2, ENABLE);                  

}

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-29 08:14

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表