cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32f4 tim7 interrupt problem

M_1
Associate II
Posted on July 26, 2013 at 16:48

Hi 2All!

I'm trying to toggle LED with TIM7 interrupt. I'm using STM32f417VG with Coocox CoIDE Code is here:

void
TIM7Config(
void
)
{
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7,ENABLE);
/*TIM7->PSC = 63000 - 1;
TIM7->ARR = 1000 ;
TIM7->DIER |= TIM_DIER_UIE;
TIM7->CR1 |= TIM_CR1_CEN; */
//NVIC_EnableIRQ(TIM7_IRQn);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBase_InitStructure.TIM_Period = 42000;
TIM_TimeBase_InitStructure.TIM_Prescaler = 1000;
TIM_TimeBaseInit(TIM7, &TIM_TimeBase_InitStructure);
TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM7, ENABLE);
}
void
TIM7_IRQHandler(
void
)
{
if
(TIM4->CCR1==0){TIM4->CCR1=1000;}
else
{TIM4->CCR1=0;};
TIM7->SR=0;
}

As you can see, I tried structures and direct registers access but nothing worksd. By the way if I enter debug mode and add breakpoint at Irq handler, everything works fine. As soon as I disable the breakpoint, still nothing woks. Please, help me! #stm32f4-timer-tim7-interrupt
4 REPLIES 4
Posted on July 26, 2013 at 16:54

I'd probably explicitly clear the IRQ, and do so early in the IRQ Handler. Post an example which is concise and complete.

This is how a complete example should look

// STM32 TIM7 PD.12 Toggle STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* TIM7 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
/* GPIOD clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_ResetBits(GPIOD, GPIO_Pin_12);
}
/**************************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable TIM7 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**************************************************************************************/
void TIM7_IRQHandler(void) // 2Hz
{
static int state = 0;
TIM_ClearITPendingBit(TIM7,TIM_IT_Update);
state ^= 1;
if (state) // Toggle PD12 for 1 Hz rate
GPIO_SetBits(GPIOD, GPIO_Pin_12);
else
GPIO_ResetBits(GPIOD, GPIO_Pin_12);
}
/**************************************************************************************/
void TIM7_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 42000 - 1; // N-1, 84 MHz to 2 KHz
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 2 KHz to 2 Hz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure);
/* TIM7 enable counter */
TIM_Cmd(TIM7, ENABLE);
/* TIM7 enable update interrupt */
TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
TIM7_Configuration();
while(1); // Don't want to exit
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
joe239955_stm1_stmicro
Associate II
Posted on August 01, 2013 at 03:30

The stm32f4xx_tim.h file states that the TIM_TimeBaseInitTypeDef  structure is used with all TIMx except for TIM6 and TIM7.  Is this part of the problem? Which structure definition is used for TIM6 and TIM7?

Posted on August 01, 2013 at 04:33

No, I think the issue here is that TIM_ClockDivision is ignored, they can only count up, have no channels, and more generally that TIM6 and TIM7 are internal, driving no external pins. About the only field used are TIM_Prescaler and TIM_Period.

The same include file suggests TIM_Period must be between 0 and 0xFFFF, which clearly isn't the case for 32-bit timers.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
joe239955_stm1_stmicro
Associate II
Posted on August 01, 2013 at 04:44

I now understand, thanks.