在进行原理图设计的时候发现管脚的分配之间有冲突,需要对管脚进行重映射,在手册中了解到STM32 上有很多I/O口,也有很多的内置外设像:I2C,ADC,ISP,USART等 ,为了节省引出管脚,这些内置外设基本上是与I/O口共用管脚的,也就是I/O管脚的复用功能。但是STM32还有一特别之处就是:很多复用内置的外设的 I/O引脚可以通过重映射功能,从不同的I/O管脚引出,即复用功能的引脚是可通过程序改变的。 第一次这么干感觉心里没底,所以针对USART1在STM32F103RBT6的板子上实现了一把,以下是相关的测试代码: - /*****************************************************************************
- //函数名:void Uart1_Init(void)
- //功能:串口(USART1)重映射初始化配置函数,由TX PA9~PB6 RX PA10~~PB7
- *****************************************************************************/
- void Uart1_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |
- RCC_APB2Periph_AFIO,ENABLE);//开启端口B和复用功能时钟
- GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);//使能端口重映射
- GPIO_InitTypeDef GPIO_InitStructure;
- //uart 的GPIO重映射管脚初始化 PB6 usart1_TX PB7 USART_RX
- 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);
- }
复制代码
简要分析重映射步骤为: 1.打开重映射时钟和USART重映射后的I/O口引脚时钟, - RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RC C_APB2Periph_AFIO,ENABLE);
复制代码
2.I/O口重映射开启. - GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);
复制代码
3.配制重映射引脚, 这里只需配置重映射后的I/O,原来的不需要去配置. - 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);<br>
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
复制代码
只需要简单的以上三步就能轻松搞定。
|