【发布时间】:2014-08-18 06:33:48
【问题描述】:
我正在使用 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);
【问题讨论】:
-
你看过STM32标准库提供的例子了吗?
-
是的,我检查了几个例子,但它们都在异步模式下工作,我找不到任何关于时钟设置的信息。
-
查看stm32标准库示例:STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/USART/Synchronous
-
非常感谢。我也检查了它并且很有帮助,但是没有关于频率时钟的任何设置。我想我找到了解决方案。 APB1 有一个预分频器,我将其更改为 APB1(或 PCLK1)的 9MHZ 时钟。但是有一个大问题。我的 usart2 时钟引脚显示 200 到 400 HZ!!!!在任何情况下。为什么即使我使用默认时钟(36MHZ)也会出错???
-
您的问题到底是什么? USART 的波特率与其所连接的总线(在您的情况下为 APB1)的时钟不同。您是否正确设置了波特率?
标签: c synchronous stm32 usart