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);

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值