定时器配置:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);
/*---------------------------- TIM16 Configuration ----------------------------*/
/* Time base configuration */
TIM_DeInit(TIM16);
TIM_TimeBaseStructure.TIM_Period = 0xffff;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM16, 0, TIM_PSCReloadMode_Update);
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 = 0;
TIM_ICInit(TIM16, &TIM_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM16_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearITPendingBit(TIM16,TIM_IT_CC1|TIM_IT_Update);
TIM_ITConfig(TIM16, TIM_IT_CC1|TIM_IT_Update,ENABLE);
TIM_Cmd(TIM16, ENABLE);
中断处理:
void TIM16_IRQHandler(void)
{
if((TIM16CH1_CAPTURE_STA&0X80)==0)
{
if((TIM_GetITStatus(TIM16, TIM_IT_CC1) != RESET))
{
TIM_ClearITPendingBit(TIM16, TIM_IT_CC1);
if(TIM16CH1_CAPTURE_STA&0x40)
{
TIM16CH1_CAPTURE_STA |= 0x80;
Cap2 = TIM_GetCapture1(TIM16);
if(CapCount_H>0)
{
TIM16CH1_CAPTURE_VAL2 = CapCount_H*65536 + 65536-Cap1+Cap2;
}
else
{
TIM16CH1_CAPTURE_VAL2 = Cap2-Cap1;
}
}
else
{
CapCount_H = 0;
Cap2 = 0;
Cap1 = TIM_GetCapture1(TIM16);
TIM16CH1_CAPTURE_STA |= 0x40;
}
}
if (TIM_GetITStatus(TIM16, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM16, TIM_IT_Update);
if (TIM16CH1_CAPTURE_STA&0x40)
{
CapCount_H++;
}
}
}
else
TIM_ClearITPendingBit(TIM16, TIM_IT_Update);
}
结果就是不对,望大神帮我找找bug!或者有别的思路或者程序都可以,急求急求!
此帖出自
小平头技术问答
一周热门 更多>