cancel
Showing results for 
Search instead for 
Did you mean: 

timer interupt ----please help for TIM2 in stm32F103xx

mayurr
Associate II
Posted on August 18, 2008 at 14:23

timer interupt ----please help for TIM2 in stm32F103xx

2 REPLIES 2
mayurr
Associate II
Posted on May 17, 2011 at 12:42

hi Friends,

I have to generate an timer interupt after every 0.005 ms for tim2 in stm32f103xx.

and i have to on LED for 5 second and off it for 5 second .

in practical case

LED ramain on for 2 second and remain off for 2 second .

can anyone give an idea where am i wrong? why LED is not on for 5 second and not off for 5 second .

is there any setting in TIM2 ?

my program :

GPIO_InitTypeDef GPIO_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

#ifdef DEBUG

debug();

#endif

/* Configure the system clocks */

RCC_Configuration();

/* NVIC Configuration */

//NVIC_Configuration();

/* Configure PC0,PC1,PC2,PC3 output push-pull */

GPIO_DeInit(GPIOC);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //| GPIO_Pin_1| GPIO_Pin_2;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOC, &GPIO_InitStructure); // PORT C as output

NVIC_DeInit();

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

/* Time base configuration */

TIM_DeInit(TIM2);

TIM_TimeBaseStructure.TIM_Period =0xFFFF;

TIM_TimeBaseStructure.TIM_Prescaler =0x02;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

/* Clear TIM2 update pending flag */

TIM_ClearFlag(TIM2, TIM_FLAG_Update);

/* TIM IT enable */

TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

/* TIM2 enable counter */

TIM_Cmd(TIM2, ENABLE);

while (1)

{

}

}

//interupt timer routine:

void TIM2_IRQHandler(void)

{

static unsigned long Timer_Value=0;

if(Flag==0)

{

GPIO_SetBits(GPIOC,GPIO_Pin_0);

if(++Timer_Value==1000000)

{

Timer_Value=0;

Flag=1;

}

}

else

{

GPIO_ResetBits(GPIOC,GPIO_Pin_0);

if(++Timer_Value==1000000)

{

Timer_Value=0;

Flag=0;

}

}

}

16-32micros
Associate III
Posted on May 17, 2011 at 12:42

Hi mayurr,

Note that TIM2CLK is equal to APB1 Clock If (APB1 prescaler =1)

else it is APB1 Clock x2 : refer to RM0008 rev5 on web (Figure 8),

You can also refer to STM32F10xFWLib_v2.0.2/FWLib/examples/TIM/TimeBase

example that you can find on web

http://www.st.com/stonline/products/support/micro/files/um0427.zip

Hope this helps you.

Cheers,

STOne-32.