STM8系列--PWM捕獲輸入檢測

不折不扣發表於2024-05-29

最重要的下載程式的option bety 設定;

函式1:

STM8系列--PWM捕獲輸入檢測
void Init_Timer1(void)
{    
    GPIO_Init(GPIOC, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT);   //輸入浮動,沒有外部中斷     
    TIM1_DeInit();                                         //tim1 定時器暫存器全部復位  
    TIM1_TimeBaseInit(31, TIM1_COUNTERMODE_UP, 15625-1, 0);//  500ms  最小捕獲5hz      設定時間分頻  向上計數    捕捉5Hz及 以上的週期和佔空比         
    TIM1_PWMIConfig(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV1, 0x00); //選擇TI1輸入上升沿觸發 選擇TI2輸入下降沿觸發 輸入捕捉預定標器  無濾波
    TIM1_SelectInputTrigger(TIM1_TS_TI1FP1);                 //濾波後輸入TI1
    TIM1_SelectSlaveMode(TIM1_SLAVEMODE_RESET);  //復位輸入
    TIM1_ClearFlag(TIM1_FLAG_CC1);                                 //指定要清除的標誌
    TIM1_ClearFlag(TIM1_FLAG_CC2);                                //指定要清除的標誌
    TIM1_ITConfig( TIM1_IT_CC1, ENABLE);                     //去掉是查詢方式,否則是中斷方式
    TIM1_ITConfig( TIM1_IT_CC2, ENABLE);                    //去掉是查詢方式,否則是中斷方式
    TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE);          //使能輸入捕獲通道1
    TIM1_CCxCmd(TIM1_CHANNEL_2, ENABLE);          //使能輸入捕獲通道2
    TIM1_Cmd(ENABLE);                                                     //使能定時器
 
}
TIME1_INIT

函式2:

STM8系列--PWM捕獲輸入檢測
extern __IO uint32_t LSIClockFreq;
extern __IO uint32_t LSIDuty;
INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
  uint16_t num = 0;
  static uint16_t num1;
  static uint32_t num2;

 if(TIM1_GetITStatus(TIM1_IT_CC1) != RESET) //如果CH1邊沿觸
  {  
      num1 = TIM1_GetCapture1();  
      TIM1_ClearITPendingBit(TIM1_IT_CC1);    //清除標誌位  
  }  
 else if(TIM1_GetITStatus(TIM1_IT_CC2) != RESET) //如果CH2邊沿觸發  
  {  
      num2 = TIM1_GetCapture2();             //
      TIM1_ClearITPendingBit(TIM1_IT_CC2);    //清除標誌位  
      if(flag_send==0)
      {
           LSIDuty = ((num2 * 100) / num1); //計算佔空比
           SIClockFreq = 62500/( num1);        //計算頻率
          if(LSIClockFreq>=0xff)
         {
             LSIClockFreq=0xff;
         }
     }
  }
}
中斷12

相關文章