/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
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_ICInit(TIM3, &TIM_ICInitStructure);
TIM_PWMIConfig(TIM3, &TIM_ICInitStructure); //根据参数配置TIM外设信息
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); //选择IC2为始终触发源
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);//TIM从模式:触发信号的上升沿重新初始化计数器和触发寄存器的更新事件
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); //启动定时器的被动触发
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET)
{
/* Clear TIM3 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
if(CaptureNumber == 0)
{
USART_SendData(USART1, TIM3 -> CNT);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
/* Get the Input Capture value */
IC3ReadValue1 = TIM_GetCapture2(TIM3);
USART_SendData(USART1, IC3ReadValue1);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
CaptureNumber = 1;
}
else if(CaptureNumber == 1)
{
/* Get the Input Capture value */
IC3ReadValue2 = TIM_GetCapture1(TIM3);
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
CaptureNumber = 2;
}
else if(CaptureNumber == 2)
{
IC3ReadValue3 = TIM_GetCapture2(TIM3);
/* Capture computation */
if (IC3ReadValue3 > IC3ReadValue1)
{
Capture = (IC3ReadValue3 - IC3ReadValue1);
}
else
{
Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue3);
}
/* Frequency computation */
capture = (IC3ReadValue2 - IC3ReadValue1)*100/ Capture;
//TIM3Freq = (u32) 72000000 / Capture;
CaptureNumber = 0;
}
}
以上是我的捕获代码,但是每次从IC3ReadValue3、IC3ReadValue1这两个值都是3,不知道是怎么回事 |