I use the output capture mode to generate the delay interrupt. eg: after I receive a frame from uart, I will delay for 20 bit time after I get each of byte. My code like below:
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = dwTickCount; TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
after I get each character, I enable the timer interrupt.:
wCapture = TIM_GetCounter(TIM2); wCapture += 1750; TIM_SetCompare1(TIM2, wCapture); TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
until I don't get the characters from uart, the timer interrupt should be fired. But I find the timer interrupt is fired earlier than I set. I add the code in interrupt routine like below.
if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
wCapture = TIM_GetCapture2(TIM2); if (TIM_GetCounter(TIM2) >= wCapture) { a = 0; } else { a = 1; // set breakpoint here. } }
I always get the breakpoint at a = 1; That means the MCU fired the interrupt before the Tickcount reach the capture value.
Anyone have same problem? Your suggestion will be very appreciated.