CN109782841B - Real-time clock implementation method based on RTC chip, terminal equipment and storage medium - Google Patents

Real-time clock implementation method based on RTC chip, terminal equipment and storage medium Download PDF

Info

Publication number
CN109782841B
CN109782841B CN201811636003.9A CN201811636003A CN109782841B CN 109782841 B CN109782841 B CN 109782841B CN 201811636003 A CN201811636003 A CN 201811636003A CN 109782841 B CN109782841 B CN 109782841B
Authority
CN
China
Prior art keywords
timer
value
time
rtc chip
real
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201811636003.9A
Other languages
Chinese (zh)
Other versions
CN109782841A (en
Inventor
董时舫
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
PAX Computer Technology Shenzhen Co Ltd
Original Assignee
PAX Computer Technology Shenzhen Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by PAX Computer Technology Shenzhen Co Ltd filed Critical PAX Computer Technology Shenzhen Co Ltd
Priority to CN201811636003.9A priority Critical patent/CN109782841B/en
Publication of CN109782841A publication Critical patent/CN109782841A/en
Application granted granted Critical
Publication of CN109782841B publication Critical patent/CN109782841B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Landscapes

  • Electric Clocks (AREA)

Abstract

The application is applicable to the technical field of real-time clocks, and provides a real-time clock implementation method based on an RTC chip, terminal equipment and a computer readable storage medium, wherein the method comprises the following steps: the real-time clock control method comprises the steps of controlling the RTC chip to generate interruption according to a preset period, initializing a preset initial value and a preset comparison value of a timer after the RTC chip generates the interruption, controlling the timer to start timing from the initial value, outputting a real-time clock value based on first time of the RTC chip and second time of the timer after receiving a reading request of the real-time clock, and providing a high-precision real-time clock for application of the terminal equipment on the premise of not replacing the RTC chip of the terminal equipment through the real-time clock control method.

Description

Real-time clock implementation method based on RTC chip, terminal equipment and storage medium
Technical Field
The present application belongs to the technical field of real-time clocks, and in particular, to a method for implementing a real-time clock based on an RTC chip, a terminal device, and a computer-readable storage medium.
Background
A Real Time Clock (RTC) chip is one of the most widely used consumer electronic devices in daily life. The real-time clock chip can provide accurate real-time or be integrated in the terminal to provide an accurate time reference for the terminal equipment.
However, many RTC chips integrated in terminal devices have a low resolution (e.g., the RTC chip generates a new timing value every second at the fastest time, and the resolution of the RTC chip is 1 second at this time), and some applications require a high resolution (e.g., a resolution of 1 ms), so that some applications relying on a high-precision real-time clock on the terminal device cannot be realized.
Disclosure of Invention
In view of this, embodiments of the present application provide a method for implementing a real-time clock based on an RTC chip, a terminal device, and a computer-readable storage medium, so as to provide a high-precision real-time clock for an application of the terminal device on the premise of not replacing the RTC chip of the terminal device.
A first aspect of the embodiments of the present application provides a method for implementing a real-time clock based on an RTC chip, including:
controlling the RTC chip to generate interruption according to a preset period;
after the RTC chip is interrupted, initializing a preset initial value and a preset comparison value of a timer, and controlling the timer to start timing from the initial value;
and after receiving a reading request of the real-time clock, outputting a real-time clock value based on the first time of the RTC chip and the second time of the timer.
A second aspect of an embodiment of the present application provides a terminal device, including:
the interrupt control unit is used for controlling the RTC chip to generate interrupt according to a preset period;
the timing unit is used for initializing a preset initial value and a preset comparison value of a timer after the RTC chip is interrupted, and controlling the timer to start timing from the initial value;
and the clock output unit is used for outputting a real-time clock value based on the first time of the RTC chip and the second time of the timer after receiving a reading request of the real-time clock.
A third aspect of an embodiment of the present application provides a terminal device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, where the processor implements the steps of the method provided in the first aspect of the embodiment of the present application when executing the computer program.
A fourth aspect of embodiments of the present application provides a computer-readable storage medium storing a computer program which, when executed by one or more processors, performs the steps of the method provided by the first aspect of embodiments of the present application.
A fifth aspect of embodiments of the present application provides a computer program product comprising a computer program that, when executed by one or more processors, performs the steps of the method provided by the first aspect of embodiments of the present application.
The embodiment of the application provides a real-time clock implementation method based on an RTC chip, which comprises the steps of generating an interrupt by setting the RTC chip in a preset period, initializing a starting value and a comparison value of a preset timer after each interrupt is generated, and controlling the timer to start timing from the initial value; and after receiving a request of an application for reading a real-time clock, outputting a real-time clock value based on the first time of the RTC chip and the second time of the timer. The timing of low resolution is realized through the RTC chip, and the timing of high resolution is realized through the timer to under the prerequisite of not changing terminal equipment's RTC chip, provide high accuracy real-time clock for terminal equipment's application.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the embodiments or the prior art descriptions will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without inventive exercise.
Fig. 1 is a schematic flowchart of a method for implementing a real-time clock based on an RTC chip according to an embodiment of the present disclosure;
fig. 2 is a schematic flowchart of another method for implementing a real-time clock based on an RTC chip according to an embodiment of the present disclosure;
fig. 3 is a schematic block diagram of a terminal device provided in an embodiment of the present application;
fig. 4 is a schematic block diagram of another terminal device provided in an embodiment of the present application.
Detailed Description
In the following description, for purposes of explanation and not limitation, specific details are set forth, such as particular system structures, techniques, etc. in order to provide a thorough understanding of the embodiments of the present application. It will be apparent, however, to one skilled in the art that the present application may be practiced in other embodiments that depart from these specific details. In other instances, detailed descriptions of well-known systems, devices, circuits, and methods are omitted so as not to obscure the description of the present application with unnecessary detail.
It will be understood that the terms "comprises" and/or "comprising," when used in this specification and the appended claims, specify the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof.
It is also to be understood that the terminology used in the description of the present application herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the application. As used in the specification of the present application and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise.
It should be further understood that the term "and/or" as used in this specification and the appended claims refers to and includes any and all possible combinations of one or more of the associated listed items.
As used in this specification and the appended claims, the term "if" may be interpreted contextually as "when", "upon" or "in response to a determination" or "in response to a detection". Similarly, the phrase "if it is determined" or "if a [ described condition or event ] is detected" may be interpreted contextually to mean "upon determining" or "in response to determining" or "upon detecting [ described condition or event ]" or "in response to detecting [ described condition or event ]".
In order to explain the technical solution described in the present application, the following description will be given by way of specific examples.
Fig. 1 is a schematic flow chart of an implementation method of a real-time clock based on an RTC chip according to an embodiment of the present application, as shown in the figure, the method may include the following steps:
and step S101, controlling the RTC chip to generate interruption according to a preset period.
In this embodiment, the RTC chip may be a module built in the CPU chip; or an external chip. The RTC chip usually has a resolution, which refers to the minimum unit time for the RTC chip to count, for example: when the RTC chip generates a new timing value every second at the fastest time, the RTC resolution is 1 second. The RTC chip may be controlled to generate an interrupt at a time interval corresponding to the resolution.
Of course, in practical applications, it may also be configured that a time interval greater than the resolution is set to generate an interrupt, for example, the resolution of the RTC chip is 1s, and the RTC chip may be controlled to generate an interrupt every 2s, which is not limited herein.
Step S102, after the RTC chip is interrupted, initializing a preset starting value and a comparison value of a timer, and controlling the timer to start timing from the starting value.
In this embodiment of the present application, an RTC interrupt service routine may be set, and after the RTC chip generates an interrupt, the RTC interrupt service routine initializes a preset start value and a comparison value of a timer.
There may be several timers in the terminal equipment, from which one may be selected in advance, and the timers can count up or down from their initial values in fixed time beats. There are two kinds of timers: the timer is called an increment timer for starting to count up from a preset value, and the timer is called a decrement timer for starting to count down from a preset value.
In practical applications, a timing value register and a comparison value register may be provided, and a storage unit in the timer for recording the current time count value is called as a timing value register. The storage unit in the timer for storing the comparison threshold value is called a comparison value register. After the start value and the comparison value of the timer are initialized, the timer can be controlled to start timing from the start value.
Since the RTC chip generates interrupts according to a preset period, if the resolution of the RTC chip is 1s, the RTC chip may be set to generate interrupts every 1s, in order to obtain a more accurate real-time clock, the time duration from the starting value to the comparison value of the timer may be set to be equal to or as close as possible to the period of the RTC chip generating interrupts, if the target resolution to be obtained is 0.001s, the starting value may be set to 0, the comparison value may be 1000, the elapsed time is 0.001s every time the timer counts, of course, the starting value may be set to 0, the comparison value may be 2000, and the elapsed time is 0.0005s every time the timer counts. The above is only used as an example, and in practical applications, other starting values and comparison values may also be set.
As another embodiment of the present application, the initializing the start value and the comparison value of the preset timer includes:
acquiring the period of interruption generated by the RTC chip, the target resolution, the input clock frequency of the timer and the clock frequency division factor;
determining a starting value and a comparison value for the timer based on the period, the target resolution, an input clock frequency of the timer, and a clock division factor, wherein,
Figure BDA0001930058320000051
and is
Figure BDA0001930058320000052
S represents the total count number of times of the timer from the start value to the comparison value, S is associated with the start value Ns of the timer and the comparison value Ne of the timer, T represents the period of time when the RTC chip generates an interrupt, [ 2 ]]Representing a rounding operation, f representing the input clock frequency of the timer, k representing the clock division factor of the timer, R representing a target resolution, the duration of the timer from the start value to the comparison value being equal to the period of the RTC chip generating an interrupt.
In the embodiment of the application, k, T, f and R can be preset according to
Figure BDA0001930058320000053
And
Figure BDA0001930058320000054
determines the total number of counts of the timer from the starting value to the comparison value, finally determines the starting value or the comparison value, and then determines the comparison value from the determined starting value or determines the starting value from the determined comparison value.
The timer is essentially a clock-driven counter, and the counter changes once when the clock periodicity changes once. In practice, the clock also has an input clock frequency f and a clock division factor k, the actual clock frequency applied to the timer being f/k. That is, the elapsed time is k/f for every count of the timer. Wherein f and k are both manually set, and the setting standard is as follows: such that the selected clock frequency f and the clock division factor k satisfy: k/f is less than or equal to R, and R is the target resolution.
For example, the minimum period of interrupt generation of most RTC chips is 1 second, and assuming that T is 1 second, then
Figure BDA0001930058320000061
Can be combined with
Figure BDA0001930058320000062
And is recorded as Nc.
For clarity, the total number of times S (or Nc) the timer counts from the start value to the comparison value is related to the start value Ns of the timer and the comparison value Ne of the timer, which is illustrated by the following example. The timer is provided with a current timing value register and a comparison value register which are used for controlling the timing range. The comparison value register of some timers can be set by software; the comparison value register of some timers is not settable and fixed to 0. The following therefore occurs:
if the timer is incremented by one and the compare value register is set, the start value Ns is zero, the compare value Ne is T × Nc,
Figure BDA0001930058320000063
if the period T of the RTC chip generating interruption is 1 second, comparing a value Ne with Nc;
if the timer is in a timing increasing mode, the comparison value register cannot be set and is fixed to 0, and the maximum timing value of the timer is Nm (the typical value of Nm is 0xffffffff), the initial value Ns is Nm-T multiplied by Nc + 1; if the period T of the RTC chip generating interruption is 1 second, the initial value Ns is Nm-Nc + 1;
if the timer is in a one-off timing mode, the comparison value register can be set, and the maximum timing value of the timer is Nm, the initial value Ns is Nm, and the comparison value Ne is Nm-T multiplied by Nc; if the period T of interruption generated by the RTC chip is 1 second, comparing a value Ne with Nm-Nc;
if the timer is in a one-down timing mode, and the comparison value register is not settable and is fixed to 0, the initial value Ns is T × Nc; if the period T of the RTC chip generating the interrupt is 1 second, the start value Ns is Nc.
Certainly, in practical application, after the timing value of the timer reaches the comparison value, the starting value and the comparison value of the timer are initialized just when the RTC chip generates the next interrupt, and the timer just performs the next timing; if the comparison value of the timer is set to jump to the initial value by itself, the read real-time clock may be wrong, and therefore, in order to avoid accumulating errors and also in order to avoid a significant error in the timing value of the time adjacent to the time of the RTC interrupt, the timer is set to be in a single-shot mode.
As another embodiment of the present application, after controlling the timer to start timing from the starting value, the method further includes:
and if the current timing value of the timer is equal to the comparison value, controlling the timer to stop timing.
In the embodiment of the present application, the single-shot mode is: and if the current timing value of the timer is equal to the comparison value, controlling the timer to stop timing instead of automatically wrapping to the initial value.
Step S103, after receiving the reading request of the real-time clock, outputting the real-time clock value based on the first time of the RTC chip and the second time of the timer.
In this embodiment of the application, the RTC chip can realize low-resolution timing, and the timer can count time more finely while the RTC chip realizes low-resolution timing. By way of example, the RTC chip can count once per second, and the timer can count once every millisecond at an intermediate time when the RTC chip counts once per second. In this way, the first time (in the order of seconds) of the RTC chip can be obtained, the second time (in the order of milliseconds) of the timer can be obtained, and then the first time of the RTC chip and the second time of the timer can be combined together to obtain the real-time clock value. In practical application, the first time of the RTC chip and the second time of the timer can be read in real time and output in real time. Or two types of registers can be preset, the first time of the RTC chip is stored in the first type of register, the second time of the timer is stored in the second type of register, and finally the time in the two types of registers is output as a real-time clock value. In practical application, the first time of the RTC chip may also be stored in the structural variable, and when the real-time clock needs to be read each time, the second time of the timer is obtained, and the second time is also stored in the structural variable, and the time in the last structural variable is output as the real-time clock value. And are not intended to be limiting herein.
As another embodiment of the present application, before outputting a real-time clock value based on the first time of the RTC chip and the second time of the timer, the method further includes:
reading the current timing value of the timer and passing through the formula
Figure BDA0001930058320000081
Converting the current timing value of the timer into a second time;
wherein C represents a second time, [ ] represents a rounding operation, n represents a current timing value of the timer, Ns represents a start value of the timer, Ne represents a comparison value of the timer, T represents a period in which the RTC chip generates an interrupt, and R represents a target resolution.
In this embodiment, if the period of the RTC chip generating the interrupt is 1s, the target resolution is 1ms, and the timer needs to count 1000 times from the starting value to the comparison value, then how many times the timer passes from the initial value to the current timer indicates how many milliseconds. However, in practical applications, the period of the interrupt generated by the RTC chip may not be 1s, for example, 2s, the target resolution may not be 1ms, and the number of times that the timer needs to count from the starting value to the comparison value may not be 1000 times, and may be 5000 times. The second time represented by the timer is required to pass through a formula
Figure BDA0001930058320000082
And (6) calculating.
Fig. 2 is a schematic flow chart of an implementation method of another real-time clock based on an RTC chip according to an embodiment of the present application, where as shown in the figure, the method may include the following steps:
step S201, a structure variable for accessing a real-time clock value and a parallel read count variable are set, and the count variable is set as an initial value.
In the embodiment of the present application, the RTC chip and the timer may be initialized in advance, for example, a configuration variable may be set for accessing the real-time clock value. The structure variable includes the following fields: year, month, day, hour, minute, second, number of time segments, and total elapsed seconds. The time segment number may be a count value in units of target resolution, and the total elapsed seconds is the total seconds from when a certain time point (for example, 0 min 0 s at 1/0 in 1970) starts to the current time point.
A parallel read count variable may also be set and assigned with an initial value (which may be assigned to 0), where the count variable is used to indicate the number of applications currently reading the real-time clock. In a multitasking operating system, there may be multiple applications concurrently performing a read operation of the real-time clock. If the application a finishes reading first and starts the interrupt function of the RTC chip in advance without mistake, the application B that has not finished reading may have an RTC interrupt in the middle of reading, and thus B reads an erroneous clock value, that is: one part of data is before the RTC interrupt, and the other part of data is after the RTC interrupt.
For example, if the current clock is 999 minutes, 19 minutes and 1 second, 999 milliseconds at 11, 8 and 2018, B is interrupted by RTC interruption after reading the current clock for 19 minutes and 1 second at 11, 8, 11 and 2018, 19 minutes and 2 seconds at 11, 8 and 2018; b continues to read the clock 000 milliseconds below the second, the clock value finally read by B is 000 milliseconds at 11/8/11/19/1/4/2018, the correct value is 999 milliseconds at 19/1/19/11/2018, and the error reaches 999 milliseconds. The counting variable is used for indicating how many application programs are currently performing real-time clock reading operation and turning off the interrupt function of the RTC chip, and further used for controlling the RTC interrupt turning on to avoid reading errors possibly caused by the concurrent reading operation. Only when the reading of other application programs executing the real-time clock is finished, the application program starts the RTC interrupt when the reading is finished. Of course, it is required that after receiving the read request of the real-time clock, the interrupt function of the RTC chip is turned off first, and the reading operation is performed, which may be specifically described in the following steps.
Of course, the initialization process may also set the period of time that the RTC chip generates the interrupt. The frequency f of the driving clock of the timer and the clock division factor k may also be set. And a starting value and a comparison value of a timer can be set, and subsequently, after the RTC chip generates interrupt, the set starting value and the comparison value are assigned to corresponding registers.
Step S202, controlling the RTC chip to generate interrupt according to a preset period.
Step S203, initializing a preset starting value and a comparison value of a timer after the RTC chip generates an interrupt, and controlling the timer to start timing from the starting value.
The contents of steps S202 to S203 are the same as those of steps S101 to S102, and the description of step S102 may be specifically referred to, which is not repeated herein.
Step S204, reading the current timing value of the RTC chip, determining first time based on the current timing value of the RTC chip, and assigning the first time to the following fields in the structure variable: year, month, day, hour, minute, second, and total elapsed seconds.
In this embodiment, each time the RTC chip generates an interrupt, the RTC chip indicates that the time is increased by a preset period. The current timing value of the RTC chip can be read and the first time can be determined based on the current timing value of the RTC chip, and the timing value of the RTC chip can be obtained in various ways, for example: a subfield expression method and an integral expression method. The subfield expression method is to express the current clock information of year, month, day, hour, minute, second, etc. by using different registers, and the whole expression method is to express the current clock information as the total number of seconds elapsed from a specific time (for example: 0 minute 0 second at 1 month 1 day 0 in 1970).
If the real-time clock value of the current RTC chip is expressed by a field division method, reading time values of year, month, day, hour, minute, second and the like in sequence, and writing the time values into corresponding fields in the structure variable V respectively; the time value in this format is then converted to the total number of seconds elapsed since 0 minutes 0 seconds at 1 month 1 day 0 of 1970 and assigned to the elapsed total seconds field in the configuration variable.
If the real-time clock value of the current RTC chip is an integral expression method, reading a time value of the total elapsed seconds, and assigning the time value to the total elapsed seconds in the structural variable; thereafter, the elapsed time value is converted into time values expressed by a plurality of fields of year, month, day, hour, minute, second, etc., and written into corresponding fields in the configuration variables, respectively.
When the RTC chip generates interrupt every time, an interrupt mark is set, and after the real-time clock value of the RTC chip is read and written into the structure variable, the interrupt mark of the RTC chip is cleared. When the interrupt flag is in a set state, the RTC chip cannot generate interrupt, and after the interrupt flag is cleared, the RTC chip has the capability of generating interrupt again. In this way, access disturbances caused by nested triggering of the same interrupt source can be avoided.
In practical applications, the process from step S203 to step S205 can be completed soon after the RTC chip generates the interrupt in step S202, and the faster the process from step S203 to step S205 is completed, the more the accuracy of the real-time clock can be ensured.
Step S205, after receiving the read request of the real-time clock, turning off the interrupt function of the RTC chip, and adding 1 to the value corresponding to the count variable, where the interrupt function of the RTC chip cannot send an interrupt request in a turned-off state.
In the embodiment of the application, an API function may be set, and the application of the real-time clock to be read may call the API function to read the real-time clock value. After a read request of the real-time clock is received (for example, a request for calling an API function by an application is received), an interrupt function of the RTC chip needs to be turned off first, so as to avoid a read real-time clock error caused by an interrupt generated by the RTC chip in the reading process. In a multitasking operating system, there may be a plurality of applications concurrently performing a read operation of the real-time clock, and therefore, in order to ensure that the interrupt function of the RTC chip is turned on after the reading of all the applications is completed, a parallel read count variable may be set. Each time an application request to read the real time clock is received, the count variable is incremented by 1, thus determining how many applications are currently reading the real time clock.
Step S206, reading the current timing value of the timer, and converting the current timing value of the timer into a second time.
And step S207, assigning the first time to the number of time segments in the structure variable, and outputting each field in the structure variable as a real-time clock value.
In the embodiment of the present application, a time fragment number exists in the structure variable, and a second time of the timer is assigned to a field corresponding to the time fragment number in the structure variable. At this time, the time stored in the structure variable is the real-time clock value, and each field in the structure variable can be output as the real-time clock value.
And step S208, subtracting 1 from the value corresponding to the counting variable, and judging whether the counting variable is an initial value.
In step S209, if the counting variable is an initial value, the interrupt function of the RTC chip is turned on. And the interrupt function of the RTC chip sends an interrupt request according to a preset period in an opening state.
In this embodiment of the application, after each application reads a real-time clock value, correspondingly, a value corresponding to the counting variable is subtracted by 1, and if the timing variable becomes an initial value, for example, 0, it indicates that no application reads a real-time clock currently, and an interrupt function of the RTC chip may be started.
As an example, application a prepares to read the real-time clock value, after calling the API function, adds 1 to the value corresponding to the parallel read count variable, if the initial value of the counting variable is 0, the current value of the counting variable is 1, the application B prepares to read the real-time clock value and calls the API function, the value corresponding to the counting variable is incremented by 1 to 2, at which time application a has read the real time clock value, the counting variable may be decremented by 1 to 1, application C is ready to read the real time clock value, after calling the API function, adding 1 to the value corresponding to the counting variable to become 2, reading the real-time clock value at application B, the count variable may be decremented by 1 to become 1, and if the application C reads the real-time clock value, the count variable may be decremented by 1 to become 0, and at this time, the count variable becomes an initial value of 0, and the interrupt function of the RTC chip may be turned on.
In the embodiment of the application, the RTC chip triggers interruption every time when the timing is completed by one period; and immediately starting the single-click mode timing of the timer after the interruption is triggered, and restarting the timing by the timer at the moment. This means that the RTC chip and the timer start counting from the same time 0 seconds, i.e.: each second of timing achieves synchronization. When the application program reads the real-time clock value, the real-time clock value is a multi-field structure variable, so that in a common CPU, reading can be finished only by adopting a plurality of instructions without reading a machine instruction. In order to ensure that the middle of the reading operation is not modified by RTC interruption, a method of closing RTC interruption service and then reading is adopted, so that the synchronization of each field value in the read data is ensured.
It should be understood that, the sequence numbers of the steps in the foregoing embodiments do not imply an execution sequence, and the execution sequence of each process should be determined by its function and inherent logic, and should not constitute any limitation to the implementation process of the embodiments of the present application.
Fig. 3 is a schematic block diagram of a terminal device according to an embodiment of the present application, and only a part related to the embodiment of the present application is shown for convenience of description.
The terminal device 3 may be a software unit, a hardware unit, or a combination of software and hardware in a computer connected to an existing terminal device such as a mobile phone, a computer, or a POS device. Or as a stand-alone terminal device.
The terminal device 3 includes:
an interrupt control unit 31, configured to control the RTC chip to generate an interrupt according to a preset period;
the timing unit 32 is configured to initialize a preset starting value and a comparison value of a timer after the RTC chip generates an interrupt, and control the timer to start timing from the starting value;
the clock output unit 33 is configured to output a real-time clock value based on the first time of the RTC chip and the second time of the timer after receiving a read request of the real-time clock.
As another embodiment of the present application, the timing unit 32 includes:
a determining module 321, configured to obtain a period of interrupt generation of the RTC chip, a target resolution, an input clock frequency of the timer, and a clock frequency division factor;
an initialization module 322 for determining a start value and a comparison value for the timer based on the period, the target resolution, an input clock frequency of the timer, and a clock division factor, wherein,
Figure BDA0001930058320000131
and is
Figure BDA0001930058320000132
S represents the total count number of times of the timer from the start value to the comparison value, S is associated with the start value Ns of the timer and the comparison value Ne of the timer, T represents the period of time when the RTC chip generates an interrupt, [ 2 ]]Representing a rounding operation, f representing the input clock frequency of the timer, k representing the clock division factor of the timer, R representing a target resolution, the duration of the timer from the start value to the comparison value being equal to the period of the RTC chip generating an interrupt.
As another embodiment of the present application, the terminal device 3 further includes:
a second time obtaining unit 34, configured to read a current timing value of the timer,and by the formula
Figure BDA0001930058320000133
Converting the current counting value of the timer into a second time,
wherein C represents a second time, [ ] represents a rounding operation, n represents a current timing value of the timer, Ns represents a start value of the timer, Ne represents a comparison value of the timer, T represents a period in which the RTC chip generates an interrupt, and R represents a target resolution.
As another embodiment of the present application, the terminal device 3 further includes:
an interrupt function shutdown unit 35, configured to shut down an interrupt function of the RTC chip before reading a current timing value of the timer, where the interrupt function of the RTC chip cannot generate an interrupt in a shutdown state;
and an interrupt function starting unit 36, configured to start an interrupt function of the RTC chip after the real-time clock value is output, where the interrupt function of the RTC chip generates an interrupt according to a preset period in a starting state.
As another embodiment of the present application, the terminal device 3 further includes:
the initialization unit 37 is configured to set a parallel read count variable and set the count variable as an initial value before controlling the RTC chip to generate an interrupt according to a preset period;
the interrupt function shutdown unit 35 is further configured to:
before reading the current timing value of the timer, adding 1 to the value corresponding to the counting variable;
the interrupt function starting unit 36 is further configured to:
before starting the interrupt function of the RTC chip, subtracting 1 from the value corresponding to the counting variable, and judging whether the counting variable is an initial value;
and if the counting variable is an initial value, starting an interrupt function of the RTC chip.
As another embodiment of the present application, the timing unit 32 is further configured to:
and if the current timing value of the timer is equal to the comparison value, controlling the timer to stop timing.
As another embodiment of the present application, the initialization unit 37 is further configured to:
before controlling the RTC chip to generate interruption according to a preset period, setting a structure variable for accessing a real-time clock value, wherein the structure variable comprises the following fields: year, month, day, hour, minute, second, number of time segments and total elapsed seconds;
the terminal device 3 further includes:
a first time obtaining unit 38, configured to read a current timing value of the RTC chip after the RTC chip generates an interrupt, determine a first time based on the current timing value of the RTC chip, and assign the first time to the following fields in the structure variable: year, month, day, hour, minute, second, and total elapsed seconds;
the clock output unit 33 is further configured to:
and assigning the first time to the number of time segments in the structure variable, and outputting each field in the structure variable as a real-time clock value.
It is obvious to those skilled in the art that, for convenience and simplicity of description, the foregoing division of the functional units and modules is merely used as an example, and in practical applications, the foregoing function distribution may be performed by different functional units and modules as needed, that is, the internal structure of the terminal device is divided into different functional units or modules to perform all or part of the above-described functions. Each functional unit and module in the embodiments may be integrated in one processing unit, or each unit may exist alone physically, or two or more units are integrated in one unit, and the integrated unit may be implemented in a form of hardware, or in a form of software functional unit. In addition, specific names of the functional units and modules are only for convenience of distinguishing from each other, and are not used for limiting the protection scope of the present application. The specific working processes of the units and modules in the terminal device may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
Fig. 4 is a schematic block diagram of a terminal device according to another embodiment of the present application. As shown in fig. 4, may include: one or more processors 40, a memory 41, and a computer program 42 stored in the memory 41 and executable on the processors 40. The processor 40, when executing the computer program 42, implements the steps in the various method embodiments described above, such as the steps S101 to S103 shown in fig. 1. Alternatively, the processor 40, when executing the computer program 42, implements the functions of the modules/units in the terminal device embodiments described above, such as the functions of the modules 31 to 33 shown in fig. 3.
Illustratively, the computer program 42 may be partitioned into one or more modules/units that are stored in the memory 41 and executed by the processor 40 to accomplish the present application. The one or more modules/units may be a series of computer program instruction segments capable of performing specific functions, which are used to describe the execution process of the computer program 42 in the terminal device 4. For example, the computer program 42 may be divided into an interrupt control unit, a timing unit, a clock output unit. Specific examples are as follows:
the interrupt control unit is used for controlling the RTC chip to generate interrupt according to a preset period;
the timing unit is used for initializing a preset initial value and a preset comparison value of a timer after the RTC chip is interrupted, and controlling the timer to start timing from the initial value;
and the clock output unit is used for outputting a real-time clock value based on the first time of the RTC chip and the second time of the timer after receiving a reading request of the real-time clock.
Other units or modules can be referred to the description of the embodiment shown in fig. 3, and are not described again here.
The terminal device includes, but is not limited to, a processor 40, and a memory 41. Those skilled in the art will appreciate that fig. 4 is only one example of a terminal device 4 and does not constitute a limitation of terminal device 4 and may include more or fewer components than shown, or some components may be combined, or different components, for example, the terminal device may also include an input device, an output device, a network access device, a bus, etc.
The Processor 40 may be a Central Processing Unit (CPU), other general purpose Processor, a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), an off-the-shelf Programmable Gate Array (FPGA) or other Programmable logic device, discrete Gate or transistor logic device, discrete hardware component, etc. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
The memory 41 may be an internal storage unit of the terminal device 4, such as a hard disk or a memory of the terminal device 4. The memory 41 may also be an external storage device of the terminal device 4, such as a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) Card, a Flash memory Card (Flash Card), and the like, which are provided on the terminal device 4. Further, the memory 41 may also include both an internal storage unit and an external storage device of the terminal device 4. The memory 41 is used for storing the computer program and other programs and data required by the terminal device. The memory 41 may also be used to temporarily store data that has been output or is to be output.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
Those of ordinary skill in the art will appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present application.
In the embodiments provided in the present application, it should be understood that the disclosed terminal device and method may be implemented in other ways. For example, the above-described terminal device embodiments are merely illustrative, and for example, the division of the modules or units is only one logical function division, and there may be other divisions when actually implemented, for example, a plurality of units or components may be combined or may be integrated into another system, or some features may be omitted or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present application may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The integrated modules/units, if implemented in the form of software functional units and sold or used as separate products, may be stored in a computer readable storage medium. Based on such understanding, all or part of the flow in the method of the embodiments described above can be realized by a computer program, which can be stored in a computer-readable storage medium and can realize the steps of the embodiments of the methods described above when the computer program is executed by a processor. Wherein the computer program comprises computer program code, which may be in the form of source code, object code, an executable file or some intermediate form, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, usb disk, removable hard disk, magnetic disk, optical disk, computer Memory, Read-Only Memory (ROM), Random Access Memory (RAM), electrical carrier wave signals, telecommunications signals, software distribution medium, and the like. It should be noted that the computer readable medium may contain other components which may be suitably increased or decreased as required by legislation and patent practice in jurisdictions, for example, in some jurisdictions, computer readable media which may not include electrical carrier signals and telecommunications signals in accordance with legislation and patent practice.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present application, and not for limiting the same; although the present application has been described in detail with reference to the foregoing embodiments, it should be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; such modifications and substitutions do not substantially depart from the spirit and scope of the embodiments of the present application and are intended to be included within the scope of the present application.

