User Tools

Site Tools


dht11_stm32

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
dht11_stm32 [2024/07/29 15:41] – created osodht11_stm32 [2024/10/17 21:42] (current) – external edit 127.0.0.1
Line 87: Line 87:
  
 Keep in mind that this approach assumes the CPU is running at exactly 72 MHz. If your MCU operates at a different frequency, you will need to adjust the inner loop count accordingly. Keep in mind that this approach assumes the CPU is running at exactly 72 MHz. If your MCU operates at a different frequency, you will need to adjust the inner loop count accordingly.
 +
 +
 +===== _delay_uSec =====
 +
 +
 +Let's break down the `asm` line in the `_delay_uSec` function:
 +
 +<code c>
 +__asm__ __volatile__("nop\n\t":::"memory");
 +</code>
 +
 +Here's what each part means:
 +
 +1. **`__asm__`**: This is a GCC (GNU Compiler Collection) keyword used to embed assembly code within C/C++ code.
 +
 +2. **`__volatile__`**: This keyword tells the compiler not to optimize away this piece of assembly code. In other words, it instructs the compiler that this assembly code must be executed exactly as written and cannot be removed or reordered. This is particularly important for delay loops because the compiler might otherwise try to optimize them out if it thinks they do nothing useful.
 +
 +3. **`"nop\n\t"`**: This is the actual assembly instruction. Here's a breakdown:
 +   - **`nop`**: This is the "no operation" instruction. It effectively tells the CPU to do nothing for one cycle.
 +   - **`\n\t`**: These are escape sequences. `\n` is a newline character, and `\t` is a tab character. These are used for formatting the assembly code when it's outputted by the compiler. They help to make the generated assembly code more readable but don't affect the execution.
 +
 +4. **`:::"memory"`**: This part of the `asm` statement is known as the clobber list. It tells the compiler that the assembly code will use (or modify) certain resources. 
 +   - The first `:::` part separates the assembly instructions from the list of input, output, and clobbered registers.
 +   - **`"memory"`**: This indicates that the assembly code may read or write to memory. It's a way of telling the compiler that it should not make any assumptions about the state of memory across this assembly instruction. This can prevent certain optimizations that could otherwise lead to incorrect behavior.
 +
 +In summary, the line `__asm__ __volatile__("nop\n\t":::"memory");` inserts a "no operation" instruction into the compiled code, ensures the instruction is not optimized out, and informs the compiler about potential memory access, thus preventing unwanted optimizations around this code. This is useful for creating precise timing delays in embedded systems programming.
 +
 +Here's the complete `_delay_uSec` function with this assembly code for context:
 +
 +<code c>
 +void _delay_uSec(unsigned int i) {
 +    for (; i > 0; i--) { // 'i' = the uSeconds needed to delay
 +        for (int j = 0; j < 72; ++j) { // the 72 being the frequency in MHz the MCU is running
 +            __asm__ __volatile__("nop\n\t":::"memory");
 +        }
 +    }
 +}
 +</code>
 +
 +This function creates a delay by looping a number of times, with each loop containing 72 `nop` instructions to approximate a 1 microsecond delay, assuming the MCU is running at 72 MHz.
dht11_stm32.1722267703.txt.gz · Last modified: 2024/10/17 21:42 (external edit)