cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 timer3

Nmo.1
Associate III

Hi everyone. I wrote this code to run the stm32f0 timer3, which toggle 2 LEDs at a time, but it doesn't work.

Can anyone please help me؟

void TIM3_Init()
{
	      TIM_TimeBaseInitTypeDef TIM3_InitStructure;
		  NVIC_InitTypeDef NVIC_InitStructure;
 
          RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, ENABLE);
		  TIM3_InitStructure.TIM_Prescaler = 359;
		  TIM3_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
		  TIM3_InitStructure.TIM_Period = 199;
		  TIM3_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
		  TIM3_InitStructure.TIM_RepetitionCounter = 0;
		  TIM_TimeBaseInit(TIM3, &TIM3_InitStructure);
 
          TIM_Cmd(TIM3, ENABLE);
 
          NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
		  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	      NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
	      NVIC_Init(&NVIC_InitStructure);
 
		  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
 
 
}
 
void TIM3_IRQHandler(void)
{
 
	if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET){
 
     TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
		GPIOB->ODR ^= GPIO_Pin_2|GPIO_Pin_3;
 
 
	}
}

2 REPLIES 2

Read out and check/post the TIM and relevant GPIO registers content.

JW

Line:

RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, ENABLE);

is wrong. You want call function:

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

to enable clock ! For next similar issues i suggest to do thist "checklist":

  1. Check if "non working" peripheral has clock enabled
  2. Check if used GPIOs has clock enabled
  3. Check if used GPIOs are configured as proper alternate function
  4. Check if you are looking at right GPIOs