AnsweredAssumed Answered

STM32F303K8 timer interrupt hangs

Question asked by mirk.hjl on Jan 25, 2017
Latest reply on Jan 25, 2017 by gbm

Hi,

 

I try to implement a timer with interrupt routine, but it always hangs after the timer is activated. This can be seen if I use the debugger over SWD: when I execute the next command after TIM_Cmd(TIM2, ENABLE); there is no more response from the uC. Does someone have an idea?

 

My code:

 

/**
******************************************************************************
* @file main.c
* @author Ac6
* @version V1.0
* @date 01-December-2013
* @brief Default main function.
******************************************************************************
*/


#include "stm32f30x.h"
//#include "USART_DMA.h"
//#include "FOC.h"

int main(void)
{

SystemInit();

//USART init
//init_usart(57600);
//send_usart_data("Go!");

//FOC init
//initTimerFOC();
initTimer();


//LED init
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);


while(1) {
GPIO_ResetBits(GPIOA, GPIO_Pin_6);
for(int i=0;i<100000;i++);
GPIO_SetBits(GPIOA, GPIO_Pin_6);
for(int i=0;i<100000;i++);
//send_usart_data("X");
}
}

void TIM2_IRQHandler(void) {
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

}
}

void initTimer() {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);

TIM_TimeBaseInitTypeDef TIM2Init;

TIM2Init.TIM_Prescaler = 10;
TIM2Init.TIM_Period = 2000;
TIM2Init.TIM_ClockDivision = TIM_CKD_DIV1;
TIM2Init.TIM_CounterMode = TIM_CounterMode_Up;
TIM2Init.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM2Init);

TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

//Timer
TIM_Cmd(TIM2, ENABLE);
}

Outcomes