Skip to main content
Tom Power
Associate II
August 16, 2017
Question

STM32F0308 TIM3 Interrupt not triggering

  • August 16, 2017
  • 7 replies
  • 1398 views
Posted on August 16, 2017 at 16:46

Hi, 

I am having trouble getting an interrupt working with the TIM3, roughly following this guide: 

http://www.micromouseonline.com/2016/02/03/tim3-arr-regular-interrupts-stm32f4/

 

but adapting for the STM32F0308. 

TIM3 is running, and reloading (verified with the commented out code in the main loop ), but the interrupt flag does not appear to be triggering. 

Any help would be greatly appreciated, my code is below:

&sharpinclude 'stm32f0xx.h'

&sharpdefine BSRR_VAL        0x0300

volatile uint16_t count = 0;

void timer_interrupt_init( void );

void timer_init( void );

void timer_start( void );

void timer_interrupt_enable( void );

void leds_init( void );

/***********************************************************************************************/

void timer_interrupt_init( void )

{

NVIC_InitTypeDef NVIC_InitStructure;

/*Enable timer global interrupt */

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init( &NVIC_InitStructure );

}

/***********************************************************************************************/

void timer_init( void )

{

/*  Set timer to 1kHz and reload at 1Hz */

uint16_t prescaler = 23999; 

uint16_t reload = 1000; 

RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

/* Set everything back to default values */

TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );

/* Changes from defaults */

TIM_TimeBaseStructure.TIM_Period = reload;

TIM_TimeBaseStructure.TIM_Prescaler = prescaler;

TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );

}

/***********************************************************************************************/

void timer_start( void )

{

TIM_Cmd( TIM3, ENABLE );

}

/***********************************************************************************************/

void timer_interrupt_enable( void )

{

/* Clear interrupt flags */

TIM_ClearITPendingBit( TIM3, TIM_IT_Update );

/* Put counter into known state */

TIM_SetCounter( TIM3, 0 );

TIM_ITConfig( TIM3, TIM_IT_Update, ENABLE );

}

/***********************************************************************************************/

void leds_init( void )

{

RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE );

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_StructInit( &GPIO_InitStructure );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;

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_NOPULL;

GPIO_Init( GPIOC, &GPIO_InitStructure );

}

/***********************************************************************************************/

void TIM3_IRQHandler( void )

{

if ( count == 0 )

{

GPIOC->BSRR = BSRR_VAL;

count = 1;

}

else

{

GPIOC->BRR = BSRR_VAL;

count = 0;

}

TIM_ClearITPendingBit( TIM3, TIM_IT_Update );

}

/***********************************************************************************************/

int main(void)

{

uint16_t CurrentTimerVal;

timer_init();

timer_interrupt_init();

timer_start();

timer_interrupt_enable();

leds_init();

while (1)

{

//CurrentTimerVal = TIM_GetCounter( TIM3 );

//if ( CurrentTimerVal > 500 )

// GPIOC->BSRR = BSRR_VAL;

//else

// GPIOC->BRR = BSRR_VAL;

}

}

#stm32f0 #timer #interrups #nvic
This topic has been closed for replies.

7 replies

waclawek.jan
Super User
August 16, 2017
Posted on August 16, 2017 at 17:47

C++?

JW

Tom Power
Tom PowerAuthor
Associate II
August 16, 2017
Posted on August 16, 2017 at 17:58

No, I am writing in C

waclawek.jan
Super User
August 16, 2017
Posted on August 16, 2017 at 18:11

Okay but don't you use by mistake a C++ compiler which would mangle function names thus avoid properly linking the ISR?

You should check the compiler outputs whether the ISR is properly linked to in the interrupts table.

JW

Tesla DeLorean
Guru
August 16, 2017
Posted on August 16, 2017 at 19:29

You're using C++ syntax, calling functions before definitions.

extern 'C' void TIM3_IRQHandler( void ) // use this form if using C++ or .CPP syntax/compilation

{

// Ideally validate the source

TIM_ClearITPendingBit( TIM3, TIM_IT_Update ); // Clear first to avoid Cortex-Mx NVIC hazard

if ( count == 0 )

{

GPIOC->BSRR = BSRR_VAL;

count = 1;

}

else

{

GPIOC->BRR = BSRR_VAL;

count = 0;

}

}

Look at the .MAP file. Check the vector in startup_stm32xxxx.s

Period and Prescaler are both programmed as N-1, not N

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Tom Power
Tom PowerAuthor
Associate II
August 17, 2017
Posted on August 17, 2017 at 11:01

Waclawek.Jan

‌,

Turvey.Clive.002

,

Thank you both for you help, I managed to get it working by adding a function prototype for the IRQ3_Handler, and also by recompiling from scratch.