cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f100: how start TIM1 interrupt handler

Posted on June 23, 2011 at 18:03

Hi!

I've to program STM32f100R4H6; particularly i've to manage TIM1 interrupt.

Below there is my code..Thank you in advance..

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

//In the first part of the main..

void RCC_init(void){

// 1. Clocking the controller from internal HSI RC (8 MHz)

  RCC_HSICmd(ENABLE);

  // wait until the HSI is ready

  while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);

  RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);

  // 2. Enable ext. high frequency OSC

  RCC_HSEConfig(RCC_HSE_ON);

  // wait until the HSE is ready

  // 3. Init PLL

  RCC_PLLCmd(ENABLE);

  // wait until the PLL is ready

  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

  // 4. Set system clock dividers

  RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

  RCC_ADCCLKConfig(RCC_PCLK2_Div8);

  RCC_PCLK2Config(RCC_HCLK_Div1);

  RCC_PCLK1Config(RCC_HCLK_Div2);

  RCC_HCLKConfig(RCC_SYSCLK_Div1);

  /*&sharpdefine EMB_FLASH

&sharpifdef EMB_FLASH

  // 5. Init Embedded Flash

  // Zero wait state, if 0 < HCLK 24 MHz

  // One wait state, if 24 MHz < HCLK 56 MHz

  // Two wait states, if 56 MHz < HCLK 72 MHz

  // Flash wait state

  FLASH_SetLatency(FLASH_Latency_2);

  // Half cycle access

  FLASH_HalfCycleAccessCmd(FLASH_HalfCycleAccess_Disable);

  // Prefetch buffer

  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

&sharpendif // EMB_FLASH */

 

  // 5. Clock system from PLL

  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

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

//Function that manage TIM1

void tim1_init(void){

/*-------TIMER START----------*/

 TIM_TimeBaseInitTypeDef TIM1_TimeBaseInitStruct;

  // Timer1 Init

  // Enable Timer1 clock and release reset

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

  RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1,DISABLE);

  /*-----------------SETTAGGIO PARAMETRI TIMER1----------------*/

  //  Prescaler=7200  &   Period=10000 & Repetition =0-->Trigger event ogni secondo

  //  Prescaler=3600  &   Period=10000 & Repetition =0-->Trigger event ogni 0.5 secondi

  //  Prescaler=720   &   Period=10000 & Repetition =0-->Trigger event ogni 100ms

  //  Prescaler=6534  &   Period=10   & Repetition =0-->Trigger event ogni 1ms

  /*-----------------------------------------------------------*/

  TIM1_TimeBaseInitStruct.TIM_Prescaler = 1; 

  TIM1_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;

  TIM1_TimeBaseInitStruct.TIM_Period = 1; 

  TIM1_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;

  TIM1_TimeBaseInitStruct.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM1,&TIM1_TimeBaseInitStruct);

  // Clear update interrupt bit

  TIM_ClearITPendingBit(TIM1,TIM_IT_Update);

  // Enable update interrupt

  TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);

  NVIC_InitTypeDef NVIC_InitStructure; //create NVIC structure

  NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM16_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  // Enable timer counting

  TIM_Cmd(TIM1,ENABLE);

/*---------END TIMER----------*/ 

}

void TIM1_UP_TIM16_IRQHandler(void){

...

}

#problem #tim1 #stm32 #timer #interrupt
10 REPLIES 10
Posted on June 23, 2011 at 22:29

To repeat previous advice given to you, initialize the NVIC before enabling interrupts it is expected to service.

You enable the PLL, but don't appear to be setting up multipliers/dividers for it.

Does the STM32F100 series support USB?

The STM32F100 has a max speed of 24 MHz, no need to set wait-states for the flash. But again watch the PLL settings. The APB1/2 and AHB will all clock comfortably at 24 MHz.

You enable the TIM1 with ridiculous prescale and period values. The STM32 is incapable of servicing interrupts in the MHz range, you will simply saturate the processor. At 24 KHz you'd have 1000 cycles to play with.

So for a 24 MHz TIM clock, this would give a 1 KHz update interrupt.

 

  TIM1_TimeBaseInitStruct.TIM_Prescaler = 24 - 1; 

  TIM1_TimeBaseInitStruct.TIM_Period = 1000 - 1; 

A 1 Hz rate

  TIM1_TimeBaseInitStruct.TIM_Prescaler = 24000 - 1; 

  TIM1_TimeBaseInitStruct.TIM_Period = 1000 - 1; 

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on June 24, 2011 at 00:39

This is a workable example w/TIM2 using the VL Discovery library (V3.3.0)

#include ''stm32F10x.h''

#include ''STM32vldiscovery.h''

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

* Function Name  : TIM2_IRQHandler

* Description    : This function handles TIM2 global interrupt request.

* Input          : None

* Output         : None

* Return         : None

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

void TIM2_IRQHandler(void)

