gpt4 book ai didi

c - USART3初始化STM32F103RB

转载 作者:行者123 更新时间:2023-12-02 18:43:25 25 4
gpt4 key购买 nike

我是一个使用STM芯片的初学者,我有一个项目,我必须在Uvision中使用所有三个USART终端。

我使用的是 STM32F103RB 芯片,并且我已经使前两个 USART_init 函数正常工作,但由于某种原因我无法使第三个函数正常工作。我真的很感激任何帮助这是我的 USART_init 函数:

/*----------------------------------------------------------------------------
Initialize UART pins, Baudrate
*----------------------------------------------------------------------------*/
void USART1_Init (void) {
int i;

RCC->APB2ENR |= ( 1UL << 0); /* enable clock Alternate Function */
AFIO->MAPR &= ~( 1UL << 2); /* clear USART1 remap */

RCC->APB2ENR |= ( 1UL << 2); /* enable GPIOA clock */
GPIOA->CRH &= ~(0xFFUL << 4); /* clear PA9, PA10 */
GPIOA->CRH |= (0x0BUL << 4); /* USART1 Tx (PA9) output push-pull */
GPIOA->CRH |= (0x04UL << 8); /* USART1 Rx (PA10) input floating */

RCC->APB2ENR |= ( 1UL << 14); /* enable USART#1 clock */

USART1->BRR = 0x0271; /* 115200 baud @ PCLK2 72MHz */
USART1->CR1 = (( 1UL << 2) | /* enable RX */
( 1UL << 3) | /* enable TX */
( 0UL << 12) ); /* 1 start bit, 8 data bits */
USART1->CR2 = 0x0000; /* 1 stop bit */
USART1->CR3 = 0x0000; /* no flow control */
for (i = 0; i < 0x1000; i++) __NOP(); /* avoid unwanted output */

USART1->CR1 |= (( 1UL << 13) ); /* enable USART */
}

/*----------------------------------------------------------------------------
Initialize UART2 pins, Baudrate
*----------------------------------------------------------------------------*/
void USART2_Init (void) {

RCC->APB2ENR |= 1; // enable clock for AF
AFIO->MAPR |= 1<<3; // set USART2 remap
RCC->APB2ENR |= 1<<5; // enable clock for GPIOD
GPIOD->CRL &= ~(0xFF << 20); // Clear PD5, PD6
GPIOD->CRL |= (0x0B << 20); // USART2 Tx (PD5) output push-pull
GPIOD->CRL |= (0x04 << 24); // USART2 Rx (PD6) input floating
RCC->APB1ENR |= 1<<17; // enable clock for USART2
USART2->BRR = 0x138; // set baudrate -115.2kB from 36MHz
USART2->CR1 &= ~(1<<12); // force 8 data bits
USART2->CR2 &= ~(3<<12); // force 1 stop bit
USART2->CR3 &= ~(3<<8); // force no flow control
USART2->CR1 &= ~(3<<9); // force no parity
USART2->CR1 |= 3<<2; // RX, TX enable
USART2->CR1 |= 1<<5; // Rx interrupts if required
NVIC->ISER[1] = (1 << 6); // enable interrupts if required
USART2->CR1 |= 1<<13; // USART enable

}

/*----------------------------------------------------------------------------
Initialize UART3 pins, Baudrate
*----------------------------------------------------------------------------*/
void USART3_Init (void) {

RCC->APB2ENR |= 1; // enable clock for AF
AFIO->MAPR |= 1<<3; // set USART3 remap
RCC->APB2ENR |= 1<<4; // enable clock for GPIOC

GPIOC->CRH &= ~(0xFFUL << 4); /* clear PC10, PC11 */
GPIOC->CRH |= (0x04UL << 8); /* USART3 Rx (PC10) input floating */
GPIOC->CRH |= (0x01UL << 8); /* USART3 Tx (PC11) output push-pull */
RCC->APB1ENR |= 1<<20; // enable clock for USART3

USART3->BRR = 0x138; // set baudrate -115.2kB from 36MHz; USART3
//should have the same baudrate as USART2
USART3->CR1 &= ~(1<<12); // force 8 data bits
USART3->CR2 &= ~(3<<12); // force 1 stop bit
USART3->CR3 &= ~(3<<8); // force no flow control
USART3->CR1 &= ~(3<<9); // force no parity
USART3->CR1 |= 3<<2; // RX, TX enable
USART3->CR1 |= 1<<5; // Rx interrupts if required
NVIC->ISER[1] = (1 << 6); // enable interrupts if required
USART3->CR1 |= 1<<13; // USART enable

}

最佳答案

USART3 初始化的第一行是错误的:-)

RCC->APB2ENR |= 1; // enable clock for AF

必须是(没有时钟,USART 无法工作)

RCC->APB1ENR |= (1<<18); // enable clock for USART

作为附加提示,不要将所有这些魔数(Magic Number)用于位。这更具可读性(并且所需的定义已在 CMSIS 中完成:

RCC->APB1ENR |= RCC_APB1ENR_USART3EN; // enable clock for USART

关于c - USART3初始化STM32F103RB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67766841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com