STM32串口设置时为什么一定要打开对应GPIO的时钟才能工作?

[复制链接]
6974|6
 楼主| yzhu 发表于 2009-5-31 14:45 | 显示全部楼层 |阅读模式

设置代码如下:

void UART_Configuration(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;
 USART_InitTypeDef USART_InitStructure;
 /* Enable USART1 and GPIOA clocks */
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
 
 /* Configure USART1 Tx (PA.09) as alternate function push-pull */
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 /* Configure USART1 Rx (PA.10) as input floating */
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 /* USARTx configuration --------------------------------------------------*/
 /* USARTx configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
 */
 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;
  
 /* Configure the USART1 */ 
 USART_Init(USART1, &USART_InitStructure);
 /* Enable the USART1 */
 USART_Cmd(USART1, ENABLE);
}


没有红色部分时,串口就没有输出。
请问这是什么原因?
McuPlayer 发表于 2009-5-31 15:12 | 显示全部楼层

这对设计是非常有用的

不用的模块关闭,耗电只有静态电流,占比90%以上的动态功耗不会有。
ST这也是为系统设计提供了更多的灵活选择。
香水城 发表于 2009-5-31 18:22 | 显示全部楼层

为了降低功耗,STM32使用了时钟门控技术(Clock Gating)

请看我的博客:
相关链接:http://blog.**/STM32/215446/message.aspx
 楼主| yzhu 发表于 2009-5-31 20:05 | 显示全部楼层

可是我已经使能了USART1的时钟了呀。

但这样不行
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
还得是
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
香水城 发表于 2009-5-31 21:49 | 显示全部楼层

USART1模块是借用GPIO的端口实现输入输出

使能了USART1模块并不能自动地使能对应的输入输出端口。
bhsdlmj 发表于 2009-6-1 14:56 | 显示全部楼层

楼上各位解释的过于简单,并且并没有道破本质!

brotherwen 发表于 2009-6-2 08:51 | 显示全部楼层

确实是没有道破本质

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//不妨一试
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, DISABLE);
您需要登录后才可以回帖 登录 | 注册

本版积分规则

29

主题

66

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部