The role of crystal oscillator, the advantages and disadvantages of high-speed crystal oscillator

foreword

(1) We all know that the crystal oscillator is the heart of an MCU. Because it has been explained with this abstract concept for a long time, many people do not know the actual function of this heart. Therefore, I will introduce the actual function of the crystal oscillator for the MCU in detail here.
(2) Next, I will discuss from the perspectives of MCU processing power, power consumption, communication speed, and timer accuracy.

Effect of crystal oscillator on MCU processing speed

What is machine cycle and instruction cycle

(1) Before explaining the processing capability of the crystal oscillator for the MCU, you have to understand two terms-machine cycle and instruction cycle.
(2) So what are machine cycles and instruction cycles? Then we take the software delay of stc89c52 as an example to explain.
(3) First, we need to know what a machine cycle is. The machine period is the reciprocal of the crystal oscillator. For example, the common crystal oscillator of stc89c52 is 12MHZ, so its crystal oscillator period at this time is

1 12 ∗ 1 0 6 S \frac{1}{12*10^{6}} S 121061S

(4) Some instruction cycles of stc89c52 can choose 6 machine cycles or 12 machine cycles . Generally speaking, the default is 12 machine cycles . Therefore, we can calculate the instruction cycle of stc89c52 as

1 12 ∗ 1 0 6 ∗ 12 = 1 1 0 6 S = 1 u s \frac{1}{12*10^{6}} * 12 = \frac{1}{10^{6}} S = 1us 12106112=1061S=1us

How to calculate the execution time of each assembly in terms of instruction cycles

(1) What is the instruction cycle? We can now look at the assembly instructions. In assembly, some instructions only take one instruction cycle to execute, some instructions take two instruction cycles, and some take three. Then based on this, we can know the execution time of each instruction.
(2) Now I open the tool of Hongjing Company and check the assembly execution time of STC89C52. Note: The unit here is the machine cycle, not the instruction cycle .

insert image description here

(3) We generated a program with a delay of 1us through the tool and software of Hongjing Company, and found that it is a NOP instruction. This instruction has no effect, it is a delay of one instruction cycle. So a delay of 1us is a NOP.

insert image description here

(4) If we need to delay 10us at this time, check the result.
<1> NOP executes one instruction cycle, RET two instruction cycles. The final calculated result should be 8 instruction cycles, which is 8us.
<2> At this time, someone will ask, why is it 8us? Shouldn't it be 10us? Students who have studied the delay part of 51 single-chip microcomputer must know that the software delay of 51 single-chip microcomputer is not accurate, so after each instruction may be executed, it will be slightly greater than 1us, and the final error will accumulate, so only 8 instructions are needed cycle, it will delay approximately 10us.

DELAY10US:			;@12.000MHz
	NOP     
	NOP   2
	NOP
	NOP   2
	NOP
	NOP   2
	RET   2

(5) Now let's perform another 50us delay function to see what is the code generated by Hongjing?
<1> Check the STC-Y1 instruction set, you can know how many instruction cycles they occupy. (The number marked at the back)
<2>Through calculation, it is found that in fact, only 47 instruction cycles are used, which is approximately 50us.

DELAY50US:			;@12.000MHz  
	PUSH 30H                     2          
	MOV 30H,#20                  1
NEXT:                            
	DJNZ 30H,NEXT                2 * 20 =40
	POP 30H                      2
	RET                          2

(6)
<1> Now we have a simple understanding of the crystal oscillator and MCU execution speed. Now we try to replace the 12MHZ crystal oscillator with the more commonly used 11.0592, and generate a 10us code at this time.
<2> We will find that the code to execute is reduced! What does this mean,In the same period of time, the higher the frequency of the crystal oscillator, the more instructions it can execute.

DELAY10US:			;@11.0592MHz
	NOP
	NOP
	NOP
	NOP
	NOP
	RET

