cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement Stm32f0 output compare

jsaun
Associate II

This is my code. It must be generete output compare interrupt when UPEventCount == DelayloopCount then, TIM_IT_CCR2 interrupt must be occur when CNT = my value. But CCR2 interrupt occur before CNT not reach my value. I looked for a long time but couldn't find the solution. Thanks for now..

void config(){
 
 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_High
 
  TIM_OC2Init(TIM1, &TIM_OCInitStructure);
 
 
 
  TIM1->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; 
 
  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));
 
 
 
}
 
 
 
void TIM1_BRK_UP_TRG_COM_IRQHandler(){
 
UPEventCount++ ;
 
TIM_ClearITPendingBit(TIM1,TIM_IT_Update) ;
 
 
 
	if(UPEventCount == DelayloopCount){
 
 
 
	capture = TIM_GetCapture2(ExternalOSCTimer);
 
	TIM_SetCompare2(TIM1, capture + DelayOverFlow);
 
 TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);//enable interrupt
 
 
 
	}
 
 
 
if (TIM_GetITStatus(TIM1, TIM_IT_CC2) != RESET){
 
 
 
 GPIOF->ODR ^= GPIO_Pin_7;
 
}
 
}

4 REPLIES 4
Javier1
Principal

Use the code format in your text so we dont die trying to read you wall of text0693W00000Y9ufyQAB.png

we dont need to firmware by ourselves, lets talk
gbm
Lead III

TIM1 has >1 interrupt vector - for compare/capture use TIM1_CC_IRQHandler and enable TIM1_CC_IRQn;

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

Sorry :grinning_face_with_sweat:

jsaun
Associate II

That is not working