2013-07-26 07:48 AM
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
2013-07-26 07:54 AM
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
}
2013-07-31 06:30 PM
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?
2013-07-31 07:33 PM
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.2013-07-31 07:44 PM
I now understand, thanks.