嵌入式蓝桥杯PWM输入捕获的

首先咱们要用的固件库是路径是
\STM32固件库v3.5\STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\TIM\PWM_Input\main.c
而后咱们找到这一串代码
【很好找的 由于名字上写了pwm input】web

/* TIM3 configuration: PWM Input mode ------------------------ 
/* TIM3 configuration: PWM Input mode ------------------------ The external signal is connected to TIM3 CH2 pin (PA.01), The Rising edge is used as active edge, The TIM3 CCR2 is used to compute the frequency value The TIM3 CCR1 is used to compute the duty cycle value ------------------------------------------------------------ */

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0x0;

  TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);

  /* Select the TIM3 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);

  /* Enable the Master/Slave Mode */
  TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */
  TIM_Cmd(TIM3, ENABLE);

  /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);

最后把结构体加上去就行了svg

TIM_ICInitTypeDef  TIM_ICInitStructure;

接下来把时钟 和GPIO配置加上去 都不用改的直接复制粘贴加上去就行了函数

/* TIM3 channel 2 pin (PA.07) configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);
/* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* GPIOA clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

而后这个main.c就已经复制完了
出门右拐去复制it.c中断处理函数的
tim3的中断处理函数ui

void TIM3_IRQHandler(void)
{ 
 
  
  /* Clear TIM3 Capture compare interrupt pending bit */
  TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);

  /* Get the Input Capture value */
  IC2Value = TIM_GetCapture2(TIM3);

  if (IC2Value != 0)
  { 
 
  
    /* Duty cycle computation */
    DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value;

    /* Frequency computation */
    Frequency = SystemCoreClock / IC2Value;
  }
  else
  { 
 
  
    DutyCycle = 0;
    Frequency = 0;
  }
}

最后把变量给他补上去就ok了spa

__IO uint16_t IC2Value = 0;
__IO uint16_t DutyCycle = 0;
__IO uint32_t Frequency = 0;

那么到这里PWM的输入就算是配置完成了
能够看出没上面难度
这里提两个重要的参数 一个是占空比 一个是频率code

DutyCycle = 0;
Frequency = 0;

这两个将帮助咱们获得输入的值xml