stm32系列--同一定时器双通道捕获

 

void TIM3_Cap_Init(u16 arr,u16 psc)//定时器3通道2输入捕获配置
{
    TIM_ICInitTypeDef  TIM_ICInitStructure; 
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
         GPIO_InitTypeDef  GPIO_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
    
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//先占优先级0级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//从优先级3级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//IRQ通道被使能
    NVIC_Init(&NVIC_InitStructure);//根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
 
    //INIT_GPIO(RCC_APB2Periph_GPIOA,GPIOA,GPIO_Pin_6,GPIO_Mode_IPU); //初始化A06为上拉输入,用于输入捕捉
    

     
         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);     //使能PA端口时钟
            
         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;                      
         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;          //上拉输入
         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         //IO口速度为50MHz
         GPIO_Init(GPIOA, &GPIO_InitStructure);    
 
         //初始化定时器4 TIM2     
        TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值 
        TIM_TimeBaseStructure.TIM_Prescaler =psc;     //预分频器   
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
        
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;    //选择输入端,
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //双沿捕获
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;   //配置输入分频,不分频
    TIM_ICInitStructure.TIM_ICFilter = 0x0;     //配置输入滤波器 不滤波  
    TIM_ICInit(TIM3, &TIM_ICInitStructure); //初始化参数
        
        TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;    //选择输入端,
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //双沿捕获  //TIM_ICPolarity_Rising
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;   //配置输入分频,不分频
    TIM_ICInitStructure.TIM_ICFilter = 0x0;     //配置输入滤波器 不滤波  
    TIM_ICInit(TIM3, &TIM_ICInitStructure); //初始化参数
    
        TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2 ,ENABLE);
        TIM_Cmd(TIM3, ENABLE);      //使能定时器3
}

void TIM3_IRQHandler(void)   //TIM3中断服务函数
{
        if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)     
        {        
             TIM_ClearITPendingBit(TIM3, TIM_IT_Update);             //清除中断标志位
        }
        if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)            //捕获1发生捕获事件
        {    
            myt3.TIM3CH1_CAPTURE_VAL=TIM_GetCapture1(TIM3);

            TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);             //清除中断标志位 
        }    
        if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
        {    
            myt3.TIM3CH2_CAPTURE_VAL=TIM_GetCapture2(TIM3);
            myt3.timout=5;
            myt3.handle_timout=5000;//不再有测量 5s后输出结果
            TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);             //清除中断标志位 
        }            
}
TIM3_Cap_Init
    TIM3_Cap_Init(0xffff,71); //PA6
main
void TIM4_IRQHandler(void)   //TIM3中断 1MS
{
    u8 st;
    if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
    {      
        st= TIM_GetFlagStatus(TIM4, TIM_FLAG_Update);    
        if(st==SET)
        {
          TIM_ClearFlag(TIM4, TIM_FLAG_Update);
            
            if(myusart.timout>0)//5MS 开始处理数据
            {
                if(--myusart.timout==0)
                {
                    myusart.reflag=1;  //收到一帧数据

                }
            }
            if(myt3.timout>0)//5MS 开始处理数据
            {
                if(--myt3.timout==0)
                {
                    myt3.sum++;    
          myt3.TIM3CH1_CAPTURE_VAL += 400;
                    myt3.TIM3RET=myt3.TIM3CH1_CAPTURE_VAL-myt3.TIM3CH2_CAPTURE_VAL;
                    if(myt3.TIM3RET<2000 && myt3.TIM3RET>0)
                    myt3.pass++;
                    
//                    if((myt3.k_num>=3) && (myt3.pass >= 3))
//                    {
//                        printf("pass\r\n");
//                        GPIO_SetBits(GPIOA,GPIO_Pin_0);;
//                    }
//                    printf("TIM3RET= %d\r\n",myt3.TIM3RET);
                }
            }
            if(myt3.judge_count++ >= 500)//没500MS 做一次判断;还是不行,会把测试通过的结果清除掉
            {
                myt3.judge_count=0;    
                if(myt3.finish_flag==0)
                {
                    if((myt3.pass >= myt3.sum/2) && (myt3.pass >= 3))
                    {
                        GPIO_SetBits(GPIOA,GPIO_Pin_0);
                        myt3.finish_flag=1;
                        
                    }
                    else
                    {
                        GPIO_ResetBits(GPIOA,GPIO_Pin_0);
                        myt3.finish_flag=0;
                    }
                }        
            }
            if(myt3.handle_timout>0)//5S 开始处理数据
            {
                if(--myt3.handle_timout==0)
                {
                    if((myt3.pass >= myt3.sum/2) && (myt3.pass >= 3))
                    {
                        printf("pass\r\n");
                        GPIO_SetBits(GPIOA,GPIO_Pin_0);
                        myt3.finish_flag=1;
                    }
                    else
                    {
                        printf("null\r\n");
                        GPIO_ResetBits(GPIOA,GPIO_Pin_0);
                        myt3.finish_flag=0;
                    }
                    
                    myt3.pass=0;
                    myt3.sum=0;
                }
            }            
        }    
        TIM_ClearITPendingBit(TIM4, TIM_IT_Update  );  //清除TIMx的中断待处理位:TIM 中断源 
    }
}
TIM4_IRQHandler

 

posted @ 2024-06-05 16:32  不折不扣  阅读(45)  评论(0编辑  收藏  举报