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