A method for calibrating the frequency of an MCU crystal oscillator by a simulation serial port receiving program

By calibrating the MCU crystal oscillator frequency through a simulated serial port receiving program, the problem of serial communication failure caused by the internal RC crystal oscillator frequency error of the MCU was solved, and the accurate calibration of the serial communication baud rate was achieved.

CN115437991BActive Publication Date: 2025-11-18BEIJING FUAOXING ELECTRONIC TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211135639.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-09-19
Publication Date
2025-11-18
Estimated Expiration
2042-09-19

AI Technical Summary

Technical Problem

The frequency error of the RC crystal oscillator inside the MCU causes the baud rate error of the serial communication to be too large. When it exceeds 3 to 4%, the serial communication will fail.

Method used

The MCU crystal oscillator frequency is calibrated by simulating a serial port receiving program. The crystal oscillator frequency error is calculated by using a specific serial communication frame header design and interrupt program. The crystal oscillator frequency is then calibrated by adjusting the crystal oscillator frequency register or correcting the delay function.

Benefits of technology

After calibrating the MCU crystal oscillator frequency, ensure the accuracy of the serial communication baud rate, reduce the error to within 3%, and guarantee the accuracy of data transmission.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115437991B_ABST
    Figure CN115437991B_ABST
Patent Text Reader

Abstract

The application relates to a method for calibrating the frequency of an MCU crystal oscillator through an analog serial port receiving program, which solves the technical problem of serial port communication failure caused by errors in MCU communication, and is characterized in that a specific serial port communication baud rate is set in the MCU program; the MCU analog serial port receiving program can receive non-0 bits; after a delay of half a bit, the data of bits 0-bit 7 are read in sequence, after bit 7 is read, the program runs to the moment when the serial port data is 0, a fixed delay time t is added, the total number n of checks is increased by 1 after each check, and after the high level of the stop bit is read, the counting is stopped, a total number n of cyclic reading is obtained, and the error of the frequency of the MCU crystal oscillator is calculated according to the size of the n value; after the error of the frequency of the MCU crystal oscillator is calculated, the expected frequency is achieved through the method of modifying the hardware value and modifying the serial port data of the analog serial port program. The application can be widely applied to MCU and host computer communication.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data communication, and in particular to a method for calibrating the crystal oscillator frequency of an MCU by using a simulated serial port receiving program. Background Technology

[0002] When a microcontroller unit (MCU) communicates with a host computer via serial port, if there is an error in the frequency of the RC crystal oscillator inside the MCU, it will cause an error in the baud rate of serial port transmission and reception. If the error is large (more than 3-4%), it will cause serial communication failure. Summary of the Invention

[0003] To address the technical problem of serial communication failure caused by errors in existing MCU communication, this invention provides a method for calibrating the MCU crystal oscillator frequency by simulating a serial port receiving program.

[0004] This invention provides a method for calibrating the crystal oscillator frequency of an MCU by receiving a program via a simulated serial port, the steps of which include:

[0005] Step 1: Set the serial communication baud rate in the MCU program;

[0006] Step 2: Set the frame header byte of the serial port data sent from the host computer to the MCU to have consecutive 0s in the high bits and consecutive 1s in the low bits, so that the MCU's simulated serial port receiving program can receive non-0 bits.

[0007] Step 3: When the host computer sends the serial port frame header data, the start bit changes from high level to low level. The MCU enters the interrupt program. After confirming that it is the serial port start bit, it delays for half a bit and then reads the data from bit 0 to bit 7 in sequence. After reading bit 7, when the program reaches the moment when the serial port data is 0, the MCU program starts to check the high level of the stop bit in a loop. A fixed delay time t is added between each check. After each check, the total number of checks n is incremented by 1 until the high level of the stop bit is read. The counting stops, and a total number of loop reads n is obtained. The error of the MCU crystal oscillator frequency is calculated based on the value of n.

[0008] Step 4: After calculating the error of the MCU crystal oscillator frequency, for MCUs with internal registers to adjust the crystal oscillator frequency, correct the value of the crystal oscillator frequency register to make the MCU crystal oscillator frequency reach the expected frequency and calibrate the MCU crystal oscillator frequency; for MCUs without internal registers to adjust the crystal oscillator frequency, calibrate the communication baud rate of the analog serial port by correcting the delay function for reading 1 bit in the analog serial port receiving program.

[0009] Preferably, in step 4, when the MCU with an internal register for adjusting the crystal oscillator frequency also has a hardware serial port, the value of the crystal oscillator frequency register is corrected to obtain an accurate baud rate, and the hardware serial port is used to communicate with the host computer.

