野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13039|回复: 6

用USART2通信问题

[复制链接]
发表于 2015-7-17 01:53:25 | 显示全部楼层 |阅读模式
用USART1通信有案例,我想用USART2通信试试,修改了一些配置,但是还是不能成功,求帮助,下面是我修改的代码
void USART2_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
       
        /* config USART2 clock */
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
        /* USART2 GPIO config */
        /* Configure USART2 Tx (PA.09) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);   
        /* Configure USART2 Rx (PA.10) as input floating*/
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        /* USART2 mode config */
        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;
        USART_Init(USART2, &USART_InitStructure);
       

        USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
       
        USART_Cmd(USART2, ENABLE);
}


int fputc(int ch, FILE *f)
{
               
                USART_SendData(USART2, (uint8_t) ch);
               
               
                while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);               
       
                return (ch);
}


主函数:

        USART2_Config();
       
        NVIC_Configuration();

        printf("\r\n串口显示 \r\n");       
        printf("\r\n成功 \r\n");       

回复

使用道具 举报

发表于 2015-7-17 08:46:22 | 显示全部楼层
PA9 PA10是usart1的引脚,你要使用usart2的专用引脚才可以
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-7-18 13:31:46 | 显示全部楼层
flyleaf 发表于 2015-7-17 08:46
PA9 PA10是usart1的引脚,你要使用usart2的专用引脚才可以

扯,A2,和A3改了不行
回复 支持 反对

使用道具 举报

发表于 2015-7-18 15:51:19 | 显示全部楼层
逗,改了之后是要用pa2 pa3引脚的,你还是用usb to usart那个接口当然不行,那个接口是接到pa9、pa10的,你要另外引出来测试
回复 支持 反对

使用道具 举报

发表于 2015-7-18 15:58:08 | 显示全部楼层
这里有代码,可以下载来学习下:
http://www.firebbs.cn/thread-8673-1-1.html
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-7-18 20:15:40 | 显示全部楼层
除了USART1能用其他的都不行,真的
回复 支持 反对

使用道具 举报

发表于 2015-10-10 16:36:34 | 显示全部楼层
跳线帽在作怪,拔了就好
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-9-21 04:25 , Processed in 0.023993 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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