2017-01-25 02:07 AM
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);}2017-01-25 02:22 AM
And what happens when you properly initialize the timer init struct?
JW
2017-01-25 02:23 AM
2017-01-25 03:10 AM
I noticed you are using AC6. If the project was generated by hand (not by CubeMX), the startup file does not contain a populated interrupt vector table, so your routine is actually never called.
2017-01-25 03:13 AM
Ok, so you recommend to use CubeMX to generate the startup and system code and then build the project on top of it?
2017-01-25 05:09 AM
Looks like a good idea. Projects generated by AC6 are far from being complete. Atollic, Keil and others generate complete empty projects with usable startup files and ready-to-use headers with a few mouse clicks.