Crystal oscillator switching method and device, controller, gas meter and storage medium
By switching between internal and external crystal oscillators, the problem of inaccurate gas meter readings caused by external crystal oscillator failure was solved, enabling the gas meter to operate normally when the external crystal oscillator is abnormal and ensuring accurate readings after recovery.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-22
- Publication Date
- 2026-04-07
AI Technical Summary
The failure of the external crystal oscillator in electronic gas meters can lead to inaccurate or no measurement, and existing technologies cannot effectively guarantee the accuracy and reliability of the measurement.
An internal and external crystal oscillator switching scheme is adopted. The status of the external crystal oscillator is judged by anomaly detection counting and real-time clock. The system switches to the internal crystal oscillator in a timely manner to ensure the normal operation of the metering function. When the external crystal oscillator returns to normal, it switches back to the external crystal oscillator to ensure the accuracy of the metering.
It effectively reduces the impact of external crystal oscillator failure on gas meter measurement, ensures the normal operation and accuracy of metering function, and reduces metering losses.
Smart Images

Figure CN115979364B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of crystal oscillator switching technology, and in particular to a crystal oscillator switching method, device, controller, gas meter, and storage medium. Background Technology
[0002] Electronic gas meters require high accuracy from their timers because they calculate cumulative amounts based on instantaneous flow and cycles. Using an internal crystal oscillator results in significant timer errors, so an external crystal oscillator is generally used to provide the clock source for the timer and ensure its accuracy.
[0003] However, during the use of electronic gas meters, there is a certain probability that the circuit board may become damp or corroded due to external protection issues, leading to the failure of the external crystal oscillator. This would prevent the timer from operating properly, causing the electronic gas meter to fail to measure gas and some functions to malfunction. This would result in the gas meter being in a "dead" state, with the meter not measuring gas and causing metering losses.
[0004] Therefore, existing technologies cannot both reduce the serious impact of gas meter failure due to external crystal oscillator failure and ensure the accuracy of metering in a timely and effective manner. Summary of the Invention
[0005] This application provides a crystal oscillator switching method, device, controller, gas meter, and storage medium, which can reduce the serious impact of gas meter failure due to external crystal oscillator failure, and ensure the accuracy of metering in a timely and effective manner.
[0006] In a first aspect, embodiments of this application provide a crystal oscillator switching method applied to a controller in a gas meter, wherein both the metering timer and the real-time clock in the gas meter are initialized using a clock provided by an external crystal oscillator of the gas meter; the method includes:
[0007] If the controller is in operation and it is determined that the metering timer uses a clock provided by an external crystal oscillator, then for each cycle, an anomaly detection count is performed on the clock provided by the external crystal oscillator; wherein, each cycle is a cycle formed by executing each business function of the gas meter in sequence once.
[0008] Based on the anomaly detection count results and the real-time clock, determine whether the external crystal oscillator is abnormal;
[0009] If the external crystal oscillator is abnormal, switch the external crystal oscillator to the internal crystal oscillator of the gas meter. When the external crystal oscillator is detected to be back to normal, switch the internal crystal oscillator back to the external crystal oscillator.
[0010] In one possible design, determining whether the external crystal oscillator is abnormal based on the anomaly detection count result and the real-time clock includes:
[0011] If the anomaly detection count is greater than the crystal oscillator anomaly threshold before the external crystal oscillator detection timer of the real-time clock is triggered, then the external crystal oscillator is determined to be abnormal.
[0012] The real-time clock is used to perform an interrupt operation when the external crystal oscillator detection timer is triggered, so that the abnormal detection count result is cleared to zero, and the time interval of the external crystal oscillator detection timer divided by a multiple of each cycle period is less than the crystal oscillator abnormal threshold.
[0013] In one possible design, the method further includes:
[0014] If the controller is in a sleep state and is not woken up when the target timer in the gas meter reaches the overflow condition, the external crystal oscillator is determined to be abnormal, and the abnormality detection count result is cleared to zero by the overflow interrupt of the target timer in the gas meter.
[0015] Determine whether the metering timer and real-time clock are in a state of using the clock provided by the internal crystal oscillator;
[0016] If the metering timer and real-time clock are not in a state of using the clock provided by the internal crystal oscillator, switch the external crystal oscillator to the internal crystal oscillator of the gas meter.
[0017] In one possible design, the method further includes:
[0018] If both the metering timer and the real-time clock are in a state of using the clock provided by the internal crystal oscillator, the state of the external crystal oscillator is periodically detected.
[0019] If it is determined that the external crystal oscillator has returned to normal, then the internal crystal oscillator is switched to the external crystal oscillator.
[0020] In one possible design, the timing detection of the state of the external crystal oscillator includes:
[0021] If an abnormality detection recovery count of the clock provided by the external crystal oscillator is detected, the abnormality detection count result of the recount within a predefined period is obtained, and the recounted abnormality detection count result is used as the number of crystal oscillator abnormality recovery detections;
[0022] The recounted anomaly detection count is compared with the crystal oscillator anomaly recovery threshold. If the recounted anomaly detection count is greater than the crystal oscillator anomaly recovery threshold, then the external crystal oscillator is determined to have returned to normal.
[0023] In one possible design, switching the external crystal oscillator to the internal crystal oscillator of the gas meter includes:
[0024] Switch the clock provided by the external crystal oscillator used by both the metering timer and the real-time clock to the clock provided by the internal crystal oscillator;
[0025] Accordingly, switching the internal crystal oscillator to the external crystal oscillator includes:
[0026] Switch the clock provided by the internal crystal oscillator used by both the metering timer and the real-time clock to the clock provided by the external crystal oscillator.
[0027] Secondly, embodiments of this application provide a crystal oscillator switching device applied to a controller in a gas meter, wherein both the metering timer and the real-time clock in the gas meter are initialized using a clock provided by an external crystal oscillator of the gas meter; the device includes:
[0028] An anomaly detection module is used to perform anomaly detection and counting on the clock provided by the external crystal oscillator for each cycle when the controller is in operation and it is determined that the metering timer uses a clock provided by an external crystal oscillator; wherein each cycle is a cycle formed by the sequential execution of each business function in the gas meter;
[0029] The processing module is used to determine whether the external crystal oscillator is abnormal based on the anomaly detection count result and the real-time clock.
[0030] A switching module is used to switch the external crystal oscillator to the internal crystal oscillator of the gas meter when the external crystal oscillator is abnormal.
[0031] Thirdly, embodiments of this application provide a controller installed in a gas meter, the controller being used to perform the method as described in any of the first aspects.
[0032] Fourthly, embodiments of this application provide a gas meter including the controller described in the third aspect.
[0033] Fifthly, embodiments of this application provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the method described in any of the first aspects.
[0034] Sixthly, embodiments of this application provide a computer program product, including a computer program that, when executed by a processor, implements the method as described in any of the first aspects.
[0035] The crystal oscillator switching method, device, controller, gas meter, and storage medium provided in this embodiment are applied to the controller in the gas meter. Both the metering timer and real-time clock in the gas meter are initialized using the clock provided by the external crystal oscillator of the gas meter. If the controller is in operation and it is determined that the metering timer uses the clock provided by the external crystal oscillator, then for each cycle, an anomaly detection count is performed on the clock provided by the external crystal oscillator. Each cycle is a cycle formed by sequentially executing each business function in the gas meter. Then, based on the anomaly detection count result and the real-time clock, it is determined whether the external crystal oscillator is abnormal. If the external crystal oscillator is abnormal, it is switched to the internal crystal oscillator of the gas meter. When the external crystal oscillator is detected to have returned to normal, the internal crystal oscillator is switched back to the external crystal oscillator. Therefore, by initializing the clock provided by an external crystal oscillator, the metering function and accuracy of the gas meter can be guaranteed. When an abnormality is detected in the external crystal oscillator, the meter can be switched to the clock provided by the internal crystal oscillator in a timely manner, so that the metering function of the gas meter can be used normally and the gas meter can be measured normally. This effectively reduces the serious impact of the meter not measuring due to the failure of the external crystal oscillator. At the same time, by detecting whether the external crystal oscillator has returned to normal, the meter can be switched to the external crystal oscillator once it is detected that the external crystal oscillator has returned to normal, which can ensure the accuracy of the metering in a timely and effective manner. Attached Figure Description
[0036] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0037] Figure 1 A schematic flowchart illustrating the crystal oscillator switching method provided in an embodiment of this application;
[0038] Figure 2 A schematic flowchart of a crystal oscillator switching method provided in another embodiment of this application;
[0039] Figure 3 A schematic flowchart of a crystal oscillator switching method provided in another embodiment of this application;
[0040] Figure 4 A schematic flowchart illustrating a crystal oscillator switching method provided in another embodiment of this application;
[0041] Figure 5 A schematic flowchart of a crystal oscillator switching method provided in another embodiment of this application;
[0042] Figure 6This is a schematic diagram of the crystal oscillator switching device provided in an embodiment of this application. Detailed Implementation
[0043] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0044] The terms “first,” “second,” “third,” “fourth,” etc. (if present) in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a particular order or sequence. It should be understood that such data can be interchanged where appropriate so that embodiments of the application described herein can be implemented, for example, in orders other than those illustrated or described herein. Furthermore, the terms “comprising” and “having,” and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0045] During the use of electronic gas meters, there is a certain probability that external protection issues can cause the circuit board to become damp or corroded, leading to the failure of the external crystal oscillator. This failure results in an inaccurate clock, causing the timer to malfunction, rendering the electronic gas meter unable to measure gas and rendering some functions unusable. This essentially puts the gas meter in a "frozen" state, resulting in metering losses. Therefore, current technology cannot both mitigate the serious impact of gas meter failure due to external crystal oscillator failure and effectively ensure metering accuracy.
[0046] Therefore, to address the aforementioned issues, the technical concept of this application is to adopt a switching scheme between internal and external crystal oscillators. When an external crystal oscillator malfunction is detected, the metering timer is switched to use the internal crystal oscillator to ensure normal metering operation; when the external crystal oscillator recovers, the metering timer is switched back to the external crystal oscillator to ensure metering accuracy. This solves the problem of inaccurate metering caused by using only the internal crystal oscillator, and also avoids the problem of metering failure when using only the external crystal oscillator. It achieves both reducing the serious impact of gas meter failure due to external crystal oscillator failure and ensuring timely and effective metering accuracy.
[0047] In practical applications, see Figure 1 As shown, Figure 1 This is a flowchart illustrating the crystal oscillator switching method provided in this application. When the gas meter controller (the system) is running, if the anomaly detection count (the anomaly detection count result) is greater than the crystal oscillator anomaly threshold, or when the gas meter controller is in sleep mode and detects an overflow of the target timer (watchdog timer), the metering timer and real-time clock (RTC) in the gas meter are switched to internal clock mode to ensure the normal operation of the metering function. Then, the controller periodically checks whether the external crystal oscillator has returned to normal. If the number of crystal oscillator anomaly recovery detections is greater than the crystal oscillator anomaly recovery threshold, it indicates that the external crystal oscillator has returned to normal. Then, the metering timer and RTC clock in the gas meter are switched to external clock mode to ensure metering accuracy. Therefore, this method effectively reduces the serious impact of gas meter failure due to external crystal oscillator failure while ensuring timely and effective metering accuracy.
[0048] The technical solutions of this application will be described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments.
[0049] Figure 2 This is a flowchart illustrating a crystal oscillator switching method provided in another embodiment of this application. The method may include:
[0050] S101. If the controller is in operation and it is determined that the metering timer uses a clock provided by an external crystal oscillator, then for each cycle, an anomaly detection count is performed on the clock provided by the external crystal oscillator.
[0051] Each cycle is formed by executing each business function of the gas meter in sequence.
[0052] In this embodiment, the executing entity can be a crystal oscillator switching device, such as a controller (the controller may include a microcontroller unit (MCU)). This crystal oscillator switching device can be configured in the gas meter to switch between the external clock and the internal clock of the gas meter according to different scenarios. This enables timely switching to the internal crystal oscillator after the external crystal oscillator fails, ensuring that the meter can still work normally and maintain metering when the external crystal oscillator fails. At the same time, it reports to the business system. When the external crystal oscillator recovers, it switches back to the external crystal oscillator to ensure the accuracy of metering, thereby effectively reducing the losses caused by the meter not measuring due to the failure of the external crystal oscillator.
[0053] In this embodiment, both the metering timer and the real-time clock in the gas meter are initialized using the clock provided by the external crystal oscillator of the gas meter. Specifically, the controller operates at full speed using a high-speed clock source, the metering timer (hereinafter referred to as Timer 1) is initialized using the clock provided by the external crystal oscillator, the RTC clock is initialized using the clock provided by the external crystal oscillator, that is, the external crystal oscillator detection timer for the RTC clock (hereinafter referred to as Timer 2) is initialized using the clock provided by the external crystal oscillator, and other timers use an internal clock source.
[0054] Taking the scenario where the controller is in operation as an example, the system first determines whether the metering timer is using a clock provided by an external crystal oscillator. If it is currently using an external clock (i.e., a clock provided by an external crystal oscillator), the anomaly detection count is accumulated for each cycle. Each cycle is formed by executing each business function of the gas meter sequentially once.
[0055] S102. Based on the anomaly detection count results and the real-time clock, determine whether the external crystal oscillator is abnormal.
[0056] S103. If the external crystal oscillator is abnormal, switch the external crystal oscillator to the internal crystal oscillator of the gas meter. When the external crystal oscillator is detected to be back to normal, switch the internal crystal oscillator back to the external crystal oscillator.
[0057] In this embodiment, timer 2 resets the external clock anomaly detection count to 0 each time it is triggered. When the external clock anomaly detection count exceeds the crystal oscillator anomaly threshold, it is determined that the external clock is abnormal, and a switch to the internal clock (referring to the clock provided by the internal crystal oscillator) is initiated. When switching to the internal clock, the status of the external crystal oscillator is periodically checked. If the external clock is detected to have returned to normal, the internal clock is switched back to the external clock.
[0058] The crystal oscillator switching method provided in this embodiment can ensure the metering function and accuracy of the gas meter by initializing the clock provided by an external crystal oscillator. When an abnormality is detected in the external crystal oscillator, it can switch to the clock provided by the internal crystal oscillator in a timely manner, so that the metering function of the gas meter can be used normally and the gas meter can measure normally. This effectively reduces the serious impact of the meter not measuring due to the failure of the external crystal oscillator. At the same time, by detecting whether the external crystal oscillator has returned to normal, once the external crystal oscillator is detected to be normal, it can switch to the external crystal oscillator in a timely and effective manner to ensure the accuracy of the metering.
[0059] In one possible design, determining whether the external crystal oscillator is abnormal based on the anomaly detection count result and the real-time clock can be achieved through the following steps:
[0060] If the anomaly detection count is greater than the crystal oscillator anomaly threshold before the external crystal oscillator detection timer of the real-time clock is triggered, then the external crystal oscillator is determined to be abnormal.
[0061] The real-time clock is used to perform an interrupt operation when the external crystal oscillator detection timer is triggered, so that the abnormal detection count result is cleared to zero, and the time interval of the external crystal oscillator detection timer divided by a multiple of each cycle period is less than the crystal oscillator abnormal threshold.
[0062] In this embodiment, see Figure 3 As shown, Figure 3 This is a flowchart illustrating a crystal oscillator switching method provided in another embodiment of this application. During system (referring to the controller) operation, in each loop, it is determined whether the system is operating on the internal clock. If it is operating on the internal clock, the status of the external clock is periodically checked. If it is operating on the external clock, the external clock anomaly detection count is accumulated, with the external clock anomaly detection count incremented by 1 for each loop cycle. Specifically, timer 2 resets the external clock anomaly detection count to 0 each time it is triggered, indicating that the external clock is normal. Because of an external clock anomaly, timer 2 cannot execute an interrupt to reset the count. Therefore, when the external clock anomaly detection count exceeds the crystal oscillator anomaly threshold (e.g., 10 seconds), it is determined that the external clock is abnormal, and a switch to the internal clock operation is initiated.
[0063] In this system, the count is incremented by 1 regardless of whether the external clock is abnormal or not. Furthermore, the time interval of the external crystal oscillator detection timer divided by a multiple of each cycle is less than the crystal oscillator abnormality threshold. Therefore, if the external clock is normal, the number of cycles accumulated in the period before the external crystal oscillator detection timer is triggered (i.e., the time interval of timer 2) is cleared to zero by the RTC before reaching the crystal oscillator abnormality threshold. Thus, based on the above implementation principle, it can be determined that the external crystal oscillator is normal. If the external clock abnormality detection count is greater than the crystal oscillator abnormality threshold, it indicates that the RTC has not cleared the count periodically, which in turn indicates that the external clock used by the RTC is in an abnormal state, i.e., the external crystal oscillator is abnormal.
[0064] In one possible design, the method can also be implemented through the following steps:
[0065] Step a1: If the controller is in a sleep state and is not woken up when the target timer in the gas meter reaches the overflow condition, then the external crystal oscillator is determined to be abnormal, and the abnormality detection count result is cleared to zero by the overflow interrupt of the target timer in the gas meter.
[0066] Step a2: Determine whether the metering timer and real-time clock are in a state of using the clock provided by the internal crystal oscillator;
[0067] Step a3: If the metering timer and real-time clock are not in a state of using the clock provided by the internal crystal oscillator, switch the external crystal oscillator to the internal crystal oscillator of the gas meter.
[0068] In this embodiment, see Figure 4 As shown, Figure 4 This is a flowchart illustrating a crystal oscillator switching method provided in another embodiment of this application. If the controller is in a sleep state, taking the sleep state scenario as an example, the switching process can be as follows: When the controller is in a sleep state, if the system cannot be woken up due to the failure of the external crystal oscillator, the overflow interrupt of the target timer (here referring to the watchdog timer, hereinafter referred to as the watchdog) is used for processing. When the watchdog overflows, the system switches to the internal clock.
[0069] Specifically, when the watchdog overflows, the exception detection count is cleared, and it is determined whether the device is in the internal clock. If it is not in the internal clock, it switches to the internal clock and determines whether the switch is successful. If the switch is successful, the current switching process ends. If the switch fails, or it is determined that the device is not in the internal clock, the event and data storage is restored.
[0070] In one possible design, switching the external crystal oscillator to the internal crystal oscillator of the gas meter can be achieved through the following steps:
[0071] Switch the clock provided by the external crystal oscillator used by both the metering timer and the real-time clock to the clock provided by the internal crystal oscillator.
[0072] In this embodiment, when switching to the internal clock, Timer 1 and RTC are switched to the internal clock to ensure that the device (here referring to the gas meter) can measure and work normally.
[0073] In one possible design, the method can also be implemented through the following steps:
[0074] If both the metering timer and the real-time clock are in a state of using the clock provided by the internal crystal oscillator, the state of the external crystal oscillator is periodically detected.
[0075] If it is determined that the external crystal oscillator has returned to normal, then the internal crystal oscillator is switched to the external crystal oscillator.
[0076] In this embodiment, in order to ensure the accuracy of measurement in a timely and effective manner, when using the clock provided by the internal crystal oscillator, the status of the external crystal oscillator can be detected periodically, so that the internal crystal oscillator can be switched to the external crystal oscillator once the external crystal oscillator is detected to be back to normal.
[0077] In one possible design, switching the internal crystal oscillator to the external crystal oscillator can be achieved through the following steps:
[0078] Switch the clock provided by the internal crystal oscillator used by both the metering timer and the real-time clock to the clock provided by the external crystal oscillator.
[0079] In this embodiment, when the external crystal oscillator returns to normal, Timer 1 and RTC are switched to the external clock to ensure the accuracy of the measurement.
[0080] In one possible design, the timing detection of the external crystal oscillator's state can be achieved through the following steps:
[0081] Step b1: If an abnormal detection recovery count of the clock provided by the external crystal oscillator is detected, the abnormal detection count result of the recount within a predefined period is obtained, and the recounted abnormal detection count result is used as the number of crystal oscillator abnormal recovery detections.
[0082] Step b2: Compare the recounted anomaly detection count with the crystal oscillator anomaly recovery threshold. If the recounted anomaly detection count is greater than the crystal oscillator anomaly recovery threshold, then the external crystal oscillator is determined to have returned to normal.
[0083] In this embodiment, see Figure 5 As shown, Figure 5 This is a flowchart illustrating a crystal oscillator switching method provided in another embodiment of this application. The gas meter is in internal clock mode: The external crystal oscillator status is periodically detected. If timer 2 returns to normal, the crystal oscillator abnormality recovery detection count is incremented within the timer 2 interrupt; if it does not return to normal, the count is not incremented. When, within a certain period (counted by the timer using the internal clock), the crystal oscillator abnormality recovery detection count (or the number of crystal oscillator abnormality recovery detections, here referring to the result of the recounted abnormality detection count) reaches the crystal oscillator abnormality recovery threshold (e.g., crystal oscillator abnormality recovery threshold: 6, unit: seconds), i.e., the detection timer count > the crystal oscillator abnormality recovery threshold, the external crystal oscillator is considered normal, and the external crystal oscillator switching operation is then performed, i.e., switching back from the internal clock to the external clock.
[0084] Furthermore, this application can achieve the detection of the external clock without adding a new timer. It can periodically switch from the internal clock to the external clock to determine the clock recovery. Once the external clock is found to be normal, it can be used without switching back to the internal clock. If the external clock is still abnormal, it can continue to switch from the external clock to the internal clock to ensure the use of the metering function. The above steps are continuously repeated until the external clock is restored to normal.
[0085] Therefore, this application achieves the following: when an external crystal oscillator malfunction is detected, the metering timer switches to the internal crystal oscillator to ensure normal metering operation; when the external crystal oscillator recovers, the metering timer switches back to the external crystal oscillator to ensure metering accuracy. This solves the problem of metering inaccuracy caused by using only the internal crystal oscillator, and also avoids the problem of metering failure when using only the external crystal oscillator. This ensures product reliability while maintaining metering accuracy, reducing metering losses in gas meters.
[0086] To implement the crystal oscillator switching method, this embodiment provides a crystal oscillator switching device. See also... Figure 6 , Figure 6 This is a schematic diagram of the structure of the crystal oscillator switching device provided in the embodiment of this application; the crystal oscillator switching device is applied to the controller of the gas meter, and the metering timer and real-time clock in the gas meter are initialized using the clock provided by the external crystal oscillator of the gas meter; the device includes: an anomaly detection module 601, a processing module 602 and a switching module 603.
[0087] An anomaly detection module 601 is used to perform anomaly detection and counting on the clock provided by the external crystal oscillator for each cycle when the controller is in operation and it is determined that the metering timer uses a clock provided by an external crystal oscillator; wherein, each cycle is a cycle formed by executing each business function of the gas meter in sequence once.
[0088] Processing module 602 is used to determine whether the external crystal oscillator is abnormal based on the anomaly detection count result and the real-time clock;
[0089] The switching module 603 is used to switch the external crystal oscillator to the internal crystal oscillator of the gas meter when the external crystal oscillator is abnormal, and to switch the internal crystal oscillator back to the external crystal oscillator when the external crystal oscillator is detected to be normal.
[0090] In this embodiment, the anomaly detection module 601, processing module 602, and switching module 603 are used to perform anomaly detection counting on the clock provided by the external crystal oscillator for each cycle when the controller is in running state and it is determined that the metering timer uses the clock provided by the external crystal oscillator; wherein, each cycle is a cycle formed by the sequential execution of each business function in the gas meter; then, based on the anomaly detection count result and the real-time clock, it is determined whether the external crystal oscillator is abnormal; when the external crystal oscillator is abnormal, the external crystal oscillator is switched to the internal crystal oscillator of the gas meter; when the external crystal oscillator is detected to have returned to normal, the internal crystal oscillator is switched back to the external crystal oscillator. Therefore, by initializing the clock provided by an external crystal oscillator, the metering function and accuracy of the gas meter can be guaranteed. When an abnormality is detected in the external crystal oscillator, the meter can be switched to the clock provided by the internal crystal oscillator in a timely manner, so that the metering function of the gas meter can be used normally and the gas meter can be measured normally. This effectively reduces the serious impact of the meter not measuring due to the failure of the external crystal oscillator. At the same time, by detecting whether the external crystal oscillator has returned to normal, the meter can be switched to the external crystal oscillator once it is detected that the external crystal oscillator has returned to normal, which can ensure the accuracy of the metering in a timely and effective manner.
[0091] The apparatus provided in this embodiment can be used to execute the technical solutions of the above method embodiments. Its implementation principle and technical effects are similar, and will not be described again here.
[0092] In one possible design, the processing module is specifically used for:
[0093] If the anomaly detection count is greater than the crystal oscillator anomaly threshold before the external crystal oscillator detection timer of the real-time clock is triggered, then the external crystal oscillator is determined to be abnormal.
[0094] The real-time clock is used to perform an interrupt operation when the external crystal oscillator detection timer is triggered, so that the abnormal detection count result is cleared to zero, and the time interval of the external crystal oscillator detection timer divided by a multiple of each cycle period is less than the crystal oscillator abnormal threshold.
[0095] In one possible design, the switching module is also used for:
[0096] When the controller is in a sleep state and is not woken up when the target timer in the gas meter reaches the overflow condition, the external crystal oscillator is determined to be abnormal, and the abnormality detection count result is cleared to zero by the overflow interrupt of the target timer in the gas meter.
[0097] Determine whether the metering timer and real-time clock are in a state of using the clock provided by the internal crystal oscillator;
[0098] If the metering timer and real-time clock are not in a state of using the clock provided by the internal crystal oscillator, switch the external crystal oscillator to the internal crystal oscillator of the gas meter.
[0099] In one possible design, the switching module is also used for:
[0100] When both the metering timer and the real-time clock are in a state of using the clock provided by the internal crystal oscillator, the state of the external crystal oscillator is periodically detected.
[0101] If it is determined that the external crystal oscillator has returned to normal, then the internal crystal oscillator is switched to the external crystal oscillator.
[0102] In one possible design, the switching module is specifically used for:
[0103] When an anomaly detection recovery count of the clock provided by the external crystal oscillator is detected, the anomaly detection count result of the recount within a predefined period is obtained, and the recounted anomaly detection count result is used as the number of crystal oscillator anomaly recovery detections;
[0104] The recounted anomaly detection count is compared with the crystal oscillator anomaly recovery threshold. If the recounted anomaly detection count is greater than the crystal oscillator anomaly recovery threshold, then the external crystal oscillator is determined to have returned to normal.
[0105] In one possible design, the switching module is specifically used for:
[0106] Switch the clock provided by the external crystal oscillator used by both the metering timer and the real-time clock to the clock provided by the internal crystal oscillator;
[0107] In one possible design, the switching module is also specifically used for:
[0108] Switch the clock provided by the internal crystal oscillator used by both the metering timer and the real-time clock to the clock provided by the external crystal oscillator.
[0109] Therefore, this application achieves the following: when an external crystal oscillator malfunction is detected, the metering timer switches to the internal crystal oscillator to ensure normal metering operation; when the external crystal oscillator recovers, the metering timer switches back to the external crystal oscillator to ensure metering accuracy. This solves the problem of metering inaccuracy caused by using only the internal crystal oscillator, and also avoids the problem of metering failure when using only the external crystal oscillator. This ensures product reliability while maintaining metering accuracy, reducing metering losses in gas meters.
[0110] To implement the crystal oscillator switching method, this embodiment provides a controller. This controller is used to execute the steps of the crystal oscillator switching method described above.
[0111] The controller can be used to execute the technical solutions of the above method embodiments. Its implementation principle and technical effect are similar, and will not be described again in this embodiment.
[0112] To implement the crystal oscillator switching method, this embodiment provides a gas meter. The gas meter includes a controller as described above, which is used to execute the steps of the crystal oscillator switching method described above.
[0113] The gas meter can be an electronic gas meter; no specific restrictions are made here.
[0114] The gas meter can be used to implement the technical solution of the above method embodiment. Its implementation principle and technical effect are similar, and will not be described again in this embodiment.
[0115] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the above-described method.
[0116] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the above-described method.
[0117] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative; for instance, the division of modules is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple modules may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be indirect coupling or communication connection through some interfaces, devices, or modules, and may be electrical, mechanical, or other forms. Additionally, the functional modules in the various embodiments of this application may be integrated into one processing unit, or each module may exist physically separately, or two or more modules may be integrated into one unit. The above-mentioned modular units can be implemented in hardware or in the form of hardware plus software functional units.
[0118] The integrated modules implemented as software functional modules described above can be stored in a computer-readable storage medium. These software functional modules, stored in a storage medium, include several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute some steps of the methods of the various embodiments of this application. It should be understood that the processor may be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), etc. A general-purpose processor may be a microprocessor or any conventional processor. The steps of the methods disclosed in this invention can be directly implemented by a hardware processor, or implemented by a combination of hardware and software modules within the processor.
[0119] The memory may include high-speed RAM, and may also include non-volatile memory (NVM), such as at least one disk drive, and may also be a USB flash drive, external hard drive, read-only memory, disk, or optical disc. The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus. Buses can be categorized as address buses, data buses, control buses, etc. For ease of illustration, the buses in the accompanying drawings are not limited to a single bus or a single type of bus. The aforementioned storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, disk, or optical disc. The storage medium can be any available medium accessible to general-purpose or special-purpose computers.
[0120] An exemplary storage medium is coupled to a processor, enabling the processor to read information from and write information to the storage medium. Alternatively, the storage medium can be an integral part of the processor. Both the processor and the storage medium can reside in an Application Specific Integrated Circuit (ASIC). Alternatively, the processor and storage medium can exist as discrete components in an electronic device or host device.
[0121] Those skilled in the art will understand that all or part of the steps of the above-described method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments; and the aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.
[0122] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this application.
Claims
1. A crystal oscillator switching method, characterized in that, The controller is applied to a gas meter, wherein the metering timer and real-time clock in the gas meter are initialized using the clock provided by the external crystal oscillator of the gas meter; The controller uses a high-speed clock source; Other timers use an internal clock source, and the method includes: If the controller is in operation and it is determined that the metering timer uses a clock provided by an external crystal oscillator, then for each cycle, an anomaly detection count is performed on the clock provided by the external crystal oscillator; wherein, each cycle is a cycle formed by executing each business function of the gas meter in sequence once. Based on the anomaly detection count results and the real-time clock, determine whether the external crystal oscillator is abnormal; If the external crystal oscillator is abnormal, switch the external crystal oscillator to the internal crystal oscillator of the gas meter; when the external crystal oscillator is detected to be back to normal, switch the internal crystal oscillator back to the external crystal oscillator. The step of determining whether the external crystal oscillator is abnormal based on the anomaly detection count result and the real-time clock includes: If the anomaly detection count is greater than the crystal oscillator anomaly threshold before the external crystal oscillator detection timer of the real-time clock is triggered, then the external crystal oscillator is determined to be abnormal. The real-time clock is used to perform an interrupt operation when the external crystal oscillator detection timer is triggered, so that the abnormal detection count result is cleared to zero, and the time interval of the external crystal oscillator detection timer divided by a multiple of each cycle period is less than the crystal oscillator abnormal threshold. If the controller is in a sleep state and is not woken up when the target timer in the gas meter reaches the overflow condition, the external crystal oscillator is determined to be abnormal, and the abnormality detection count result is cleared to zero by the overflow interrupt of the target timer in the gas meter. Determine whether the metering timer and real-time clock are in a state of using the clock provided by the internal crystal oscillator; If the metering timer and real-time clock are not in a state of using the clock provided by the internal crystal oscillator, switch the external crystal oscillator to the internal crystal oscillator of the gas meter.
2. The method according to claim 1, characterized in that, The method further includes: If both the metering timer and the real-time clock are in a state of using the clock provided by the internal crystal oscillator, the state of the external crystal oscillator is periodically detected. If it is determined that the external crystal oscillator has returned to normal, then the internal crystal oscillator is switched to the external crystal oscillator.
3. The method according to claim 2, characterized in that, The timing detection of the external crystal oscillator's state includes: If an abnormality detection recovery count of the clock provided by the external crystal oscillator is detected, the abnormality detection count result of the recount within a predefined period is obtained, and the recounted abnormality detection count result is used as the number of crystal oscillator abnormality recovery detections; The recounted anomaly detection count is compared with the crystal oscillator anomaly recovery threshold. If the recounted anomaly detection count is greater than the crystal oscillator anomaly recovery threshold, then the external crystal oscillator is determined to have returned to normal.
4. The method according to claim 2, characterized in that, The step of switching the external crystal oscillator to the internal crystal oscillator of the gas meter includes: Switch the clock provided by the external crystal oscillator used by both the metering timer and the real-time clock to the clock provided by the internal crystal oscillator; Accordingly, switching the internal crystal oscillator to the external crystal oscillator includes: Switch the clock provided by the internal crystal oscillator used by both the metering timer and the real-time clock to the clock provided by the external crystal oscillator.
5. A crystal oscillator switching device, characterized in that, The controller is applied to a gas meter, wherein the metering timer and real-time clock in the gas meter are initialized using the clock provided by the external crystal oscillator of the gas meter; The controller uses a high-speed clock source; Other timers use an internal clock source, and the device includes: An anomaly detection module is used to perform anomaly detection and counting on the clock provided by the external crystal oscillator for each cycle when the controller is in operation and it is determined that the metering timer uses a clock provided by an external crystal oscillator; wherein each cycle is a cycle formed by the sequential execution of each business function in the gas meter; The processing module is used to determine whether the external crystal oscillator is abnormal based on the anomaly detection count result and the real-time clock. The switching module is used to switch the external crystal oscillator to the internal crystal oscillator of the gas meter when the external crystal oscillator is abnormal, and to switch the internal crystal oscillator back to the external crystal oscillator when the external crystal oscillator is detected to be normal. The processing module is specifically used to determine that the external crystal oscillator is abnormal if the abnormal detection count result is greater than the crystal oscillator abnormality threshold before the external crystal oscillator detection timer of the real-time clock is triggered; wherein, the real-time clock is used to perform an interrupt operation when the external crystal oscillator detection timer is triggered, so that the abnormal detection count result is cleared to zero, and the time interval of the external crystal oscillator detection timer divided by a multiple of each cycle period is less than the crystal oscillator abnormality threshold. The switching module is further configured to: if the controller is in a sleep state and the controller is not woken up when the target timer in the gas meter reaches the overflow condition, determine that the external crystal oscillator is abnormal, and reset the abnormality detection count result to zero through the overflow interrupt of the target timer in the gas meter; determine whether the metering timer and real-time clock are in the state of using the clock provided by the internal crystal oscillator; if the metering timer and real-time clock are not in the state of using the clock provided by the internal crystal oscillator, switch the external crystal oscillator to the internal crystal oscillator of the gas meter.
6. A controller, characterized in that, The controller is installed in the gas meter and is used to perform the crystal oscillator switching method as described in any one of claims 1-4.
7. A gas meter, characterized in that, Includes the controller as described in claim 6.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by the processor, implement the crystal oscillator switching method as described in any one of claims 1-4.
9. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by the processor, it implements the crystal oscillator switching method according to any one of claims 1-4.
Citation Information
Patent Citations
Combined crystal oscillator switching method suitable for humid environment
CN111614319A
Controller and checking device
JP2000235413A