SCM accurate millisecond delay function

There are typically two common methods for implementing a delay in microcontroller programming: hardware-based and software-based. Hardware delays utilize timers or counters, which help improve CPU efficiency and allow for precise timing. On the other hand, software delays are implemented using loops, making them simpler but less accurate depending on the system clock speed. In this article, we will focus on software delays and how to achieve accurate millisecond delays in microcontrollers. **Understanding the Clock Cycle of Microcontrollers** In electronics, a pulse signal is a periodic signal that occurs at specific voltage levels and time intervals. The time between two consecutive pulses is known as the cycle, while the number of pulses per second is referred to as frequency. Frequency is measured in Hertz (Hz), and it indicates how often a periodic signal repeats within a given time frame. The system clock in a computer is a typical example of a pulse signal generator, offering stable and accurate frequency. **Key Timing Concepts in Microcontrollers** - **Instruction Cycle**: This refers to the time it takes for the CPU to execute a single instruction. It is measured in machine cycles, and different instructions may require varying numbers of machine cycles. - **Clock Cycle**: Also called the oscillation period, it is the fundamental time unit in a microcontroller. One clock cycle equals the reciprocal of the crystal frequency. - **Machine Cycle**: This represents the basic operation cycle of the microcontroller. It consists of six state cycles, or 12 clock cycles. - **State Cycle**: A sub-cycle of the machine cycle, consisting of two clock cycles. For instance, a single-byte instruction in an 8051 microcontroller usually takes one machine cycle (12 clock cycles) to execute. With a typical crystal frequency of 12 MHz, each machine cycle lasts 1 μs, allowing for simple timing calculations. **Accurate Millisecond Delay Function in Microcontrollers** When precise timing is required, it's crucial to calculate the exact delay time of a function. Two common implementations of a delay function in C are: ```c void delay(uint x) // Delay x milliseconds { uint y, z; for(z = x; z > 0; z--) for(y = 110; y > 0; y--); } void delay_ms(uint n) // Delay n milliseconds { uchar i; while(n--) { for(i = 0; i < 120; i++); } } ``` Both functions use nested loops to create delays, with different constants (110 vs. 120) affecting the duration. To evaluate their accuracy, a test program was executed and disassembled using a C51 decompiler. The results showed that the first function (with 110) had a total of 906 machine cycles, while the second (with 120) had 986. At a 12 MHz crystal, this translates to approximately 0.906 ms and 0.986 ms, respectively. When using a 11.0592 MHz crystal, the first function produced a more accurate delay of about 983 μs, while the second resulted in 1069.8 μs. This shows that the choice of loop constant significantly affects precision, especially when longer delays are needed. Therefore, careful selection of delay constants is essential for achieving accurate timing in embedded applications.

Harness Component

Blade Fuse is a kind of current fuse, when the circuit current exceeds the fuse rated current of 2 times in a few seconds when the fuse will play the role of circuit protection. Widely used in equipment circuit protection standard for foreign and domestic automobiles and trucks. Readily identifiable and easily replaced, this fuse can be specified for a variety of low voltage electronic Applications.

Harness Component

Dongguan YAC Electric Co,. LTD. , https://www.yacentercn.com