嵌入式藍橋杯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