STM32之时钟输出功能

很多时候,我们想让ARM发出固定频率的脉冲,作为另一个芯片的时钟时,有两个方法一个是定时器或者时钟输出功能,利用定时器输出会吃中断源并且不利于发出高频率脉冲,所以选择第二个方法对应时钟源的时钟,并且可以进行分频之后再输出。

STM32F4手册说明:
时钟输出功能
共有两个微控制器时钟输出 (MCO) 引脚:
 MCO1
用户可通过可配置的预分配器(从 1 到 5)向 MCO1 引脚 (PA8) 输出四个不同的时钟源:
— HSI 时钟
— LSE 时钟
— HSE 时钟
— PLL 时钟
所需的时钟源通过 RCC  时钟配置寄存器 (RCC_CFGR) 中的 MCO1PRE[2:0] 和 MCO1[1:0]
位选择。
 MCO2
用户可通过可配置的预分配器(从 1 到 5)向 MCO2 引脚 (PC9) 输出四个不同的时钟源:
— HSE 时钟
— PLL 时钟
— 系统时钟 (SYSCLK)
— PLLI2S 时钟
所需的时钟源通过 RCC  时钟配置寄存器 (RCC_CFGR) 中的 MCO2PRE[2:0] 和 MCO2
位选择。
对于不同的 MCO 引脚,必须将相应的 GPIO 端口在复用功能模式下进行设置。
MCO 输出时钟不得超过 100 MHz(最大 I/O 速度)。

如图所示;

比较简单:直接说具体配置示例仅用MCO1进行示例
配置取主时钟168MHz再进行5分频后输出
void MCO1_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_ClockSecuritySystemCmd(ENABLE);
 
    /* Enable GPIOs clocks */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO);//打开引脚复用功能
         
    /* Configure MCO (PA8) */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  //UP
    GPIO_Init(GPIOA, &GPIO_InitStructure);
//输出时钟
    RCC_MCO1Config(RCC_MCO1Source_PLLCLK, RCC_MCO1Div_5);
}

如上初始化过后,引脚就可以输出响应的时钟了。

库函数中RCC_MCO1Config()解释如下

形参1:填入要选择的时钟源
RCC_MCO1Source_HSI
 RCC_MCO1Source_LSE
RCC_MCO1Source_HSE
RCC_MCO1Source_PLLCLK
从时钟树上很容易看出该每个时钟源
形参2:分频系数
RCC_MCO1Div_1
 RCC_MCO1Div_2
RCC_MCO1Div_3
RCC_MCO1Div_4
RCC_MCO1Div_5
分别是对时钟源进行1-5分频之后输出的时钟

/*

  * @brief  Selects the clock source to output on MCO1 pin(PA8).
  * @note   PA8 should be configured in alternate function mode.
  * @param  RCC_MCO1Source: specifies the clock source to output.
  *          This parameter can be one of the following values:
  *            @arg RCC_MCO1Source_HSI: HSI clock selected as MCO1 source
  *            @arg RCC_MCO1Source_LSE: LSE clock selected as MCO1 source
  *            @arg RCC_MCO1Source_HSE: HSE clock selected as MCO1 source
  *            @arg RCC_MCO1Source_PLLCLK: main PLL clock selected as MCO1 source
  * @param  RCC_MCO1Div: specifies the MCO1 prescaler.
  *          This parameter can be one of the following values:
  *            @arg RCC_MCO1Div_1: no division applied to MCO1 clock
  *            @arg RCC_MCO1Div_2: division by 2 applied to MCO1 clock
  *            @arg RCC_MCO1Div_3: division by 3 applied to MCO1 clock
  *            @arg RCC_MCO1Div_4: division by 4 applied to MCO1 clock
  *            @arg RCC_MCO1Div_5: division by 5 applied to MCO1 clock
  * @retval None
  */
void RCC_MCO1Config(uint32_t RCC_MCO1Source, uint32_t RCC_MCO1Div)
{
  uint32_t tmpreg = 0;
  
  /* Check the parameters */
  assert_param(IS_RCC_MCO1SOURCE(RCC_MCO1Source));
  assert_param(IS_RCC_MCO1DIV(RCC_MCO1Div));  

  tmpreg = RCC->CFGR;

  /* Clear MCO1[1:0] and MCO1PRE[2:0] bits */
  tmpreg &= CFGR_MCO1_RESET_MASK;

  /* Select MCO1 clock source and prescaler */
  tmpreg |= RCC_MCO1Source | RCC_MCO1Div;

  /* Store the new value */
  RCC->CFGR = tmpreg;  
}

=======================================================

如有不对之处望指出。一起学习共同进步

邮箱:[email protected]

——十五

猜你喜欢

转载自blog.csdn.net/fifteenPeng/article/details/85457145
今日推荐