{

  if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

  {

    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

    /* Whatever */

    STM32vldiscovery_LEDToggle(LED3);

  }

}

/**

  * @brief  Main program.

  * @param  None

  * @retval None

  */

int main(void)

{

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  /* Initialise LEDs LD3&LD4, both off */

  STM32vldiscovery_LEDInit(LED3);

  STM32vldiscovery_LEDInit(LED4);

  STM32vldiscovery_LEDOff(LED3);

  STM32vldiscovery_LEDOff(LED4);

  /* Enable the TIM2 gloabal 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);

  /* TIM2 clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* VL clocks at 24 MHz, change to 72 - 1 for a 72 MHz system

  /* Time base configuration */

  TIM_TimeBaseStructure.TIM_Period = 1000 - 1;  // 1 MHz down to 1 KHz (1 ms)

  TIM_TimeBaseStructure.TIM_Prescaler = 24 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock)

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  /* TIM IT enable */

  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

  /* TIM2 enable counter */

  TIM_Cmd(TIM2, ENABLE);

  while (1);

}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on June 24, 2011 at 08:53

Thank you so much for your help.

I've done what you suggest me, but again the interrupt does't start!!

In the first post of this topic, in the code i've posted, the problem was the same..doesn't start the interrupt..i don't understand why.

I've just try the code you've post and i've try that code in my board whit STM32F100R4H6...no error, but the interrupt does't start.In my project the only file where interrupt were citate is startup_stm32f10x_ld_vl.s (attach file)...maybe in the project miss the right file.h.

I attach also the complete project...Again thank you!!!

________________

Attachments :

Firmware_811.7z : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0t3&d=%2Fa%2F0X0000000bf7%2Fsg7ZsSq2Ro1_JTAgGvmlNF5j0M7ongQivw36AdXIjOY&asPdf=false

startup_stm32f10x_cl.s : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0sy&d=%2Fa%2F0X0000000bf5%2FEj_MpB0W_ELh7Gnq9G921iheXSjpDCajxQgMgB.zzYw&asPdf=false
ezyreach
Associate II
Posted on June 24, 2011 at 09:05

Check the Linker - edit settings and check whether the interrupt vector address is mapped to 0x08003000..

If the address is 0x08000000 then the interrupt will not work.

Posted on June 24, 2011 at 23:27

Check the Linker - edit settings and check whether the interrupt vector address is mapped to 0x08003000..

 

 

If the address is 0x08000000 then the interrupt will not work.

 

That would be a hugely asinine thing for IAR to do. By default if the FLASH is booting the vector table should appear at zero and 0x08000000

Check the value of __vector_table in the .MAP file

One could presumably configure it in the NVIC directly so there's no chance the compiler can screw it up.

extern void * __vector_table;

NVIC_SetVectorTable((u32)(&__vector_table), 0x0);

Either way, the code is viable in Keil, so check your environment.

I'd also suggest, that instead of pasting my code into a broken project, you get the VL Discovery firmware, build a couple of those in IAR, prove they appear to be working. Then paste in my main() and interrupt code into those.

I'm not convinced you RCC_init() code is viable, certainly in the VL Discovery project the system clocks are already set up via the CMSIS library and the defines the compiler uses to build the project.

Oddly the web address silk screened on the board ISN'T VIABLE

Instead use :

http://www.st.com/internet/evalboard/product/250863.jsp

http://www.st.com/internet/com/SOFTWARE_RESOURCES/SW_COMPONENT/FIRMWARE/stm32vldiscovery_package.zip

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
enor28910
Associate
Posted on October 17, 2011 at 09:17

Hello, everybody !.

I'm a new member , i have been finding out about Tim2 interrupt and i'm having a problem about

''/* Enable the TIM2 gloabal Interrupt */

''

and

  ''  

/* Enable USARTy Receive and Transmit interrupts */

'' .I'm using both of them for my project . The first, I want to make pre-emption priority UART and then, I want to make subpriority TIM2 but i don't understand about 

'' NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;''  clearly .

Can you help me about it ?.

Posted on October 17, 2011 at 15:49

Do you need different priorities? If so the preemption priority is the way to go.

Try reviewing the technical documentation ARM provides for the Cortex M3 core.

Take a look at Joseph Yiu's book on the M3.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
sinan2
Associate
Posted on November 04, 2015 at 22:42

Hi clive1,

I applied your timer example for smt32f103, it works but I noticed that when Timer is initialized as in the example. It branches to IRQ_handler right after running code  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE) at initialization stage even its counter isn't equal to period value.

When I check the registers UIF is set by TIM_TimeBaseInit(TIM2, &timerInitStructure) and after runing TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE) program branches the program to IRQ_handler.

Is there any extra procedure that should be applied to prevent runing timer_irq _handler suddenly after initialization? Is that problem possible or Did I do something wrong?

PS: Timer period is set to 0.5 second.

Regards

Sinan