2023-12-14 08:26 AM
Hi !
I am currently using STM32 Nucleo-L073RZ
The problem is that I've set it to 3 s but it executes after 8 s. In a different application it finished exactly after 3 s. I just don't understand what went wrong.
The code looks like that :
What main has :
uint8_t min = 1;
uint8_t max = 6;
uint8_t result_rand;
uint8_t timer;
uint8_t Display[4];
srand(time(NULL));
while (1)
{
if(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin) == GPIO_PIN_RESET || spin == 1)
{
result_rand = (rand() % (max - min + 1) + min);
Display[0] = int2inta(result_rand);
Display[1] = int2inta(result_rand-2);
Display[2] = int2inta(result_rand-4);
Display[3] = char2segments(' ');
tm1637_DisplayHandle(7, Display);
if(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin) == GPIO_PIN_RESET)
{
timer = 1;
}
}
else
{
if(timer == 1)
{
timer = 0;
spin = 1;
HAL_TIM_Base_Start_IT(&htim2);
}
}
What the interrupt has :
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
spin = 0;
HAL_TIM_Base_Stop_IT(&htim2);
}
Also there is a problem because when I don't click the button it is always "1" but when I click it then it is "0" in other boards it was the opposite. Weird.
2023-12-14 10:12 AM
Update I have used this GetTick function and it shows that the the code works in 3 sekunds 3000 ms.
But when I used the timer from my phone or in my desktop it says it worked for 6 sekunds.
I don't understand what is going on ...
uint8_t min = 1;
uint8_t max = 6;
uint8_t result_rand;
uint8_t timer;
uint8_t Display[4];
srand(time(NULL));
while (1)
{
if(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin) == GPIO_PIN_RESET || spin == 1)
{
result_rand = (rand() % (max - min + 1) + min);
Display[0] = int2inta(result_rand);
Display[1] = int2inta(result_rand-2);
Display[2] = int2inta(result_rand-4);
Display[3] = char2segments(' ');
tm1637_DisplayHandle(7, Display);
if(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin) == GPIO_PIN_RESET)
{
timer = 1;
}
}
else
{
if(timer == 1)
{
timer = 0;
spin = 1;
HAL_TIM_Base_Start_IT(&htim2);
tick = HAL_GetTick();
printf("1. %ld \n", tick);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
tick = HAL_GetTick();
printf("2. %ld \n", tick);
HAL_TIM_Base_Stop_IT(&htim2);
spin = 0;
tick = HAL_GetTick();
printf("3. %ld \n", tick);
}
I believe something is wrong I don't know what. It shows exactly 3 sekunds :
Did I made a mistake somewhere ????
Also edit in the picture it shows Counter = 1000 but It was the old picture it should be 3000 sorry. But I still don't get it what is going on.
2023-12-14 11:56 AM
just what i see , is not good:
1. set hse clock to bypass ;
2. set auto reload preload: enable
2023-12-14 12:04 PM
>>Did I made a mistake somewhere ????
Hard to tell, you only show selective pieces of code, which not be the ones at issue.
Suggest using a scope, measuring a GPIO
Check the MCU is actually running at 32 MHz, print out the SYSCLK, AHB, APB1, APB2 clocks via a printf()
Output the MCU clock via MCO PA8 pin, and measure that.
2023-12-14 12:17 PM
Oh it worked.
Can I ask 3 things ?
1. Why set clock into bypass and not into crystal resonator ? Many tutorials says to use crystal resonator and I don't know why bypass, because in both I have 32MHz or it changes something ? I am a bit confused. Because in tutorials it says to use crystal resonator. When I set the timer to 1 sec or 5 sec it worked for crystal resonator but in this program 3 s didn't work I don't know why.
2. Why do I need auto reload preload enabled ? Why in this example because usually in tutorials it says not to use it as well.
3. Was systick wrong ? I mean HAL_GetTick showed that it worked for 3 s when in reality it worked 6 s. Why gettick showed 3 s ?
2023-12-14 01:18 PM - edited 2023-12-14 01:23 PM
1. on your board the clk comes from st-link, not a crystal. so set to this. the input might (i dont know) be different, in switching level etc. , when set to crystal.
2. if you want a constant frequency and write it once to arr, you want it reload every cycle with this value. What its doing without this - i am not sure, i have to read the manual..as you. :)
thats why i wrote: is not good. its like : you start driving your car in 3rd gear, but you should use 1st gear - so maybe, it works, or has new side effect. :)
3. dont know - seems the clock was wrong; if you want know for sure, do like @Tesla DeLorean wrote: check real clk by using mco and a scope; and try what happens with arr preload ena or disabled .
2023-12-14 01:36 PM
> "1. on your board the clk comes from st-link, not a crystal. so set to this. the input might (i dont know) be different, in switching level etc. , when set to crystal."
What ? CLK comes from st-link ? So the BYPASS takes the oscilation from st link and the Crystal from the extern oscilator which is resonator ?
But okey, I read that the resonator is
So I thought that it would work. And it worked for some cases and for other it didn't I don't understand why. Is there any reason ? The settings seems okey, for some application when I use crystal it works normally I set for 1 s and it works for 1 sec. But here it didn't.
>
"2. if you want a constant frequency and write it once to arr, you want it reload every cycle with this value. What its doing without this - i am not sure, i have to read the manual..as you. :)
thats why i wrote: is not good. its like : you start driving your car in 3rd gear, but you should use 1st gear - so maybe, it works, or has new side effect. :)"
I have used the timer without the preload and it also worked.
I read that preload is needed when I change the counter value in real time. But I use only 1 counter value and I don't change it. So I guess it is not needed ??? Or maybe I am wrong sorry. I don't know what else a value he could load.
>"3. dont know - seems the clock was wrong; if you want know for sure, do like @Tesla DeLorean wrote: check real clk by using mco and a scope; and try what happens with arr preload ena or disabled ."
I don't know how to check these pins.
How to read these pins or so.
>@Tesla DeLorean Hard to tell, you only show selective pieces of code, which not be the ones at issue.
If there is any need to show more I can show more. Although the main program and the ISR is fully posted so here is the timer :
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 = 32000-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 3000;
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();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
}
The code seems ok, but for some application the timer works ok and for some it doesn't, when I use the crystal and not the bypass I don't get it.
2023-12-14 02:00 PM - edited 2023-12-14 03:03 PM
1. there is no hse crystal...see circuit of board. (btw which board you have ? i looked at -C04 )
+ 32kHz is lse crystal, should be there.
2. right - it should not be needed. just i never test this - so i wrote... : read rm or test it, to be sure.
- so : just buffered or not.
3. set mco output "on" . then in clock tree choose, what there should be : master clk or whatever you like to see and is possible (on this chip).
this you "see" on a scope then, on mco pin. thats all.
i do this on a new board always - just to be sure, it works as i want. then disable and use pin for something else ...or let it run , as you like.
from H563: (more complex)