Software Clock-use timer to simulate clock

Source: Internet
Author: User
Tags volatile

Software Clock

 

# Include "Global. H "// time base function struct typedef struct {uint16_t delaytime; uint16_t timebackup; uint16_t Init;} timer_t; static volatile every u16day; // daily static volatile uint16_t u16hour; // static volatile uint16_t u16minute; // static volatile uint16_t u16s; // static volatile uint16_t u16ms; // millisecond /************************************ **************************************** * ** # function: delayadd # descripti On: calculation time # parameter: nowtime current time * time base structure # Return: NULL # authoranddata: huangzhigang 20140716 ************************************** **************************************** */static void delayadd (uint16_t nowtime, timer_t * Time) {If (nowtime> (time-> timebackup) {uint16_t temp = nowtime-time-> timebackup; If (0 xFFFF-time-> delaytime)> = temp) {time-> delaytime + = temp;} Time-> timeback Up = nowtime;} void timeh (timer_t * Time) // Add a timer task in hours {If (! Time-> init) // start time initialization {time-> init = 1; time-> timebackup = u16hour;} else {delayadd (u16hour, time );}} void timem (timer_t * Time) // Add a timer task in minutes {If (! Time-> init) // start time initialization {time-> init = 1; time-> timebackup = u16minute;} else {delayadd (u16minute, time );}} void times (timer_t * Time) // Add a timer task in seconds {If (! Time-> init) // start time initialization {time-> init = 1; time-> timebackup = u16s;} else {delayadd (u16s, time );}} void timems (timer_t * Time) // Add a timing task in milliseconds {If (! Time-> init) // start time initialization {time-> init = 1; time-> timebackup = u16ms;} else {delayadd (u16ms, time );}} /*************************************** **************************************** # function: interrup_time # Description: The function called in the interrupt is called once every 1 ms to record the clock running # parameter: NULL # Return: NULL # authoranddata: huangzhigang 20140716 ************************************** **************************************** */void int Errup_time (void) {If (! (+++ U16ms % 1000) {If (! (++ U16s % 60) {u16ms = 0; // clear millisecond after 60000ms accumulators if (! (++ U16minute % 60) {If (! (++ U16hour % 24) {++ u16day;} If (u16s >=61200) {u16s = 0 ;}// second after 17 hours complete processors }}}}



Software Clock-use timer to simulate clock

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.