2020-09-19 04:29 AM
Hi:
My platform: STM32WB55CE.
I'm using internal clock.
I have rigged the TIM2 with an interrupt function. The interrupt could fire, but it will be fired right after all the initialization were done. More over, no matter how I change the prescaler value or the period value for TIM2's init struct, the interrupt seems always fire at the same short, <1second interval.
I don't know where to begin, this is quite strange! Did I configured the wrong clock source? I heard that if you designated the wrong clock source the timer shouldn't work at all.
Please help!
2020-09-19 07:49 AM
Are you adjusting the registers or just the initialization structure?
2020-09-19 07:02 PM
Hi: Thank you for your reply!
Only the init struct.
But please understand that by "time hasn't changed" I mean it hasn't changed even after I reset the chip and ran from the very start. I understand that you need to adjust the registers in real time for it to take effect in real time. In my case I don't need real time changes.
Unless even for the first time, you need to do a run-time change on the register for everything to work, that would be nuts!
Could it be I'm overflowing the prescaler and period related registers? For prescaler I'm using value 9999, and period I'm also using 9999. I've tried to increase it to 999,999 each in order to observe changes, but nothing.
2020-09-19 07:30 PM
2020-09-20 06:15 AM
"You're probably just not running the code you think you're running."
Unlikely... or the ISR shouldn't fire.
"Examine the register values while running to ensure they're updated and behaving correctly."
Yea, I'll do just that.
"Note that 999,999 is not a 16-bit value so isn't going to work for 16-bit registers."
See here's the problem, I have spent most of the time tinkering older mcus like the kind with 8Mhz clock, so I might have greatly overestimated how great the max possible time period for a timer is. I wish I could go like 5 seconds without a software counter... that may be a pipe dream. In the old days it was relatively easy because the core clock was at 4Mhz or 8Mhz instead of easily 32Mhz. Would 5 secs be possible? Or even 1 sec be possible?
2020-09-20 08:30 AM
> Unlikely... or the ISR shouldn't fire.
You don't think incorrect code could also trigger the ISR?
> See here's the problem
You can adjust the prescaler, reload counter and clock divider. You could also set the interval to smaller than you want and have a counter variable which increments and only takes action when appropriate. There are also 32-bit timers (TIM2) which accept a 32-bit ARR value (but still only a 16-bit PSC). You could also set the system clock to 4MHz if that's what you really want. Lots of solutions here.
Edit:
If you want a timer to update at 0.2 Hz, this is possible even at max clock speed and without using the clock divider:
32MHz / 65536 / 65536 = 0.00745 Hz
2020-09-26 06:03 AM
Hi TDK! Thank you for your replies!
Sorry to bother you again but you are right! My code isn't working... so
>You don't think incorrect code could also trigger the ISR?
I sure do now lol
Anyway here's my HAL code:
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 16000;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 16000;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_RegisterCallback(&htim2,HAL_TIM_PERIOD_ELAPSED_CB_ID,TIM2_Cb) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
}
if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
And here's the screen cap of my registers, obviously not right...
I put the breakpoint after all the initialization were done. What went wrong? Some of the registers have the right value, most don't.
Please help... once more!