How to generate a 1 microsecond (1 µs) timer interrupt on the STM32G0B1RCT6 using the internal clk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-05 01:24 AM
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 screenshot for your reference.
- Labels:
-
STM32G0 Series
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-05 01:38 AM
Please use this button to properly post source code - not as images:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-05 01:59 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-06 12:57 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-06 01:52 AM
Running MHz rate interrupt is a bad idea.
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-09 08:54 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-10 03:02 AM
> Is it possible to generate a 1µs timer interrupt?
By "It's a bad idea" I mean "practically, it's not possible".
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-10 03:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-10 05:55 AM
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.
Up vote any posts that you find helpful, it shows what's working..