cancel
Showing results for 
Search instead for 
Did you mean: 

STM32- Timer not working as expected,

jsaun
Associate II
void External_Clock_Config(){
		  
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
 NVIC_InitTypeDef   NVIC_InitStructure;
 GPIO_InitTypeDef   GPIO_InitStructure;
	
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //Clock enable 
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9| GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(ExternalOSCport, &GPIO_InitStructure);
 
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2); //alternate func
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2); //alternate func
 
 TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //TIM_Channel_3
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0x0;
    TIM_ICInit(ExternalOSCTimer, &TIM_ICInitStructure);
 
 
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_UP_TRG_COM_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
		
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle ;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OC2Init(TIM1, &TIM_OCInitStructure);
 
    ExternalOSCTimer->CCER |= TIM_CCER_CC2E ;
		
    TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Disable);
		
    TIM1->CR2 &= (uint16_t)(~(TIM_CR1_DIR | TIM_CR1_CMS | TIM_CR1_CKD)); 
    TIM1->CR2 &= 0XFF7F; 
    TIM1->ARR = 0xFFFFFFFF;
    TIM1->PSC = 0x0000; //set prescale value
    TIM1->SMCR |= (uint16_t)(TIM_TS_TI1FP1 | TIM_SMCR_SMS); // external mode 1
   TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE);
   TIM1->CR1 &= (uint16_t)(~((uint16_t)TIM_CR1_CEN));  //disable timer counting
   TIM1->CR1 |= (uint16_t)TIM_CR1_URS;  //disable timer counting
 
    NVIC_EnableIRQ(TIM1_CC_IRQn);
		
}
 
 
void TIM1_BRK_UP_TRG_COM_IRQHandler(){ /*OverFlow interrupt **/
 
	if(TIM_GetITStatus(TIM1,TIM_IT_Update) != RESET){
	
			UPEventCount++  ;
 
		if(.UPEventCount  == .DelayloopCount){
           
			 capture = TIM1->CCR2 ;
			TIM1->CCR2 = capture  +  DelayOverFlow ;
			TIM1->DIER |= TIM_IT_CC2;  
}
 
}
 
void TIM1_CC_IRQHandler(){
	
if (TIM_GetITStatus(TIM1, TIM_IT_CC2) != RESET){
		
    TIM_ClearITPendingBit(TIM1, TIM_IT_CC2 );
TIM1->DIER &= (uint16_t)~TIM_IT_CC2; }
}

Hello

I use stm32f0. I want to generete interrupt when UPEventCount == DelayloopCount(line 65) then, TIM_IT_CCR2 interrupt must be occur when CNT = my value. But CCR2 interrupt occur before CNT not reach my value. But it happens(line 73) when I enable interrupt but not when CCR2 reaches CNT value

In short, this is my expectation that the interrupt will be enabled when it reaches a certain overflow number, and then the caputure compare interrupt will occur when it reaches the specified CNT value.

I haven't figured it out for a long time and I really need help :(

Thanks for now..

@Community member​ @Community member​ 

jsaun

1 REPLY 1

Clear the respective interrupt flag (TIM1->SR.CC2IF) just before you enable the CC2 interrupt.

JW