Claims (9)

1. A real-time clock implementation method based on an RTC chip is characterized by comprising the following steps:
controlling the RTC chip to generate interruption according to a preset period;
after the RTC chip is interrupted, initializing a preset initial value and a preset comparison value of a timer, and controlling the timer to start timing from the initial value;
after receiving a reading request of a real-time clock, outputting a real-time clock value based on the first time of the RTC chip and the second time of the timer;
wherein the second time of the timer is obtained by:
reading the current timing value of the timer and passing through the formula
Figure FDA0002594629640000011
Converting the current counting value of the timer into a second time,
wherein C represents a second time, [ ] represents a rounding operation, n represents a current timing value of the timer, Ns represents a start value of the timer, Ne represents a comparison value of the timer, T represents a period of interruption generated by the RTC chip, and R represents a target resolution.
2. The method of claim 1, wherein initializing the preset start value and the preset comparison value of the timer comprises:
acquiring the period of interruption generated by the RTC chip, the target resolution, the input clock frequency of the timer and the clock frequency division factor;
determining a starting value and a comparison value for the timer based on the period, the target resolution, an input clock frequency of the timer, and a clock division factor, wherein,
Figure FDA0002594629640000012
and is
Figure FDA0002594629640000013
S represents the total count number of times of the timer from the start value to the comparison value, S is associated with the start value Ns of the timer and the comparison value Ne of the timer, T represents the period of time when the RTC chip generates an interrupt, [ 2 ]]Representing a rounding operation, f representing the input clock frequency of the timer, k representing the clock division factor of the timer, R representing a target resolution, the duration of the timer from the start value to the comparison value being equal to the period of the RTC chip generating an interrupt.
3. The method of claim 1, wherein prior to reading the current timing value of the timer, the method further comprises:
and turning off the interrupt function of the RTC chip, wherein the interrupt function of the RTC chip cannot generate interrupt in the off state.
4. The method for implementing the real-time clock based on the RTC chip as claimed in claim 3, further comprising, before controlling the RTC chip to generate the interrupt according to the preset period:
setting a parallel reading counting variable and setting the counting variable as an initial value;
correspondingly, before reading the current counting value of the timer, the method further comprises the following steps:
adding 1 to a value corresponding to the counting variable;
correspondingly, before starting the interrupt function of the RTC chip, the method further includes:
subtracting 1 from the value corresponding to the counting variable, and judging whether the counting variable is an initial value;
and if the counting variable is an initial value, starting an interrupt function of the RTC chip.
5. The method for implementing the real-time clock based on the RTC chip as claimed in any one of claims 1 to 4, further comprising, after controlling the timer to start counting from the starting value:
and if the current timing value of the timer is equal to the comparison value, controlling the timer to stop timing.
6. The method for implementing the real-time clock based on the RTC chip as claimed in any one of claims 1 to 4, wherein before controlling the RTC chip to generate the interrupt according to the preset period, the method further comprises:
setting a structure variable for accessing a real time clock value, the structure variable comprising the following fields: year, month, day, hour, minute, second, number of time segments and total elapsed seconds;
correspondingly, after the RTC chip generates an interrupt, the method further includes:
reading a current timing value of the RTC chip, determining a first time based on the current timing value of the RTC chip, and assigning the first time to the following fields in the structure variable: year, month, day, hour, minute, second, and total elapsed seconds;
correspondingly, the outputting a real-time clock value based on the first time of the RTC chip and the second time of the timer includes:
and assigning the first time to the number of time segments in the structure variable, and outputting each field in the structure variable as a real-time clock value.
7. A terminal device, comprising:
the interrupt control unit is used for controlling the RTC chip to generate interrupt according to a preset period;
the timing unit is used for initializing a preset initial value and a preset comparison value of a timer after the RTC chip is interrupted, and controlling the timer to start timing from the initial value;
a second time obtaining unit for reading the current timing value of the timer and passing through the formula
Figure FDA0002594629640000031
Converting the current counting value of the timer into a second time,
wherein, C represents a second time, [ ] represents a rounding operation, n represents a current timing value of the timer, Ns represents an initial value of the timer, Ne represents a comparison value of the timer, T represents a period of interruption generated by the RTC chip, and R represents a target resolution;
and the clock output unit is used for outputting a real-time clock value based on the first time of the RTC chip and the second time of the timer after receiving a reading request of the real-time clock.
8. A terminal device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that the processor implements the steps of the method according to any of claims 1 to 6 when executing the computer program.
9. A computer-readable storage medium, characterized in that the computer-readable storage medium stores a computer program which, when executed by one or more processors, implements the steps of the method according to any one of claims 1 to 6.
CN201811636003.9A 2018-12-29 2018-12-29 Real-time clock implementation method based on RTC chip, terminal equipment and storage medium Active CN109782841B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201811636003.9A CN109782841B (en) 2018-12-29 2018-12-29 Real-time clock implementation method based on RTC chip, terminal equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201811636003.9A CN109782841B (en) 2018-12-29 2018-12-29 Real-time clock implementation method based on RTC chip, terminal equipment and storage medium

