CN105717979A - Clock device based on timestamp and counters and achieving method thereof - Google Patents
Clock device based on timestamp and counters and achieving method thereof Download PDFInfo
- Publication number
- CN105717979A CN105717979A CN201610067032.2A CN201610067032A CN105717979A CN 105717979 A CN105717979 A CN 105717979A CN 201610067032 A CN201610067032 A CN 201610067032A CN 105717979 A CN105717979 A CN 105717979A
- Authority
- CN
- China
- Prior art keywords
- enumerator
- time
- value
- year
- natural law
- 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.)
- Pending
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F1/00—Details not covered by groups G06F3/00 - G06F13/00 and G06F21/00
- G06F1/04—Generating or distributing clock signals or signals derived directly therefrom
- G06F1/14—Time supervision arrangements, e.g. real time clock
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Electric Clocks (AREA)
Abstract
The invention discloses a clock device based on a timestamp and counters and an achieving method thereof.The clock device comprises the first counter and the second counter which are sequentially connected.The first counter serves as a prescaler of the second counter, and each counting period of the first counter can produce a carry signal for the second counter to drive the second counter to add one.After each counting period is finished, the first counter automatically reloads and begins the next counting period.The clock device has the advantages that the pure hardware clock can achieve very high clock precision (millisecond, microsecond and nanosecond), and the system time reading method can prevent reading errors caused by critical problems so that reading can be more reliable and high in timeliness; the timestamp is set to be in year 2,000, calculation can be simplified, and the time expression range can be enlarged; the RTC function can be achieved on a hardware platform without RTC.
Description
Technical field
The present invention relates to system clock technical field, be specifically related to a kind of clock apparatus based on timestamp sum counter and its implementation.
Background technology
The present invention refer to two kinds of times:
(1) calendar time: time form intuitively, is year, month, day, hour, min, second, millisecond by time representation;
(2) system time: based on the time representation form of timestamp, is the number of seconds+millisecond number starting experience from timestamp by time representation, is the form in the embedded system internal representation time;
The both time, it is merely meant that form is different, may serve to represent the time in a certain moment, and the time of both forms can change mutually.
When needed in target Embedded Application, it is necessary to hardware or software provide a kind of time mechanism, and system needs the clock providing a real time execution to call at any time for application program, and often this clock needs to comprise the information such as date Hour Minute Second.
Existing a solution is the real-time clock (Real-TimeClock, hereinafter referred to as RTC) using chip internal or outside.The advantage of this mode is fairly simple, and application program can directly read calendar time, it is not necessary to extra software support.But there is also following shortcoming:
1. available clock accuracy is relatively low, and general RTC is merely able to provide the clock being accurate to the second, for some need to be accurate to millisecond and higher precision should be just inapplicable in this way;
2. some chip does not have RTC block truly, uses outside RTC can increase hardware cost undoubtedly;
3. use outside RTC that bus can be brought to read to postpone, cause that the clock real-time read reduces.
Another solution existing is to use timer interruption to be aided with carry algorithm to safeguard one section of internal storage data, comprises the information of date Hour Minute Second in data, and intervalometer often interrupts once making clock data add one accordingly.Chip is required minimum by this mode, and any chip having intervalometer all can realize.But the problem that this as easy as rolling off a log generation of the mode based on interruption+global variable is critical and concurrent, if software is not controlled by, the clock that application program reads is likely to an incomplete clock, it has not been convenient to application program calls at any time, is not suitable for the application that clock request is stricter.
Summary of the invention
The purpose of the present invention is contemplated to solve the problems referred to above, propose a kind of clock apparatus based on timestamp sum counter and its implementation, this device and method is hardware clock completely, running is without software intervention, it is adaptable to high-precision applications, in real time application and the application scenario without hardware RTC.
To achieve these goals, the present invention adopts the following technical scheme that
A kind of clock apparatus based on timestamp sum counter, including: the first enumerator being sequentially connected with and the second enumerator;Described first enumerator is as the pre-divider of the second enumerator, and each count cycle of the first enumerator all can produce a carry signal to the second enumerator and drive the second enumerator to add 1;Described first enumerator is automatic heavy cartridges after the count cycle terminates, and automatically begins to the next count cycle.
The count cycle of the first enumerator is arranged to 1S, the value of the second enumerator is used for representing second value, the value of the first enumerator is used for extrapolating the value of millisecond, microsecond, nanosecond or higher time precision, and namely system time represents with the first Counter Value and the second Counter Value.
The enumerator of more than 16 selected by described first enumerator when needing millisecond, microsecond precision, selects the enumerator of more than 32 when needing nanosecond above precision;The enumerator of more than 32 selected by described second enumerator.
A kind of clock apparatus based on timestamp sum counter realize method, including:
The count cycle of the first enumerator is arranged to 1S, and namely per second to second enumerator one carry signal of generation, the second enumerator is per second adds 1;
When reading system time, read continuously twice, it is judged that whether the value of the second enumerator that twi-read arrives is consistent, if consistent, take the value read for the first time, if inconsistent, take the value that second time is read;
The data structure of definition calendar time, carries out the conversion to calendar time of the conversion from calendar time to system time and system time respectively.
Timestamp is defined as during 1 day 0 January in 2000 0 point 0 second 0 millisecond.
Described system time to calendar time conversion method particularly as follows:
Convert the value of the second enumerator in system time to the year, month, day, hour, min in calendar time, second;
Convert the value of the first enumerator in system time to the millisecond in calendar time, microsecond or nanosecond;
The time value that the year, month, day, hour, min calculated according to the second enumerator, second and the first enumerator obtain is combined, obtains calendar time.
Second value according to the second enumerator, calculates respectively from the year, month, day, hour, min of self defined time stamp beginning, the value of second;
Concrete grammar is as follows:
Calculate the number of minutes according to second value, calculate hourage according to the number of minutes, calculate natural law according to hourage, calculate year number according to natural law, then calculate moon number and natural law then then;
Wherein, according to natural law calculate year number time, it is assumed that all years are all non-leap years, calculate the whole year number and residue natural law m;
Calculate the leap year number n in number in the whole year;
Calculate year number: judge that whether residue natural law m is less than leap year number n;If it is, year number=whole year number, natural law then=residue natural law m;Otherwise, year number=whole year number-1;
Judge whether upper one year is the leap year, if it is, natural law=m+366-n then;Otherwise, natural law=m+365-n then.
Described calendar time to system time conversion method particularly as follows:
Convert the year, month, day, hour, min in calendar time, second to the number of seconds starting experience from timestamp namely the value of the second enumerator;
By the millisecond in calendar time, microsecond or nanosecond value convert the value of the first enumerator to.
Convert the year, month, day, hour, min in calendar time, second to start from self defined time stamp number of seconds;
Detailed process is:
Calculate according to calendar time and stab, from self defined time, the natural law that beginning number in the whole year is corresponding;
The cumulative natural law that the whole month number is corresponding then, described moon number then does not include this month;
Cumulative of that month natural law obtains total natural law, and the natural law in described this month does not include the same day;
The number of seconds that number of whole days is corresponding is calculated according to the total natural law calculated;
The number of seconds on the cumulative same day obtains total number of seconds, is the number of seconds started from self defined time stamp.
The invention has the beneficial effects as follows:
The clock that the present invention realizes is hardware clock completely, and clock running is without software intervention;
The clock accuracy that the present invention realizes is high, it is possible to be accurate to millisecond even higher, it may be achieved significantly high clock accuracy, and concrete precision is certainly in the clock source of intervalometer.
The clock real-time that the present invention realizes is high, only needs 4 read operations can obtain clock value.
The system time read method of the present invention is prevented from the error in reading that critical problem brings, and makes reading more accurately reliable.
Timestamp is set as 2000 by the present invention, so can simplify calculating, it is also possible to increase time representation scope;
The present invention can realize RTC function on the hardware platform without hardware RTC.
Accompanying drawing explanation
Fig. 1 is clock apparatus structural representation of the present invention;
Fig. 2 is the flow chart that the present invention obtains system time;
Fig. 3 is present invention conversion method flow chart of value from calendar time to second;
Fig. 4 is that the present invention is worth the conversion method flow chart of calendar time from the second;
Fig. 5 is that the present invention counts to year number and remains the conversion method flow chart of natural law then from sky.
Detailed description of the invention:
Below in conjunction with accompanying drawing, the present invention will be further described with embodiment:
A kind of clock apparatus based on timestamp sum counter, as it is shown in figure 1, include: the first enumerator being sequentially connected with and the second enumerator;First enumerator can as the pre-divider of the second enumerator, and each count cycle can produce a carry signal to the second enumerator and drive the second enumerator to add 1, and this process hardware completes, it is not necessary to software intervention;First enumerator after the count cycle terminates can heavy cartridges automatically, automatically begin to the next count cycle.
The count cycle of the first enumerator is arranged to 1S, and namely per second to second enumerator one carry signal of generation, the second enumerator is per second adds 1.Namely the value of the second enumerator can be used to represent second value, the value of the first enumerator can be used to extrapolate millisecond or the microsecond of higher precision, nanosecond value, the present embodiment illustrates for millisecond.
Namely system time represents with the first Counter Value and the second Counter Value.In order to ensure that longer gate time the second enumerator is necessary for more than 32 enumerators.
Because the count cycle of the first enumerator is 1S, the value of the first enumerator becomes the proportional relation of time conversion with actual time value, utilizes the value of the first enumerator can extrapolate the clock value of millisecond, microsecond or nano second precision.The clock accuracy that first enumerator can be extrapolated is determined by clock source, and the bit wide of the first enumerator is determined by the clock accuracy needed for user.If desired ensure millisecond, precision first enumerator of microsecond is necessary for more than 16 enumerators, if desired ensures that precision first enumerator of nanosecond is necessary for more than 32 enumerators.
A kind of clock apparatus based on timestamp sum counter realize method, including:
(1) count cycle of the first enumerator is arranged to 1S, the value of such second enumerator is used for representing the second, the value of the first enumerator can be used to extrapolate millisecond (or higher precision), and namely system time represents with the first Counter Value and the second Counter Value;
(2) timestamp be defined as during 1 day 0 January in 2000 0 point 0 second 0 millisecond, so can simplified operation, it is also possible to increase time representation scope;
(3) the Clockreading method of critical problem is prevented;Concrete grammar is as in figure 2 it is shown, when reading system time, read the first Counter Value and the second Counter Value respectively, repeat said process, judge that whether the second value of the second enumerator that twi-read arrives is consistent, if consistent, take the second value that first time reads, if inconsistent, take the second value read for the second time;
(4) conversion between system time and calendar time is carried out.
(1) conversion from calendar time to system time, including:
Year, month, day, hour, min in calendar time, second are converted to the value (from the number of seconds that timestamp starts) of the second enumerator in system time;Specifically as shown in Figure 3:
Calculate stabbing from self defined time according to calendar time, the natural law that calculating number in the whole year is corresponding, calculating process to consider the process to the leap year;
The natural law of cumulative the whole month then, described moon number then does not include this month;
Cumulative of that month natural law obtains total natural law, and the natural law in described this month does not include the same day;
The number of seconds that number of whole days is corresponding is calculated according to the total natural law calculated;
The number of seconds on the cumulative same day obtains total number of seconds, is the number of seconds started from self defined time stamp.
Millisecond in calendar time converts to the value of the first enumerator in system time, and both are proportional relation.
(2) conversion from system time to calendar time, including:
Convert the value of the second enumerator in system time to the year, month, day, hour, min in calendar time, second;As shown in Figure 4, calculate the number of minutes according to second value, calculate hourage according to the number of minutes, calculate natural law according to hourage, calculate year number and remain natural law then according to natural law, calculating process to consider the process to the leap year, as shown in Figure 5;Moon number and of that month natural law then is calculated further according to remaining natural law then.
In Fig. 5, according to natural law calculate year number time, it is assumed that all years are all non-leap years, calculate the whole year number and residue natural law m;
Calculate the leap year number n in number in the whole year;
Calculate year number: judge that whether residue natural law m is less than leap year number n;If it is, year number=whole year number, natural law then=residue natural law m;Otherwise, year number=whole year number-1;
Judge whether upper one year is the leap year, if it is, natural law=m+366-n then;Otherwise, natural law=m+365-n then.
Converting the value of the first enumerator in system time to millisecond in calendar time, both are proportional relation.
It should be noted that on the basis that time precision is millisecond of the first enumerator, those skilled in the art are fully able to the method according to the present embodiment and draw the method that time precision is microsecond, nanosecond or higher time precision of the first enumerator;Further, after obtaining microsecond, nanosecond or higher time precision, according to time conversion relation, it is possible to obtain the random time accuracy value that second value is following.Such as, calculate according to the first enumerator and obtain microsecond value, then according to time conversion relation, it is possible to obtain millisecond value.
The specific embodiment of the present invention is described in conjunction with accompanying drawing although above-mentioned; but not limiting the scope of the invention; one of ordinary skill in the art should be understood that; on the basis of technical scheme, those skilled in the art need not pay various amendments or deformation that creative work can make still within protection scope of the present invention.
Claims (9)
1., based on a clock apparatus for timestamp sum counter, it is characterized in that, including: the first enumerator being sequentially connected with and the second enumerator;Described first enumerator is as the pre-divider of the second enumerator, and each count cycle of the first enumerator all can produce a carry signal to the second enumerator and drive the second enumerator to add 1;Described first enumerator is automatic heavy cartridges after the count cycle terminates, and automatically begins to the next count cycle.
2. a kind of clock apparatus based on timestamp sum counter as claimed in claim 1, it is characterized in that, the count cycle of the first enumerator is arranged to 1S, the value of the second enumerator is used for representing second value, the value of the first enumerator is used for extrapolating the value of millisecond, microsecond, nanosecond or higher time precision, and namely system time represents with the first Counter Value and the second Counter Value.
3. a kind of clock apparatus based on timestamp sum counter as claimed in claim 2, is characterized in that, the enumerator of more than 16 selected by described first enumerator when needing millisecond, microsecond precision, selects the enumerator of more than 32 when needing nanosecond above precision;The enumerator of more than 32 selected by described second enumerator.
4. the clock apparatus based on timestamp sum counter as claimed in claim 1 realize a method, it is characterized in that, including:
The count cycle of the first enumerator is arranged to 1S, and namely per second to second enumerator one carry signal of generation, the second enumerator is per second adds 1;
When reading system time, read continuously twice, it is judged that whether the value of the second enumerator that twi-read arrives is consistent, if consistent, take the value read for the first time, if inconsistent, take the value that second time is read;
The data structure of definition calendar time, carries out the conversion to calendar time of the conversion from calendar time to system time and system time respectively.
5. as claimed in claim 4 a kind of clock apparatus based on timestamp sum counter realize method, it is characterized in that, timestamp is defined as during 1 day 0 January in 2000 0 point 0 second 0 millisecond.
6. as claimed in claim 4 a kind of clock apparatus based on timestamp sum counter realize method, it is characterized in that, described system time to calendar time conversion method particularly as follows:
Convert the value of the second enumerator in system time to the year, month, day, hour, min in calendar time, second;
Convert the value of the first enumerator in system time to the millisecond in calendar time, microsecond or nanosecond;
The time value that the year, month, day, hour, min calculated according to the second enumerator, second and the first enumerator obtain is combined, obtains calendar time.
7. as claimed in claim 6 a kind of clock apparatus based on timestamp sum counter realize method, it is characterized in that, be worth according to the second of the second enumerator, calculate respectively from self defined time stamp start year, month, day, hour, min, the value of second;
Concrete grammar is as follows:
Calculate the number of minutes according to second value, calculate hourage according to the number of minutes, calculate natural law according to hourage, calculate year number according to natural law, then calculate moon number and natural law then then;
Wherein, according to natural law calculate year number time, it is assumed that all years are all non-leap years, calculate the whole year number and residue natural law m;
Calculate the leap year number n in number in the whole year;
Calculate year number: judge that whether residue natural law m is less than leap year number n;If it is, year number=whole year number, natural law then=residue natural law m;Otherwise, year number=whole year number-1;
Judge whether upper one year is the leap year, if it is, natural law=m+366-n then;Otherwise, natural law=m+365-n then.
8. as claimed in claim 4 a kind of clock apparatus based on timestamp sum counter realize method, it is characterized in that, described calendar time to system time conversion method particularly as follows:
Convert the year, month, day, hour, min in calendar time, second to the number of seconds starting experience from timestamp namely the value of the second enumerator;
By the millisecond in calendar time, microsecond or nanosecond value convert the value of the first enumerator to.
9. as claimed in claim 8 a kind of clock apparatus based on timestamp sum counter realize method, it is characterized in that, the year, month, day, hour, min in calendar time, second are converted to the number of seconds started from self defined time stamp;
Detailed process is:
Calculate according to calendar time and stab, from self defined time, the natural law that beginning number in the whole year is corresponding;
The cumulative natural law that the whole month number is corresponding then, described moon number then does not include this month;
Cumulative of that month natural law obtains total natural law, and the natural law in described this month does not include the same day;
The number of seconds that number of whole days is corresponding is calculated according to the total natural law calculated;
The number of seconds on the cumulative same day obtains total number of seconds, is the number of seconds started from self defined time stamp.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201610067032.2A CN105717979A (en) | 2016-01-29 | 2016-01-29 | Clock device based on timestamp and counters and achieving method thereof |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201610067032.2A CN105717979A (en) | 2016-01-29 | 2016-01-29 | Clock device based on timestamp and counters and achieving method thereof |
Publications (1)
Publication Number | Publication Date |
---|---|
CN105717979A true CN105717979A (en) | 2016-06-29 |
Family
ID=56155452
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201610067032.2A Pending CN105717979A (en) | 2016-01-29 | 2016-01-29 | Clock device based on timestamp and counters and achieving method thereof |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN105717979A (en) |
Cited By (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN107659305A (en) * | 2017-10-19 | 2018-02-02 | 郑州云海信息技术有限公司 | A kind of clock count method, apparatus and medium based on CPLD |
CN108924461A (en) * | 2018-06-20 | 2018-11-30 | 斑马网络技术有限公司 | Method of video image processing and device |
CN108932007A (en) * | 2017-05-25 | 2018-12-04 | 纬颖科技服务股份有限公司 | Method for acquiring time stamp and computer device |
CN110308638A (en) * | 2019-07-05 | 2019-10-08 | 卢宇 | Perpetual calendar wrist-watch and watch dial setting method |
Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN1878252A (en) * | 2006-07-07 | 2006-12-13 | 天津三星电子显示器有限公司 | TV set opening time cumulation recording method |
CN203275896U (en) * | 2013-05-07 | 2013-11-06 | 贵州省计量测试院 | Low-cost subnanosecond-grade time interval detection circuit |
CN103576074A (en) * | 2012-08-06 | 2014-02-12 | 上海海尔集成电路有限公司 | Real-time clock module test device |
WO2016000352A1 (en) * | 2014-06-30 | 2016-01-07 | 中兴通讯股份有限公司 | Method and device for compensating time stamp of clock |
-
2016
- 2016-01-29 CN CN201610067032.2A patent/CN105717979A/en active Pending
Patent Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN1878252A (en) * | 2006-07-07 | 2006-12-13 | 天津三星电子显示器有限公司 | TV set opening time cumulation recording method |
CN103576074A (en) * | 2012-08-06 | 2014-02-12 | 上海海尔集成电路有限公司 | Real-time clock module test device |
CN203275896U (en) * | 2013-05-07 | 2013-11-06 | 贵州省计量测试院 | Low-cost subnanosecond-grade time interval detection circuit |
WO2016000352A1 (en) * | 2014-06-30 | 2016-01-07 | 中兴通讯股份有限公司 | Method and device for compensating time stamp of clock |
Cited By (6)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN108932007A (en) * | 2017-05-25 | 2018-12-04 | 纬颖科技服务股份有限公司 | Method for acquiring time stamp and computer device |
CN108932007B (en) * | 2017-05-25 | 2021-05-11 | 纬颖科技服务股份有限公司 | Method for acquiring time stamp and computer device |
CN107659305A (en) * | 2017-10-19 | 2018-02-02 | 郑州云海信息技术有限公司 | A kind of clock count method, apparatus and medium based on CPLD |
CN108924461A (en) * | 2018-06-20 | 2018-11-30 | 斑马网络技术有限公司 | Method of video image processing and device |
CN108924461B (en) * | 2018-06-20 | 2021-08-13 | 斑马网络技术有限公司 | Video image processing method and device |
CN110308638A (en) * | 2019-07-05 | 2019-10-08 | 卢宇 | Perpetual calendar wrist-watch and watch dial setting method |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN105717979A (en) | Clock device based on timestamp and counters and achieving method thereof | |
CN102929735B (en) | A kind of method and apparatus of clock correction | |
CN106571887B (en) | A kind of management core of twin-core intelligent electric energy meter and metering core clock synchronization process method and device | |
CN104156302A (en) | Maintenance and timing system and method for satellite-borne computer real-time clock | |
CN102281114B (en) | Service time leap second processing method | |
CN203069745U (en) | High-precision clock chip output pulse time interval detection apparatus | |
CN106227293A (en) | A kind of system clock | |
CN110275799B (en) | Method for snapshot balance of daily point-cut without shutdown of accounting system | |
CN107798039B (en) | Data synchronization method and device | |
CN105843935A (en) | Data acquisition method and ETL (Extraction-Transformation-Loading) assembly | |
CN106597097A (en) | High-precision frequency measurement method | |
CN104182826A (en) | A method and an apparatus for processing allocation data | |
CN109752738A (en) | Target IMU and GNSS data synchronous method when based on standard | |
CN101556325A (en) | Method for quickly verifying electric energy error | |
CN103838183A (en) | Numerical control system and output control method thereof | |
JP2007334812A (en) | Simulator and simulation method | |
CN105911852A (en) | Analogue watch timing method | |
CN103576074A (en) | Real-time clock module test device | |
CN103075961B (en) | Support that appearing method is surveyed in the position of multiple grating scale based on monolithic FPGA | |
CN111124452A (en) | Software upgrading method and device for instrument, instrument and storage medium | |
CN110674214B (en) | Big data synchronization method, device, computer equipment and storage medium | |
CN104483649A (en) | High-precision time synchronization method and system applied to electric energy meter | |
CN105183930A (en) | Methods and devices for setting and using hardware timer | |
CN111143482B (en) | Service data processing method and device of energy Internet of things and electronic equipment | |
CN114595936A (en) | Engineering project carbon emission monitoring method and device |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
C06 | Publication | ||
PB01 | Publication | ||
C10 | Entry into substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
CB02 | Change of applicant information | ||
CB02 | Change of applicant information |
Address after: 250101 block B, Yinhe building, 2008 Xinjie street, hi tech Zone, Ji'nan, Shandong. Applicant after: Shandong Luneng Intelligent Technology Co., Ltd. Address before: 250101 B block 626, Yinhe building, 2008 Xinjie street, Ji'nan high tech Zone, Shandong. Applicant before: Shandong Luneng Intelligent Technology Co., Ltd. |
|
RJ01 | Rejection of invention patent application after publication | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20160629 |