There are usually two ways to implement the delay: one is the hardware delay, the timer/counter is used, this method can improve the working efficiency of the CPU, and can also achieve the precise delay; the other is the software delay. This method is mainly carried out using a loop body. Today we mainly introduce the software delay and the precise millisecond delay function of the microcontroller.
Introduction to the cycle of the microcontrollerIn electronic technology, a pulse signal is a pulse signal that is continuously emitted at a certain voltage interval and at a certain time interval. The time interval between pulse signals is called a cycle; the number of pulses generated in a unit time (for example, 1 second) is called a frequency. The frequency is a measure of the number of pulses that occur in a unit of time for periodic cyclic signals (including pulse signals); the standard unit of measure for frequency is Hz (Hz). The system clock in a computer is a typical pulse signal generator with a fairly accurate and stable frequency.
Instruction cycle: The time required for the CPU to execute an instruction is called the instruction cycle. It is in machine cycle units. The instructions are different and the required machine cycles are different. For some simple single-byte instructions, in the instruction fetch cycle, after the instruction is fetched into the instruction register, the instruction is decoded and executed, and no other machine cycles are needed. For some more complex instructions, such as branch instructions, multiplication instructions, two or more machine cycles are required. An instruction that typically contains one machine cycle is called a one-cycle instruction, and an instruction that contains two machine cycles is called a two-cycle instruction.
Clock cycle: Also known as the oscillation period, one clock cycle = the reciprocal of the crystal. For the microcontroller clock cycle, the clock cycle is the basic time unit of the microcontroller, and two oscillation cycles (clock cycles) form a state cycle.
Machine cycle: The basic operation cycle of the microcontroller. In one operation cycle, the MCU completes a basic operation, such as fetching instructions, memory read/write, and so on.
Machine cycle = 6 state cycles = 12 clock cycles.
The instructions of 51 MCUs are single-byte, double-byte and three-byte. Their instruction cycles are different. A single-cycle instruction contains one machine cycle, that is, 12 clock cycles, so a single-cycle instruction is executed. The time is 12* (1/crystal frequency) = x μs. The crystal oscillator of the commonly used single chip microcomputer is 11.0592MHz, 12MHz, 24MHz. Among them, the 11.0592MHz crystal oscillator is more likely to produce various standard baud rates. The latter two machine cycles are 1 μs and 2 μs, respectively, for precise delay.
SCM accurate millisecond delay functionFor applications that require precise delays, the exact delay time of the delay function needs to be known accurately. There are two different versions of the MCU delay function written in C language (all said to be delayed by n milliseconds), as follows:
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++);
}
}
The implementation of the above two functions is the same, except that the time constants are different, one is 110 and the other is 120. Recently, because of the high precision of time, the time taken for the operation of these two functions is specifically verified. The main program is:
Void main(void)
{
Delay(1);
}
After disassembly (recommended C51 smart decompiler, but manually add ORG address and END terminator):
Label instruction address machine code machine cycle
ORG 0000H
Q0000: LJMP Q0021; 0000 02 00 21
;========================================================== ==========================
ORG 0003H
Q0003: SETB C ;0003 D3 1
MOV A, R7 ;0004 EF 1
SUBB A, #00H ;0005 94 00 1
MOV A, R6; 0007 EE 1
SUBB A, #00H ;0008 94 00 1
JC Q0020 ;000A 40 14 2
MOV R5, #6EH ;000C 7D 6E 1
MOV R4, #00H ;000E 7C 00 1
Q0010: MOV A, R5; 0010 ED 1
DEC R5 ;0011 1D 1
JNZ Q0015 ;0012 70 01 2
DEC R4 ;0014 1C 1
Q0015: MOV A, R5; 0015 ED 1
ORL A, R4; 0016 4C 1
JNZ Q0010 ;0017 70 F7 2
MOV A, R7; 0019 EF 1
DEC R7 ;001A 1F 1
JNZ Q0003 ;001B 70 E6 2
DEC R6 ;001D 1E 1
SJMP Q0003 ;001E 80 E3 2
Q0020: RET ;0020 22 2
;====================RESET 00-7F AND SET SP=07H===================== ========
ORG 0021H
Q0021: MOV R0, #7FH ;0021 78 7F
CLR A ;0023 E4
Q0024: MOV @R0,A ;0024 F6
DJNZ R0, Q0024; 0025 D8 FD
MOV SP, #07H ;0027 75 81 07
LJMP Q002D ;002A 02 00 2D
;==========================MAIN PROGRAM============================ ===============
ORG 002DH
Q002D: MOV R7, #01H ;002D 7F 01 1
MOV R6, #00H ;002F 7E 00 1
LJMP Q0003 ;0031 02 00 03 2
;========================================================== ==========================
END
Single-step execution can track the running of the program, and the final result is that the total number of execution machine cycles is:
The first function: 4+9+8*110+4+9=906
The second function: 4+9+8*120+4+9=986
If the crystal oscillator of the microcontroller is 12M, the time of one machine cycle is 12/(12*10e6)=1us
It can be seen that the second program is closer to 1ms (0.986us), the first one is 0.906us
When the crystal oscillator is 11.05926M, the exact delay time of the first delay function is 983us, and the second is 1069.8us. It can be seen that the delay time of the first function is more accurate.
And the longer the delay time, the greater the error.
ZGAR AZ Bingo Vape
ZGAR electronic cigarette uses high-tech R&D, food grade disposable pod device and high-quality raw material. All package designs are Original IP. Our designer team is from Hong Kong. We have very high requirements for product quality, flavors taste and packaging design. The E-liquid is imported, materials are food grade, and assembly plant is medical-grade dust-free workshops.
Our products include disposable e-cigarettes, rechargeable e-cigarettes, rechargreable disposable vape pen, and various of flavors of cigarette cartridges. From 600puffs to 5000puffs, ZGAR bar Disposable offer high-tech R&D, E-cigarette improves battery capacity, We offer various of flavors and support customization. And printing designs can be customized. We have our own professional team and competitive quotations for any OEM or ODM works.
We supply OEM rechargeable disposable vape pen,OEM disposable electronic cigarette,ODM disposable vape pen,ODM disposable electronic cigarette,OEM/ODM vape pen e-cigarette,OEM/ODM atomizer device.
ZGAR AZ Bingo Vape Disposable Vapes,ZGAR AZ Bingo Vape disposable electronic cigarette, ZGAR bingo vape pen atomizer ,ZGAR AZ Bingo Vape E-cig,AZ Bingo Vape disposable electronic cigarette,disposable vape,zgar bingo box
ZGAR INTERNATIONAL(HK)CO., LIMITED , https://www.zgarvapepen.com