Skip to main content
Visitor II
June 5, 2024
Question

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

  • June 5, 2024
  • 6 replies
  • 4378 views

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.

    This topic has been closed for replies.

    6 replies

    Super User
    June 5, 2024

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

    AndrewNeil_0-1717576651989.png

     

    Graduate II
    June 5, 2024

     

     

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

     

    Visitor II
    June 6, 2024

    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?

    Super User
    June 6, 2024

    Running MHz rate interrupt is a bad idea.

    JW

    Visitor II
    June 10, 2024

    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

    Graduate II
    June 10, 2024

    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.

    Super User
    June 10, 2024

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

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

    JW

    Super User
    June 10, 2024

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

    :thinking_face: :face_savoring_food:

     

    @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