Fading Coder

An Old Coder’s Final Dance

You are here: Home > Tech > Content

Methods for Implementing Delay Functions in STM32 Microcontrollers

Tech 3

Delay functionalities in microcontroller programing are essential for managing timing. In the context of STM32 microcontrollers, this document explains the implementation of four diverse delay methods, focusing on their mechanics and specific use cases.

1. Simple Loop-Based Delay

This simplest delay technique uses iterative loops to achieve time delays. While efficient and accessible, accuracy is often compromised due to compiler optimizations. Suitable applications include general scenarios where high precision is not critical.

Example implementation:

void delay_microseconds(uint32_t microseconds) {
    volatile uint32_t count;

    for (count = 0; count < microseconds; count++) {
        volatile uint16_t temp = 12;
        while (temp > 0) {
            temp--;
        }
    }
}

void delay_milliseconds(uint16_t milliseconds) {
    for (uint32_t i = 0; i < milliseconds; i++) {
        delay_microseconds(1000);
    }
}

2. Timer-Based Delay via Interrupts

Utilizing timer peripherals of STM32 microcontrollers allows precise delay functions via interrupt mechanisms. While effective, this method engages the system's interrupt infrastructure, making it unsuitable for scenarios where interrupt reliability is paramount.

Initialization Example:

void init_systick(uint32_t frequency) {
    SysTick_Config(SystemCoreClock / frequency);
}

void SysTick_Handler(void) {
    TimingDelay_Decrement();
}

void TimingDelay_Decrement(void) {
    if (TimingDelay > 0) {
        TimingDelay--;
    }
}

void delay(uint32_t duration) {
    TimingDelay = duration;
    while (TimingDelay > 0);
}

3. Timer Query-Based Delay

This approach uses internal timers as decremanting counters without enabling their respective interrupts. It provides precision while avoiding interrupt interaction.

Implementation Example:

void delay_timer(uint32_t microseconds) {
    uint32_t start_ticks = SysTick->VAL;
    uint32_t ticking = SysTick->LOAD / (1000000 / microseconds);

    while ((SysTick->VAL - start_ticks) < ticking);
}

4. Assembly Instruction-Based Delay

Direct assembly instructions can execute precise delays with out relying heavily on peripheral resources. This is optimal for resource-constricted setups and ensures accurate timing since no compiler optimizations apply.

Assembly Example:

void delay_assembly(unsigned long cycles) {
    __asm("subs r0, #1
 bne delay_assembly
 bx lr");
}

void delay_microseconds(unsigned long microseconds) {
    delay_assembly(microseconds * (SystemCoreClock / 1000000));
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.