cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate a 1 microsecond (1 µs) timer interrupt on the STM32G0B1RCT6 using the internal clk

Berlin-raj123
Associate II

Hi, actually, I want to generate a 1 microsecond (1 µs) timer interrupt on the STM32G0B1RCT6  microcontroller using their internal clocks.

For the STM32G0B1RCT6 controller running at 16MHz APB timer clock, I set the prescaler value to 0 [16MHz clock frequency / (0+1) = 16MHz counter clock -> 1/16MHz = 62.5ns counter period]  and the counter period to 15 [ARR is set to 15: 62.5ns*(15+1)=62.5ns*16=1us]. This configuration is intended to represent a 62.5ns  interval once the counter starts counting. I then set the ARR (Auto-Reload Register) value to 15+1=16. However, on the oscilloscope, the interrupt is generated every 4.24 microseconds. Could you explain why this is happening and tell me the correct values for the prescaler and ARR?

 

 

i'm not using only hal Drivers,i will attached the screenshotScreenshot 2024-06-05 135203.pngScreenshot 2024-06-05 135133.png for your reference.

8 REPLIES 8
Andrew Neil
Evangelist III

Please use this button to properly post source code - not as images:

AndrewNeil_0-1717576651989.png

 

Muhammed Güler
Senior III

 

 

Each code snippet consumes one or more clocks of time.
If you do not directly manipulate the addresses, the HAL library will spend extra time with its internal checks etc.
Below are a few code snippets from the Hal library.
When you examine these codes, you can see that it is not strange that the code you wrote takes 68 clocks.

#define IS_GPIO_PIN(__PIN__)        (((((uint32_t)__PIN__) & GPIO_PIN_MASK) != 0x00U) &&\
                                     ((((uint32_t)__PIN__) & ~GPIO_PIN_MASK) == 0x00U))

/**
  * @brief  Toggle the specified GPIO pin.
  * @PAram  GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
  * @PAram  GPIO_Pin specifies the pin to be toggled.
  * @retval None
  */
void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
  uint32_t odr;

  /* Check the parameters */
  assert_param(IS_GPIO_PIN(GPIO_Pin));

  /* get current Output Data Register value */
  odr = GPIOx->ODR;

  /* Set selected pins that were at low level, and reset ones that were high */
  GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
}

 

Berlin-raj123
Associate II

If I do not directly manipulate the addresses, the HAL library will spend extra time. Okay, now I understand. Can I configure TIM2 to generate the code, and manually configure only the pin I need to toggle every second? Can I achieve a 1 µs delay?

Running MHz rate interrupt is a bad idea.

JW

Berlin-raj123
Associate II

How can I generate a 1µs interrupt in the STM32G0B1RCT6 microcontroller? Is it possible to generate a 1µs timer interrupt? If so, how can I achieve it? Please provide a detailed explanation

> Is it possible to generate a 1µs timer interrupt?

By "It's a bad idea" I mean "practically, it's not possible".

JW

Generating it is one thing - being able to usefully service it is another ... ?

🤔 😋

 

@Berlin-raj123 This sounds like it may be an X-Y Question.

So perhaps it would be better to describe what you're actually trying to achieve here - what is your goal - there may be better ways ...

http://www.catb.org/~esr/faqs/smart-questions.html#goal

 

Why do you need it to INTERRUPT?

Can't you just clock the TIM at 1 MHz, and have a 16-bit count that advances at 1us (or faster)

For a delay, observe the count, measure the delta between entry and currently.

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