cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 TIM2 configuration

daniel239955_stm1
Associate II
Posted on May 06, 2015 at 19:21

Hi there..!

sorry if my question seems duplicate but I dont know what's wrong, I want to set TIM2 interrupt to 1 ms without success. this is my clock configuration

#define __RCC_CR_VAL 0x01010082
#define __RCC_CFGR_VAL 0x00118402
#define __HSE 12000000
void ClockSetup (void) {
/* Clock Configuration*/
RCC->CFGR = __RCC_CFGR_VAL; // set clock configuration register
RCC->CR = __RCC_CR_VAL; // set clock control register
if (__RCC_CR_VAL & RCC_CR_HSEON) { // if HSE enabled
while ((RCC->CR & RCC_CR_HSERDY) == 0); // Wait for HSERDY = 1 (HSE is ready)
}
if (__RCC_CR_VAL & RCC_CR_PLLON) { // if PLL enabled
while ((RCC->CR & RCC_CR_PLLRDY) == 0); // Wait for PLLRDY = 1 (PLL is ready)
}
/* Wait till SYSCLK is stabilized (depending on selected clock) */
while ((RCC->CFGR & RCC_CFGR_SWS) != ((__RCC_CFGR_VAL<<2) & RCC_CFGR_SWS));
} // end of ClockSetup

and this is my TIM2 initialization

void vTIMER_InitRCC(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
void vTIMER_InitGeneral(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* Time Base configuration 1ms based on HCLK 72MHz using HSE * 6 */
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 9;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); /* Enable the TIM Update Interrupt */
TIM_Cmd(TIM2, ENABLE); /* TIM counter enable */
NVIC_EnableIRQ(TIM2_IRQn); /* Enable TIM2 interrupt */
}
void TIM2_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); /* Clear the Update pending bit */
timer1ms++;
/* Periodic cyclic function call */
if(timer1ms == 1000)
{
timer1ms = 0;
timer1s++;
}
}

and this is my code for turning on LED

... //initializations
while(1)
{
if(timer1s == 1)
{
//readDoorStatus(I2C1, 13);
timer1s++;
STM_EVAL_LEDOn(LED1);
}
else if(timer1s == 11)
{
STM_EVAL_LEDOff(LED1);
timer1s = 0;
} 
}
...

but my LED is ON only for 7 second??
6 REPLIES 6
Posted on May 06, 2015 at 19:39

Not really up to decipher the registers, but you look to turn on HSE, and PLL, but not seeing the PLL being selected as the clock source for the CPU.

Might want to review the code in system_stm32f1xx.c and whether SystemInit() is called prior to main()

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
daniel239955_stm1
Associate II
Posted on May 06, 2015 at 20:03

thanks for your quick reply.

the registry values set for this configuration PLLMUL = 6 HESON PLLSRC = HSE SYSCLK = PLLCLK HCLCK = SYSCLCK // AHB PCLCK2 = HCLCK // APB2 PCLCK1 = HCLCK / 2 // APB1 but in debug mode these lines passed without problem,

if (__RCC_CR_VAL & RCC_CR_PLLON) { // if PLL enabled
while ((RCC->CR & RCC_CR_PLLRDY) == 0); // Wait for PLLRDY = 1

}
 i couldn't find any good code for setting clock for HSE using stdperiph library, i use below code but program cant pass this line : 
 while (RCC_GetSYSCLKSource() != 0x08)

#define HSE_VALUE 12000000 // this line add to compiler defines too
void SetSysClockTo72(void)
{
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------*/ 
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
ErrorStatus HSEStartUpStatus = RCC_WaitForHSEStartUp();
if (HSEStartUpStatus == SUCCESS)
{
/* Flash 0 wait state */
FLASH_SetLatency(FLASH_Latency_0);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1); 
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1); 
/* PCLK1 = HCLK */
RCC_PCLK1Config(RCC_HCLK_Div1);
/* PLLCLK = (8MHz/2) * 6 = 24 MHz */
//RCC_(RCC_PREDIV1_Source_HSE, RCC_PREDIV1_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6);
/* Enable PLL */ 
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while (RCC_GetSYSCLKSource() != 0x08)
{
}
}
else
{
/* If HSE fails to start-up, the application will have wrong clock configuration.
User can add here some code to deal with this error */ 
/* Go to infinite loop */
while (1)
{
}
}
}

daniel239955_stm1
Associate II
Posted on May 07, 2015 at 00:11

nobody! i need some code for configuration of clock and tim2 for 1 msec interupt thanks in advance.

Posted on May 07, 2015 at 02:55

FLASH_SetLatency(FLASH_Latency_0);

Zero wait states isn't going to cut it at 72 MHz, going to need 2 or 3

RCC_PCLK1Config(RCC_HCLK_Div1);

// Needs a Div2 Your timer code prescaler/period seems reasonable enough for a 72 MHz clock.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
daniel239955_stm1
Associate II
Posted on May 07, 2015 at 09:33

thanks a lot, its work now correctly 🙂

just one another question, how to choose the right frequency for my micro? in my project I used CAN with 500kbps, USART (RS232 19200BR and RS485 19200BR) and I2C with 100kbps , how can i find the right frequency from these numbers? thanks in advance.

Posted on May 07, 2015 at 19:44

You'd want to figure out the system loading, in an RTOS with an idle task you could use a GPIO and WFI (Wait For Interrupt) and gauge it. If your tool chain provides profiling you could use that, or instrument other critical portions of your code.

You could evaluate the current consumption, with the WFI, and at different frequencies.

The awkwardness of the STM32's divider chains may limit the speeds you can select, and the outputs you can achieve via CAN and TIM peripherals.

I had to run one of my F1's at 64 MHz to get a true 256 KHz clock out of it.

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