cancel
Showing results for 
Search instead for 
Did you mean: 

INTERRUPTION TIM2 STM32F107

mahmoud_dhaiwi
Associate II
Posted on December 18, 2012 at 09:17

Hi,

I use STM32F107 microcontroller.

I want to configure an interruption each one second with TIM2, to toggle led in port C pin 6.

But the led don't toggle !!

any help please ?

this is my code:

 

&sharpinclude

 

 

''stm32f10x_tim.h''

&sharpinclude

''stm32f10x_rcc.h''

&sharpinclude

''misc.h''

&sharpinclude

''stm32f10x_gpio.h''

&sharpinclude

''stm32f10x.h''

void

INTtim2(

void

)

{

TIM_TimeBaseInitTypeDef

TIM_TimeBaseStructure;

GPIO_InitTypeDef

GPIO_InitStructure;

NVIC_InitTypeDef

NVIC_InitStructure;

//enable clocks to tim2 and

gpio

port c

RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2,

ENABLE

);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,

ENABLE

);

//

init

LED (port C,pin6)

GPIO_InitStructure.

GPIO_Speed

=

GPIO_Speed_2MHz

;

GPIO_InitStructure.

GPIO_Mode

=

GPIO_Mode_Out_PP

;

GPIO_InitStructure.

GPIO_Pin

= GPIO_Pin_6;

GPIO_Init(GPIOC, &GPIO_InitStructure);

//setting timer 2 interrupt each 1 second

TIM_TimeBaseStructure.

TIM_Prescaler

= ????  ;

TIM_TimeBaseStructure.

TIM_CounterMode

= TIM_CounterMode_Up;

TIM_TimeBaseStructure.

TIM_Period

= ?????;

TIM_TimeBaseStructure.

TIM_ClockDivision

= ????;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

/* TIM IT enable */

TIM_ITConfig(TIM2, TIM_IT_Update,

ENABLE

);

/* TIM2 enable counter */

TIM_Cmd(TIM2,

ENABLE

);

//enable tim2

irq

NVIC_InitStructure.

NVIC_IRQChannel

=

TIM2_IRQn

;

NVIC_InitStructure.

NVIC_IRQChannelPreemptionPriority

= 1;

NVIC_InitStructure.

NVIC_IRQChannelSubPriority

= 1;

NVIC_InitStructure.

NVIC_IRQChannelCmd

=

ENABLE

;

NVIC_Init(&NVIC_InitStructure);

//timer 2 interrupt

  </p>

 

void

 

</b>

 

TIM2_IRQHandler(

void

)

 

{

 

//if interrupt happens the do this

 

if

(TIM_GetITStatus(TIM2, TIM_IT_Update) !=

RESET

)

 

{

 

//clear interrupt and start counting again to get precise

freq

 

TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

 

 

//toggle led in port C pin 6

 

if

(GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_6) ==

RESET

)

 

{

 

GPIO_WriteBit(GPIOC,GPIO_Pin_6,

SET

);

 

}

 

else

 

{

 

GPIO_WriteBit(GPIOC,GPIO_Pin_6,

RESET

);

 

}

 

//GPIOC->ODR ^= GPIO_Pin_6;

 

}

 

}

 

 

and this is main.c:

 

 

 

void

 

 

 

INTtim2(

void

);

 

 

int

main(

void

)

 

{

 

INTtim2();

 

while

(1)

 

{

 

}

 

}

 

 

#stm32-timer2-interrupt
1 REPLY 1
Posted on December 18, 2012 at 13:56

// Assume 72 MHz clock, factor 72000000 cycles
//setting timer 2 interrupt each 1 second
TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 10000 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..