1

I'm using Timer to make the LED on/off for every 1 second (instead of using delay function). However, when I execute this code on IAR IDE, the LED on/off at about 2.5-3 second, not 1 second like I want. I wonder if there is any mistake in the code, or do I have to modify somewhere for the correct clock speed?

I'm using standard peripheral library, and the IDE I'm using is IAR.

#include "stm32f4xx.h"

GPIO_InitTypeDef  GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_BaseStruct;

void GPIO_Configuration(void);
void TIM_Configuration(void);
void Delay(__IO uint32_t nCount);

int main(void)
{
  GPIO_Configuration();
  TIM_Configuration();
  while(1)
  {
    if(TIM_GetFlagStatus(TIM2,TIM_FLAG_Update) != RESET)
    {
      TIM_ClearFlag(TIM2,TIM_IT_Update);
      GPIO_ToggleBits(GPIOD,GPIO_Pin_14);
    }
  }
}

void GPIO_Configuration(void)
{
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  /* Configure PB0 PB1 in output pushpull mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}

void TIM_Configuration(void)
{
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

  TIM_BaseStruct.TIM_Prescaler = 42000-1;
  TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_BaseStruct.TIM_Period = 2000-1;
  TIM_BaseStruct.TIM_ClockDivision = 0;
  TIM_BaseStruct.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM2,&TIM_BaseStruct);

  TIM_Cmd(TIM2,ENABLE);
}


void Delay(__IO uint32_t nCount)
{
  while(nCount--)
  {
  }
}

#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif
  • 1
    The timer just counts clock cycles, so the meaning of the prescaler/period values depend entirely on your clock frequency - which you haven't specified. – jasonharper Mar 02 '17 at 21:37
  • Could you please specify in detail where I can see and modify the clock frequency in IAR? (not in Keil C) – Nguyễn Thanh Vũ Mar 03 '17 at 11:31
  • There is no place in your code where you would specify the clock frequency directly. You just need to take it into account when choosing the timer parameters. – jasonharper Mar 03 '17 at 13:04

1 Answers1

0

I'm going to assume your clock frequency is 168Mhz. With your current prescaler and period values, this would put your timer at 2s.

(CLK_FREQ / (PRESCALER * PERIOD)) = (168MHZ / (42000 * 2000)) = 2 seconds

Try changing the period to 1000 to make it run at 1s. This, again, is assuming your clock frequency is 168Mhz. If your clock frequency is different, you can easily calculate the PRESCALER and PERIOD values (shown above) to get to 1s.

mban
  • 422
  • 1
  • 6
  • 19