我正在尝试使用替代功能(重新映射)在引脚 PC10 (USART3_TX) /PC11(USART3_RX) 上的
STM32F103VCT6 上使用 USART3,但我没有成功。
从 MCU 数据表中,我可以使用这些引脚进行
通信。
- void UART3_GPIO_Config (void)
- {
- //Enable Port C clock
- RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
- RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;//Remap ON
- //Mode to AF and Fast speed
- GPIOC->CRH &= ~GPIO_CRH_CNF10;
- GPIOC->CRH |= GPIO_CRH_CNF10_1;
- GPIOC->CRH &= ~GPIO_CRH_CNF11;
- GPIOC->CRH |= GPIO_CRH_CNF11_0;//Input Floating
- //Output max 10Mhz
- GPIOC->CRH &= ~GPIO_CRH_MODE10;
- GPIOC->CRH |= GPIO_CRH_MODE10_0;
- GPIOC->CRH &= ~GPIO_CRH_MODE11;
- //Map to PC10, PC11
- //AFIO->MAPR &= ~AFIO_MAPR_USART3_REMAP;
- //AFIO->MAPR |= AFIO_MAPR_USART3_REMAP_PARTIALREMAP;
- AFIO->MAPR |= (0UL << 5);
- AFIO->MAPR |= (0x1 << 4);
- }
- void UART3_Config (void)
- {
- //UART3 clock enable
- RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
- //Transmit Enable
- USART3->CR1 |= USART_CR1_TE;
- //Receive Enable
- USART3->CR1 |= USART_CR1_RE;
- //Parity - Even
- USART3->CR1 &= ~(USART_CR1_PS);
- //Parity Control Enable
- USART3->CR1 &= ~(USART_CR1_PCE);
- //Word length = 8bit
- USART3->CR1 &= ~(USART_CR1_M);
- //Stop bits = 1
- USART3->CR2 &= ~(USART_CR2_STOP);
- //Disable Hardware flow control (RTS, CTS)
- USART3->CR3 &= ~(USART_CR3_CTSE);
- USART3->CR3 &= ~(USART_CR3_RTSE);
- //Set Baud rate to 115200 (72MHz = 39.0625 -> 39 / 1)
- //BRR = 72MHz/115200/16 = 39.0625
- //Mantissa = 39
- //Fraction = .0625*16 = 1
- USART3->BRR = 0;
- USART3->BRR |= (39UL << 4);
- USART3->BRR |= (1UL << 0);
- //USART3->BRR = 0x1D4C;//set baud 9600
- //Clear LINEN and CLKEN in CR2
- USART3->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
- //Clear SCEN, HDSEL and IREN in CR3
- USART3->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
- //Enable UART
- USART3->CR1 |= USART_CR1_UE;
- /*------------------------------------------------------*/
- // UART3 Receive Interrupt Enable.
- USART3->CR1 |= USART_CR1_RXNEIE;
- NVIC_EnableIRQ(USART3_IRQn);
- /*------------------------------------------------------*/
- }
你能帮助我吗?