[0010] Preferably, in step 4, when the MCU with an internal register for adjusting the crystal oscillator frequency does not have a hardware serial port, a simulated serial port receiving program is used to receive serial port data from the host computer, or a simulated serial port sending program is used to send serial port data to the host computer.

[0011] Preferably, in step 1, the serial communication baud rate is a delay function delay(m) designed in the MCU program that is close to the time taken for 1 bit of serial communication, where m is an adjustable integer. When the MCU crystal oscillator frequency is accurate, the running time of delay(m) plus the time taken for the MCU to check the serial port level is exactly equal to the time taken for 1 bit of serial communication.

[0012] Preferably, the specific method for correcting the 1-bit delay function in the simulated serial port receiving program in step 4 is as follows: after calculating the error of the MCU crystal oscillator frequency, use the simulated serial port receiving and sending program to communicate with the host computer via serial port, correct the delay function delay(m), add a deviation value to the value of m, so that the actual time of MCU running delay(m) and checking the serial port level is within 3% of the 1-bit time error of serial port communication.

[0013] The beneficial effects of this invention are:

[0014] This invention simulates serial port reception from a host computer by interrupting the pin level change of the MCU. Under the condition of agreed serial communication baud rate, an algorithm for calibrating the MCU crystal oscillator frequency can be added to the MCU simulated serial port reception program through a specific communication frame header design. After the MCU crystal oscillator frequency is calibrated, the serial port baud rate of the MCU can be calibrated, ensuring the accuracy of serial communication data with the host computer. Attached Figure Description

[0015] Figure 1 This is a schematic diagram of the MCU crystal oscillator frequency calibration mode of the present invention;

[0016] Figure 2 This is a schematic diagram of the normal simulated serial port receiving mode of the present invention. Detailed Implementation

[0017] The present invention will be further described below with reference to the accompanying drawings and embodiments, so that those skilled in the art can easily implement the present invention.

[0018] Example:

[0019] The method for frequency calibration according to this invention is as follows:

[0020] Step 1: First, agree on a specific serial communication baud rate. In the MCU program, design a delay function delay(m) that is close to the time taken for 1 bit of serial communication, where m is an adjustable integer. Assuming the MCU crystal oscillator frequency is accurate, the running time of delay(m) plus the time taken for the MCU to check the serial port level is exactly equal to the time taken for 1 bit of serial communication.

[0021] Step 2: Set the header byte (first byte) of the serial port data sent from the host computer to the MCU to have consecutive 0s in the high bits and consecutive 1s in the low bits, such as 0X03, 0X06, 0X07, 0X0C, 0X0E, 0X0F, 0X13, 0X16, 0X17, 0X1C, 0X1E, 0X1F, etc. This ensures that the stop bits of the serial port data always transition from 0 to 1, with a relatively long low level before the stop bits. The MCU's simulated serial port receiving program can receive certain non-zero bits, which can be used as a judgment condition for the frequency calibration program, preventing the MCU from mistakenly identifying incorrect serial port data as data that can be calibrated for frequency calibration.

[0022] Step 3, as follows Figure 1 As shown, when the MCU frequency is not calibrated, the MCU serial port receiving pin is set to enable level change interrupt. The host computer sends a serial port data frame header format of 8 bits, with no parity bit. The time frame header data between the start bit and the end bit is 9 bits. Taking 0X1C as an example: the start bit jumps from high level (1) to low level (0), the MCU enters the interrupt program, first immediately checks that the level of the serial port receiving pin should be 0, confirming that it is the serial port start bit. Then, after a delay of about half a bit, the data from bit 0 to bit 7 is read in sequence. This will not read the accurate frame header data, but will read the non-0 bits in the middle of the frame header. After reading bit 7, when the program runs to the moment when the serial port data is 0, there is still about 1.5 bits of time before the stop bit of the serial port data. The time before the stop bit is related to the error of the MCU crystal oscillator frequency. If the MCU crystal oscillator frequency is too fast, the time before the stop bit after reading bit 7 will be longer. At this point, the MCU program begins to cyclically check the high level of the stop bit, adding a fixed delay time t between each check. After each check, the total number of checks n is incremented by 1, until the high level of the stop bit is read, at which point the counting stops, resulting in a total number of loop reads n. If the MCU crystal oscillator frequency is too fast, the value of n will be relatively large; if the MCU crystal oscillator frequency is too slow, the value of n will be relatively small. The error of the MCU crystal oscillator frequency can be calculated based on the value of n.

