无双个妹 发表于 2014-12-30 15:13:13

关于stm32f103的usart1的端口重映射问题

本帖最后由 无双个妹 于 2014-12-30 15:40 编辑

我参考了网上有人发的帖子,但是发现自己用了,TX发出来全是高电平,没有竖线(低电平)

以下是我的程序请大家帮忙端口设置

      GPIO_InitTypeDef GPIO_InitStructure;
            

          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_Init(GPIOB , &GPIO_InitStructure);
   

          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
          GPIO_Init(GPIOB , &GPIO_InitStructure);usart1重映射设置

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
       GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);
       RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
   
          USART_InitTypeDef USART_InitStructure;
      
          USART_ClockInitTypeDefUSART_ClockInitStructure;

      USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
      USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
      USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
      USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
      USART_ClockInit(USART1 , &USART_ClockInitStructure);

      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(USART1 , &USART_InitStructure);
            
          USART_Cmd(USART1 , ENABLE);为了使用这个端口我这样设置,AFIO_MAPR|=1<<2;
但是总是报错                           Error: identifier "AFIO_MAPR" is undefined

wambob 发表于 2014-12-30 15:37:03

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);//开启端口B和复用功能时钟
GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);//使能端口重映射
GPIO_InitTypeDef GPIO_InitStructure;
//uart 的GPIO重映射管脚初始化
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//悬空输入
GPIO_Init(GPIOB,&GPIO_InitStructure);




RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_InitTypeDef USART_InitStructure;
//串口参数配置:9600,8,1,无奇偶校验,无硬流量控制 ,使能发送和接收
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(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);//串口接收中断
USART_Cmd(USART1, ENABLE);

无双个妹 发表于 2014-12-30 15:38:13

wambob 发表于 2014-12-30 15:37
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);//开启端口B和复用功能时钟...

我就是抄这个人的例程的,有问题,实习不了,可能是单片机不一样

wambob 发表于 2014-12-30 15:48:14

本帖最后由 wambob 于 2014-12-30 15:50 编辑

貌似你的顺序不对吗,而且有地方不一样,你改程序了

无双个妹 发表于 2014-12-30 15:49:54

wambob 发表于 2014-12-30 15:48
貌似你的顺序不对吗,你改程序了

我把程序的初始化,分成时钟,端口分别使能

wambob 发表于 2014-12-30 15:52:27

无双个妹 发表于 2014-12-30 15:49
我把程序的初始化,分成时钟,端口分别使能

按你的说法,除了顺序外,因该代码一样多,看你的好象不全

奔跑小蜗牛 发表于 2014-12-30 15:56:07

单片机不一样?你的不是103系列吗?

无双个妹 发表于 2014-12-30 16:14:24

奔跑小蜗牛 发表于 2014-12-30 15:56
单片机不一样?你的不是103系列吗?

是103,但是后面型号不一样,

奔跑小蜗牛 发表于 2014-12-30 16:28:38

你是用的IAR吧?关于 Error: identifier "AFIO_MAPR" is undefined

1、在程序前面加上定义。
#include <xxxx.h>
还不行的话就自己在程序开头重新定义一下就100% OK了。
#define AFIO_MAPR 0x......;
2、重新安装过软件。
也许是软件出错了,这个当然包括软件的安装目录有中文;安装的时候更改了安装路径等等都会出现这种情况。所以最好的安装方法就是“一路回车”。

无双个妹 发表于 2014-12-30 16:43:42

奔跑小蜗牛 发表于 2014-12-30 15:56
单片机不一样?你的不是103系列吗?

其实都是一样的,STM32F103C8和STM32F103RB都差不多
页: [1] 2
查看完整版本: 关于stm32f103的usart1的端口重映射问题