STM32——定时器强制输出模式

强制输出模式其实特别简单,下面来说一下步骤:

  1. 打开外设时钟
  2. 配置GPIO
  3. 配置极性(默认是TIM_OCPolarity_High,即高电平有效):TIM_OC1PolarityConfig(TIM3,TIM_OCPolarity_Low);
  4. 设置为强制输出模式:TIM_ForcedOC1Config(TIM3,TIM_ForcedAction_Active);
  5. 打开定时器:TIM_Cmd(TIM3, ENABLE);

下面以定时器3的通道1为例(强制PA6输出低电平):

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{
    
    
  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
}

/**
  * @brief  Configure the TIM3 Ouput Channels.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
    
    
  GPIO_InitTypeDef GPIO_InitStructure;
  /* GPIOA Configuration:TIM3 Channel1 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void tim3_init(uint16_t fre)
{
    
    
  TIM_DeInit(TIM3);
	
	/* System Clocks Configuration */
  RCC_Configuration();

  /* GPIO Configuration */
  GPIO_Configuration();
  
  TIM_OC1PolarityConfig(TIM3,TIM_OCPolarity_Low);
  
  TIM_ForcedOC1Config(TIM3,TIM_ForcedAction_Active);
  
  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
}

如果需要强制输出高电平直接去掉上面的TIM_OC1PolarityConfig(TIM3,TIM_OCPolarity_Low);,或者改为TIM_OC1PolarityConfig(TIM3,TIM_OCPolarity_High);

Guess you like

Origin blog.csdn.net/qq_43715171/article/details/115524862