stm32f030 输入捕捉

void initInputSnap()
{
      TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
      TIM_ICInitTypeDef TIM_ICInitStructure; 
      NVIC_InitTypeDef NVIC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
      uint16_t TimerPeriod;
    
    TimerPeriod = (SystemCoreClock / 36 ) - 1;                    //精度为1us,
    
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能TIM3时钟
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;            //推挽输出模式
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;            //下拉
  GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_1);
    
    TIM_TimeBaseInitStructure.TIM_Prescaler = 0;                //预分频器参数
  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;  // Time 定时设置为上升沿计算模式
  TIM_TimeBaseInitStructure.TIM_Period = TimerPeriod;        //周期
  TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;            //时钟分频     
  TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;    //计数周期

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
    
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
    TIM_ICInitStructure.TIM_ICFilter = 0x00;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    
    TIM_ICInit(TIM3,&TIM_ICInitStructure);
        
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;        //pwm中断使能
    NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
    NVIC_Init(&NVIC_InitStructure);
    
    TIM_ClearITPendingBit(TIM3,TIM_IT_Update | TIM_IT_CC2);    //清理中断标志
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
    TIM_ITConfig(TIM3,TIM_IT_CC2,ENABLE);
    
    TIM_Cmd(TIM3,ENABLE);
    
}
void TIM3_IRQHandler(void)
{
    if(TIM_GetITStatus(TIM3,TIM_IT_CC2) == SET)
    {
        if(statusflg == 0x10)
        {
            //LED_Open();
            statusflg &= 0x00;
            cnt = TIM_GetCapture2(TIM3);
        
            //TIM_OC1NPolarityConfig(TIM3,TIM_ICPolarity_Rising);
            TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Rising);
            TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);
            
            xent = (cyclecnt*65536 + cnt)/2;        //us
            if(xent <= 500)
            {
                sgnPluse = 0;
            }
            else if(xent > 1500)
            {
                sgnPluse = 1023;
            }
            else
            {
                sgnPluse = xent/1500*1023;
            }
        }
        else
        {
            //LED_Close();
            statusflg |= 0x10;
            cyclecnt = 0;
            cnt = 0;
            TIM_SetCounter(TIM3,0);
            //TIM_OC1NPolarityConfig(TIM3,TIM_ICPolarity_Falling);
            TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Falling);
            TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);
        }
        
    }
    if(TIM_GetITStatus(TIM3,TIM_IT_Update) == SET)
    {
        //led_trans();
        if(statusflg == 0x10)
        {
            cyclecnt++;
        }
        TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
    }
}
posted @ 2021-12-20 09:07  steven_lg  阅读(539)  评论(0)    收藏  举报