Pin Re-mapping is a simple but important skills in programming STM32.
Here shows a simple example to re-map USART1_TX and USART1_RX from PA9, PA10 (not Five-volt tolerant)
to PB6, PB7 (Five-volt tolerant) for STM32F103RB.
Comparisons between the steps in re-mapping and not re-mapping the pins
When PA9, PA10 are used for USART1
i. Turn on Peripheral Clock for GPIOA
ii. Config PA9 as Alternate Function Push-pull output, and PA10 as Input Floating
iii. Turn on Peripheral Clock for USART1
iv. Config USART1 baud rate and properties...
When PB6, PB7 are used for USART1 (Re-mapping)
i. Turn on Peripheral Clock for GPIOB
ii. Config PA6 as Alternate Function Push-pull output, and PA7 as Input Floating
iii. Turn on Peripheral Clock for USART1
iv. Config USART1 baud rate and properties...
v. Enable Alternate Function Clock
vi. Enable USART1 re-mapping
From above, we can see that only two more steps is needed... It is so simple!
Sample Coding for re-mapping the USART1:
----------------------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Turn on periph clock for both GPIOB and USART */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1, ENABLE);
/* Config 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_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Config USART1 */
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(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_Cmd(USART1, ENABLE);
/* Re-mapping Config */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO , ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
----------------------------------------------------
The peripheral clock command of AFIO can be combined with that of GPIOB and USART1,
i.e. the re-mapping only requires only 1 more line of code.
文章评论(0条评论)
登录后参与讨论