cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with TIM1 in stm32f103RB

msanchez
Associate II
Posted on November 24, 2015 at 12:34

Hello,

I'm programming a timer in stm32f103rb but when I load the code the IAR show me an error. This code goes well in another microcontroller stm32f103RD. I show the code:

/* Public variables ----------------------------------------------------------*/

GPIO_InitTypeDef GPIO_InitStruct;

NVIC_InitTypeDef NVIC_InitStruct;

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

USART_InitTypeDef USART_InitStructure;

uint8_t TXON_INTERRUPCION[14] = ''\nINTERRUPCIÓN'';

 

/* Public functions ----------------------------------------------------------*/

void RCC_Config()

{

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);                         // Enable GPIOA clock 

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);                          // Enable TIM1 clock

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);                        // Enable USART1 clock 

}

void GPIO_Config()

{

  /* Configuración GPIO para poner pin a nivel alto y bajo*/

  GPIO_InitStruct.GPIO_Pin =  GPIO_Pin_11;                                      // We are going to use PA11

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;                                 // Alternate function Push-Pull mode                                  

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;                                // Set GPIO speed

  GPIO_Init(GPIOA, &GPIO_InitStruct);                                           // GPIOA11 ready

  

  /* Configure Transmissor */

  GPIO_InitStruct.GPIO_Pin =  GPIO_Pin_9;                                       // We are going to use PA9

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;                                  // Alternate function Push-Pull mode                                  

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;                                // Set GPIO speed

  GPIO_Init(GPIOA, &GPIO_InitStruct);                                           // GPIOA9 ready

}

void USART_Config()

{

  USART_InitStructure.USART_BaudRate = 9600;

  USART_InitStructure.USART_WordLength = USART_WordLength_8b;

  USART_InitStructure.USART_StopBits = USART_StopBits_1;

  USART_InitStructure.USART_Parity = USART_Parity_No;

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  USART_Init(USART1, &USART_InitStructure);

  USART_Cmd(USART1, ENABLE);

}

                

void txon(uint8_t TAM[], uint8_t size)

{

  uint8_t i=0;                                                                  // Index for a loop

  

  for (i=0;i<size;i++)                                                          // Sending TAM[] by USART

  {

    while (!USART_GetFlagStatus(USART1, USART_FLAG_TXE));

    USART_SendData(USART1, TAM[i]);

  }

  while(!USART_GetFlagStatus(USART1, USART_FLAG_TC));                           // Waiting for complete transmission

}

void NVIC_Config()

{

  NVIC_SetPriorityGrouping(NVIC_PriorityGroup_0);

  

/* Configure TIM1 IRQ*/

  NVIC_InitStruct.NVIC_IRQChannel = TIM1_UP_IRQn;

  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;

  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStruct);

}

void TIM_Config()

{

  TIM_TimeBaseInitStructure.TIM_Period = 51445 - 1;                             // Configures the period value to be loaded into the active Auto-Reload Register at the next update event 60000 - 1 (0xEA5F)

  TIM_TimeBaseInitStructure.TIM_Prescaler = 36000 - 1;                          // Configure the prescaler value used to divide the TIM clock [0x0000 - 0xFFFF] 36000 - 1 (0x8C9F)

  TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;                   // Configures the clock division

  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;               // TIM Upcounting mode

  TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 6;                          // TIM counter repetition

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);

}

void TIM1_UP_IRQHandler(void)

{

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

  {

    /*Clear the Update pending bit*/

    TIM_ClearITPendingBit(TIM1, TIM_IT_Update);

    

    /*Comprobación del tiempo que tarda en generar la interrupción*/

    GPIO_SetBits(GPIOA, GPIO_Pin_11);

    GPIO_ResetBits(GPIOA, GPIO_Pin_11);

    txon(TXON_INTERRUPCION,15);

  }

}

/**

  * @brief  Main program

  * @param  None

  * @retval None

  */

void main(void)

{

  RCC_Config(); 

  GPIO_Config();                                                                // Setup GPIO

  USART_Config();

  TIM_Config();

  NVIC_Config();

  

  /*Enable the TIM Update Interrupt*/

  TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);

  /*TIM counter enable*/

  TIM_Cmd(TIM1, ENABLE);

  

  NVIC_EnableIRQ(TIM1_UP_IRQn);

  

  while(1);

}
3 REPLIES 3
Posted on November 24, 2015 at 12:44

but when I load the code the IAR show me an error

Error says what?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
msanchez
Associate II
Posted on November 25, 2015 at 09:18

Now I don't have any error, the only problem is that the timer doesn't work. The first time the function work but then doesn't work again.

msanchez
Associate II
Posted on November 25, 2015 at 10:37

The code is correct and works well. I think the problem is in the PCB design. Sorry!!

Thank you.