AnsweredAssumed Answered

INTERRUPTION TIM2 STM32F107

Question asked by satour.abou on Dec 18, 2012
Latest reply on Dec 18, 2012 by Clive One
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:

 

#include

 

 

"stm32f10x_tim.h"
#include "stm32f10x_rcc.h"
#include "misc.h"
#include "stm32f10x_gpio.h"
#include "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)
{
}
}

 

 

Outcomes