Publications (2)

Publication Number Publication Date
CN109782841A CN109782841A (en) 2019-05-21
CN109782841B true CN109782841B (en) 2020-09-11

Family

ID=66498908

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201811636003.9A Active CN109782841B (en) 2018-12-29 2018-12-29 Real-time clock implementation method based on RTC chip, terminal equipment and storage medium

Country Status (1)

Country Link
CN (1) CN109782841B (en)

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111580378B (en) * 2020-04-27 2021-11-23 广东尚研电子科技有限公司 Off-line high-precision clock control method and clock device
CN114884602B (en) * 2022-03-22 2023-05-30 广州汽车集团股份有限公司 Clock synchronization control method and system
CN115022204B (en) * 2022-05-26 2023-12-05 阿里巴巴(中国)有限公司 RTC transmission delay detection method, device and equipment

Family Cites Families (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6408397B1 (en) * 1999-05-24 2002-06-18 Dell Usa, L.P. Using RTC wake-up to enable recovery from power failures
CN106502312B (en) * 2016-10-08 2020-01-14 南京熊猫电子股份有限公司 Design method of high-precision clock synchronization equipment
CN106455039B (en) * 2016-11-08 2019-08-23 石家庄科林电气股份有限公司 Communication terminal, network and the method for clock synchronization precision are improved in a kind of power distribution network
CN107219395B (en) * 2017-05-23 2019-06-18 郑州云海信息技术有限公司 A kind of detection method of RTC crystal oscillator
CN107765760B (en) * 2017-11-17 2021-05-04 上海伟世通汽车电子***有限公司 RTC module clock source dynamic calibration method and RTC module clock source dynamic calibration system in instrument sleep mode
CN108304031A (en) * 2018-03-07 2018-07-20 杭州先锋电子技术股份有限公司 A kind of clock travel time system, method, equipment and computer storage media

Also Published As

Publication number Publication date
CN109782841A (en) 2019-05-21

Similar Documents

Publication Publication Date Title
CN109782841B (en) Real-time clock implementation method based on RTC chip, terminal equipment and storage medium
CN111130510B (en) Method and device for outputting second pulse signal
US20210357235A1 (en) Method, device, apparatus for identifying graphics card of gpu server and medium
US8458506B2 (en) Real time clock and method for recording data in real time clock
CN109445954B (en) Timing task execution method, terminal device and storage medium
CN111831056A (en) Real-time clock calibration module and method and real-time clock chip
CN111323626A (en) Uncovering detection circuit integrated in MCU and uncovering detection method based on MCU
CN112328289B (en) Firmware upgrading method, device, equipment and storage medium
US4330840A (en) Multi-function electronic digital watch
CN212159910U (en) MCU integrated uncapping detection circuit and electric energy meter control chip
WO2024103739A1 (en) Arm server and starting method and apparatus therefor, and readable storage medium
CN109799872B (en) Method and device for improving awakening precision of low-resolution real-time clock and electronic equipment
CN110134219B (en) Application program background power consumption processing method and device
CN109213589B (en) Message processing method and device
CN111722671B (en) Timing method, timing device, terminal equipment and storage medium
CN111175573A (en) Method, device, equipment and medium for detecting alternating voltage frequency
CN110716747B (en) Program operation efficiency optimization method based on function parameter statistics and terminal equipment
EP0511573A2 (en) Clock with clock adjusting data memory
CN115826731B (en) Sleep control method and device, storage medium and computing equipment
CN112558458B (en) Bluetooth clock calculation circuit, Bluetooth clock calculation method, medium, and device
CN115729313A (en) Method and device for calibrating real-time clock and electronic equipment
US4468133A (en) Electronic timepiece
KR101917526B1 (en) Real time clock and method for input/output of time information using the same
CN115292189A (en) Vehicle testing method and device, electronic equipment and storage medium
CN115166284A (en) Frequency signal acquisition system and frequency signal acquisition method

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant