1

我正在使用 stm32f103,我想使用 USART2 的同步模式发送和接收数据。所以我有一个问题,例如,如何将 USART 的时钟设置为 10 MHZ?哪个寄存器?

GPIO_InitTypeDef GPIO_InitStructure;                    

RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO ,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);                                       

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;               
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;         
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;       
GPIO_Init(GPIOA, &GPIO_InitStructure);                  

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;                       
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;               
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;         
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;       
GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitTypeDef USART_InitStructure;

USART_ClockInitTypeDef USART_ClkInitStructure;                        

USART_InitStructure.USART_BaudRate            = 4390 ;              
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(USART2, &USART_InitStructure);                               
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);                            
__nop(); __nop();

USART_ClkInitStructure.USART_Clock=USART_Clock_Enable;
USART_ClkInitStructure.USART_CPOL=USART_CPOL_Low;
USART_ClkInitStructure.USART_CPHA=USART_CPHA_2Edge;
USART_ClkInitStructure.USART_LastBit=USART_LastBit_Enable;
USART_ClockInit(USART2, &USART_ClkInitStructure);

USART_Cmd(USART2, ENABLE);
4

1 回答 1

1

您应该查看 STM32 标准外设库中提供的示例(位于 STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/USART/Synchr‌​onous)

在您的情况下,两条重要的线是:

USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;

和:

USART_InitStructure.USART_BaudRate = 115200;

在您的代码中,您应该替换:

GPIO_Speed_10MHz

经过:

GPIO_Speed_50MHz
于 2014-07-06T09:26:03.643 回答