Skip to main content
hossein hosseini
Associate III
January 25, 2018
Solved

why interrup occerd when TIM1 is not ENABLE??

  • January 25, 2018
  • 6 replies
  • 1908 views
Posted on January 25, 2018 at 10:19

hi guys.

i wrote a code with IAR , and when is run the code in stm32f103ret , cpu go to  the interrupt s routin.

i dont know why??!!

i want to have 1msecond delay for each timer priode , but i cant do that. when the code runs , cpu go to interrupt routine fanction. i do some thing that i know. but i cant do this activity. this is my code:

who know why I cant??

so thanks.

#include 'stm32f10x.h'

#include 'stm32f10x_gpio.h'  // for Enable LCD

#include 'stm32f10x_rcc.h'   //for Enable LCD

#include 'stm32f10x_tim.h'

#include 'LCDLib.h'

#include 'delay.h'

void tim_init(void);

void GPIO_init(void);

void TIM1_UP_TIM16_IRQHandler (void){

  int i;

  i = TIM_GetPrescaler(TIM1);

 

  lcdGoxy(1 , 2);

  lcdPutInt(i);

  lcdGoxy(1 , 3);

  lcdPutString ('tim activated');

  TIM_ClearITPendingBit(TIM1,TIM_IT_Update);

}

int main()

{  

  lcd_init();

  tim_init();

  GPIO_init();

  while(1){

    

  }

 

 

}

void tim_init(){     

 

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  /* Time base configuration */

  TIM_TimeBaseStructure.TIM_Period = 55000;

  TIM_TimeBaseStructure.TIM_Prescaler = 55999;

  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

 

  TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); 

 

  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the TIM2 global Interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

}

void GPIO_init(void){  

 

  RCC_APB2PeriphClockCmd  ( RCC_APB2Periph_GPIOA, ENABLE );  

 

  GPIO_InitTypeDef gpioA;     

  gpioA.GPIO_Mode = GPIO_Mode_Out_PP ;

  gpioA.GPIO_Pin = GPIO_Pin_0;

  gpioA.GPIO_Speed = GPIO_Speed_10MHz;  

  GPIO_Init  ( GPIOA,  &gpioA );

}

    This topic has been closed for replies.
    Best answer by Tesla DeLorean
    Posted on January 27, 2018 at 22:05

    &sharpinclude 'stm32f10x.h'

    &sharpinclude 'stm32f10x_gpio.h'  // for Enable LCD

    &sharpinclude 'stm32f10x_rcc.h'   //for Enable LCD

    &sharpinclude 'stm32f10x_tim.h'

    &sharpinclude 'LCDLib.h'

    &sharpinclude 'delay.h'

    void tim_init(void);

    void GPIO_init(void);

    void TIM1_UP_IRQHandler(void) // assuming startup_stm32f103xe.s pulled

    {

      static int i = 0;

      if (TIM_GetITStatus(TIM1, TIM_IT_Update) == SET)

      {

          TIM_ClearITPendingBit(TIM1,TIM_IT_Update);

            GPIOA->ODR ^= GPIO_Pin_0; // Toggle PA0

        lcdGoxy(1 , 2);

              lcdPutInt(i++); // Incrementing count

          lcdGoxy(1 , 3);

          lcdPutString ('tim activated');

        }

    }

    void TIM1_UP_TIM16_IRQHandler(void) // if startup_stm32f10x_hd_vl.s being pulled

    {

        TIM1_UP_IRQHandler();

    }

    int main()

    {

      RCC_ClocksTypeDef RCC_Clocks;

      uint32_t j;

      lcd_init();

      lcdPutString ('Believe Your Dreams.');

      tim_init();

      GPIO_init();

      RCC_GetClocksFreq(&RCC_Clocks);

      j = (RCC_Clocks.PCLK2_Frequency/1000);

      lcdGoxy(1 , 4);

      lcdPutInt(j);

      GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET);

      while(1){

      }

    }

    void tim_init()

    {

      NVIC_InitTypeDef NVIC_InitStructure;

      TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

      /* Enable the TIM1 global Interrupt, before we enable it on TIM */

      NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;

      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

      NVIC_Init(&NVIC_InitStructure);

      /* Time base configuration */

      TIM_TimeBaseStructure.TIM_Prescaler = 36000 - 1; // KHz, take 36 MHz clock to 1 KHz

      TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // Hz (1) 1000 ticks of 1 KHz clock

      TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

      TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

      TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

      TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

      /* TIM1 enable counter */

      TIM_Cmd(TIM1, ENABLE);

      /* Enable the update interrupt */

      TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);

    }

    void GPIO_init(void)

    {

      GPIO_InitTypeDef gpioA;

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

      gpioA.GPIO_Mode = GPIO_Mode_Out_PP;

      gpioA.GPIO_Pin = GPIO_Pin_0;

      gpioA.GPIO_Speed = GPIO_Speed_10MHz;

      GPIO_Init  (GPIOA,  &gpioA);

    }

    6 replies

    Tesla DeLorean
    Guru
    January 25, 2018
    Posted on January 25, 2018 at 16:33

    TIM1_UP_IRQn not equivalent to TIM1_UP_TIM16_IRQHandler, needs some coherency. 

    Check the name in the vector table in startup_stm32f1xxx.s if there is no linkage, the function isn't going to get called.

    If using .CPP files, makes sure to use extern 'C' so the names aren't mangled.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    hossein hosseini
    Associate III
    January 27, 2018
    Posted on January 27, 2018 at 15:11

    thanks.

    i change  to tim2 . and i change startup file (got it from cube).

    but cant slove my problem.

    i test many ways,

    i dont know what do i do !!!!

    do you have any seggestions for me??

    thanks you.

    Tesla DeLorean
    Guru
    January 27, 2018
    Posted on January 27, 2018 at 15:30

    You test many ways but show next to no detail. What file? What are the vectors in it? Consider ZIPing up a project and attaching so the context can be understood.

    The sample shown looks to come from SPL rather than Cube/HAL

    The TIM divides down 36 MHz, it will interrupt every 85 seconds, not 1 ms

    TIM1_UP_IRQHandler would be expected with 

    TIM1_UP_IRQn

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..