cancel
Showing results for 
Search instead for 
Did you mean: 

Timer for 3 s interrupts after 8 s

Xenon02
Senior

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. 

Xenon02_0-1702571070276.pngXenon02_1-1702571087813.png

Xenon02_2-1702571096793.png


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. 

7 REPLIES 7
Xenon02
Senior

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 : 

Xenon02_0-1702577531691.png

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. 

AScha.3
Chief III

just what i see , is not good:

1. set hse clock to bypass ; 

2. set auto reload preload: enable 

 

If you feel a post has answered your question, please click "Accept as Solution".

>>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.

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

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 ? 




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.

AScha3_0-1702588972139.png

 

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 .

If you feel a post has answered your question, please click "Accept as Solution".
Xenon02
Senior

> "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 

  • 32.768 kHz crystal oscillator

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. 

1. there is no hse crystal...see circuit of board. (btw which board you have ? i looked at -C04 )

AScha3_0-1702591190849.png

+ 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.

AScha3_0-1702594574594.png

- 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) 

AScha3_1-1702591393449.png

 

If you feel a post has answered your question, please click "Accept as Solution".