Clock correction method based on asynchronous communication and electric energy meter
By correcting the deviation of the internal RC circuit of the energy meter through asynchronous communication, the problem of low RC accuracy after crystal oscillator failure is solved, ensuring the normal operation of the energy meter and achieving a highly efficient correction effect.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- YANTAI DONGFANG WISDOM ELECTRIC
- Filing Date
- 2023-08-16
- Publication Date
- 2026-06-12
AI Technical Summary
In existing technologies, when the crystal oscillator fails, the electricity meter cannot effectively correct the accuracy of the internal high-frequency RC circuit, resulting in distorted data and communication abnormalities, thus failing to guarantee normal operation.
An asynchronous communication-based method is adopted, which connects the independent clock source of the external module to the timer of the MCU to calculate the deviation value of the internal RC, and adjusts the frequency of the RC by adjusting the ADJ_REG register to achieve accurate correction.
It achieves precise correction of the internal RC after the external crystal oscillator stops oscillating, ensuring the normal operation of the electricity meter with low resource consumption and no additional energy consumption.
Smart Images

Figure CN117008447B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a method for calibrating the internal clock of a smart energy meter, and also to an energy meter. Background Technology
[0002] The clock system is a crucial hardware component of a smart meter, providing a stable clock pulse for the entire meter's software functions, ensuring stable and orderly program operation. The crystal oscillator, as the core of the clock system, possesses extremely high accuracy, making it ideal as the clock source for the smart meter system. With increasingly complex electricity environments, users are placing higher demands on the stability of smart meters. For example, faster communication speeds, more accurate timekeeping, more sensitive voltage and current data acquisition, and more accurate energy metering. The realization of these functions all relies on a precise clock signal from the crystal.
[0003] However, if the crystal oscillator malfunctions and stops oscillating, it will lead to data corruption in the electricity meter, communication failures, malfunctions in opening and closing the circuit breaker, and even other unpredictable and serious consequences. After the crystal oscillator stops oscillating, the electricity meter typically switches to its internal high-frequency RC clock source as a temporary clock source. However, this clock source has low accuracy and is easily affected by temperature. If the error exceeds a certain limit, it will cause abnormal communication between the MCU and peripherals, making it impossible to guarantee the normal operation of the electricity meter. Currently, there is no effective means in the technology to correct the accuracy of the internal high-frequency RC clock in real time. Summary of the Invention
[0004] This invention proposes a clock correction method and an energy meter based on asynchronous communication. Its purpose is to correct the accuracy of the internal high-frequency RC in real time, thereby solving the problem of low accuracy of the internal high-frequency RC and the inability to guarantee the normal operation of the energy meter.
[0005] The technical solution of this invention is as follows:
[0006] A clock correction method based on asynchronous communication is used to correct the clock error of the internal RC of an MCU. After enabling the internal RC as the clock source of the MCU, the MCU receives data sent by an external module through asynchronous communication. The external module has an independent clock source. The MCU calculates the deviation value of the internal RC by the duration between the high and low level changes when receiving data, and then corrects the internal RC based on the deviation value.
[0007] As a further improvement to the clock correction method: the TXD pin of the peripheral module is connected to the capture pin of the MCU's timer; the MCU obtains the deviation value of the internal RC by means of the timer based on the timing difference between two specified rising or falling edges captured when receiving fixed data.
[0008] As a further improvement to the clock correction method, when the peripheral module sends data to the MCU, the first byte of the data sent is known fixed data.
[0009] As a further improvement to the clock correction method: assuming the baud rate of the peripheral module is Bd, the time interval between two rising or falling edges specified in the fixed data is N bits, and the timing difference captured by the timer is T, then the deviation value of the internal RC is: Offset=N / (Bd*T).
[0010] As a further improvement to the clock correction method: after the MCU obtains the deviation value of the internal RC, it determines whether the deviation value is within a preset range: if it is not within the range and the offset is greater than 1, the frequency of the internal RC is reduced; if it is not within the range and the offset is less than 1, the frequency of the internal RC is increased.
[0011] As a further improvement to the clock correction method: the MCU adjusts the frequency of the internal RC by modifying the value of the internal ADJ_REG register. Let the current value of the ADJ_REG register be Old_ADJ, and the adjustment step size of the frequency of the internal RC corresponding to the least significant bit of the ADJ_REG register be LSB_ADJ. Then, the value of the ADJ_REG register is set to Old_ADJ+(Offset-1) / LSB_ADJ.
[0012] As a further improvement to the clock correction method, the specific steps are as follows:
[0013] Step 1: Connect the TXD pin of the peripheral module to the capture pin of the MCU's timer;
[0014] Step 2: Enable the external crystal oscillator stop detection function of the MCU and check the external crystal stop flag. When the MCU detects that the external crystal oscillator has stopped oscillating, switch the MCU clock to the internal RC clock and then execute Step 3.
[0015] Step 3: The peripheral module sends data to the MCU in an asynchronous communication manner. The first byte of the data sent is known fixed data.
[0016] Step 4: The MCU obtains the deviation value of the internal RC by using a timer based on the timing difference between two specified rising or falling edges captured when receiving fixed data;
[0017] Step 5: Determine if the deviation value is within the preset range. If not, adjust the frequency of the internal RC and return to step 3. If it is within the preset range, wait for a period of time and then return to step 3.
[0018] As a further improvement to the clock correction method: in any of steps 3 to 5, it is further determined whether the external crystal oscillator has restarted. If it has restarted, the execution of the current step is stopped, the external crystal oscillator is used as the clock source of the MCU, and step 2 is executed.
[0019] As a further improvement to the clock correction method: when the MCU communicates asynchronously with the peripheral module, if the communication fails, the baud rate of the MCU is adjusted until the communication is normal.
[0020] The present invention also provides an energy meter, which includes an MCU, an external crystal oscillator, and a metering chip. The MCU, the external crystal oscillator, and the metering chip are respectively connected. The external crystal oscillator serves as the clock source for the MCU. The MCU obtains metering data through the metering chip. When the external crystal oscillator stops oscillating, the MCU starts its internal RC as a clock source and then uses the above-mentioned clock correction method to correct the internal RC. During correction, the metering chip is used as the peripheral module.
[0021] Compared with the prior art, the present invention has the following positive effects: After the external crystal oscillator stops oscillating, the present invention uses an independent clock source inside the peripheral module as a reference and data during asynchronous communication as a medium. By comparing the interval of the high and low level changes during data transmission with the timing difference of the MCU's internal timer, the deviation value of the internal RC is obtained, thereby achieving accurate correction of the internal RC. This not only solves the problem of the internal RC being unable to be corrected, but also has the advantages of low resource consumption, high correction efficiency, and no need for additional power consumption. Attached Figure Description
[0022] Figure 1 This is a waveform diagram of the data received in a specific implementation. Detailed Implementation
[0023] The technical solution of the present invention will be described in detail below with reference to the accompanying drawings: Example 1
[0024] A clock correction method based on asynchronous communication is proposed to correct the clock error of the internal RC circuit of an MCU. The specific steps are as follows:
[0025] Step 1: Connect the TXD pin of the peripheral module to the capture pin of the MCU's timer T1. Simultaneously, establish a communication connection between the external module and the MCU.
[0026] The external module has an independent clock source with a higher accuracy than the MCU's internal RC clock. The external module can be a metering chip, an infrared module, a RS-485 module, or a communication module.
[0027] Step 2: Enable the external crystal oscillator stop detection function of the MCU and check the external crystal stop flag. When the MCU detects that the external crystal oscillator has stopped oscillating, switch the MCU clock to the internal RC clock, and then proceed to Step 3.
[0028] In this embodiment, the oscillation stop detection function is enabled by configuring OSC_CONFIG (external clock control register) = ERR_ON. Then, it is checked whether the oscillation stop flag (OSC_STATE) is set. If it is not set, it indicates that the external crystal oscillator is normal, and no further steps are performed; the process ends directly. If it is set, SYS_CLK_REG = RC_CLK (system clock is internal RC), T1_CONFIG = RC_CLK (T1 clock source is switched to internal RC), T1_DIV = 0 (T1 clock is not divided), then the loop count is initialized to 0, and step 3 is executed.
[0029] Step 3: The peripheral module sends data to the MCU in an asynchronous communication manner. The first byte of the data sent is known fixed data.
[0030] Specifically, the metering chip reading function is initialized, and the metering chip register data is read. If the reading fails, the baud rate of the communication between the MCU and the metering chip is fine-tuned by about ±20% until normal communication with the metering chip is possible.
[0031] This embodiment takes the metering chip HT7017 as an example. Its communication baud rate with the MCU is 4800 by default. When reading data, the first byte is always returned as 0x6A.
[0032] Step 4: The MCU obtains the deviation value of the internal RC by using a timer based on the timing difference between two specified rising or falling edges captured when receiving fixed data.
[0033] Specifically, configure T1_CONFIG (T1 control register) = START to enable the timer capture function, read the metering chip register data, and capture the TXD signal. For example... Figure 1 The transmitted data includes 0x6A, a start bit of 0, and a parity bit of 0, totaling 10 bits. Here, we capture the time interval between the first and third rising edges. When the first rising edge is detected, the timing value C1 = T1_CNT is recorded; when the second rising edge is detected, the timing value C2 = T1_CNT is recorded; and when the third rising edge is detected, the timing value C3 = T1_CNT is recorded. At this point, the capture stops, and the time difference T = C3 - C1 is obtained.
[0034] Assuming the baud rate of the peripheral module is Bd=4800, and the time occupied between the first rising edge and the third rising edge in the data is fixed at N=4 bits, then the deviation value of the internal RC is: Offset=4 / (4800*T).
[0035] Step 5: Determine if the deviation value is within the preset range. If not, adjust the frequency of the internal RC and return to step 3. If it is within the preset range, wait for a period of time and then return to step 3.
[0036] Specifically, after the MCU obtains the offset value of the internal RC, if the offset is outside the range and the offset is greater than 1, the frequency of the internal RC needs to be reduced; if it is outside the range and the offset is less than 1, the frequency of the internal RC needs to be increased.
[0037] In this embodiment, the "preset range" refers to Offset=1, that is: if Offset is not 1, adjustment is required.
[0038] Furthermore, the MCU adjusts the frequency of the internal RC circuit by modifying the value of the internal ADJ_REG register. ADJ_REG is a clock adjustment register used to improve the accuracy of the RC clock source. Assuming the current value of the ADJ_REG register is Old_ADJ, and the adjustment step size of the internal RC frequency corresponding to the least significant bit of the ADJ_REG register is LSB_ADJ, then the value of the ADJ_REG register is set to Old_ADJ + (Offset-1) / LSB_ADJ.
[0039] After each offset adjustment, the process returns to step 3 for re-detection and adjustment, ensuring that the offset always fluctuates around 1, meaning that the internal RC frequency is close to the clock frequency of the peripheral module.
[0040] In any of steps 3 to 5, it is further determined whether the external crystal oscillator has restarted. If it has restarted, the execution of the current step is stopped, the external crystal oscillator is used as the clock source of the MCU, and step 2 is executed. Example 2
[0041] This embodiment provides an energy meter based on Embodiment 1, which includes an MCU, an external crystal oscillator, and a metering chip as the aforementioned peripheral module. The MCU, the external crystal oscillator, and the metering chip are respectively connected. The external crystal oscillator serves as the clock source for the MCU, and the MCU obtains metering data through the metering chip.
[0042] When the external crystal oscillator of the energy meter stops oscillating, the MCU starts the internal RC as a clock source, and then uses the clock correction method described in Example 1 to correct the internal RC.
Claims
1. A clock correction method based on asynchronous communication, used to correct clock errors in the internal RC circuit of an MCU, characterized in that: After enabling the internal RC as the clock source of the MCU, the MCU receives data sent by the external module through asynchronous communication. The external module has an independent clock source, and the accuracy of the external module's clock source is higher than that of the MCU's internal RC. The MCU calculates the deviation value of the internal RC by the duration between the high and low level changes when receiving data, and then corrects the internal RC based on the deviation value. The TXD pin of the external module is connected to the capture pin of the MCU's timer; the MCU obtains the deviation value of the internal RC by using the timer based on the timing difference between two specified rising or falling edges captured when receiving fixed data; When an external module sends data to the MCU, the first byte of the data sent is known, fixed data. If the baud rate of the external module is Bd, the time interval between two rising or falling edges specified in the fixed data is N bits, and the timing difference captured by the timer is T, then the deviation value of the internal RC is: Offset=N / (Bd*T).
2. The clock correction method based on asynchronous communication as described in claim 1, characterized in that: After the MCU obtains the deviation value of the internal RC, it determines whether the deviation value is within the preset range: if it is not within the range and the offset is greater than 1, the frequency of the internal RC is reduced; if it is not within the range and the offset is less than 1, the frequency of the internal RC is increased.
3. The clock correction method based on asynchronous communication as described in claim 2, characterized in that: The MCU adjusts the frequency of the internal RC by modifying the value of the internal ADJ_REG register. If the current value of the ADJ_REG register is Old_ADJ, and the adjustment step size of the frequency of the internal RC corresponding to the least significant bit of the ADJ_REG register is LSB_ADJ, then the value of the ADJ_REG register is set to Old_ADJ+(Offset-1) / LSB_ADJ.
4. The clock correction method based on asynchronous communication as described in claim 1, characterized in that, The specific steps are as follows: Step 1: Connect the TXD pin of the external module to the capture pin of the MCU's timer; Step 2: Enable the external crystal oscillator stop detection function of the MCU and check the external crystal stop flag. When the MCU detects that the external crystal oscillator has stopped oscillating, switch the MCU clock to the internal RC clock and then execute Step 3. Step 3: The external module sends data to the MCU in an asynchronous communication manner. The first byte of the sent data is known fixed data. Step 4: The MCU obtains the deviation value of the internal RC by using a timer based on the timing difference between two specified rising or falling edges captured when receiving fixed data; Step 5: Determine if the deviation value is within the preset range. If not, adjust the frequency of the internal RC and return to step 3. If it is within the preset range, wait for a period of time and then return to step 3.
5. The clock correction method based on asynchronous communication as described in claim 4, characterized in that: In any of steps 3 to 5, it is further determined whether the external crystal oscillator has restarted. If it has restarted, the execution of the current step is stopped, the external crystal oscillator is used as the clock source of the MCU, and step 2 is executed.
6. The clock correction method based on asynchronous communication as described in claim 1, characterized in that: When the MCU communicates asynchronously with an external module, if the communication fails, the baud rate of the MCU is adjusted until the communication is normal.
7. An electricity meter, the electricity meter comprising an MCU, an external crystal oscillator, and a metering chip, wherein the MCU, the external crystal oscillator, and the metering chip are respectively connected, the external crystal oscillator serves as the clock source for the MCU, and the MCU acquires metering data through the metering chip, characterized in that: When the external crystal oscillator stops oscillating, the MCU starts the internal RC as a clock source, and then uses the clock correction method as described in any one of claims 1 to 6 to correct the internal RC. During the correction, the metering chip is used as the external module.