|
可以实现边沿捕获功能的代码
这个程序使用了TIM3通道1的边沿脉宽捕获计数,有点麻烦,不知道有没有更简单一点的程序。 1.TIM3初始化 void TIM3_Ini(void) { /* TIM3 configuration: ------------------------ The external signal is connected to TIM3 CH1 pin (Pa.06), ------------------------------------------------------------ */ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; TIM_DeInit( TIM3);// /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 72; //0 TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //ic1 -- rising TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; 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_ICInit(TIM3,&TIM_ICInitStructure); //ic1-- falling TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM3,&TIM_ICInitStructure); /* TIM enable counter */ TIM_Cmd(TIM3, ENABLE);
/* Enable the CC1,CC2 Interrupt Request */ TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2, ENABLE); } 2. 中断处理程序 void TIM3_IRQHandler(void) { uchar i=0; if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET) { if(TIM_GetFlagStatus(TIM3,TIM_FLAG_CC1OF)== RESET) { /* Get the Input Capture value */ Curr_TIM3_IC1Val = TIM_GetCapture1(TIM3);//rising value.. /* Clear TIM2 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); i=1; } else { TIM_ClearFlag(TIM3,TIM_FLAG_CC1OF); } } else if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET) { if(TIM_GetFlagStatus(TIM3,TIM_FLAG_CC2OF)== RESET) { /* Get the Input Capture value */ Curr_TIM3_IC1Val = TIM_GetCapture2(TIM3); //fall value.. /* Clear TIM2 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); i=1; } else { TIM_ClearFlag(TIM3,TIM_FLAG_CC2OF); } } // if (i==1) { Tim3_IC1Value=Curr_TIM3_IC1Val-Prev_TIM3_IC1Val; // Uputchar(Tim3_IC1Value,0); Uputchar(Tim3_IC1Value>>8,0); Prev_TIM3_IC1Val=Curr_TIM3_IC1Val; } }
Tim3_IC1Value即为边沿脉冲宽度值。 |
|