0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Chris Stratton Jul 13 '17 at 16:32
  • \$\begingroup\$ @ChrisStratton My bad, I've edited the post. \$\endgroup\$ – tsuo euoy Jul 13 '17 at 17:19
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Chris Stratton Jul 13 '17 at 17:23
  • \$\begingroup\$ Here is a related answer. It is for a different family of stm32's but it may provide some insight... \$\endgroup\$ – bitsmack Jul 13 '17 at 19:41
  • 1
    \$\begingroup\$ Ah, I see @ChrisStratton's confusion. Your post says 1 kHz and 500 kHz, but you mean 1 kHz and 500 Hz? \$\endgroup\$ – bitsmack Jul 14 '17 at 18:05
1
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I think that is the flag clearing issue as the frequency of 500kHz is quite high. \$\endgroup\$ – P__J__ Jul 13 '17 at 21:13
  • \$\begingroup\$ 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. \$\endgroup\$ – tsuo euoy Jul 14 '17 at 17:41
  • \$\begingroup\$ @tsuoeuoy Of course you are correct. I must have been low on sleep, or something. I'll edit that part out... \$\endgroup\$ – bitsmack Jul 14 '17 at 17:58
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ My bad @dannyf, I meant to type 500Hz. \$\endgroup\$ – tsuo euoy Jul 14 '17 at 19:37
  • \$\begingroup\$ 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. \$\endgroup\$ – dannyf Jul 14 '17 at 20:57
  • \$\begingroup\$ 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. \$\endgroup\$ – dannyf Jul 14 '17 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.