cancel
Showing results for 
Search instead for 
Did you mean: 

Timer Problem

pedro
Associate II
Posted on February 28, 2015 at 23:37

Hi,

I'm having a problem with timer 2 on the stm32f407 discovery board.

I want a frequency of 1khz but I can only get 1ms before entering, for the first time, in the ISR. After that, I get Stuck on the ISR and because i can not pass the if statment, on the debugger. I can see SR register (debugger) but it's blocked for writing, i believe.

Any help would be appreciated, thanks.

-------------------------------------------------------------------------------------------------------

void INIT_TIM2(){

TIM_TimeBaseInitTypeDef TIM2_Init;

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

//Configure Timer 2 to Freq = 1khz

TIM2_Init.TIM_CounterMode = TIM_CounterMode_Up;

TIM2_Init.TIM_RepetitionCounter = 0x00;

TIM2_Init.TIM_ClockDivision = TIM_CKD_DIV2;

TIM2_Init.TIM_Prescaler = 84-1;

TIM2_Init.TIM_Period = 1000-1;

TIM_TimeBaseInit(TIM2, &TIM2_Init);

TIM2->SR &= 0xfffe;

//Confiruge Timer2 interrupt

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

TIM_Cmd(TIM2, ENABLE); // Enable Timer2

//TIM2->CR1 |= 0x001; 

}

void TIM2_IRQHandler (void) {

  if ((TIM1->SR & 0x0001) != 0)

    {

        TIM1->SR &= ~(1<<0);

}

}

int main(){

char* text[100];

INIT_TIM2();

while(1);

}
4 REPLIES 4
Posted on March 01, 2015 at 00:33

TIM2 not TIM1

SR = ~1 not SR &= ~1, it's got specific write behaviour, the RMW can clear interrupts other than the ones you want.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
pedro
Associate II
Posted on March 01, 2015 at 00:52

That was a lapse.

Still after changing tim1 to tim2 the timer only goes to 1ms on the first time it enters de if statment. After that the behaviour is irregular (on the debugger). It can go like 0.5ms or 0.2ms or 1 ms.

Also, I can not see value change on the SR register after the instruction its completed.

What I've changed:

if ((TIM2->SR & 0x0001) != 0)

    {

        TIM2->SR = ~(1<<0);

}

pedro
Associate II
Posted on March 01, 2015 at 01:31

I believe the problem is from the debugger since I passed the code to the board and I think it's working fine.

Any ideia what causes the debugger this wrong behaviour?
Posted on March 01, 2015 at 13:35

Not sure what debugger you're using, but the TIM will generally keep ticking when the CPU stops (it's a peripheral), so unless you have very quick responses a 1ms interrupt is going to occur more frequently than you can step through the code.

The DBGMCU peripheral can be set to stop timers
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..