[0023] Step 4: After calculating the error of the MCU crystal oscillator frequency, for MCUs with internal registers to adjust the crystal oscillator frequency, the value of the crystal oscillator frequency register can be corrected to bring the MCU's crystal oscillator frequency to the expected frequency. For MCUs with a hardware serial port, the hardware serial port can be used directly to obtain the accurate baud rate for communication with the host computer. For MCUs without a hardware serial port, a simulated serial port receiving program can be used to receive serial port data from the host computer. Refer to the following for the specific process of simulating serial port reception. Figure 2 The host computer sends serial data in 8-bit format with no parity bit. The MCU crystal oscillator frequency is calibrated, and a level change interrupt is used to simulate the serial port. Alternatively, a simulated serial port sending program can be used to send serial data to the host computer.

[0024] Step 5: For MCUs without internal registers to adjust the crystal oscillator frequency, after calculating the error of the MCU crystal oscillator frequency, a simulated serial port receiving and sending program can be used to communicate with the host computer via serial port. The delay function delay(m) mentioned in step 1 needs to be corrected by adding a certain deviation value to the value of m, so that the actual time for the MCU to run delay(m) and check the serial port level is close to the time of 1 bit of serial communication. An error within 3% can ensure the accuracy of the MCU's receiving and sending of serial port data.

[0025] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the scope of the claims of the present invention should be within the protection scope of the present invention.

Claims

1. A method for calibrating the crystal oscillator frequency of an MCU using a simulated serial port receiving program, characterized in that, The steps include: Step 1: Set the serial communication baud rate in the MCU program; Step 2: Set the frame header byte of the serial port data sent from the host computer to the MCU to have consecutive 0s in the high bits and consecutive 1s in the low bits, so that the MCU's simulated serial port receiving program can receive non-0 bits. Step 3: When the host computer sends the serial port frame header data, the start bit changes from high level to low level. The MCU enters the interrupt program. After confirming that it is the serial port start bit, it delays for half a bit and then reads the data from bit 0 to bit 7 in sequence. After reading bit 7, when the program reaches the moment when the serial port data is 0, the MCU program starts to check the high level of the stop bit in a loop. A fixed delay time t is added between each check. After each check, the total number of checks n is incremented by 1 until the high level of the stop bit is read. The counting stops, and a total number of loop reads n is obtained. The error of the MCU crystal oscillator frequency is calculated based on the value of n. Step 4: After calculating the error of the MCU crystal oscillator frequency, for MCUs with internal registers to adjust the crystal oscillator frequency, correct the value of the crystal oscillator frequency register to make the MCU crystal oscillator frequency reach the expected frequency and calibrate the MCU crystal oscillator frequency; for MCUs without internal registers to adjust the crystal oscillator frequency, calibrate the communication baud rate of the analog serial port by correcting the delay function for reading 1 bit in the analog serial port receiving program.

2. The method for calibrating the MCU crystal oscillator frequency by receiving a simulated serial port program according to claim 1, characterized in that, In step 4, when the MCU with an internal register for adjusting the crystal oscillator frequency also has a hardware serial port, the value of the crystal oscillator frequency register is corrected to obtain an accurate baud rate, and the hardware serial port is used to communicate with the host computer.

3. The method for calibrating the MCU crystal oscillator frequency by receiving a simulated serial port program according to claim 1, characterized in that, In step 4, when the MCU with an internal register for adjusting the crystal oscillator frequency does not have a hardware serial port, a simulated serial port receiving program is used to receive serial port data from the host computer, or a simulated serial port sending program is used to send serial port data to the host computer.

4. The method for calibrating the MCU crystal oscillator frequency by receiving a simulated serial port program according to claim 1, characterized in that, In step 1, the serial communication baud rate is a delay function delay(m) designed in the MCU program that is close to the time taken for 1 bit of serial communication. Here, m is an adjustable integer. When the MCU crystal oscillator frequency is accurate, the running time of delay(m) plus the time taken for the MCU to check the serial port level is exactly equal to the time taken for 1 bit of serial communication.

5. The method for calibrating the MCU crystal oscillator frequency by simulating a serial port receiving program according to claim 4, characterized in that, The specific method for correcting the 1-bit delay function in the simulated serial port receiving program in step 4 is as follows: After calculating the error of the MCU crystal oscillator frequency, use the simulated serial port receiving and sending program to communicate with the host computer via serial port, and correct the delay function delay(m) by adding a deviation value to the value of m, so that the actual time of the MCU running delay(m) and checking the serial port level is within 3% of the 1-bit time error of the serial port communication.

Citation Information

Patent Citations

  • Method for improving precision of built-in real-time clock of electric energy meter MCU

    CN103499803A

  • Clock frequency calibration and frequency measurement method for UART serial port master-slave communication

    CN112099568A