# Timer configuration on STM32F4

I'm trying to configure the timer3 to have a frequency of 1KHz, without success.

Here's my configurations:

System Clock:

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 64;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;


So SYSCLK=64MHz

Timer3:

htim3.Instance = TIM3;
htim3.Init.Prescaler = 480;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 66;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}

if(HAL_TIM_Base_Start_IT(&htim3) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}


So APB1 Timer Clock = 32MHz.

So the Timer3 Clock Frequency should be (1/(32MHz/480))*66, which is approximately 1Khz, but for some reason I'm getting 500Hz.

Any help will be greatly appreciated.

EDIT:

My interrupt function:

/*check if interrupt is from TIMER 3*/
if (htim->Instance!=TIM3)
return;

timer_counter++;

if (timer_counter == 65500)
timer_counter=0;

if(timer_counter%1000==0)
{
uint16_t test[4];

Send_CAN(&CanHandle2, 8, test);
}


I'm receiving the CAN message every 2 seconds instead of every second.

• You need to edit this to include the code or circuitry which attempts to do something with the fact that the timer is running. Note also that the division ratios will be 1 more than the value programmed into the divider registers, ie you are dividing by 481 and 67, not 480 and 66. – Chris Stratton Jul 13 '17 at 16:32
• @ChrisStratton My bad, I've edited the post. – tsuo euoy Jul 13 '17 at 17:19
• Your post conflicts with itself in the reported error - you say your supposed 1 KHz is coming out as 500 KHz but then you say your CAN messages are only two times too fast. And your ISR is incomplete and unclear which interrupt condition you have attached and if you are properly servicing the interrupt condition itself. Back up a bit and do something like toggle a GPIO and watch that on a scope. – Chris Stratton Jul 13 '17 at 17:23
• Here is a related answer. It is for a different family of stm32's but it may provide some insight... – bitsmack Jul 13 '17 at 19:41
• Ah, I see @ChrisStratton's confusion. Your post says 1 kHz and 500 kHz, but you mean 1 kHz and 500 Hz? – bitsmack Jul 14 '17 at 18:05

I don't see that you're clearing your interrupt flag. Perhaps you are doing so elsewhere in your ISR?

If not, the ISR will occur again as soon as it is finished.

• I think that is the flag clearing issue as the frequency of 500kHz is quite high. – P__J__ Jul 13 '17 at 21:13
• The flag gets cleared in a HAL library function that calls my function. Regarding your 2nd point, if(timer_counter%1000==0) is true whenever timer_counter divided by 1000 has a remainder of 0, not only when it is 0. – tsuo euoy Jul 14 '17 at 17:41
• @tsuoeuoy Of course you are correct. I must have been low on sleep, or something. I'll edit that part out... – bitsmack Jul 14 '17 at 17:58

I'm trying to configure the timer3 to have a frequency of 1KHz, without success.

step through your code, and make sure that the clock is set up properly and the timer is set up properly, through the debugger.

you can also go through the datasheet and make sure the right parameters are given.

which is approximately 1Khz, but for some reason I'm getting 500KHz.

1Khz vs. 500Khz is too big of a difference. Something is seriously wrong.

• My bad @dannyf, I meant to type 500Hz. – tsuo euoy Jul 14 '17 at 19:37
• that means there is a divide-by-2 factor somewhere. So go through the lock tree and check the APB/AHB clock, or check the timer setting (check the repetition counter, for example). somewhere somehow someone is dividing your count by 2. – dannyf Jul 14 '17 at 20:57
• another possibility is that your can transmission takes more than 1ms to finish. So before the can transmission is done, another interrupt arrives but is ignored - for example you might have cleared the flag at the end of the isr. I would flip a pin in lieu of the can transmission and see if it is still 500hz. – dannyf Jul 14 '17 at 20:59