cancel
Showing results for 
Search instead for 
Did you mean: 

How to exactly reset Timer

hdemi.1
Associate III

In my project, I have to count exact time and I am using very sensitive external oscilator. I configure it as a external timer. I have a certain number of overflow counts and waste values for my count, when it reaches the waste value, I create a capture compare interrupt and I need to reset everything about the timer. (once per second) When I compared to the GPS-assisted clock, I see that I am slipping. (1ms in for 1 minute) Thanks for now.

void TIM1_BRK_UP_TRG_COM_IRQHandler(){ 
		
		
if(TIM_GetITStatus(TIM1,TIM_IT_Update) != RESET){
		
TIM_ClearITPendingBit(TIM1,TIM_IT_Update) ;
 UPEventCount  ++ ,
			 
					
 
	 if(UPEventCount  == OSCloopValFlash){
						
					TIM1->CCR3 = FlashPingParam.OSCOverValFlash  ;
					TIM1->SR &= (uint16_t)~TIM_SR_CC3IF ;
				       TIM1->DIER |= TIM_IT_CC3; 
	
    } 
 
		
	}
			
}
	
void TIM1_CC_IRQHandler(){
	
		
      if(TIM_GetITStatus(TIM1, TIM_IT_CC3) != RESET){
				
				
      TIM_ClearITPendingBit(TIM1, TIM_IT_CC3 );
 
	TIM1->DIER &= (uint16_t)~TIM_IT_CC3;
				
      TIM1 -> EGR = TIM_EGR_UG;
      TIM1-> CR1 |= TIM_CR1_UDIS;
      TIM1-> CR1 &= ~TIM_CR1_UDIS;
      UPEventCount  = 0;
      TIM1->CNT &= 0;
     GPIOF->ODR ^= GPIO_Pin_7;
				
 
 
				
							}
	
	
	
	
	}

3 REPLIES 3

I don't quite understand what your current code does, but to find out period between two captures, you may leave the timer run freely, with maximum ARR, and then just subtract the two capture values (with taking into mind the overflow at ARR).

JW

LCE
Principal

What is a "waste value"?

Why the "&=" in "TIM1->CNT &= 0;"

Otherwise I support Jan's proposal.

Maybe set ARR to some value which is easier for final time calculations.

Actually, I have an origin point taken over GPS, it shifts by an ARR value in this loop, but it shouldn't be. In the capture compare interrupt the system should be completely reset and no offset or fixed offset to the origin, the system shifts as much as the ARR value each time, for example, the ARR value is 10000, the first shifted value is 10000 according to the origin, the second time it is 20000 shifted according to the origin. Sorry it was a bit messy