(7) Since the high-frequency crystal oscillator can increase the execution speed of MCU instructions, can we increase the frequency of the crystal oscillator without thinking? The answer is obviously no. An MCU is specified with the highest frequency. If it is overclocked, it will cause the MCU to run unstable.
(8) Since the high-frequency crystal oscillator can improve the execution speed of the MCU, I will increase it every time. The maximum frequency accepted by the MCU. Unfortunately, no.The faster the MCU system clock, the greater the power consumption. For some products with low power consumption, an excessively high system clock is not required.

in conclusion

Advantages: High-frequency crystal oscillator can improve the processing speed of MCU for instructions, but MCU has a maximum system clock limit, and overclocking can easily cause program failures.
Disadvantages: Too high system clock will lead to high power consumption.

Effect of crystal oscillator on serial communication

(1) From the above example, we can see that 12MHZ can not only improve the code execution speed, but also the final time is relatively accurate. Why do you need a 11.0592MHZ crystal oscillator?
(2) This is related to the serial port baud rate. We all know that the commonly used serial port baud rates are 115200, 9600 and so on.
(3) The formula for calculating the baud rate of the serial port is as follows. Generally, we use method 1 for the baud rate generator .

(4) Now that we know the formula for generating the baud rate of the serial port, and start to calculate, we will find that the baud rate generated by 11.0592MHZ has no error. And if it is a 12MHZ crystal oscillator, it is easy to produce errors, and as time accumulates, the errors will become larger and larger.
(5)
<1>Because I am afraid that some people will not be able to understand the following picture, so I directly take 11.0592 to generate a baud rate of 9600 as an example. Note that I use method 1, SMOD is 0.
<2> The final calculation of TH1 is 253, for the hexadecimal 0xFD.
<3> According to the figure below, we can clearly find that the baud rate generated by 11.0592 is very effective, so we generally use 11.0592 as the crystal oscillator of the 51 single-chip microcomputer.

2 0 32 ∗ 11.0592 ∗ 1 0 6 12 ∗ ( 256 − T H 1 ) ) = 9600 \frac{2^{0}}{32} * \frac{11.0592*10^{6}}{12*(256-TH1))} = 9600 322012(256T H 1 ))11.0592106=9600

insert image description here

Effect of crystal oscillator on timer accuracy

(1) As mentioned above, the 11.0592MHZ crystal oscillator can generate a very accurate baud rate. So besides increasing the execution speed of the MCU, can 12MHZ produce anything accurately?
(2) Of course, if the crystal oscillator of the 51 single-chip microcomputer is 12MHZ, the timer of the general 51 single-chip microcomputer divides the frequency of the crystal oscillator by 12 by default, so the value of the timing register is 1us every time. And if the crystal oscillator is 11.0592, then the time for each jump of the timer register is 12/11.0592us. Obviously, the time generated by the timer under 12MHZ is relatively accurate.
(3)
<1>Because I am afraid that your foundation is too poor, I will give an example now. Suppose, my 51 single-chip microcomputer crystal oscillator gives 12MHZ, and it is necessary to perform 100us timing.
<2> We will find that TH0 is 0xFB and TL0 is 0x50. Converted to decimal, it is 64336, and the timer of STC89C52 counts up. Then the actual count value is 65536-65535=100. As mentioned earlier, the timer value under the 12MHZ crystal oscillator jumps every 1us, which is just right.

insert image description here

(4)
<1> is also 100us, let's try the value at 11.0592MHZ.
<2> We will find that TH0 is 0xFF and TL0 is 0xA4. Converted to decimal, it is 65444, and the actual count is 92.
<3>The timer value under 11.0592MHZ jumps every time 12/11.0592us, the final calculation formula is as follows:
12 11.0592 ∗ 92 ≈ 99.826 us \frac{12}{11.0592} * 92 \approx 99.826 us11.0592129299.826 u s

insert image description here

Summarize

Therefore, we can see that the selection of crystal oscillator is not blindly pursuing high frequency, and each crystal oscillator has its advantages. In the selection process, we should choose according to our needs.

Guess you like

Origin blog.csdn.net/qq_63922192/article/details/131269018